From 28777e7338fe3b3aa0034bf036e2224c78a668a2 Mon Sep 17 00:00:00 2001 From: Rishabh k Date: Tue, 28 Mar 2023 13:57:49 -0500 Subject: [PATCH] Adding new endpoints and updating tests --- .../catapult/animals/service/BaseService.java | 56 ++++++++--- .../cx/catapult/animals/service/Service.java | 6 ++ .../catapult/animals/web/CatsController.java | 65 ++++++++----- .../animals/service/CatsServiceTest.java | 94 +++++++++++++------ .../animals/web/CatsControllerIT.java | 93 +++++++++--------- .../animals/web/CatsControllerTest.java | 66 ++++++++----- 6 files changed, 246 insertions(+), 134 deletions(-) diff --git a/src/main/java/cx/catapult/animals/service/BaseService.java b/src/main/java/cx/catapult/animals/service/BaseService.java index fa0810e..60009dd 100644 --- a/src/main/java/cx/catapult/animals/service/BaseService.java +++ b/src/main/java/cx/catapult/animals/service/BaseService.java @@ -3,26 +3,54 @@ import cx.catapult.animals.domain.Animal; import java.util.*; +import java.util.stream.Collectors; public abstract class BaseService implements Service { - private HashMap items = new HashMap<>(); + private HashMap items = new HashMap<>(); - @Override - public Collection all() { - return items.values(); - } + @Override + public Collection all() { + return items.values(); + } + + @Override + public T create(T animal) { + String id = UUID.randomUUID().toString(); + animal.setId(id); + items.put(id, animal); + return animal; + } + + @Override + public T get(String id) { + return items.get(id); + } + + @Override + public Collection filteredAll(String searchString) { + return items.entrySet().stream() + .filter(entry -> { + T rowData = entry.getValue(); + return rowData.getName().toLowerCase().contains(searchString.toLowerCase()) || rowData.getDescription().toLowerCase().contains(searchString.toLowerCase()); + }).map(Map.Entry::getValue) + .collect(Collectors.toList()); + } - @Override - public T create(T animal) { - String id = UUID.randomUUID().toString(); - animal.setId(id); - items.put(id, animal); - return animal; + @Override + public T update(String id, T animal) { + if (items.containsKey(id)) { + items.put(id, animal); } + return animal; + } - @Override - public T get(String id) { - return items.get(id); + @Override + public T delete(String id) { + T currentData = items.get(id); + if (items.containsKey(id)) { + items.remove(id); } + return currentData; + } } diff --git a/src/main/java/cx/catapult/animals/service/Service.java b/src/main/java/cx/catapult/animals/service/Service.java index e33b4f6..1a03d54 100644 --- a/src/main/java/cx/catapult/animals/service/Service.java +++ b/src/main/java/cx/catapult/animals/service/Service.java @@ -12,4 +12,10 @@ public interface Service { public T get(String id); + T update(String id, T animal); + + T delete(String id); + + public Collection filteredAll(String searchString); + } diff --git a/src/main/java/cx/catapult/animals/web/CatsController.java b/src/main/java/cx/catapult/animals/web/CatsController.java index bc88c5f..2002940 100644 --- a/src/main/java/cx/catapult/animals/web/CatsController.java +++ b/src/main/java/cx/catapult/animals/web/CatsController.java @@ -9,30 +9,51 @@ import java.util.Collection; +@CrossOrigin(origins = "*") @RestController @RequestMapping(path = "/api/1/cats", produces = MediaType.APPLICATION_JSON_VALUE) public class CatsController { - @Autowired - private CatsService service; - - @GetMapping(value = "", produces = "application/json") - public @ResponseBody - Collection all() { - return service.all(); - } - - @GetMapping(value = "/{id}") - public @ResponseBody - Cat get(@PathVariable String id) { - return service.get(id); - } - - @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE) - @ResponseStatus(HttpStatus.CREATED) - public @ResponseBody - Cat - create(@RequestBody Cat cat) { - return service.create(cat); - } + @Autowired + private CatsService service; + + @GetMapping(value = "", produces = "application/json") + public @ResponseBody + Collection all() { + return service.all(); + } + + @GetMapping(value = "/{id}") + public @ResponseBody + Cat get(@PathVariable String id) { + return service.get(id); + } + + @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE) + @ResponseStatus(HttpStatus.CREATED) + public @ResponseBody + Cat + create(@RequestBody Cat cat) { + return service.create(cat); + } + + @GetMapping(value = "", produces = "application/json", params = "searchString") + public @ResponseBody + Collection filter(@RequestParam("searchString") String searchString) { + return searchString.isEmpty() ? service.all() : service.filteredAll(searchString); + } + + @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE) + @ResponseStatus(HttpStatus.OK) + public @ResponseBody + Cat update(@PathVariable String id, @RequestBody Cat cat) { + return service.update(id, cat); + } + + @DeleteMapping(value = "/{id}") + @ResponseStatus(HttpStatus.OK) + public @ResponseBody + Cat delete(@PathVariable String id) { + return service.delete(id); + } } diff --git a/src/test/java/cx/catapult/animals/service/CatsServiceTest.java b/src/test/java/cx/catapult/animals/service/CatsServiceTest.java index 98acd6d..88fde7b 100644 --- a/src/test/java/cx/catapult/animals/service/CatsServiceTest.java +++ b/src/test/java/cx/catapult/animals/service/CatsServiceTest.java @@ -7,34 +7,68 @@ public class CatsServiceTest { - CatsService service = new CatsService(); - Cat cat = new Cat("Tom", "Bob cat"); - - @Test - public void createShouldWork() throws Exception { - Cat thisCat = new Cat(); - thisCat.setName("Jerry"); - thisCat.setDescription("Mouse Cat"); - Cat actual = service.create(thisCat); - assertThat(actual).isEqualTo(thisCat); - assertThat(actual.getName()).isEqualTo(thisCat.getName()); - assertThat(actual.getDescription()).isEqualTo(thisCat.getDescription()); - assertThat(actual.getGroup()).isEqualTo(thisCat.getGroup()); - } - - @Test - public void allShouldWork() throws Exception { - service.create(cat); - assertThat(service.all().size()).isEqualTo(1); - } - - @Test - public void getShouldWork() throws Exception { - service.create(cat); - Cat actual = service.get(cat.getId()); - assertThat(actual).isEqualTo(cat); - assertThat(actual.getName()).isEqualTo(cat.getName()); - assertThat(actual.getDescription()).isEqualTo(cat.getDescription()); - assertThat(actual.getGroup()).isEqualTo(cat.getGroup()); - } + CatsService service = new CatsService(); + Cat cat = new Cat("Tom", "Bob cat"); + + @Test + public void createShouldWork() throws Exception { + Cat thisCat = new Cat(); + thisCat.setName("Jerry"); + thisCat.setDescription("Mouse Cat"); + Cat actual = service.create(thisCat); + assertThat(actual).isEqualTo(thisCat); + assertThat(actual.getName()).isEqualTo(thisCat.getName()); + assertThat(actual.getDescription()).isEqualTo(thisCat.getDescription()); + assertThat(actual.getGroup()).isEqualTo(thisCat.getGroup()); + } + + @Test + public void allShouldWork() throws Exception { + service.create(cat); + assertThat(service.all().size()).isEqualTo(1); + } + + @Test + public void getShouldWork() throws Exception { + service.create(cat); + Cat actual = service.get(cat.getId()); + assertThat(actual).isEqualTo(cat); + assertThat(actual.getName()).isEqualTo(cat.getName()); + assertThat(actual.getDescription()).isEqualTo(cat.getDescription()); + assertThat(actual.getGroup()).isEqualTo(cat.getGroup()); + } + + @Test + public void updateShouldWork() throws Exception { + service.create(cat); + String catId = cat.getId(); + Cat actual = service.get(catId); + actual.setName("Robo"); + actual.setDescription("my new cat"); + Cat updatedActual = service.update(catId, actual); + assertThat(updatedActual.getName()).isEqualTo("Robo"); + assertThat(updatedActual.getDescription()).isEqualTo("my new cat"); + assertThat(updatedActual.getGroup()).isEqualTo(cat.getGroup()); + } + + @Test + public void deleteShouldWork() throws Exception { + service.create(cat); + String catId = cat.getId(); + service.delete(catId); + Cat actual = service.get(catId); + assertThat(actual).isEqualTo(null); + } + + @Test + public void filterShouldWork() throws Exception { + Cat catOne = new Cat("Jerry", "my fat cat"); + Cat catTwo = new Cat("Julu", "cute little cat, like old jerry"); + service.create(catOne); + service.create(catTwo); + assertThat(service.filteredAll("jerry").size()).isEqualTo(2); + assertThat(service.filteredAll("cute").size()).isEqualTo(1); + assertThat(service.filteredAll("").size()).isEqualTo(2); + assertThat(service.filteredAll("nice").size()).isEqualTo(0); + } } diff --git a/src/test/java/cx/catapult/animals/web/CatsControllerIT.java b/src/test/java/cx/catapult/animals/web/CatsControllerIT.java index 2016ba8..89f2fe0 100644 --- a/src/test/java/cx/catapult/animals/web/CatsControllerIT.java +++ b/src/test/java/cx/catapult/animals/web/CatsControllerIT.java @@ -20,48 +20,53 @@ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class CatsControllerIT { - @LocalServerPort - private int port; - - private URL base; - - private Cat cat = new Cat("Tom", "Bob cat"); - - @Autowired - private TestRestTemplate template; - - @BeforeEach - public void setUp() throws Exception { - this.base = new URL("http://localhost:" + port + "/api/1/cats"); - } - - @Test - public void createShouldWork() throws Exception { - ResponseEntity response = template.postForEntity(base.toString(), cat, Cat.class); - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); - assertThat(response.getBody().getId()).isNotEmpty(); - assertThat(response.getBody().getName()).isEqualTo(cat.getName()); - assertThat(response.getBody().getDescription()).isEqualTo(cat.getDescription()); - assertThat(response.getBody().getGroup()).isEqualTo(cat.getGroup()); - } - - @Test - public void allShouldWork() throws Exception { - Collection items = template.getForObject(base.toString(), Collection.class); - assertThat(items.size()).isGreaterThanOrEqualTo(7); - } - - @Test - public void getShouldWork() throws Exception { - Cat created = create("Test 1"); - ResponseEntity response = template.getForEntity(base.toString() + "/" + created.getId(), String.class); - assertThat(response.getBody()).isNotEmpty(); - } - - Cat create(String name) { - Cat created = template.postForObject(base.toString(), new Cat(name, name), Cat.class); - assertThat(created.getId()).isNotEmpty(); - assertThat(created.getName()).isEqualTo(name); - return created; - } + @LocalServerPort + private int port; + + private URL base; + + private Cat cat = new Cat("Tom", "Bob cat"); + + @Autowired + private TestRestTemplate template; + + @BeforeEach + public void setUp() throws Exception { + this.base = new URL("http://localhost:" + port + "/api/1/cats"); + } + + @Test + public void createShouldWork() throws Exception { + ResponseEntity response = template.postForEntity(base.toString(), cat, Cat.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); + assertThat(response.getBody().getId()).isNotEmpty(); + assertThat(response.getBody().getName()).isEqualTo(cat.getName()); + assertThat(response.getBody().getDescription()).isEqualTo(cat.getDescription()); + assertThat(response.getBody().getGroup()).isEqualTo(cat.getGroup()); + } + + @Test + public void allShouldWork() throws Exception { + Collection items = template.getForObject(base.toString(), Collection.class); + assertThat(items.size()).isGreaterThanOrEqualTo(7); + } + + @Test + public void getShouldWork() throws Exception { + Cat created = create("Test 1"); + ResponseEntity response = template.getForEntity(base.toString() + "/" + created.getId(), String.class); + assertThat(response.getBody()).isNotEmpty(); + } + + @Test + public void updateShouldWork() throws Exception { + template.put(base.toString(), cat); + } + + Cat create(String name) { + Cat created = template.postForObject(base.toString(), new Cat(name, name), Cat.class); + assertThat(created.getId()).isNotEmpty(); + assertThat(created.getName()).isEqualTo(name); + return created; + } } diff --git a/src/test/java/cx/catapult/animals/web/CatsControllerTest.java b/src/test/java/cx/catapult/animals/web/CatsControllerTest.java index 1264b57..122af2b 100644 --- a/src/test/java/cx/catapult/animals/web/CatsControllerTest.java +++ b/src/test/java/cx/catapult/animals/web/CatsControllerTest.java @@ -17,27 +17,45 @@ @AutoConfigureMockMvc public class CatsControllerTest { - @Autowired - private MockMvc mvc; - - private Cat cat = new Cat("Tom", "Bob cat"); - private String json = "{ \"name\": \"Tom\", \"description\": \"Bob cat\" }"; - - @Test - public void all() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/api/1/cats").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - } - - @Test - public void get() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/api/1/cats/123").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - } - - @Test - public void create() throws Exception { - mvc.perform(MockMvcRequestBuilders.post("/api/1/cats").content(json).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isCreated()); - } -} \ No newline at end of file + @Autowired + private MockMvc mvc; + + private Cat cat = new Cat("Tom", "Bob cat"); + private String json = "{ \"name\": \"Tom\", \"description\": \"Bob cat\" }"; + + @Test + public void all() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/api/1/cats").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void get() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/api/1/cats/123").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void create() throws Exception { + mvc.perform(MockMvcRequestBuilders.post("/api/1/cats").content(json).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isCreated()); + } + + @Test + public void update() throws Exception { + mvc.perform(MockMvcRequestBuilders.put("/api/1/cats/123").content(json).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isOk()); + } + + @Test + public void delete() throws Exception { + mvc.perform(MockMvcRequestBuilders.delete("/api/1/cats/123").contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isOk()); + } + + @Test + public void filteredAll() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/api/1/cats?search=cat").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } +}