From c8453198749b02e15d8cafa55cbffefbdf549ac2 Mon Sep 17 00:00:00 2001 From: Sonali Goel <112942761+goelsonali@users.noreply.github.com> Date: Sun, 6 Oct 2024 23:40:22 +0100 Subject: [PATCH] review comments updates --- .../platform/controller/EventController.java | 10 ++-- .../domain/cms/pages/FiltersSection.java | 3 +- .../wcc/platform/domain/platform/Filters.java | 2 +- .../com/wcc/platform/service/CmsService.java | 17 +------ .../wcc/platform/service/EventService.java | 36 +++++++++++++ src/main/resources/eventsFiltersSection.json | 12 ++--- .../controller/EventControllerTest.java | 8 +-- .../factories/SetUpFiltersFactories.java | 8 ++- .../CmsServiceIntegrationTest.java | 10 ---- .../EventControllerIntegrationTest.java | 19 +++++-- .../wcc/platform/service/CmsServiceTest.java | 24 --------- .../platform/service/EventServiceTest.java | 51 +++++++++++++++++++ .../platform/service/FiltersServiceTest.java | 2 +- 13 files changed, 124 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/wcc/platform/service/EventService.java create mode 100644 src/test/java/com/wcc/platform/service/EventServiceTest.java diff --git a/src/main/java/com/wcc/platform/controller/EventController.java b/src/main/java/com/wcc/platform/controller/EventController.java index 265eb8be..8961cbaa 100644 --- a/src/main/java/com/wcc/platform/controller/EventController.java +++ b/src/main/java/com/wcc/platform/controller/EventController.java @@ -2,7 +2,7 @@ import com.wcc.platform.domain.cms.pages.EventsPage; import com.wcc.platform.domain.cms.pages.FiltersSection; -import com.wcc.platform.service.CmsService; +import com.wcc.platform.service.EventService; import com.wcc.platform.service.FilterService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -20,12 +20,12 @@ @Tag(name = "APIs relevant Event Page") public class EventController { - private final CmsService cmsService; + private final EventService eventService; private final FilterService filterService; @Autowired - public EventController(final CmsService service, final FilterService filterService) { - this.cmsService = service; + public EventController(final EventService eventService, final FilterService filterService) { + this.eventService = eventService; this.filterService = filterService; } @@ -34,7 +34,7 @@ public EventController(final CmsService service, final FilterService filterServi @Operation(summary = "API to retrieve information about events page") @ResponseStatus(HttpStatus.OK) public ResponseEntity getEventsPage() { - return ResponseEntity.ok(cmsService.getEvents()); + return ResponseEntity.ok(eventService.getEvents()); } /** API to retrieve filters on events page. */ diff --git a/src/main/java/com/wcc/platform/domain/cms/pages/FiltersSection.java b/src/main/java/com/wcc/platform/domain/cms/pages/FiltersSection.java index feb45556..7b7c1260 100644 --- a/src/main/java/com/wcc/platform/domain/cms/pages/FiltersSection.java +++ b/src/main/java/com/wcc/platform/domain/cms/pages/FiltersSection.java @@ -5,7 +5,6 @@ /** * Filters section for any page. * - * @param page {@link Page} * @param filters {@link Filters} */ -public record FiltersSection(Page page, Filters filters) {} +public record FiltersSection(String title, Filters filters) {} diff --git a/src/main/java/com/wcc/platform/domain/platform/Filters.java b/src/main/java/com/wcc/platform/domain/platform/Filters.java index e3d4bd9a..79653282 100644 --- a/src/main/java/com/wcc/platform/domain/platform/Filters.java +++ b/src/main/java/com/wcc/platform/domain/platform/Filters.java @@ -21,5 +21,5 @@ public class Filters { private List type; private List topics; private List date; - private List region; + private List location; } diff --git a/src/main/java/com/wcc/platform/service/CmsService.java b/src/main/java/com/wcc/platform/service/CmsService.java index 09a3dbaa..ec8cce09 100644 --- a/src/main/java/com/wcc/platform/service/CmsService.java +++ b/src/main/java/com/wcc/platform/service/CmsService.java @@ -5,7 +5,6 @@ import com.wcc.platform.domain.cms.ApiResourcesFile; import com.wcc.platform.domain.cms.pages.CodeOfConductPage; import com.wcc.platform.domain.cms.pages.CollaboratorPage; -import com.wcc.platform.domain.cms.pages.EventsPage; import com.wcc.platform.domain.cms.pages.FooterPage; import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.domain.cms.pages.TeamPage; @@ -81,7 +80,7 @@ public CollaboratorPage getCollaborator() { throw new PlatformInternalException(e.getMessage(), e); } } - + /** * Read JSON and convert to Pojo CodeOfConductPage. * @@ -95,18 +94,4 @@ public CodeOfConductPage getCodeOfConduct() { throw new PlatformInternalException(e.getMessage(), e); } } - - /** - * Read Json and convert to POJO event page. - * - * @return POJO eventsPage - */ - public EventsPage getEvents() { - try { - final var data = FileUtil.readFileAsString(ApiResourcesFile.EVENTS.getFileName()); - return objectMapper.readValue(data, EventsPage.class); - } catch (JsonProcessingException e) { - throw new PlatformInternalException(e.getMessage(), e); - } - } } diff --git a/src/main/java/com/wcc/platform/service/EventService.java b/src/main/java/com/wcc/platform/service/EventService.java new file mode 100644 index 00000000..359567b8 --- /dev/null +++ b/src/main/java/com/wcc/platform/service/EventService.java @@ -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); + } + } +} diff --git a/src/main/resources/eventsFiltersSection.json b/src/main/resources/eventsFiltersSection.json index e6ade24d..a11803f1 100644 --- a/src/main/resources/eventsFiltersSection.json +++ b/src/main/resources/eventsFiltersSection.json @@ -1,11 +1,10 @@ { - "page": { - "title": "Filter By" - }, + "title": "Filter By", "filters": { "type": [ "IN_PERSON", - "ONLINE_MEETUP" + "ONLINE_MEETUP", + "HYBRID" ], "topics": [ "Book Club", @@ -17,9 +16,8 @@ "IN_30_TO_60_DAYS", "MORE_THAN_60_DAYS" ], - "region": [ - "London", - "Spain" + "location": [ + "London" ] } } \ No newline at end of file diff --git a/src/test/java/com/wcc/platform/controller/EventControllerTest.java b/src/test/java/com/wcc/platform/controller/EventControllerTest.java index 460c5938..4e7d246a 100644 --- a/src/test/java/com/wcc/platform/controller/EventControllerTest.java +++ b/src/test/java/com/wcc/platform/controller/EventControllerTest.java @@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.wcc.platform.domain.exceptions.PlatformInternalException; -import com.wcc.platform.service.CmsService; +import com.wcc.platform.service.EventService; import com.wcc.platform.service.FilterService; import java.util.List; import org.junit.jupiter.api.Test; @@ -29,13 +29,13 @@ class EventControllerTest { @Autowired private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; - @MockBean private CmsService cmsService; + @MockBean private EventService eventService; @MockBean private FilterService filterService; @Test void testInternalServerError() throws Exception { - when(cmsService.getEvents()) + when(eventService.getEvents()) .thenThrow(new PlatformInternalException("Invalid Json", new RuntimeException())); mockMvc @@ -50,7 +50,7 @@ void testInternalServerError() throws Exception { void testOkResponseForEvents() throws Exception { var eventPage = createEventPageTest(List.of(createEventTest())); - when(cmsService.getEvents()).thenReturn(eventPage); + when(eventService.getEvents()).thenReturn(eventPage); mockMvc .perform(get("/api/cms/v1/events").contentType(APPLICATION_JSON)) diff --git a/src/test/java/com/wcc/platform/factories/SetUpFiltersFactories.java b/src/test/java/com/wcc/platform/factories/SetUpFiltersFactories.java index f8de1779..a248c48a 100644 --- a/src/test/java/com/wcc/platform/factories/SetUpFiltersFactories.java +++ b/src/test/java/com/wcc/platform/factories/SetUpFiltersFactories.java @@ -1,7 +1,5 @@ package com.wcc.platform.factories; -import static com.wcc.platform.factories.SetupFactories.createPageTest; - import com.wcc.platform.domain.cms.attributes.EventDays; import com.wcc.platform.domain.cms.attributes.EventType; import com.wcc.platform.domain.cms.pages.FiltersSection; @@ -22,7 +20,7 @@ public static Filters createFilterTest() { .type(List.of(EventType.IN_PERSON)) .topics(List.of(ProgramType.BOOK_CLUB)) .date(List.of(EventDays.IN_30_DAYS)) - .region(List.of("London", "Spain")) + .location(List.of("London", "Spain")) .build(); } @@ -37,7 +35,7 @@ public static Filters createFilterTest(final List eventType) { .type(eventType) .topics(List.of(ProgramType.BOOK_CLUB)) .date(List.of(EventDays.IN_30_DAYS)) - .region(List.of("London", "Spain")) + .location(List.of("London", "Spain")) .build(); } @@ -47,6 +45,6 @@ public static Filters createFilterTest(final List eventType) { * @return {@link FiltersSection} */ public static FiltersSection createFilterSectionTest() { - return new FiltersSection(createPageTest(), createFilterTest()); + return new FiltersSection("default_title", createFilterTest()); } } diff --git a/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java index dd03b64c..050525c6 100644 --- a/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java +++ b/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java @@ -2,10 +2,8 @@ import static com.wcc.platform.domain.cms.ApiResourcesFile.CODE_OF_CONDUCT; import static com.wcc.platform.domain.cms.ApiResourcesFile.COLLABORATOR; -import static com.wcc.platform.domain.cms.ApiResourcesFile.EVENTS; import static com.wcc.platform.domain.cms.ApiResourcesFile.FOOTER; import static com.wcc.platform.domain.cms.ApiResourcesFile.TEAM; -import static com.wcc.platform.factories.SetupEventFactories.createEventTest; import static com.wcc.platform.factories.SetupFactories.OBJECT_MAPPER; import static com.wcc.platform.factories.SetupFactories.createCodeOfConductPageTest; import static com.wcc.platform.factories.SetupFactories.createCollaboratorPageTest; @@ -84,14 +82,6 @@ void testGetCodeOfConductPage() { assertEquals(expectedCodeOfConductPage, result); } - @Test - void testGetEventsPage() { - var result = service.getEvents(); - var expectedEventsPage = createEventTest(EVENTS.getFileName()); - - assertEquals(expectedEventsPage, result); - } - @SneakyThrows @Test void testGetLandingPage() { diff --git a/src/test/java/com/wcc/platform/integrationtests/EventControllerIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/EventControllerIntegrationTest.java index ed44d383..e83b2bde 100644 --- a/src/test/java/com/wcc/platform/integrationtests/EventControllerIntegrationTest.java +++ b/src/test/java/com/wcc/platform/integrationtests/EventControllerIntegrationTest.java @@ -1,10 +1,13 @@ package com.wcc.platform.integrationtests; +import static com.wcc.platform.domain.cms.ApiResourcesFile.EVENTS; +import static com.wcc.platform.domain.cms.ApiResourcesFile.EVENT_FILTERS; +import static com.wcc.platform.factories.SetupEventFactories.createEventTest; import static com.wcc.platform.factories.SetupFactories.OBJECT_MAPPER; import static org.junit.jupiter.api.Assertions.assertEquals; import com.wcc.platform.controller.EventController; -import com.wcc.platform.domain.cms.ApiResourcesFile; +import com.wcc.platform.service.EventService; import com.wcc.platform.utils.FileUtil; import lombok.SneakyThrows; import org.junit.jupiter.api.Test; @@ -19,6 +22,8 @@ public class EventControllerIntegrationTest { @Autowired private EventController eventController; + @Autowired private EventService service; + @SneakyThrows @Test void testEventsAPISuccess() { @@ -26,7 +31,7 @@ void testEventsAPISuccess() { assertEquals(HttpStatus.OK, response.getStatusCode()); - var expected = FileUtil.readFileAsString(ApiResourcesFile.EVENTS.getFileName()); + var expected = FileUtil.readFileAsString(EVENTS.getFileName()); var jsonResponse = OBJECT_MAPPER.writeValueAsString(response.getBody()); JSONAssert.assertEquals(expected, jsonResponse, false); @@ -39,9 +44,17 @@ void testEventsFiltersAPISuccess() { assertEquals(HttpStatus.OK, response.getStatusCode()); - var expected = FileUtil.readFileAsString(ApiResourcesFile.EVENT_FILTERS.getFileName()); + var expected = FileUtil.readFileAsString(EVENT_FILTERS.getFileName()); var jsonResponse = OBJECT_MAPPER.writeValueAsString(response.getBody()); JSONAssert.assertEquals(expected, jsonResponse, false); } + + @Test + void testGetEventsPage() { + var result = service.getEvents(); + var expectedEventsPage = createEventTest(EVENTS.getFileName()); + + assertEquals(expectedEventsPage, result); + } } diff --git a/src/test/java/com/wcc/platform/service/CmsServiceTest.java b/src/test/java/com/wcc/platform/service/CmsServiceTest.java index 9ff1ccca..55031c6e 100644 --- a/src/test/java/com/wcc/platform/service/CmsServiceTest.java +++ b/src/test/java/com/wcc/platform/service/CmsServiceTest.java @@ -1,7 +1,5 @@ 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; @@ -12,14 +10,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.wcc.platform.domain.cms.pages.CodeOfConductPage; import com.wcc.platform.domain.cms.pages.CollaboratorPage; -import com.wcc.platform.domain.cms.pages.EventsPage; import com.wcc.platform.domain.cms.pages.FooterPage; import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.domain.cms.pages.TeamPage; import com.wcc.platform.domain.exceptions.PlatformInternalException; import com.wcc.platform.factories.SetupFactories; import java.io.IOException; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -117,26 +113,6 @@ void whenGetCodeOfConductGivenValidJson() throws IOException { assertEquals(codeOfConductPage, response); } - @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()); - } - @Test void whenGetLandingPageGivenInvalidJson() throws IOException { when(objectMapper.readValue(anyString(), eq(LandingPage.class))) diff --git a/src/test/java/com/wcc/platform/service/EventServiceTest.java b/src/test/java/com/wcc/platform/service/EventServiceTest.java new file mode 100644 index 00000000..a9372a52 --- /dev/null +++ b/src/test/java/com/wcc/platform/service/EventServiceTest.java @@ -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()); + } +} diff --git a/src/test/java/com/wcc/platform/service/FiltersServiceTest.java b/src/test/java/com/wcc/platform/service/FiltersServiceTest.java index abbe988b..35409ebb 100644 --- a/src/test/java/com/wcc/platform/service/FiltersServiceTest.java +++ b/src/test/java/com/wcc/platform/service/FiltersServiceTest.java @@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -public class FiltersServiceTest { +class FiltersServiceTest { private ObjectMapper objectMapper;