- GET /features/{id} added and tested on controller level

This commit is contained in:
steam 2020-02-25 22:26:44 +01:00
parent 923e000474
commit 90d3f81aac
4 changed files with 41 additions and 8 deletions

View File

@ -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<Feature> features() {
@ResponseBody
public List<Feature> 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"));
}
}

View File

@ -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<Feature> getFeature(final String featureId);
/**

View File

@ -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<Feature> getAllFeatures() {
return null;
return Collections.emptyList();
}
@Override
public Feature getFeature(String featureId) {
return null;
public Optional<Feature> getFeature(String featureId) {
return Optional.empty();
}
@Override

View File

@ -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());
}
}