diff --git a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java index 978784d..adcbbbe 100644 --- a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java +++ b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java @@ -4,10 +4,13 @@ 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.http.HttpStatus; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.server.ResponseStatusException; import java.util.List; @@ -18,9 +21,15 @@ public class FeaturesController { DataService dataService; @RequestMapping(value = "/features", method = RequestMethod.GET, produces = "application/json") - public @ResponseBody - List features() { + @ResponseBody + public List features() { return dataService.getAllFeatures(); } + @RequestMapping(value = "/features/{id}", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Feature featureById(@PathVariable String id) { + return dataService.getFeature(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find feature")); + } + } diff --git a/src/main/java/click/poweronoff/satellite/service/DataService.java b/src/main/java/click/poweronoff/satellite/service/DataService.java index 8bc4ced..a21c003 100644 --- a/src/main/java/click/poweronoff/satellite/service/DataService.java +++ b/src/main/java/click/poweronoff/satellite/service/DataService.java @@ -3,6 +3,7 @@ package click.poweronoff.satellite.service; import click.poweronoff.satellite.domain.Feature; import java.util.List; +import java.util.Optional; public interface DataService { @@ -17,9 +18,11 @@ public interface DataService { * returned specific Feature by given id * * @param featureId feature id - * @return Feature + * @return Optional of Feature in case of the feature can be + * found + * or an empty Optional */ - Feature getFeature(final String featureId); + Optional getFeature(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 index 2a88fd8..40e06c5 100644 --- a/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java +++ b/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java @@ -3,18 +3,20 @@ package click.poweronoff.satellite.service; import click.poweronoff.satellite.domain.Feature; import org.springframework.stereotype.Component; +import java.util.Collections; import java.util.List; +import java.util.Optional; @Component public class DataServiceImpl implements DataService { public List getAllFeatures() { - return null; + return Collections.emptyList(); } @Override - public Feature getFeature(String featureId) { - return null; + public Optional getFeature(String featureId) { + return Optional.empty(); } @Override diff --git a/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java b/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java index 40e30f9..70da8c6 100644 --- a/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java +++ b/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java @@ -7,11 +7,14 @@ 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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import java.util.List; +import java.util.Optional; import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.anyString; 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; @@ -33,7 +36,7 @@ public class FeaturesControllerTest { 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(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$[0].id").exists()) .andExpect(jsonPath("$[0].id", is("feature-id"))) .andExpect(jsonPath("$[0].timestamp", is(123))) @@ -43,4 +46,20 @@ public class FeaturesControllerTest { ); } + @Test + public void getFeatureByIdShouldReturnAMessage() throws Exception { + when(dataService.getFeature(anyString())).thenReturn(Optional.of(new Feature("feature-id", 123L, 234L, 345L, "mission"))); + this.mockMvc.perform(get("/features/feature-id")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + public void getFeatureByIdShouldReturnNotFound() throws Exception { + when(dataService.getFeature(anyString())).thenReturn(Optional.empty()); + this.mockMvc.perform(get("/features/non-existing-feature")) + .andExpect(status().isNotFound()); + } + + } \ No newline at end of file