From 923e0004743fe17de5c2c4d85204eb32e28d8c9b Mon Sep 17 00:00:00 2001 From: steam Date: Mon, 24 Feb 2020 22:31:13 +0100 Subject: [PATCH] - GET /features is now tested on controller level - added DataService as a data provider --- pom.xml | 33 +++++++++++++++++++ .../satellite/api/FeaturesController.java | 16 +++++++-- .../poweronoff/satellite/domain/Feature.java | 21 ++++++++++++ .../satellite/service/DataService.java | 32 ++++++++++++++++++ .../satellite/service/DataServiceImpl.java | 24 ++++++++++++++ .../satellite/api/FeaturesControllerTest.java | 27 ++++++++++++--- 6 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 src/main/java/click/poweronoff/satellite/domain/Feature.java create mode 100644 src/main/java/click/poweronoff/satellite/service/DataService.java create mode 100644 src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java diff --git a/pom.xml b/pom.xml index 6f19171..b90501e 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,10 @@ 11 UTF-8 UTF-8 + 1.18.12 + 3.3.0 + 4.12 + 2.1 @@ -39,6 +43,35 @@ + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + + + org.mockito + mockito-core + ${mockito.version} + test + + + junit + junit + ${junit.version} + test + + + org.hamcrest + hamcrest + 2.1 + test + + diff --git a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java index e9e7e9c..978784d 100644 --- a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java +++ b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java @@ -1,16 +1,26 @@ package click.poweronoff.satellite.api; +import click.poweronoff.satellite.domain.Feature; +import click.poweronoff.satellite.service.DataService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; +import java.util.List; + @Controller public class FeaturesController { - @RequestMapping("/features") + + @Autowired + DataService dataService; + + @RequestMapping(value = "/features", method = RequestMethod.GET, produces = "application/json") public @ResponseBody - String features() { - return "a dummy message"; + List features() { + return dataService.getAllFeatures(); } } diff --git a/src/main/java/click/poweronoff/satellite/domain/Feature.java b/src/main/java/click/poweronoff/satellite/domain/Feature.java new file mode 100644 index 0000000..3815239 --- /dev/null +++ b/src/main/java/click/poweronoff/satellite/domain/Feature.java @@ -0,0 +1,21 @@ +package click.poweronoff.satellite.domain; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +@Getter +@EqualsAndHashCode +@AllArgsConstructor +public class Feature { + + private String id; + + private long timestamp; + + private long beginViewingDate; + + private long endViewingDate; + + private String missionName; +} diff --git a/src/main/java/click/poweronoff/satellite/service/DataService.java b/src/main/java/click/poweronoff/satellite/service/DataService.java new file mode 100644 index 0000000..8bc4ced --- /dev/null +++ b/src/main/java/click/poweronoff/satellite/service/DataService.java @@ -0,0 +1,32 @@ +package click.poweronoff.satellite.service; + +import click.poweronoff.satellite.domain.Feature; + +import java.util.List; + +public interface DataService { + + /** + * returned all possible Features as a list + * + * @return list of Features + */ + List getAllFeatures(); + + /** + * returned specific Feature by given id + * + * @param featureId feature id + * @return Feature + */ + Feature getFeature(final String featureId); + + + /** + * returned a picture as a Base64 encoded String representation for given feature id + * + * @param featureId feature id + * @return a Base64 encoded String representation for a picture + */ + String getPictureAsB64(final String featureId); +} diff --git a/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java b/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java new file mode 100644 index 0000000..2a88fd8 --- /dev/null +++ b/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java @@ -0,0 +1,24 @@ +package click.poweronoff.satellite.service; + +import click.poweronoff.satellite.domain.Feature; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class DataServiceImpl implements DataService { + + public List getAllFeatures() { + return null; + } + + @Override + public Feature getFeature(String featureId) { + return null; + } + + @Override + public String getPictureAsB64(String featureId) { + return null; + } +} diff --git a/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java b/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java index ffa4a4f..40e30f9 100644 --- a/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java +++ b/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java @@ -1,15 +1,22 @@ package click.poweronoff.satellite.api; +import click.poweronoff.satellite.domain.Feature; +import click.poweronoff.satellite.service.DataService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; -import static org.hamcrest.Matchers.containsString; +import java.util.List; + +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @@ -18,10 +25,22 @@ public class FeaturesControllerTest { @Autowired private MockMvc mockMvc; + @MockBean + DataService dataService; + @Test - public void shouldReturnDefaultMessage() throws Exception { - this.mockMvc.perform(get("/features")).andDo(print()).andExpect(status().isOk()) - .andExpect(content().string(containsString("a dummy message"))); + public void getFeaturesShouldReturnDefaultMessage() throws Exception { + when(dataService.getAllFeatures()).thenReturn(List.of(new Feature("feature-id", 123L, 234L, 345L, "mission"))); + this.mockMvc.perform(get("/features")).andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$[0].id").exists()) + .andExpect(jsonPath("$[0].id", is("feature-id"))) + .andExpect(jsonPath("$[0].timestamp", is(123))) + .andExpect(jsonPath("$[0].beginViewingDate", is(234))) + .andExpect(jsonPath("$[0].endViewingDate", is(345))) + .andExpect(jsonPath("$[0].missionName", is("mission")) + ); } } \ No newline at end of file