diff --git a/backend/src/main/whattocook/configs/RepositoryConfig.java b/backend/src/main/whattocook/configs/RepositoryConfig.java index 0d879c6..6338abc 100644 --- a/backend/src/main/whattocook/configs/RepositoryConfig.java +++ b/backend/src/main/whattocook/configs/RepositoryConfig.java @@ -12,7 +12,6 @@ class RepositoryConfig implements RepositoryRestConfigurer { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) { config.exposeIdsFor(Item.class); - config.setBasePath("/api/v1"); } } diff --git a/backend/src/main/whattocook/controller/ItemController.java b/backend/src/main/whattocook/controller/ItemController.java deleted file mode 100644 index 1a244f9..0000000 --- a/backend/src/main/whattocook/controller/ItemController.java +++ /dev/null @@ -1,57 +0,0 @@ -package whattocook.controller; - -import whattocook.exception.ItemNotFoundException; -import lombok.extern.slf4j.Slf4j; -import whattocook.models.Item; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; -import whattocook.services.ItemService; - -import java.util.List; -@Slf4j -@RestController() -public class ItemController { - - @Autowired - private ItemService itemService; - - @GetMapping("/item") - public Item getItem(@RequestParam String name){ - return itemService.findByName(name).orElseThrow(() -> new ItemNotFoundException("item " + name + " not found")); - } - - @GetMapping("/items") - public List getItemList() { - return itemService.findAll(); - } - - @GetMapping("/items/{itemId}") - public Item getItem(@PathVariable(value = "itemId") Long itemId) { - return itemService.findById(itemId).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found")); - } - - @PostMapping("/items") - public Item createItem(@RequestBody Item item) { - return itemService.save(item); - - } - - @PutMapping("/items/{itemId}") - 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()); - itemService.save(i); - return "Item updated"; - }).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found")); - } - - @DeleteMapping("/items/{itemId}") - 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/main/whattocook/implementation/ItemServiceImpl.java b/backend/src/main/whattocook/implementation/ItemServiceImpl.java index 5264498..5b174b7 100644 --- a/backend/src/main/whattocook/implementation/ItemServiceImpl.java +++ b/backend/src/main/whattocook/implementation/ItemServiceImpl.java @@ -6,7 +6,6 @@ import org.springframework.stereotype.Service; import whattocook.repositories.ItemRepository; import whattocook.services.ItemService; -import java.util.List; import java.util.Optional; @Service @@ -36,7 +35,7 @@ public class ItemServiceImpl implements ItemService { } @Override - public List findAll() { + public Iterable findAll() { return itemRepository.findAll(); } } diff --git a/backend/src/main/whattocook/models/Item.java b/backend/src/main/whattocook/models/Item.java index 9f8f094..e520f36 100644 --- a/backend/src/main/whattocook/models/Item.java +++ b/backend/src/main/whattocook/models/Item.java @@ -1,18 +1,22 @@ package whattocook.models; import lombok.*; +import org.hibernate.Hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import java.util.Objects; @NoArgsConstructor -@AllArgsConstructor -@Data +@Getter +@Setter +@ToString @Entity public class Item { + @Setter(AccessLevel.NONE) @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -22,4 +26,27 @@ public class Item { private Unit unit; private int quantity; + + public Item(String name, Unit unit, int quantity){ + this.name = name; + this.unit = unit; + this.quantity = quantity; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) { + return false; + } + Item item = (Item) o; + return id != null && Objects.equals(id, item.id); + } + + @Override + public int hashCode() { + return getClass().hashCode(); + } } \ No newline at end of file diff --git a/backend/src/main/whattocook/repositories/ItemRepository.java b/backend/src/main/whattocook/repositories/ItemRepository.java index 2b3edce..d9caa8a 100644 --- a/backend/src/main/whattocook/repositories/ItemRepository.java +++ b/backend/src/main/whattocook/repositories/ItemRepository.java @@ -1,12 +1,12 @@ package whattocook.repositories; +import org.springframework.data.repository.CrudRepository; import whattocook.models.Item; -import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository -public interface ItemRepository extends JpaRepository { +public interface ItemRepository extends CrudRepository { Optional findByName(String name); } diff --git a/backend/src/main/whattocook/services/ItemService.java b/backend/src/main/whattocook/services/ItemService.java index 86f1d0d..5db566f 100644 --- a/backend/src/main/whattocook/services/ItemService.java +++ b/backend/src/main/whattocook/services/ItemService.java @@ -2,7 +2,6 @@ package whattocook.services; import whattocook.models.Item; -import java.util.List; import java.util.Optional; public interface ItemService { @@ -10,5 +9,5 @@ public interface ItemService { void deleteById(Long id); Optional findById(long id); Optional findByName(String name); - List findAll(); + Iterable findAll(); } diff --git a/backend/src/resources/application.yml b/backend/src/resources/application.yml index 58d2a06..32275c8 100644 --- a/backend/src/resources/application.yml +++ b/backend/src/resources/application.yml @@ -7,6 +7,9 @@ spring: username: sa password: driverClassName: org.h2.Driver + data: + rest: + base-path: /api/v1 logging: level: diff --git a/backend/src/test/java/items/ItemTests.java b/backend/src/test/java/items/ItemTests.java index 490960e..ff27ce7 100644 --- a/backend/src/test/java/items/ItemTests.java +++ b/backend/src/test/java/items/ItemTests.java @@ -1,139 +1,107 @@ package items; +import org.junit.jupiter.api.BeforeEach; 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 whattocook.repositories.ItemRepository; -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 { +final class ItemTests { - - - @LocalServerPort - private int port; @Autowired - private TestRestTemplate testRestTemplate; + private ItemRepository itemRepository; - - @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); + @BeforeEach + private void resetItems() { + itemRepository.deleteAll(); } @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)); + public void saveTest() { + Item item = new Item("kartoffel", Unit.GRAMMS, 5000); + itemRepository.save(item); + assertTrue(itemRepository.existsById(item.getId())); + } + @Test + public void findByIDTest() { + Item item = new Item("tortillias", Unit.GRAMMS, 5000); + itemRepository.save(item); + assertEquals(item, itemRepository.findById(item.getId()).get()); } @Test - public void getOneExeptionTest() { - - - - - assertThat(this.testRestTemplate.getForEntity("http://localhost:" + port + "/items/"+900000L, Item.class).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); - + public void findByIdNotPresentTest() { + assertFalse(itemRepository.findById(77L).isPresent()); } @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)); + public void findByNameTest(){ + Item item = new Item("tortillias", Unit.GRAMMS, 5000); + itemRepository.save(item); + assertEquals(item, itemRepository.findByName(item.getName()).get()); + } + @Test + public void findAllTest() { + List savedItems = new ArrayList(); + savedItems.add( new Item("nachos", Unit.GRAMMS, 5000)); + savedItems.add( new Item("wurst", Unit.GRAMMS, 5000)); + savedItems.add( new Item("schinken", Unit.GRAMMS, 5000)); + savedItems.add( new Item("brokkoli", Unit.GRAMMS, 5000)); + savedItems.add( new Item("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()); + itemRepository.saveAll(savedItems); + List foundItems = (List)itemRepository.findAll(); + assertTrue(compListNotId(savedItems, foundItems)); } @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()); + Item item = new Item("elefantenfuß", Unit.GRAMMS, 5000); + itemRepository.save(item); + assertEquals(item, itemRepository.findById(item.getId()).get()); + itemRepository.delete(item); + assertTrue(itemRepository.findById(item.getId()).isEmpty()); } @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(); + public void updateTest() { + Item item = new Item("schokoküsse", Unit.GRAMMS, 5000); + itemRepository.save(item); + long itemCount = itemRepository.count(); + item.setQuantity(4574); + itemRepository.save(item); + assertEquals(itemCount, itemRepository.count()); + assertEquals(4574, itemRepository.findById(item.getId()).get().getQuantity()); } private boolean compNotId(Item item1, Item item2){ return item1.getName().equals(item2.getName())&&item1.getQuantity()==item2.getQuantity()&&item1.getUnit().equals(item2.getUnit()); } + + private boolean compListNotId(List expected, List actual){ + if (expected.size() != actual.size()){ + return false; + } + for (int i = 0; i < expected.size(); i++){ + if(!compNotId(expected.get(i), actual.get(i))){ + return false; + } + } + return true; + } } diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index 58d2a06..32275c8 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -7,6 +7,9 @@ spring: username: sa password: driverClassName: org.h2.Driver + data: + rest: + base-path: /api/v1 logging: level: