-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d44f8f5
commit c845319
Showing
13 changed files
with
124 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.wcc.platform.service; | ||
|
||
import static com.wcc.platform.domain.cms.ApiResourcesFile.EVENTS; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.wcc.platform.domain.cms.pages.EventsPage; | ||
import com.wcc.platform.domain.exceptions.PlatformInternalException; | ||
import com.wcc.platform.utils.FileUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class EventService { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
public EventService(final ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
/** | ||
* Read Json and convert to POJO event page. | ||
* | ||
* @return POJO eventsPage | ||
*/ | ||
public EventsPage getEvents() { | ||
try { | ||
final var data = FileUtil.readFileAsString(EVENTS.getFileName()); | ||
return objectMapper.readValue(data, EventsPage.class); | ||
} catch (JsonProcessingException e) { | ||
throw new PlatformInternalException(e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/com/wcc/platform/service/EventServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.wcc.platform.service; | ||
|
||
import static com.wcc.platform.factories.SetupEventFactories.createEventPageTest; | ||
import static com.wcc.platform.factories.SetupEventFactories.createEventTest; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.wcc.platform.domain.cms.pages.EventsPage; | ||
import com.wcc.platform.domain.exceptions.PlatformInternalException; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class EventServiceTest { | ||
|
||
private ObjectMapper objectMapper; | ||
private EventService service; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
objectMapper = Mockito.mock(ObjectMapper.class); | ||
service = new EventService(objectMapper); | ||
} | ||
|
||
@Test | ||
void whenGetEventsValidJson() throws IOException { | ||
var page = createEventPageTest(List.of(createEventTest())); | ||
when(objectMapper.readValue(anyString(), eq(EventsPage.class))).thenReturn(page); | ||
|
||
var response = service.getEvents(); | ||
|
||
assertEquals(page, response); | ||
} | ||
|
||
@Test | ||
void whenGetEventsInValidJson() throws IOException { | ||
when(objectMapper.readValue(anyString(), eq(EventsPage.class))) | ||
.thenThrow(new JsonProcessingException("Invalid JSON") {}); | ||
|
||
var exception = assertThrows(PlatformInternalException.class, service::getEvents); | ||
|
||
assertEquals("Invalid JSON", exception.getMessage()); | ||
} | ||
} |
Oops, something went wrong.