From 545aaa4fe48413dd962286260a18c7dc61e1183c Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Wed, 8 May 2024 20:19:15 +0700 Subject: [PATCH 01/10] [GREEN] make implementation for item repository --- .../ItemRepositoryTest.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java new file mode 100644 index 0000000..1d7cfef --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java @@ -0,0 +1,120 @@ +package id.ac.ui.cs.advprog.snackscription.repository; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; +public class ItemRepositoryTest { + + private ItemRepository itemRepository; + + @BeforeEach + public void setUp() { + itemRepository = new ItemRepository(); + } + + @Test + public void testSaveItem() { + // Create a new item + Item item = new Item(); + item.setName("Test Item"); + item.setQuantity(10); + item.setId("1"); + + // Save the item to the repository + Item savedItem = itemRepository.createItem(item); + + // Verify that the item is saved successfully + assertNotNull(savedItem.getId()); + assertEquals("Test Item", savedItem.getName()); + assertEquals(10.0, savedItem.getQuantity()); + } + + @Test + public void testFindItemById() { + // Create a new item + Item item = new Item(); + item.setName("Test Item"); + item.setQuantity(10); + item.setId("1"); + + // Save the item to the repository + Item savedItem = itemRepository.createItem(item); + + // Find the item by its ID + Item foundItem = itemRepository.getItemById(savedItem.getId()); + + // Verify that the item is found successfully + assertNotNull(foundItem); + assertEquals(savedItem.getId(), foundItem.getId()); + assertEquals("Test Item", foundItem.getName()); + assertEquals(10.0, foundItem.getQuantity()); + } + + @Test + public void testDeleteItem() { + // Create a new item + Item item = new Item(); + item.setName("Test Item"); + item.setQuantity(10); + item.setId("1"); + + // Save the item to the repository + Item savedItem = itemRepository.createItem(item); + + // Delete the item from the repository + Item deletedItem = itemRepository.deleteItem(savedItem); + + // Verify that the item is deleted successfully + assertNotNull(deletedItem); + assertEquals(savedItem.getId(), deletedItem.getId()); + assertEquals("Test Item", deletedItem.getName()); + assertEquals(10.0, deletedItem.getQuantity()); + } + + @Test + public void testEditItem() { + // Create a new item + Item item = new Item(); + item.setName("Test Item"); + item.setQuantity(10); + item.setId("1"); + + // Save the item to the repository + Item savedItem = itemRepository.createItem(item); + + // Edit the item + savedItem.setName("Edited Item"); + savedItem.setQuantity(20); + + // Edit the item in the repository + Item editedItem = itemRepository.editItem(savedItem); + + // Verify that the item is edited successfully + assertNotNull(editedItem); + assertEquals(savedItem.getId(), editedItem.getId()); + assertEquals("Edited Item", editedItem.getName()); + assertEquals(20.0, editedItem.getQuantity()); + } + + @Test + public void testGetItems() { + // Create a new item + Item item = new Item(); + item.setName("Test Item"); + item.setQuantity(10); + + // Save the item to the repository + Item savedItem = itemRepository.createItem(item); + + // Get all items from the repository + List items = itemRepository.getItems(); + + // Verify that the item is in the list of items + assertTrue(items.contains(savedItem)); + } + +} \ No newline at end of file From ad20f8dea1628a00936c97bae19528d93ff4cc05 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Wed, 8 May 2024 20:27:18 +0700 Subject: [PATCH 02/10] [RED] added test for Item --- .../model/ItemTest.java | 59 +++++++++++++++++++ ...toryTest.java => ItemsRepositoryTest.java} | 12 ++-- 2 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java rename src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/{ItemRepositoryTest.java => ItemsRepositoryTest.java} (92%) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java new file mode 100644 index 0000000..3e31d7c --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java @@ -0,0 +1,59 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ItemTest { + + Item item; + + @BeforeEach + + void setUp(){ + + this.item = new Item(); + + this.item.setItemId("eb558e9f-1c39-460e-8860-71af6af63bd6"); + + item.setItemName("Sampo cap Bambang"); + + item.setItemQuantity(100); + + } + + + + + @Test + + void testGetItemId(){ + + assertEquals("eb558e9f-1c39-460e-8860-71af6af63bd6", item.getItemId()); + + } + + + + + @Test + + void testGetItemName(){ + + assertEquals("Sampo cap Bambang", item.getItemName()); + + } + + + + + @Test + + void testGetItemQuantity(){ + + assertEquals(100, this.item.getItemQuantity()); + + } + +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java similarity index 92% rename from src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java rename to src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java index 1d7cfef..6a6b2d5 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemRepositoryTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java @@ -7,9 +7,9 @@ import java.util.List; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -public class ItemRepositoryTest { +public class ItemsRepositoryTest { - private ItemRepository itemRepository; + private ItemsRepository itemsRepository; @BeforeEach public void setUp() { @@ -19,13 +19,13 @@ public void setUp() { @Test public void testSaveItem() { // Create a new item - Item item = new Item(); + Items item = new Item(); item.setName("Test Item"); item.setQuantity(10); item.setId("1"); // Save the item to the repository - Item savedItem = itemRepository.createItem(item); + Item savedItem = itemsRepository.createItem(item); // Verify that the item is saved successfully assertNotNull(savedItem.getId()); @@ -108,10 +108,10 @@ public void testGetItems() { item.setQuantity(10); // Save the item to the repository - Item savedItem = itemRepository.createItem(item); + Item savedItem = itemsRepository.createItem(item); // Get all items from the repository - List items = itemRepository.getItems(); + List items = itemsRepository.getItems(); // Verify that the item is in the list of items assertTrue(items.contains(savedItem)); From 210899dd50c3bb2f10a3976da1c4b3985788afeb Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Fri, 24 May 2024 23:29:18 +0700 Subject: [PATCH 03/10] [RED] Added test for item --- .../factory/Factory.java | 1 + .../factory/ItemFactory.java | 16 +++ .../model/Item.java | 10 ++ .../model/ItemTest.java | 59 --------- .../service/ItemService.java | 5 + .../ItemsRepositoryTest.java | 120 ------------------ .../model/ItemTest.java | 40 ++++++ 7 files changed, 72 insertions(+), 179 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java delete mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java index f5f4d16..8bbbbb2 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java @@ -7,5 +7,6 @@ public interface Factory { T create(); + T create(String id, String name, int quantity); T create(String id, String name, String type, int price, List items ); } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java new file mode 100644 index 0000000..8f81092 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java @@ -0,0 +1,16 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; + +public class ItemFactory implements Factory { + @Override + public Item create(){ + return new Item(); + } + + public Item create(String id, String name, int quantity){ + return new Item(id, name, quantity); + } +} + + + diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java index d2b73c6..dc9049f 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java @@ -20,4 +20,14 @@ public class Item { @ManyToMany(mappedBy = "items") private List subscriptionBoxes; + + // Konstruktor default + public Item() {} + + // Konstruktor dengan parameter + public Item(String id, String name, int quantity) { + this.id = id; + this.name = name; + this.quantity = quantity; + } } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java deleted file mode 100644 index 3e31d7c..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -class ItemTest { - - Item item; - - @BeforeEach - - void setUp(){ - - this.item = new Item(); - - this.item.setItemId("eb558e9f-1c39-460e-8860-71af6af63bd6"); - - item.setItemName("Sampo cap Bambang"); - - item.setItemQuantity(100); - - } - - - - - @Test - - void testGetItemId(){ - - assertEquals("eb558e9f-1c39-460e-8860-71af6af63bd6", item.getItemId()); - - } - - - - - @Test - - void testGetItemName(){ - - assertEquals("Sampo cap Bambang", item.getItemName()); - - } - - - - - @Test - - void testGetItemQuantity(){ - - assertEquals(100, this.item.getItemQuantity()); - - } - -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java new file mode 100644 index 0000000..f8a3f27 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java @@ -0,0 +1,5 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + +public class ItemService { + +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java deleted file mode 100644 index 6a6b2d5..0000000 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/ItemsRepositoryTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription.repository; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -import java.util.List; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -public class ItemsRepositoryTest { - - private ItemsRepository itemsRepository; - - @BeforeEach - public void setUp() { - itemRepository = new ItemRepository(); - } - - @Test - public void testSaveItem() { - // Create a new item - Items item = new Item(); - item.setName("Test Item"); - item.setQuantity(10); - item.setId("1"); - - // Save the item to the repository - Item savedItem = itemsRepository.createItem(item); - - // Verify that the item is saved successfully - assertNotNull(savedItem.getId()); - assertEquals("Test Item", savedItem.getName()); - assertEquals(10.0, savedItem.getQuantity()); - } - - @Test - public void testFindItemById() { - // Create a new item - Item item = new Item(); - item.setName("Test Item"); - item.setQuantity(10); - item.setId("1"); - - // Save the item to the repository - Item savedItem = itemRepository.createItem(item); - - // Find the item by its ID - Item foundItem = itemRepository.getItemById(savedItem.getId()); - - // Verify that the item is found successfully - assertNotNull(foundItem); - assertEquals(savedItem.getId(), foundItem.getId()); - assertEquals("Test Item", foundItem.getName()); - assertEquals(10.0, foundItem.getQuantity()); - } - - @Test - public void testDeleteItem() { - // Create a new item - Item item = new Item(); - item.setName("Test Item"); - item.setQuantity(10); - item.setId("1"); - - // Save the item to the repository - Item savedItem = itemRepository.createItem(item); - - // Delete the item from the repository - Item deletedItem = itemRepository.deleteItem(savedItem); - - // Verify that the item is deleted successfully - assertNotNull(deletedItem); - assertEquals(savedItem.getId(), deletedItem.getId()); - assertEquals("Test Item", deletedItem.getName()); - assertEquals(10.0, deletedItem.getQuantity()); - } - - @Test - public void testEditItem() { - // Create a new item - Item item = new Item(); - item.setName("Test Item"); - item.setQuantity(10); - item.setId("1"); - - // Save the item to the repository - Item savedItem = itemRepository.createItem(item); - - // Edit the item - savedItem.setName("Edited Item"); - savedItem.setQuantity(20); - - // Edit the item in the repository - Item editedItem = itemRepository.editItem(savedItem); - - // Verify that the item is edited successfully - assertNotNull(editedItem); - assertEquals(savedItem.getId(), editedItem.getId()); - assertEquals("Edited Item", editedItem.getName()); - assertEquals(20.0, editedItem.getQuantity()); - } - - @Test - public void testGetItems() { - // Create a new item - Item item = new Item(); - item.setName("Test Item"); - item.setQuantity(10); - - // Save the item to the repository - Item savedItem = itemsRepository.createItem(item); - - // Get all items from the repository - List items = itemsRepository.getItems(); - - // Verify that the item is in the list of items - assertTrue(items.contains(savedItem)); - } - -} \ No newline at end of file diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java new file mode 100644 index 0000000..4343909 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemTest.java @@ -0,0 +1,40 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class ItemTest { + + @Test + public void testDefaultConstructor() { + Item item = new Item(); + assertNull(item.getId()); + assertNull(item.getName()); + assertEquals(0, item.getQuantity()); + } + + @Test + public void testParameterizedConstructor() { + String id = "123"; + String name = "Snack A"; + int quantity = 5; + + Item item = new Item(id, name, quantity); + assertEquals(id, item.getId()); + assertEquals(name, item.getName()); + assertEquals(quantity, item.getQuantity()); + } + + @Test + public void testSettersAndGetters() { + Item item = new Item(); + item.setId("456"); + item.setName("Snack B"); + item.setQuantity(10); + + assertEquals("456", item.getId()); + assertEquals("Snack B", item.getName()); + assertEquals(10, item.getQuantity()); + } +} From 915aeb488e0ee32e6046dfdf47c42ed8297af98a Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Fri, 24 May 2024 23:33:01 +0700 Subject: [PATCH 04/10] [RED] Added test for ItemRepository --- .../repository/ItemRepositoryTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepositoryTest.java diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepositoryTest.java new file mode 100644 index 0000000..b65e3a7 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepositoryTest.java @@ -0,0 +1,56 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class ItemRepositoryTest { + + private ItemRepository itemRepository; + + @BeforeEach + public void setUp() { + itemRepository = new ItemRepository(); + } + + @Test + public void testCreateItem() { + Item item = new Item("1", "Snack A", 5); + Item createdItem = itemRepository.createItem(item); + assertEquals(item, createdItem); + } + + @Test + public void testGetItemById() { + Item item = new Item("1", "Snack A", 5); + itemRepository.createItem(item); + Item retrievedItem = itemRepository.getItemById("1"); + assertEquals(item, retrievedItem); + } + + @Test + public void testDeleteItem() { + Item item = new Item("1", "Snack A", 5); + itemRepository.createItem(item); + Item deletedItem = itemRepository.deleteItem(item); + assertEquals(item, deletedItem); + assertNull(itemRepository.getItemById("1")); + } + + @Test + public void testEditItem() { + Item item = new Item("1", "Snack A", 5); + itemRepository.createItem(item); + + Item updatedItem = new Item("1", "Snack B", 10); + Item editedItem = itemRepository.editItem(updatedItem); + assertEquals(updatedItem, editedItem); + + Item retrievedItem = itemRepository.getItemById("1"); + assertEquals("Snack B", retrievedItem.getName()); + assertEquals(10, retrievedItem.getQuantity()); + } +} + From 5b4e36accfa18e14d116e8e0f5375854bc6b8280 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Sat, 25 May 2024 00:48:26 +0700 Subject: [PATCH 05/10] [RED] Added test for ItemFactory --- .../controller/ItemController.java | 4 ++ .../factory/ItemFactoryTest.java | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactoryTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java new file mode 100644 index 0000000..d3c95e7 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java @@ -0,0 +1,4 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; + +public class ItemController { +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactoryTest.java new file mode 100644 index 0000000..515a99f --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactoryTest.java @@ -0,0 +1,45 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import org.junit.jupiter.api.Test; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class ItemFactoryTest { + + @Test + public void testCreateDefaultItem() { + ItemFactory itemFactory = new ItemFactory(); + Item item = itemFactory.create(); + + assertTrue(isValidUUID(item.getId())); + assertNull(item.getName()); + assertEquals(0, item.getQuantity()); + } + + @Test + public void testCreateItemWithParameters() { + ItemFactory itemFactory = new ItemFactory(); + String id = UUID.randomUUID().toString(); + String name = "Snack A"; + int quantity = 5; + + Item item = itemFactory.create(id, name, quantity); + + // Memeriksa apakah nilai parameter sesuai dengan nilai yang diatur di item + assertEquals(id, item.getId()); + assertEquals(name, item.getName()); + assertEquals(quantity, item.getQuantity()); + } + + private boolean isValidUUID(String uuidString) { + try { + UUID.fromString(uuidString); + return true; + } catch (IllegalArgumentException e) { + return false; + } + } +} + From 9ccea88b8458bf5f470ab7281b12972c40ce9953 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Sat, 25 May 2024 00:52:08 +0700 Subject: [PATCH 06/10] [GREEN] Implement new controller for item --- .../controller/ItemController.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java index d3c95e7..640a792 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java @@ -1,4 +1,61 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/items") public class ItemController { + + private final List items = new ArrayList<>(); + + @GetMapping + public ResponseEntity> getAllItems() { + return new ResponseEntity<>(items, HttpStatus.OK); + } + + @GetMapping("/{id}") + public ResponseEntity getItemById(@PathVariable String id) { + for (Item item : items) { + if (item.getId().equals(id)) { + return new ResponseEntity<>(item, HttpStatus.OK); + } + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @PostMapping + public ResponseEntity createItem(@RequestBody Item item) { + items.add(item); + return new ResponseEntity<>(item, HttpStatus.CREATED); + } + + @PutMapping("/{id}") + public ResponseEntity updateItem(@PathVariable String id, @RequestBody Item updatedItem) { + for (Item item : items) { + if (item.getId().equals(id)) { + item.setName(updatedItem.getName()); + item.setQuantity(updatedItem.getQuantity()); + return new ResponseEntity<>(item, HttpStatus.OK); + } + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteItem(@PathVariable String id) { + for (Item item : items) { + if (item.getId().equals(id)) { + items.remove(item); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } } From 48f9874661fc417c8d8183168077195949429ddb Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Sat, 25 May 2024 01:14:40 +0700 Subject: [PATCH 07/10] [RED] Added test for new ItemController --- .../controller/ItemController.java | 10 ++- .../factory/ItemFactory.java | 7 ++ .../controller/ItemControllerTest.java | 86 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemControllerTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java index 640a792..46c1d8b 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.java @@ -2,6 +2,7 @@ import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import lombok.Getter; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -9,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +@Getter @RestController @RequestMapping("/items") public class ItemController { @@ -58,4 +60,10 @@ public ResponseEntity deleteItem(@PathVariable String id) { } return new ResponseEntity<>(HttpStatus.NOT_FOUND); } -} + + public void setItems(List items) { + this.items.clear(); + this.items.addAll(items); + } + +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java index 8f81092..bea016e 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.java @@ -1,6 +1,8 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import java.util.List; + public class ItemFactory implements Factory { @Override public Item create(){ @@ -10,6 +12,11 @@ public Item create(){ public Item create(String id, String name, int quantity){ return new Item(id, name, quantity); } + + @Override + public Item create(String id, String name, String type, int price, List items) { + return null; + } } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemControllerTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemControllerTest.java new file mode 100644 index 0000000..76f69af --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemControllerTest.java @@ -0,0 +1,86 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; + + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ItemControllerTest { + + private ItemController itemController; + private List items; + + @BeforeEach + public void setUp() { + itemController = new ItemController(); + items = new ArrayList<>(); + items.add(new Item("1", "Snack A", 5)); + items.add(new Item("2", "Snack B", 10)); + itemController.setItems(items); + } + + @Test + public void testGetAllItems() { + ResponseEntity> responseEntity = itemController.getAllItems(); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals(items, responseEntity.getBody()); + } + + @Test + public void testGetItemById() { + ResponseEntity responseEntity = itemController.getItemById("1"); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals("Snack A", Objects.requireNonNull(responseEntity.getBody()).getName()); + } + + @Test + public void testGetItemById_NotFound() { + ResponseEntity responseEntity = itemController.getItemById("100"); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); + } + + @Test + public void testCreateItem() { + Item newItem = new Item("3", "Snack C", 8); + ResponseEntity responseEntity = itemController.createItem(newItem); + assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode()); + assertEquals(newItem, responseEntity.getBody()); + } + + @Test + public void testUpdateItem() { + Item updatedItem = new Item("1", "New Snack A", 7); + ResponseEntity responseEntity = itemController.updateItem("1", updatedItem); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals(updatedItem, responseEntity.getBody()); + } + + @Test + public void testUpdateItem_NotFound() { + Item updatedItem = new Item("100", "New Snack A", 7); + ResponseEntity responseEntity = itemController.updateItem("100", updatedItem); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); + } + + @Test + public void testDeleteItem() { + ResponseEntity responseEntity = itemController.deleteItem("1"); + assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode()); + assertEquals(1, itemController.getItems().size()); + } + + @Test + public void testDeleteItem_NotFound() { + ResponseEntity responseEntity = itemController.deleteItem("100"); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); + } + +} From 435ae40087030e3af2301b3ef94790ea4c91c6fe Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Sat, 25 May 2024 11:11:35 +0700 Subject: [PATCH 08/10] Added async function --- ...ackscriptionSubscriptionboxApplication.java | 18 ++++++++++++++++++ .../factory/SubscriptionBoxFactory.java | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplication.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplication.java index 0e6c4d6..4ecf056 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplication.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplication.java @@ -2,6 +2,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +@EnableAsync @SpringBootApplication public class SnackscriptionSubscriptionboxApplication { @@ -10,4 +17,15 @@ public static void main(String[] args) { SpringApplication.run(SnackscriptionSubscriptionboxApplication.class, args); } + @Bean + public Executor taskExecutor () { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(1); + executor.setMaxPoolSize(1); + executor.setQueueCapacity(500); + executor.setThreadNamePrefix("GithubLookup-"); + executor.initialize(); + return executor; + } + } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java index c35f306..93aba1e 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java @@ -11,6 +11,11 @@ public SubscriptionBox create(){ return new SubscriptionBox(); } + @Override + public SubscriptionBox create(String id, String name, int quantity) { + return null; + } + public SubscriptionBox create(String id, String name, String type, int price, List items ){ return new SubscriptionBox( name, type, price, items); } From 76b767f91386447d94895789fcdd8fc8f46222d4 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Sat, 25 May 2024 12:32:30 +0700 Subject: [PATCH 09/10] [RED] Added test for ItemServiceImpl.java --- .../repository/ItemRepository.java | 5 ++ .../service/ItemService.java | 10 ++- .../service/ItemServiceImpl.java | 56 ++++++++++++ .../service/ItemServiceImplTest.java | 88 +++++++++++++++++++ 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImpl.java create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImplTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.java index 1ee048c..184aae5 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.java @@ -36,6 +36,11 @@ public Item editItem(Item item) { } return null; } + + public List getAllItems() { + return new ArrayList<>(items); + } + } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java index f8a3f27..ed8eda3 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.java @@ -1,5 +1,13 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; -public class ItemService { +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import java.util.List; + +public interface ItemService { + Item createItem(String id, String name, int quantity); + Item getItemById(String id); + Item deleteItem(String id); + Item editItem(String id, String name, int quantity); + List getAllItems(); } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImpl.java new file mode 100644 index 0000000..f0dc520 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImpl.java @@ -0,0 +1,56 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.ItemRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ItemServiceImpl implements ItemService { + + private final ItemRepository itemRepository; + + @Autowired + public ItemServiceImpl(ItemRepository itemRepository) { + this.itemRepository = itemRepository; + } + + @Override + public Item createItem(String id, String name, int quantity) { + Item item = new Item(id, name, quantity); + return itemRepository.createItem(item); + } + + @Override + public Item getItemById(String id) { + return itemRepository.getItemById(id); + } + + @Override + public Item deleteItem(String id) { + Item item = itemRepository.getItemById(id); + if (item != null) { + return itemRepository.deleteItem(item); + } + return null; + } + + @Override + public Item editItem(String id, String name, int quantity) { + Item existingItem = itemRepository.getItemById(id); + if (existingItem != null) { + existingItem.setName(name); + existingItem.setQuantity(quantity); + return itemRepository.editItem(existingItem); + } + return null; + } + + @Override + public List getAllItems() { + return itemRepository.getAllItems(); + } +} + diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImplTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImplTest.java new file mode 100644 index 0000000..cda7497 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImplTest.java @@ -0,0 +1,88 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.ItemRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class ItemServiceImplTest { + + private ItemServiceImpl itemService; + + @BeforeEach + void setUp() { + ItemRepository itemRepository = new ItemRepository(); + itemService = new ItemServiceImpl(itemRepository); + } + + @Test + void testCreateItem() { + Item createdItem = itemService.createItem("1", "Item 1", 10); + + assertNotNull(createdItem); + assertEquals("1", createdItem.getId()); + assertEquals("Item 1", createdItem.getName()); + assertEquals(10, createdItem.getQuantity()); + } + + @Test + void testGetItemById() { + itemService.createItem("1", "Item 1", 10); + Item fetchedItem = itemService.getItemById("1"); + + assertNotNull(fetchedItem); + assertEquals("1", fetchedItem.getId()); + assertEquals("Item 1", fetchedItem.getName()); + assertEquals(10, fetchedItem.getQuantity()); + } + + @Test + void testDeleteItem() { + itemService.createItem("1", "Item 1", 10); + Item deletedItem = itemService.deleteItem("1"); + + assertNotNull(deletedItem); + assertEquals("1", deletedItem.getId()); + assertEquals("Item 1", deletedItem.getName()); + assertEquals(10, deletedItem.getQuantity()); + + assertNull(itemService.getItemById("1")); + } + + @Test + void testEditItem() { + itemService.createItem("1", "Item 1", 10); + Item editedItem = itemService.editItem("1", "Item 1 Edited", 20); + + assertNotNull(editedItem); + assertEquals("1", editedItem.getId()); + assertEquals("Item 1 Edited", editedItem.getName()); + assertEquals(20, editedItem.getQuantity()); + } + + @Test + void testGetAllItems() { + itemService.createItem("1", "Item 1", 10); + itemService.createItem("2", "Item 2", 20); + + List allItems = itemService.getAllItems(); + + assertNotNull(allItems); + assertEquals(2, allItems.size()); + + Item firstItem = allItems.getFirst(); + assertEquals("1", firstItem.getId()); + assertEquals("Item 1", firstItem.getName()); + assertEquals(10, firstItem.getQuantity()); + + Item secondItem = allItems.get(1); + assertEquals("2", secondItem.getId()); + assertEquals("Item 2", secondItem.getName()); + assertEquals(20, secondItem.getQuantity()); + } +} From 860d6b13bb457d0e31b26d7046055a7253fb8525 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Sat, 25 May 2024 20:35:20 +0700 Subject: [PATCH 10/10] fixing merge-conflict --- .../snackscription_subscriptionbox/model/Item.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java index dc9049f..b83498d 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java @@ -1,5 +1,6 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; +import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @@ -19,15 +20,15 @@ public class Item { private int quantity; @ManyToMany(mappedBy = "items") + @JsonBackReference private List subscriptionBoxes; - // Konstruktor default - public Item() {} - - // Konstruktor dengan parameter public Item(String id, String name, int quantity) { this.id = id; this.name = name; this.quantity = quantity; } + + public Item() { + } } \ No newline at end of file