diff --git a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java index 180893b..c5bd0ee 100644 --- a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java +++ b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java @@ -23,7 +23,7 @@ public class FeaturesController { @RequestMapping(value = "/features", method = RequestMethod.GET, produces = "application/json") @ResponseBody public List features() { - return dataService.getAllFeatures(); + return dataService.getAllFeatures().orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find any feature")); } @RequestMapping(value = "/features/{id}", method = RequestMethod.GET, produces = "application/json") diff --git a/src/main/java/click/poweronoff/satellite/service/DataService.java b/src/main/java/click/poweronoff/satellite/service/DataService.java index f9ed70b..5dc081c 100644 --- a/src/main/java/click/poweronoff/satellite/service/DataService.java +++ b/src/main/java/click/poweronoff/satellite/service/DataService.java @@ -10,17 +10,17 @@ public interface DataService { /** * returned all possible Features as a list * - * @return list of Features + * @return Optional of list of Feature in case of features can + * be found or empty Optional */ - List getAllFeatures(); + Optional> getAllFeatures(); /** * returned specific Feature by given id * * @param featureId feature id * @return Optional of Feature in case of the feature can be - * found - * or an empty Optional + * found or an empty Optional */ Optional getFeature(final String featureId); @@ -29,7 +29,8 @@ public interface DataService { * returned a picture as a byte[] representation for given feature id * * @param featureId feature id - * @return a picture as a byte[] + * @return Optional of byte[] representing a picture or + * empty optional if no picture can be found for given feature id */ Optional getPicture(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 f7178e6..a843727 100644 --- a/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java +++ b/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java @@ -3,15 +3,14 @@ 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 Collections.emptyList(); + public Optional> getAllFeatures() { + 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 91f7539..8d89666 100644 --- a/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java +++ b/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java @@ -33,7 +33,7 @@ public class FeaturesControllerTest { @Test public void getFeaturesShouldReturnDefaultMessage() throws Exception { - when(dataService.getAllFeatures()).thenReturn(List.of(createTestFeature())); + when(dataService.getAllFeatures()).thenReturn(Optional.of(List.of(createTestFeature()))); this.mockMvc.perform(get("/features")).andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) @@ -46,6 +46,13 @@ public class FeaturesControllerTest { ); } + @Test + public void getFeaturesShouldReturnNotFound() throws Exception { + when(dataService.getAllFeatures()).thenReturn(Optional.empty()); + this.mockMvc.perform(get("/features")) + .andExpect(status().isNotFound()); + } + @Test public void getFeatureByIdShouldReturnAMessage() throws Exception { when(dataService.getFeature(anyString())).thenReturn(Optional.of(createTestFeature()));