From a1b0e7d838727dcebbd6f50aa697dc1e6ec7dee7 Mon Sep 17 00:00:00 2001 From: cato <47259183+cato447@users.noreply.github.com> Date: Sun, 5 Jun 2022 17:35:37 +0200 Subject: [PATCH] Items endpoint tests and updates to the item endpoint (#13) (#16) * Creation of base template (#1) * Sample template created * added findByName functionality for item * Solve Cors errors and inhibit DefaultExposure * changed project structure * Added frontend * changed base path of REST api and updated frontend api quering * Items enpoint tests and updates to the item endpoint (#13) * Config setup * add tests for items Co-authored-by: Bruno --- .../whattocook/controller/ItemController.java | 12 +- backend/src/test/java/ItemTests.java | 137 ++++++++++++++++++ backend/src/test/resources/application.yml | 13 ++ build.gradle | 2 +- 4 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 backend/src/test/java/ItemTests.java create mode 100644 backend/src/test/resources/application.yml diff --git a/backend/src/main/whattocook/controller/ItemController.java b/backend/src/main/whattocook/controller/ItemController.java index a106d62..1a244f9 100644 --- a/backend/src/main/whattocook/controller/ItemController.java +++ b/backend/src/main/whattocook/controller/ItemController.java @@ -32,13 +32,13 @@ public class ItemController { @PostMapping("/items") public Item createItem(@RequestBody Item item) { - itemService.save(item); - return item; + return itemService.save(item); + } @PutMapping("/items/{itemId}") - public String updateItem(@PathVariable(value = "itemId") Long itemId, @RequestBody Item item) { - return itemService.findById(itemId).map(i -> { + public void updateItem(@PathVariable(value = "itemId") Long itemId, @RequestBody Item item) { + itemService.findById(itemId).map(i -> { i.setName(item.getName()); i.setQuantity(item.getQuantity()); i.setUnit(item.getUnit()); @@ -48,8 +48,8 @@ public class ItemController { } @DeleteMapping("/items/{itemId}") - public String deleteItem(@PathVariable(value = "itemId") Long itemId) { - return itemService.findById(itemId).map(p -> { + public void deleteItem(@PathVariable(value = "itemId") Long itemId) { + itemService.findById(itemId).map(p -> { itemService.deleteById(itemId); return "Item deleted"; }).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found")); diff --git a/backend/src/test/java/ItemTests.java b/backend/src/test/java/ItemTests.java new file mode 100644 index 0000000..f8d711a --- /dev/null +++ b/backend/src/test/java/ItemTests.java @@ -0,0 +1,137 @@ +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import whattocook.controller.ItemController; +import whattocook.models.Item; +import whattocook.models.Unit; + + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + + +@SpringBootTest(classes = whattocook.Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public final class ItemTests { + + + + @LocalServerPort + private int port; + @Autowired + private TestRestTemplate testRestTemplate; + + + @Test + public void postTest() { + Item item = new Item(5L, "kartoffel", Unit.GRAMMS, 5000); + + + assertThat(this.testRestTemplate.postForEntity("http://localhost:" + port + "/items", item, Item.class).getBody()).isEqualTo(item); + } + + @Test + public void getOneTest() { + Item item = new Item(1L, "tortillias", Unit.GRAMMS, 5000); + + + Long id= this.testRestTemplate.postForEntity("http://localhost:" + port + "/items", item, Item.class).getBody().getId(); + + assertTrue(compNotId(this.testRestTemplate.getForObject("http://localhost:" + port + "/items/"+id, Item.class),item)); + + } + + + @Test + public void getOneExeptionTest() { + + + + + assertThat(this.testRestTemplate.getForEntity("http://localhost:" + port + "/items/"+900000L, Item.class).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + + } + + @Test + public void getallTest() { + List items = new ArrayList(); + items.add( new Item(1L, "nachos", Unit.GRAMMS, 5000)); + items.add( new Item(2L, "wurst", Unit.GRAMMS, 5000)); + items.add( new Item(3L, "schinken", Unit.GRAMMS, 5000)); + items.add( new Item(4L, "brokkoli", Unit.GRAMMS, 5000)); + items.add( new Item(5L, "eiscreme", Unit.GRAMMS, 5000)); + + + for (Item item:items) { + this.testRestTemplate.postForEntity("http://localhost:" + port + "/items", item, Item.class); + } + + List getlist = this.testRestTemplate.getForObject("http://localhost:" + port + "/items", + List.class); + + assertThat(mapListToName(getlist,"name")).containsAll(items.stream().map(Item::getName).toList()); + + } + + @Test + public void deleteTest() { + Item item = new Item(16L, "elefantenfuß", Unit.GRAMMS, 5000); + + + long id=this.testRestTemplate.postForEntity("http://localhost:" + port + "/items", item, Item.class).getBody().getId(); + this.testRestTemplate.delete("http://localhost:" + port + "/items/"+id); + List getlist = this.testRestTemplate.getForObject("http://localhost:" + port + "/items", + List.class); + assertThat(mapListToName(getlist,"name")).doesNotContain(item.getName()); + } + + @Test + public void deleteExceptionTest() throws URISyntaxException { + ResponseEntity resp = testRestTemplate.exchange(new URI("http://localhost:" + port + "/items/"+70000), HttpMethod.DELETE, HttpEntity.EMPTY, Void.class); + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + + @Test + public void putTest() { + Item item = new Item(32L, "schokoküsse", Unit.GRAMMS, 5000); + Item changed = new Item(32L, "schokoküsse", Unit.GRAMMS, 45634); + + + long id= this.testRestTemplate.postForEntity("http://localhost:" + port + "/items", item, Item.class).getBody().getId(); + this.testRestTemplate.put("http://localhost:" + port + "/items/"+id, changed); + List getlist = this.testRestTemplate.getForObject("http://localhost:" + port + "/items", + List.class); + assertThat(mapListToName(getlist,"quantity")).contains(changed.getQuantity()); + } + + @Test + public void putExceptionTest() throws URISyntaxException { + Item changed = new Item(70000L, "gazellenkopf", Unit.GRAMMS, 80000); + String resourceUrl = + "http://localhost:" + port + "/items/"+700000; + HttpEntity requestUpdate = new HttpEntity<>(changed); + + ResponseEntity resp = testRestTemplate.exchange(resourceUrl, HttpMethod.PUT, requestUpdate,Void.class); + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + + private List mapListToName(List input, String param){ + return input.stream().map(t -> t.get(param)).toList(); + } + + private boolean compNotId(Item item1, Item item2){ + return item1.getName().equals(item2.getName())&&item1.getQuantity()==item2.getQuantity()&&item1.getUnit().equals(item2.getUnit()); + } +} diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml new file mode 100644 index 0000000..58d2a06 --- /dev/null +++ b/backend/src/test/resources/application.yml @@ -0,0 +1,13 @@ +server: + port: 9000 + +spring: + datasource: + url: jdbc:h2:mem:whattocook + username: sa + password: + driverClassName: org.h2.Driver + +logging: + level: + root: DEBUG \ No newline at end of file diff --git a/build.gradle b/build.gradle index a70d6c6..26ce9e8 100644 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,7 @@ sourceSets { srcDir 'backend/src/test/java' } resources { - 'backend/src/test/resources' + srcDir 'backend/src/test/resources' } } }