From 8e779b587da53af038f90ec0bc00e2aebe1f34d4 Mon Sep 17 00:00:00 2001 From: steam Date: Thu, 27 Feb 2020 11:26:46 +0100 Subject: [PATCH] - GET /features/{id}/quicklook added and tested on controller level --- .../satellite/api/FeaturesController.java | 6 +++++ .../poweronoff/satellite/domain/Feature.java | 4 +++ .../satellite/service/DataService.java | 6 ++--- .../satellite/service/DataServiceImpl.java | 4 +-- .../satellite/api/FeaturesControllerTest.java | 25 +++++++++++++++++-- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java index adcbbbe..180893b 100644 --- a/src/main/java/click/poweronoff/satellite/api/FeaturesController.java +++ b/src/main/java/click/poweronoff/satellite/api/FeaturesController.java @@ -32,4 +32,10 @@ public class FeaturesController { return dataService.getFeature(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find feature")); } + @RequestMapping(value = "/features/{id}/quicklook", method = RequestMethod.GET, produces = "image/png") + @ResponseBody + public byte[] quicklookByFeatureId(@PathVariable String id) { + return dataService.getPicture(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find feature")); + } + } diff --git a/src/main/java/click/poweronoff/satellite/domain/Feature.java b/src/main/java/click/poweronoff/satellite/domain/Feature.java index 3815239..22bb8cc 100644 --- a/src/main/java/click/poweronoff/satellite/domain/Feature.java +++ b/src/main/java/click/poweronoff/satellite/domain/Feature.java @@ -1,5 +1,6 @@ package click.poweronoff.satellite.domain; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -18,4 +19,7 @@ public class Feature { private long endViewingDate; private String missionName; + + @JsonIgnore + private byte[] picture; } diff --git a/src/main/java/click/poweronoff/satellite/service/DataService.java b/src/main/java/click/poweronoff/satellite/service/DataService.java index a21c003..f9ed70b 100644 --- a/src/main/java/click/poweronoff/satellite/service/DataService.java +++ b/src/main/java/click/poweronoff/satellite/service/DataService.java @@ -26,10 +26,10 @@ public interface DataService { /** - * returned a picture as a Base64 encoded String representation for given feature id + * returned a picture as a byte[] representation for given feature id * * @param featureId feature id - * @return a Base64 encoded String representation for a picture + * @return a picture as a byte[] */ - String getPictureAsB64(final String featureId); + 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 40e06c5..f7178e6 100644 --- a/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java +++ b/src/main/java/click/poweronoff/satellite/service/DataServiceImpl.java @@ -20,7 +20,7 @@ public class DataServiceImpl implements DataService { } @Override - public String getPictureAsB64(String featureId) { - return null; + public Optional getPicture(String featureId) { + return Optional.empty(); } } diff --git a/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java b/src/test/java/click/poweronoff/satellite/api/FeaturesControllerTest.java index 70da8c6..91f7539 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(new Feature("feature-id", 123L, 234L, 345L, "mission"))); + when(dataService.getAllFeatures()).thenReturn(List.of(createTestFeature())); this.mockMvc.perform(get("/features")).andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) @@ -48,7 +48,7 @@ public class FeaturesControllerTest { @Test public void getFeatureByIdShouldReturnAMessage() throws Exception { - when(dataService.getFeature(anyString())).thenReturn(Optional.of(new Feature("feature-id", 123L, 234L, 345L, "mission"))); + when(dataService.getFeature(anyString())).thenReturn(Optional.of(createTestFeature())); this.mockMvc.perform(get("/features/feature-id")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); @@ -62,4 +62,25 @@ public class FeaturesControllerTest { } + @Test + public void getQuicklookByFeatureIdShouldReturnNotFound() throws Exception { + when(dataService.getPicture(anyString())).thenReturn(Optional.empty()); + this.mockMvc.perform(get("/features/non-existing-feature/quicklook")) + .andExpect(status().isNotFound()); + } + + @Test + public void getQuicklookByFeatureIdShouldReturnAPicture() throws Exception { + Feature testFeature = createTestFeature(); + + when(dataService.getPicture(anyString())).thenReturn(Optional.of(testFeature.getPicture())); + this.mockMvc.perform(get("/features/existing-feature/quicklook")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.IMAGE_PNG)) + .andExpect(content().bytes(testFeature.getPicture())); + } + + private Feature createTestFeature() { + return new Feature("feature-id", 123L, 234L, 345L, "mission", "test-picture".getBytes()); + } } \ No newline at end of file