Skip to content

Commit

Permalink
review comments updates
Browse files Browse the repository at this point in the history
  • Loading branch information
goelsonali committed Oct 6, 2024
1 parent d44f8f5 commit c845319
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 78 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/wcc/platform/controller/EventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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<EventsPage> getEventsPage() {
return ResponseEntity.ok(cmsService.getEvents());
return ResponseEntity.ok(eventService.getEvents());
}

/** API to retrieve filters on events page. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public class Filters {
private List<EventType> type;
private List<ProgramType> topics;
private List<EventDays> date;
private List<String> region;
private List<String> location;
}
17 changes: 1 addition & 16 deletions src/main/java/com/wcc/platform/service/CmsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,7 +80,7 @@ public CollaboratorPage getCollaborator() {
throw new PlatformInternalException(e.getMessage(), e);
}
}

/**
* Read JSON and convert to Pojo CodeOfConductPage.
*
Expand All @@ -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);
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/wcc/platform/service/EventService.java
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);
}
}
}
12 changes: 5 additions & 7 deletions src/main/resources/eventsFiltersSection.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"page": {
"title": "Filter By"
},
"title": "Filter By",
"filters": {
"type": [
"IN_PERSON",
"ONLINE_MEETUP"
"ONLINE_MEETUP",
"HYBRID"
],
"topics": [
"Book Club",
Expand All @@ -17,9 +16,8 @@
"IN_30_TO_60_DAYS",
"MORE_THAN_60_DAYS"
],
"region": [
"London",
"Spain"
"location": [
"London"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

Expand All @@ -37,7 +35,7 @@ public static Filters createFilterTest(final List<EventType> 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();
}

Expand All @@ -47,6 +45,6 @@ public static Filters createFilterTest(final List<EventType> eventType) {
* @return {@link FiltersSection}
*/
public static FiltersSection createFilterSectionTest() {
return new FiltersSection(createPageTest(), createFilterTest());
return new FiltersSection("default_title", createFilterTest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,14 +22,16 @@ public class EventControllerIntegrationTest {

@Autowired private EventController eventController;

@Autowired private EventService service;

@SneakyThrows
@Test
void testEventsAPISuccess() {
var response = eventController.getEventsPage();

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);
Expand All @@ -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);
}
}
24 changes: 0 additions & 24 deletions src/test/java/com/wcc/platform/service/CmsServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)))
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/wcc/platform/service/EventServiceTest.java
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());
}
}
Loading

0 comments on commit c845319

Please sign in to comment.