From 4b792e3ab99074a71795fabb2f1f1c0aa9c2e4e1 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 24 Apr 2024 19:06:17 +0700 Subject: [PATCH 01/28] adding packages --- .../controller/SubscriptionBoxController.java | 15 +++++++++++++ .../model/Items.java | 11 ++++++++++ .../model/SubscriptionBox.java | 18 ++++++++++++++++ .../repository/SubscriptionBoxRepository.java | 15 +++++++++++++ .../service/SubscriptionBoxService.java | 10 +++++++++ .../service/SubscriptionBoxServiceImpl.java | 21 +++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java new file mode 100644 index 0000000..2c2bae0 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -0,0 +1,15 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + + +@Controller +@RequestMapping("/") +public class SubscriptionBoxController { + @GetMapping("/Subscription-Box-Admin") + public String homePage() { + return "SubscriptionBoxAdmin"; + } +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java new file mode 100644 index 0000000..ed1ff18 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java @@ -0,0 +1,11 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class Items { + private String itemsId; + private String itemsName; + private int itemsQuantity; +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java new file mode 100644 index 0000000..3d8df0e --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -0,0 +1,18 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + + +import java.util.Date; +import java.util.List; + +import lombok.Getter; +import lombok.Setter; +@Getter@Setter +public class SubscriptionBox { + private String id; + private String name; + private String category; + private String photo; //Box Thumbnail Address from static folder + private List price; // Prices according to subscription length (1 month/3 month/ 6 month) + private List items; // Item Model + private Date dateCreated; +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java new file mode 100644 index 0000000..dc46ab0 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -0,0 +1,15 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import org.springframework.stereotype.Repository; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Repository +public class SubscriptionBoxRepository { + + // Add more methods as needed for your application. +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java new file mode 100644 index 0000000..a27761c --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java @@ -0,0 +1,10 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; + +import java.util.List; + +public interface SubscriptionBoxService { + + // Add any other service methods required by your application. +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java new file mode 100644 index 0000000..1d46a9c --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java @@ -0,0 +1,21 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class SubscriptionBoxServiceImpl implements SubscriptionBoxService { + + @Autowired + private SubscriptionBoxRepository subscriptionBoxRepository; + + + + + + +} \ No newline at end of file From ca0fa2675c0a9f87d22063703285fdc87aa0f05d Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 24 Apr 2024 19:26:19 +0700 Subject: [PATCH 02/28] [RED] Make Tests for Subscription Box --- .../model/SubscriptionBox.java | 8 +-- .../model/SubscriptionBoxTest.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 3d8df0e..33b463f 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -8,11 +8,5 @@ import lombok.Setter; @Getter@Setter public class SubscriptionBox { - private String id; - private String name; - private String category; - private String photo; //Box Thumbnail Address from static folder - private List price; // Prices according to subscription length (1 month/3 month/ 6 month) - private List items; // Item Model - private Date dateCreated; + } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java new file mode 100644 index 0000000..4cc4fd3 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -0,0 +1,55 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; +import java.time.LocalDate; + +import org.junit.jupiter.api.BeforeEach; + + + public class SubscriptionBoxTests { + + private SubscriptionBox box; + private List validPrices; + private List items; + + @BeforeEach + public void setup() { + // Initialize common test data + validPrices = Arrays.asList(100, 270, 480); + items = Arrays.asList(new Items(), new Items()); + box = new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", validPrices, items, LocalDate.now()); + } + + @Test + public void testCreateSubscriptionBox_Valid() { + assertNotNull(box.getId()); + assertEquals("ChocoBox", box.getName()); + assertEquals("Food", box.getCategory()); + assertEquals(3, box.getPrices().size()); + assertEquals(2, box.getItems().size()); + assertNotNull(box.getDateCreated()); + } + + @Test + public void testSubscriptionBox_NegativePrices() { + List negativePrices = Arrays.asList(100, -270, 480); + assertThrows(IllegalArgumentException.class, () -> { + new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", negativePrices, items, LocalDate.now()); + }); + } + + @Test + public void testSubscriptionBox_InvalidPriceCount() { + List twoPrices = Arrays.asList(100, 270); // Only two prices + assertThrows(IllegalArgumentException.class, () -> { + new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", twoPrices, items, LocalDate.now()); + }); + } + + // Additional tests can also utilize the setup data + } + + From 16e4f0589e4e5af3541aea2d2540938476466fec Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 24 Apr 2024 19:45:31 +0700 Subject: [PATCH 03/28] [GREEN] Implement Subscription Model --- .../model/SubscriptionBox.java | 31 ++++++++++++++++++- .../model/SubscriptionBoxTest.java | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 33b463f..edfa47a 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -1,12 +1,41 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; -import java.util.Date; +import java.time.LocalDate; import java.util.List; +import java.util.stream.Collectors; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; import lombok.Getter; import lombok.Setter; @Getter@Setter public class SubscriptionBox { + private String id; + private String name; + private String category; + private String photo; + private List prices; + private List items; + private LocalDate dateCreated; + public SubscriptionBox(String id, String name, String category, String photo, List prices, List items, LocalDate dateCreated) { + this.id = id; + this.name = name; + this.category = category; + this.photo = photo; + if (prices.size() != 3) { + throw new IllegalArgumentException("Prices list must contain exactly three elements"); + } + Collectors Collectors = null; + this.prices = prices.stream() + .filter(price -> price > 0) + .collect(Collectors.toList()); + if (this.prices.size() != prices.size()) { + throw new IllegalArgumentException("Prices must be positive"); + } + this.items = items; + this.dateCreated = dateCreated; + } + + // Getters and setters } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 4cc4fd3..8820272 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -9,7 +9,7 @@ import org.junit.jupiter.api.BeforeEach; - public class SubscriptionBoxTests { + public class SubscriptionBoxTest { private SubscriptionBox box; private List validPrices; From 8f8c6ce8a884ad392f3b617e9f58beb2aa81e0d4 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Wed, 24 Apr 2024 20:02:57 +0700 Subject: [PATCH 04/28] Add models, repo, service, controller --- .../controller/AddItemController.java | 15 ++++++++++++ .../model/Admin.java | 11 +++++++++ .../model/Items.java | 14 +++++++++++ .../repository/ItemsRepository.java | 23 +++++++++++++++++++ .../service/ItemsService.java | 9 ++++++++ .../service/ItemsServiceImpl.java | 9 ++++++++ 6 files changed, 81 insertions(+) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Admin.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java new file mode 100644 index 0000000..c7e1331 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java @@ -0,0 +1,15 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + + +@Controller +@RequestMapping("/") +public class SubscriptionBoxController { + @GetMapping("/Add-Item-Admin") + public String homePage() { + return "AddItemAdmin"; + } +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Admin.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Admin.java new file mode 100644 index 0000000..885730a --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Admin.java @@ -0,0 +1,11 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class Admin { + private String username; + private String password; + private int itemsQuantity; +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java new file mode 100644 index 0000000..e23dad9 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java @@ -0,0 +1,14 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class Items { + private String itemsId; + private String itemsName; + private int itemsQuantity; + private double price; + + +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java new file mode 100644 index 0000000..3937823 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java @@ -0,0 +1,23 @@ +package id.ac.ui.cs.advprog.snackscription.repository; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +@Repository +public class ItemsRepository { + private List itemsData = new ArrayList<>(); + + public Items create(Items items) { + itemsData.add(items); + return items; + } + + public Iterator findAll() { + return itemsData.iterator(); + } + +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java new file mode 100644 index 0000000..d66a67c --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java @@ -0,0 +1,9 @@ +package id.ac.ui.cs.advprog.snackscription.service; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; +import java.util.List; + +public interface ItemsService { + public Items create(Items items); + public List findAll(); +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java new file mode 100644 index 0000000..42f06c7 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java @@ -0,0 +1,9 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; +import java.util.List; + +public interface ItemsService { + public Items create(Items items); + public List findAll(); +} From 2136c191150ff7d53f14d740472b0ae7c8dd8370 Mon Sep 17 00:00:00 2001 From: sitaamirasyarifah Date: Wed, 24 Apr 2024 20:09:43 +0700 Subject: [PATCH 05/28] implements factory method pattern & observer pattern --- .../model/Items.java | 14 ++++++++++++++ .../model/ItemsFactory.java | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java index e23dad9..ded0b66 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java @@ -2,6 +2,8 @@ import lombok.Getter; import lombok.Setter; +import java.util.ArrayList; +import java.util.List; @Getter @Setter public class Items { @@ -10,5 +12,17 @@ public class Items { private int itemsQuantity; private double price; + private List observers = new ArrayList<>(); + + public void addObserver(ItemObserver observer) { + observers.add(observer); + } + + public void notifyObservers() { + for (ItemObserver observer : observers) { + observer.update(this); + } + } + } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java new file mode 100644 index 0000000..4c27096 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java @@ -0,0 +1,12 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +public class ItemsFactory { + public static Items createItems(String type) { + if (type.equalsIgnoreCase("snack")) { + return new Snack(); + } else if (type.equalsIgnoreCase("drink")) { + return new Drink(); + } + return null; + } +} From a451b7307b22c11c5b7fbcbae0518490eb6dfac9 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 24 Apr 2024 20:23:48 +0700 Subject: [PATCH 06/28] [REFACTOR] Impelenting Decorator Pattern, Adding Skeleton Model --- .../controller/SubscriptionBoxController.java | 2 +- .../model/BasicSubscriptionBox.java | 4 ++ .../MonthlySubscriptionBoxDecorator.java | 4 ++ .../QuarterlySubscriptionBoxDecorator.java | 4 ++ .../SemiAnnualSubscriptionDecorator.java | 4 ++ .../model/SubscriptionBox.java | 27 -------------- .../model/SubscriptionBoxTest.java | 37 ------------------- 7 files changed, 17 insertions(+), 65 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index 2c2bae0..7fa569b 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -10,6 +10,6 @@ public class SubscriptionBoxController { @GetMapping("/Subscription-Box-Admin") public String homePage() { - return "SubscriptionBoxAdmin"; + return "SubscriptionBoxAdmin Display"; } } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java new file mode 100644 index 0000000..371e99d --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java @@ -0,0 +1,4 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +public class BasicSubscriptionBox { +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java new file mode 100644 index 0000000..6a9ee00 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java @@ -0,0 +1,4 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +public class MonthlySubscriptionBoxDecorator { +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java new file mode 100644 index 0000000..d369dda --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java @@ -0,0 +1,4 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +public class QuarterlySubscriptionBoxDecorator { +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java new file mode 100644 index 0000000..b463660 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java @@ -0,0 +1,4 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +public class SemiAnnualSubscriptionDecorator { +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index edfa47a..9f72818 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -10,32 +10,5 @@ import lombok.Setter; @Getter@Setter public class SubscriptionBox { - private String id; - private String name; - private String category; - private String photo; - private List prices; - private List items; - private LocalDate dateCreated; - public SubscriptionBox(String id, String name, String category, String photo, List prices, List items, LocalDate dateCreated) { - this.id = id; - this.name = name; - this.category = category; - this.photo = photo; - if (prices.size() != 3) { - throw new IllegalArgumentException("Prices list must contain exactly three elements"); - } - Collectors Collectors = null; - this.prices = prices.stream() - .filter(price -> price > 0) - .collect(Collectors.toList()); - if (this.prices.size() != prices.size()) { - throw new IllegalArgumentException("Prices must be positive"); - } - this.items = items; - this.dateCreated = dateCreated; - } - - // Getters and setters } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 8820272..d358ebb 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -11,43 +11,6 @@ public class SubscriptionBoxTest { - private SubscriptionBox box; - private List validPrices; - private List items; - - @BeforeEach - public void setup() { - // Initialize common test data - validPrices = Arrays.asList(100, 270, 480); - items = Arrays.asList(new Items(), new Items()); - box = new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", validPrices, items, LocalDate.now()); - } - - @Test - public void testCreateSubscriptionBox_Valid() { - assertNotNull(box.getId()); - assertEquals("ChocoBox", box.getName()); - assertEquals("Food", box.getCategory()); - assertEquals(3, box.getPrices().size()); - assertEquals(2, box.getItems().size()); - assertNotNull(box.getDateCreated()); - } - - @Test - public void testSubscriptionBox_NegativePrices() { - List negativePrices = Arrays.asList(100, -270, 480); - assertThrows(IllegalArgumentException.class, () -> { - new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", negativePrices, items, LocalDate.now()); - }); - } - - @Test - public void testSubscriptionBox_InvalidPriceCount() { - List twoPrices = Arrays.asList(100, 270); // Only two prices - assertThrows(IllegalArgumentException.class, () -> { - new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", twoPrices, items, LocalDate.now()); - }); - } // Additional tests can also utilize the setup data } From 5f78e73e334cf27ce4416ee7de4200177eada414 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 24 Apr 2024 20:30:26 +0700 Subject: [PATCH 07/28] Revert "[REFACTOR] Impelenting Decorator Pattern, Adding Skeleton Model" This reverts commit a451b7307b22c11c5b7fbcbae0518490eb6dfac9. --- .../controller/SubscriptionBoxController.java | 2 +- .../model/BasicSubscriptionBox.java | 4 -- .../MonthlySubscriptionBoxDecorator.java | 4 -- .../QuarterlySubscriptionBoxDecorator.java | 4 -- .../SemiAnnualSubscriptionDecorator.java | 4 -- .../model/SubscriptionBox.java | 27 ++++++++++++++ .../model/SubscriptionBoxTest.java | 37 +++++++++++++++++++ 7 files changed, 65 insertions(+), 17 deletions(-) delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index 7fa569b..2c2bae0 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -10,6 +10,6 @@ public class SubscriptionBoxController { @GetMapping("/Subscription-Box-Admin") public String homePage() { - return "SubscriptionBoxAdmin Display"; + return "SubscriptionBoxAdmin"; } } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java deleted file mode 100644 index 371e99d..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/BasicSubscriptionBox.java +++ /dev/null @@ -1,4 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -public class BasicSubscriptionBox { -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java deleted file mode 100644 index 6a9ee00..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/MonthlySubscriptionBoxDecorator.java +++ /dev/null @@ -1,4 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -public class MonthlySubscriptionBoxDecorator { -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java deleted file mode 100644 index d369dda..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/QuarterlySubscriptionBoxDecorator.java +++ /dev/null @@ -1,4 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -public class QuarterlySubscriptionBoxDecorator { -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java deleted file mode 100644 index b463660..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SemiAnnualSubscriptionDecorator.java +++ /dev/null @@ -1,4 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -public class SemiAnnualSubscriptionDecorator { -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 9f72818..edfa47a 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -10,5 +10,32 @@ import lombok.Setter; @Getter@Setter public class SubscriptionBox { + private String id; + private String name; + private String category; + private String photo; + private List prices; + private List items; + private LocalDate dateCreated; + public SubscriptionBox(String id, String name, String category, String photo, List prices, List items, LocalDate dateCreated) { + this.id = id; + this.name = name; + this.category = category; + this.photo = photo; + if (prices.size() != 3) { + throw new IllegalArgumentException("Prices list must contain exactly three elements"); + } + Collectors Collectors = null; + this.prices = prices.stream() + .filter(price -> price > 0) + .collect(Collectors.toList()); + if (this.prices.size() != prices.size()) { + throw new IllegalArgumentException("Prices must be positive"); + } + this.items = items; + this.dateCreated = dateCreated; + } + + // Getters and setters } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index d358ebb..8820272 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -11,6 +11,43 @@ public class SubscriptionBoxTest { + private SubscriptionBox box; + private List validPrices; + private List items; + + @BeforeEach + public void setup() { + // Initialize common test data + validPrices = Arrays.asList(100, 270, 480); + items = Arrays.asList(new Items(), new Items()); + box = new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", validPrices, items, LocalDate.now()); + } + + @Test + public void testCreateSubscriptionBox_Valid() { + assertNotNull(box.getId()); + assertEquals("ChocoBox", box.getName()); + assertEquals("Food", box.getCategory()); + assertEquals(3, box.getPrices().size()); + assertEquals(2, box.getItems().size()); + assertNotNull(box.getDateCreated()); + } + + @Test + public void testSubscriptionBox_NegativePrices() { + List negativePrices = Arrays.asList(100, -270, 480); + assertThrows(IllegalArgumentException.class, () -> { + new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", negativePrices, items, LocalDate.now()); + }); + } + + @Test + public void testSubscriptionBox_InvalidPriceCount() { + List twoPrices = Arrays.asList(100, 270); // Only two prices + assertThrows(IllegalArgumentException.class, () -> { + new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", twoPrices, items, LocalDate.now()); + }); + } // Additional tests can also utilize the setup data } From 855938076a666b1e3cd8a7d66003cf51f282a3bb Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 24 Apr 2024 20:54:42 +0700 Subject: [PATCH 08/28] implement factory method pattern --- .../factory/Factory.java | 7 ++++++ .../factory/SubscriptionBoxFactory.java | 19 +++++++++++++++ .../model/SubscriptionBox.java | 6 ++++- .../repository/SubscriptionBoxRepository.java | 13 +++++++++- .../service/SubscriptionBoxService.java | 3 +++ .../service/SubscriptionBoxServiceImpl.java | 24 +++++++++++++++---- 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.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 new file mode 100644 index 0000000..1290410 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java @@ -0,0 +1,7 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; + +public abstract class Factory { + public abstract SubscriptionBox create(); +} 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 new file mode 100644 index 0000000..66cf108 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java @@ -0,0 +1,19 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; + + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; + +import java.time.LocalDate; +import java.util.List; + +public class SubscriptionBoxFactory extends Factory { + @Override + public SubscriptionBox create(){ + return new SubscriptionBox(); + } + + public SubscriptionBox create(String id, String name, String category, String photo, List prices, List items, LocalDate dateCreated){ + return new SubscriptionBox(id,name, category, photo,prices, items, dateCreated); + } +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index edfa47a..3fc6313 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -2,7 +2,9 @@ import java.time.LocalDate; +import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; @@ -37,5 +39,7 @@ public SubscriptionBox(String id, String name, String category, String photo, Li this.dateCreated = dateCreated; } - // Getters and setters + public SubscriptionBox() { + + } } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java index dc46ab0..4a0361f 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -1,15 +1,26 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; + import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import org.springframework.stereotype.Repository; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; -import java.util.Optional; + @Repository public class SubscriptionBoxRepository { + private List SubscriptionBoxData = new ArrayList<>(); + + public SubscriptionBox create(SubscriptionBox SubscriptionBox) { + SubscriptionBoxData.add(SubscriptionBox); + return SubscriptionBox; + } + public Iterator findAll() { + return SubscriptionBoxData.iterator(); + } // Add more methods as needed for your application. } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java index a27761c..8fb0fd9 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java @@ -5,6 +5,9 @@ import java.util.List; public interface SubscriptionBoxService { + public SubscriptionBox create(SubscriptionBox SubscriptionBox); + public List findAll(); + SubscriptionBox edit(String id); // Add any other service methods required by your application. } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java index 1d46a9c..d871451 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java @@ -1,10 +1,13 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller.SubscriptionBoxController; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @Service @@ -13,9 +16,20 @@ public class SubscriptionBoxServiceImpl implements SubscriptionBoxService { @Autowired private SubscriptionBoxRepository subscriptionBoxRepository; - - - - - + public SubscriptionBox create(SubscriptionBox SubscriptionBox) { + subscriptionBoxRepository.create(SubscriptionBox); + return SubscriptionBox; + } + + public List findAll() { + Iterator SubscriptionBoxIterator = subscriptionBoxRepository.findAll(); + List allSubscriptionBox = new ArrayList<>(); + SubscriptionBoxIterator.forEachRemaining(allSubscriptionBox::add); + return allSubscriptionBox; + } + + @Override + public SubscriptionBox edit(String id){ + return null; + } } \ No newline at end of file From 3fb8ffb9ae8629f755b1bd92b317d11bd017939e Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 19:41:29 +0700 Subject: [PATCH 09/28] [REFACTOR] create new tests for model --- .../model/SubscriptionBoxTest.java | 86 +++++++++---------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 8820272..029b3f6 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -1,55 +1,51 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; +import java.util.ArrayList; import java.util.List; -import java.time.LocalDate; +import java.util.UUID; + +public class SubscriptionBoxTest { + List items; + SubscriptionBox subscriptionBox; + @BeforeEach + public void setUp() { + this.subscriptionBox = new SubscriptionBox(); + this.subscriptionBox.setId("b060669f-4047-47ee-9d0f-1b4a123a104a"); + this.subscriptionBox.setName("BOX1"); + this.subscriptionBox.setType("MTH"); + this.subscriptionBox.setPrice(100000); + this.items = new ArrayList<>(); + items.add(new Items("aa229d97-3f69-4d6c-b40b-29f909429364", "item1", 1)); + items.add(new Items("02e41cc8-358d-4609-a082-23affafa8e05", "item2", 2)); + this.subscriptionBox.setItems(items); + } -import org.junit.jupiter.api.BeforeEach; + @Test + public void testGetId() { + assertEquals("b060669f-4047-47ee-9d0f-1b4a123a104a", subscriptionBox.getId()); + } + @Test + public void testGetName() { + assertEquals("BOX1", subscriptionBox.getName()); + } - public class SubscriptionBoxTest { - - private SubscriptionBox box; - private List validPrices; - private List items; - - @BeforeEach - public void setup() { - // Initialize common test data - validPrices = Arrays.asList(100, 270, 480); - items = Arrays.asList(new Items(), new Items()); - box = new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", validPrices, items, LocalDate.now()); - } - - @Test - public void testCreateSubscriptionBox_Valid() { - assertNotNull(box.getId()); - assertEquals("ChocoBox", box.getName()); - assertEquals("Food", box.getCategory()); - assertEquals(3, box.getPrices().size()); - assertEquals(2, box.getItems().size()); - assertNotNull(box.getDateCreated()); - } - - @Test - public void testSubscriptionBox_NegativePrices() { - List negativePrices = Arrays.asList(100, -270, 480); - assertThrows(IllegalArgumentException.class, () -> { - new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", negativePrices, items, LocalDate.now()); - }); - } - - @Test - public void testSubscriptionBox_InvalidPriceCount() { - List twoPrices = Arrays.asList(100, 270); // Only two prices - assertThrows(IllegalArgumentException.class, () -> { - new SubscriptionBox("1", "ChocoBox", "Food", "url/to/photo", twoPrices, items, LocalDate.now()); - }); - } - - // Additional tests can also utilize the setup data + @Test + public void testGetType() { + assertEquals("MTH", subscriptionBox.getType()); } + @Test + public void testGetPrice() { + assertEquals(100000, subscriptionBox.getPrice()); + } + @Test + public void testGetItems() { + assertEquals(2, subscriptionBox.getItems().size()); + } +} \ No newline at end of file From af543d45b12bb20e4b191568e8bc2e618f48eb16 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 19:45:29 +0700 Subject: [PATCH 10/28] [REFACTOR] create new tests for model --- .../model/SubscriptionBoxTest.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 029b3f6..625d0da 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -1,5 +1,6 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -9,7 +10,7 @@ import java.util.UUID; public class SubscriptionBoxTest { - List items; + List items; SubscriptionBox subscriptionBox; @BeforeEach public void setUp() { @@ -19,8 +20,16 @@ public void setUp() { this.subscriptionBox.setType("MTH"); this.subscriptionBox.setPrice(100000); this.items = new ArrayList<>(); - items.add(new Items("aa229d97-3f69-4d6c-b40b-29f909429364", "item1", 1)); - items.add(new Items("02e41cc8-358d-4609-a082-23affafa8e05", "item2", 2)); + Item item1 = new Item(); + item1.setId(UUID.randomUUID().toString()); + item1.setName("Item1"); + item1.setQuantity(1); + items.add(item1); + Item item2 = new Item(); + item2.setId(UUID.randomUUID().toString()); + item2.setName("Item2"); + item2.setQuantity(2); + items.add(item2); this.subscriptionBox.setItems(items); } From f12816438fa9ef7e43a0dce8bbf13b233e56da78 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 19:49:34 +0700 Subject: [PATCH 11/28] [REFACTOR] create new tests for model fix --- .../{model => }/SubscriptionBoxTest.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) rename src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/{model => }/SubscriptionBoxTest.java (75%) diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java similarity index 75% rename from src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java rename to src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java index 625d0da..3d9931e 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java @@ -1,6 +1,6 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - +package id.ac.ui.cs.advprog.snackscription_subscriptionbox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -20,16 +20,8 @@ public void setUp() { this.subscriptionBox.setType("MTH"); this.subscriptionBox.setPrice(100000); this.items = new ArrayList<>(); - Item item1 = new Item(); - item1.setId(UUID.randomUUID().toString()); - item1.setName("Item1"); - item1.setQuantity(1); - items.add(item1); - Item item2 = new Item(); - item2.setId(UUID.randomUUID().toString()); - item2.setName("Item2"); - item2.setQuantity(2); - items.add(item2); + items.add(new Item("aa229d97-3f69-4d6c-b40b-29f909429364", "item1", 1)); + items.add(new Item("02e41cc8-358d-4609-a082-23affafa8e05", "item2", 2)); this.subscriptionBox.setItems(items); } From 7ab3e2eeb5a416e93e8e1bce9e281835f514cb8e Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 19:50:23 +0700 Subject: [PATCH 12/28] [REFACTOR] create tests for subscription box --- .../SubscriptionBoxTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java index 3d9931e..37cbf99 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java @@ -20,8 +20,16 @@ public void setUp() { this.subscriptionBox.setType("MTH"); this.subscriptionBox.setPrice(100000); this.items = new ArrayList<>(); - items.add(new Item("aa229d97-3f69-4d6c-b40b-29f909429364", "item1", 1)); - items.add(new Item("02e41cc8-358d-4609-a082-23affafa8e05", "item2", 2)); + Item item1 = new Item(); + item1.setId(UUID.randomUUID().toString()); + item1.setName("Item1"); + item1.setQuantity(1); + items.add(item1); + Item item2 = new Item(); + item2.setId(UUID.randomUUID().toString()); + item2.setName("Item2"); + item2.setQuantity(2); + items.add(item2); this.subscriptionBox.setItems(items); } From 2646475b3b26887fb770fd4fcf9ecb44da9e0d64 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 19:54:02 +0700 Subject: [PATCH 13/28] [RED] create new subscription model skeleton --- .../controller/AddItemController.java | 15 ------ .../model/SubscriptionBox.java | 48 +++++-------------- .../{ => model}/SubscriptionBoxTest.java | 2 +- 3 files changed, 13 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java rename src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/{ => model}/SubscriptionBoxTest.java (96%) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java deleted file mode 100644 index c7e1331..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/AddItemController.java +++ /dev/null @@ -1,15 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - - -@Controller -@RequestMapping("/") -public class SubscriptionBoxController { - @GetMapping("/Add-Item-Admin") - public String homePage() { - return "AddItemAdmin"; - } -} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 3fc6313..0c9162b 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -1,45 +1,21 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -import java.time.LocalDate; +import lombok.Builder; +import lombok.Getter; import java.util.ArrayList; -import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; +import java.util.List; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -import lombok.Getter; -import lombok.Setter; -@Getter@Setter +@Builder +@Getter public class SubscriptionBox { - private String id; - private String name; - private String category; - private String photo; - private List prices; - private List items; - private LocalDate dateCreated; - - public SubscriptionBox(String id, String name, String category, String photo, List prices, List items, LocalDate dateCreated) { - this.id = id; - this.name = name; - this.category = category; - this.photo = photo; - if (prices.size() != 3) { - throw new IllegalArgumentException("Prices list must contain exactly three elements"); - } - Collectors Collectors = null; - this.prices = prices.stream() - .filter(price -> price > 0) - .collect(Collectors.toList()); - if (this.prices.size() != prices.size()) { - throw new IllegalArgumentException("Prices must be positive"); - } - this.items = items; - this.dateCreated = dateCreated; - } + String id; + String name; + String type; + int price; + List items; - public SubscriptionBox() { + public SubscriptionBox(String name, String type, int price, List items) { } -} +} \ No newline at end of file diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java similarity index 96% rename from src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java rename to src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 37cbf99..144f777 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -1,4 +1,4 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox; +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import org.junit.jupiter.api.BeforeEach; From 9c1bcb77929e8017124e66896f151775092944fb Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:00:25 +0700 Subject: [PATCH 14/28] [GREEN] make implementation for subscription box model --- .../model/SubscriptionBox.java | 7 +-- .../model/builder/SubscriptionBoxBuilder.java | 45 +++++++++++++++++++ .../repository/SubscriptionBoxRepository.java | 42 ++++++++++++----- .../service/SubscriptionBoxServiceImpl.java | 35 --------------- 4 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 0c9162b..a145751 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -2,12 +2,12 @@ import lombok.Builder; import lombok.Getter; +import lombok.Setter; import java.util.ArrayList; import java.util.UUID; import java.util.List; -@Builder -@Getter +@Getter @Setter public class SubscriptionBox { String id; String name; @@ -15,7 +15,4 @@ public class SubscriptionBox { int price; List items; - public SubscriptionBox(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/model/builder/SubscriptionBoxBuilder.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java new file mode 100644 index 0000000..235e502 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java @@ -0,0 +1,45 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.builder; + +import java.util.ArrayList; + +import org.springframework.beans.factory.annotation.Autowired; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; + +public class SubscriptionBoxBuilder { + private SubscriptionBox subscriptionBox; + + @Autowired + private SubscriptionBoxRepository subscriptionBoxRepository; + + public SubscriptionBoxBuilder() { + this.subscriptionBox = new SubscriptionBox(); + } + + public SubscriptionBoxBuilder setName(String name) { + subscriptionBox.setName(name); + return this; + } + + public SubscriptionBoxBuilder setType(String type) { + subscriptionBox.setType(type); + return this; + } + + public SubscriptionBoxBuilder setPrice(int price) { + subscriptionBox.setPrice(price); + return this; + } + + public SubscriptionBoxBuilder setItems() { + subscriptionBox.setItems(new ArrayList<>()); + return this; + } + + public SubscriptionBoxBuilder setId(String id) { + subscriptionBox.setId(id); + return this; + } + +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java index 4a0361f..b1d9c9c 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -3,24 +3,42 @@ import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import org.springframework.stereotype.Repository; - - import java.util.ArrayList; -import java.util.Iterator; +import java.util.Collections; +import java.util.Comparator; import java.util.List; - @Repository public class SubscriptionBoxRepository { - private List SubscriptionBoxData = new ArrayList<>(); - public SubscriptionBox create(SubscriptionBox SubscriptionBox) { - SubscriptionBoxData.add(SubscriptionBox); - return SubscriptionBox; + private List subscriptionBoxes = new ArrayList<>(); + private List filteredBoxesByPrice = new ArrayList<>(); + private List filteredBoxesByRating = new ArrayList<>(); + + public SubscriptionBox addBox(SubscriptionBox box) { + + return box; } - public Iterator findAll() { - return SubscriptionBoxData.iterator(); + public SubscriptionBox deleteBox(String id) { + + return null; } - // Add more methods as needed for your application. -} + + public SubscriptionBox editBox(String id, SubscriptionBox box) { + + return null; + } + + public List viewAll() { + return subscriptionBoxes; + } + + public String viewDetails(String boxId) { + + return null; + } + + public List filterByPrice(int price) { + return null;} +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java deleted file mode 100644 index d871451..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller.SubscriptionBoxController; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -@Service -public class SubscriptionBoxServiceImpl implements SubscriptionBoxService { - - @Autowired - private SubscriptionBoxRepository subscriptionBoxRepository; - - public SubscriptionBox create(SubscriptionBox SubscriptionBox) { - subscriptionBoxRepository.create(SubscriptionBox); - return SubscriptionBox; - } - - public List findAll() { - Iterator SubscriptionBoxIterator = subscriptionBoxRepository.findAll(); - List allSubscriptionBox = new ArrayList<>(); - SubscriptionBoxIterator.forEachRemaining(allSubscriptionBox::add); - return allSubscriptionBox; - } - - @Override - public SubscriptionBox edit(String id){ - return null; - } -} \ No newline at end of file From 4863d9d7a469afa6492c68b07ed5501a30c7bbab Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:05:10 +0700 Subject: [PATCH 15/28] [REFACTOR] create new tests for repository --- .../model/SubscriptionBoxTest.java | 13 +--- .../SubscriptionBoxRepositoryTest.java | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 144f777..270d210 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -10,7 +10,7 @@ import java.util.UUID; public class SubscriptionBoxTest { - List items; + List items; SubscriptionBox subscriptionBox; @BeforeEach public void setUp() { @@ -20,15 +20,10 @@ public void setUp() { this.subscriptionBox.setType("MTH"); this.subscriptionBox.setPrice(100000); this.items = new ArrayList<>(); - Item item1 = new Item(); - item1.setId(UUID.randomUUID().toString()); - item1.setName("Item1"); - item1.setQuantity(1); + Items item1 = new Items(); + items.add(item1); - Item item2 = new Item(); - item2.setId(UUID.randomUUID().toString()); - item2.setName("Item2"); - item2.setQuantity(2); + Items item2 = new Items(); items.add(item2); this.subscriptionBox.setItems(items); } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java new file mode 100644 index 0000000..f71efdc --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java @@ -0,0 +1,78 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.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.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; + +public class SubscriptionBoxRepositoryTest { + + private SubscriptionBoxRepository subscriptionBoxRepository; + + @BeforeEach + public void setUp() { + subscriptionBoxRepository = new SubscriptionBoxRepository(); + } + + @Test + public void testAddBox() { + SubscriptionBox box = new SubscriptionBox(); + SubscriptionBox addedBox = subscriptionBoxRepository.addBox(box); + assertNotNull(addedBox); + } + + @Test + public void testDeleteBox() { + SubscriptionBox box = new SubscriptionBox(); + SubscriptionBox deletedBox = subscriptionBoxRepository.deleteBox(box); + assertNotNull(deletedBox); + } + + @Test + public void testEditBox() { + SubscriptionBox box = new SubscriptionBox(); + box.setId("1"); + String id = box.getId(); + box.setName("abc"); + subscriptionBoxRepository.addBox(box); + box.setName("def"); + SubscriptionBox editedBox = subscriptionBoxRepository.editBox(id, box); + assertEquals("def", editedBox.getName()); + } + + @Test + public void testViewAll() { + SubscriptionBox box1 = new SubscriptionBox(); + SubscriptionBox box2 = new SubscriptionBox(); + subscriptionBoxRepository.addBox(box1); + subscriptionBoxRepository.addBox(box2); + List allBoxes = subscriptionBoxRepository.viewAll(); + assertEquals(2, allBoxes.size()); + } + + @Test + public void testViewDetails() { + SubscriptionBox box = new SubscriptionBox(); + box.setName("abc"); + box.setId("1"); + subscriptionBoxRepository.addBox(box); + String details = subscriptionBoxRepository.viewDetails(box.getId()); + assertEquals("abc", details); + } + + @Test + public void testFilterByPrice() { + SubscriptionBox box1 = new SubscriptionBox(); + box1.setPrice(15); + SubscriptionBox box2 = new SubscriptionBox(); + box2.setPrice(25); + subscriptionBoxRepository.addBox(box1); + subscriptionBoxRepository.addBox(box2); + List filteredBoxes = subscriptionBoxRepository.filterByPrice(15); + assertEquals(1, filteredBoxes.size()); + } +} \ No newline at end of file From 0d01fa9841de1bab329e89e6985090ab18c910a5 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:06:19 +0700 Subject: [PATCH 16/28] [GREEN] implement new subscription box repository --- .../repository/SubscriptionBoxRepository.java | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java index b1d9c9c..d52ef2a 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -16,17 +16,27 @@ public class SubscriptionBoxRepository { private List filteredBoxesByRating = new ArrayList<>(); public SubscriptionBox addBox(SubscriptionBox box) { - + subscriptionBoxes.add(box); return box; } public SubscriptionBox deleteBox(String id) { - + for (SubscriptionBox subscriptionBox : subscriptionBoxes) { + if (subscriptionBox.getId().equals(id)) { + subscriptionBoxes.remove(subscriptionBox); + return subscriptionBox; + } + } return null; } public SubscriptionBox editBox(String id, SubscriptionBox box) { - + for (SubscriptionBox subscriptionBox : subscriptionBoxes) { + if (subscriptionBox.getId().equals(id)) { + subscriptionBox = box; + return subscriptionBox; + } + } return null; } @@ -35,10 +45,22 @@ public List viewAll() { } public String viewDetails(String boxId) { - + for (SubscriptionBox subscriptionBox : subscriptionBoxes) { + if (subscriptionBox.getId().equals(boxId)) { + return subscriptionBox.getName(); + } + } return null; } public List filterByPrice(int price) { - return null;} + List filteredBoxes = new ArrayList<>(); + for (SubscriptionBox subscriptionBox : subscriptionBoxes) { + if (subscriptionBox.getPrice() == price) { + filteredBoxes.add(subscriptionBox); + } + } + filteredBoxesByPrice = filteredBoxes; + return filteredBoxesByPrice; + } } \ No newline at end of file From 35f5d1b82b9c27561d2ee6322836ed25ca9dcf6c Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:08:19 +0700 Subject: [PATCH 17/28] [REFACTOR] create new tests for service --- .../SubscriptionBoxRepositoryTest.java | 4 +- .../service/SubscriptionBoxServiceTest.java | 77 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java index f71efdc..2a71ca4 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java @@ -28,7 +28,9 @@ public void testAddBox() { @Test public void testDeleteBox() { SubscriptionBox box = new SubscriptionBox(); - SubscriptionBox deletedBox = subscriptionBoxRepository.deleteBox(box); + box.setId("1"); + subscriptionBoxRepository.addBox(box); + SubscriptionBox deletedBox = subscriptionBoxRepository.deleteBox(box.getId()); assertNotNull(deletedBox); } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java new file mode 100644 index 0000000..994d324 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java @@ -0,0 +1,77 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +public class SubscriptionBoxServiceTest { + + private SubscriptionBoxService subscriptionBoxService; + + @BeforeEach + public void setUp() { + subscriptionBoxService = new SubscriptionBoxServiceImpl(); + SubscriptionBox box1 = new SubscriptionBox(); + box1.setId("1"); + box1.setName("Box 1"); + box1.setPrice(100000); + box1.setType("MTH"); + subscriptionBoxService.addBox(box1); + } + + @Test + public void testAddBox() { + SubscriptionBox box2 = new SubscriptionBox(); + box2.setId("2"); + box2.setName("Box 2"); + box2.setPrice(200000); + box2.setType("MTH"); + subscriptionBoxService.addBox(box2); + assertEquals(2, subscriptionBoxService.viewAll().size()); + } + + @Test + public void testEditBox() { + SubscriptionBox box1 = new SubscriptionBox(); + box1.setId("1"); + box1.setName("Box 1"); + box1.setPrice(100000); + box1.setType("MTH"); + subscriptionBoxService.editBox("1", box1); + assertEquals("Box 1", subscriptionBoxService.viewDetails("1")); + } + + @Test + public void testDeleteBox() { + subscriptionBoxService.deleteBox("1"); + assertEquals(0, subscriptionBoxService.viewAll().size()); + } + + @Test + public void testViewAll() { + subscriptionBoxService.viewAll(); + assertEquals(1, subscriptionBoxService.viewAll().size()); + } + + @Test + public void testViewDetails() { + String name = subscriptionBoxService.viewDetails("1"); + assertEquals("Box 1", name); + } + + @Test + public void testFilterByPrice() { + List boxes = subscriptionBoxService.filterByPrice(100000); + assertEquals(1, boxes.size()); + } + + // Rating not implemented yet + /* + @Test + public void testFilterByRating() { + } + */ +} \ No newline at end of file From a6b5d19afc38b2cee506f331682805bfe9c8417f Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:10:34 +0700 Subject: [PATCH 18/28] [GREEN] impelement new subscription box service --- .../service/SubscriptionBoxService.java | 18 +++---- .../service/SubscriptionBoxServiceImpl.java | 47 +++++++++++++++++++ .../service/SubscriptionBoxServiceTest.java | 7 +-- 3 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java index 8fb0fd9..5596309 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java @@ -1,13 +1,15 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; - import java.util.List; -public interface SubscriptionBoxService { - public SubscriptionBox create(SubscriptionBox SubscriptionBox); - public List findAll(); +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; - SubscriptionBox edit(String id); - // Add any other service methods required by your application. -} +public interface SubscriptionBoxService { + public SubscriptionBox addBox(SubscriptionBox subscriptionBox); + public SubscriptionBox editBox(String id, SubscriptionBox subscriptionBox); + public SubscriptionBox deleteBox(String id); + public List viewAll(); + public String viewDetails(String boxId); + public List filterByPrice(int price); + // public List filterByRating(int rating); +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java new file mode 100644 index 0000000..f0624b1 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java @@ -0,0 +1,47 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; + +@Service +public class SubscriptionBoxServiceImpl implements SubscriptionBoxService{ + @Autowired + private SubscriptionBoxRepository subscriptionBoxRepository; + + @Override + public SubscriptionBox addBox(SubscriptionBox box) { + return subscriptionBoxRepository.addBox(box); + } + + @Override + public SubscriptionBox deleteBox(String id) { + return subscriptionBoxRepository.deleteBox(id); + } + + @Override + public SubscriptionBox editBox(String id, SubscriptionBox box) { + return subscriptionBoxRepository.editBox(id, box); + } + + @Override + public List viewAll() { + return subscriptionBoxRepository.viewAll(); + } + + @Override + public String viewDetails(String boxId) { + return subscriptionBoxRepository.viewDetails(boxId); + } + + @Override + public List filterByPrice(int price) { + return subscriptionBoxRepository.filterByPrice(price); + } + + +} \ No newline at end of file diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java index 994d324..770c594 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java @@ -68,10 +68,5 @@ public void testFilterByPrice() { assertEquals(1, boxes.size()); } - // Rating not implemented yet - /* - @Test - public void testFilterByRating() { - } - */ + } \ No newline at end of file From b1e90730c22974858a9a36522c709b7daa6a63bd Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:11:48 +0700 Subject: [PATCH 19/28] [REFACTOR] create new tests for controller --- .../SubscriptionBoxControllerTest.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java new file mode 100644 index 0000000..d08c204 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java @@ -0,0 +1,130 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import java.util.UUID; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@ExtendWith(SpringExtension.class) +public class SubscriptionBoxControllerTest { + + private MockMvc mockMvc; + + @Mock + private SubscriptionBoxService subscriptionBoxService; + + @InjectMocks + private SubscriptionBoxController subscriptionBoxController; + + private ObjectMapper objectMapper = new ObjectMapper(); + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.standaloneSetup(subscriptionBoxController).build(); + } + + @Test + public void testCreateSubscriptionBox() throws Exception { + SubscriptionBox subscriptionBox = new SubscriptionBox(); + subscriptionBox.setId(UUID.randomUUID().toString()); + subscriptionBox.setName("Test Subscription Box"); + subscriptionBox.setPrice(100000); + // subscriptionBox.setRating(5); + + given(subscriptionBoxService.addBox(any(SubscriptionBox.class))).willReturn(subscriptionBox); + + mockMvc.perform(post("/subscription-box/create") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(subscriptionBox))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(subscriptionBox.getId())) + .andExpect(jsonPath("$.name").value(subscriptionBox.getName())) + .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())); + } + + @Test + public void testEditSubscriptionBox() throws Exception { + SubscriptionBox subscriptionBox = new SubscriptionBox(); + subscriptionBox.setId(UUID.randomUUID().toString()); + subscriptionBox.setName("Test Subscription Box"); + subscriptionBox.setPrice(100000); + // subscriptionBox.setRating(5); + + given(subscriptionBoxService.editBox(any(String.class), any(SubscriptionBox.class))).willReturn(subscriptionBox); + + mockMvc.perform(get("/subscription-box/edit/" + subscriptionBox.getId())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(subscriptionBox.getId())) + .andExpect(jsonPath("$.name").value(subscriptionBox.getName())) + .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())) + // .andExpect(jsonPath("$.rating").value(subscriptionBox.getRating())) + ; + } + + @Test + public void testDeleteSubscriptionBox() throws Exception { + SubscriptionBox subscriptionBox = new SubscriptionBox(); + subscriptionBox.setId(UUID.randomUUID().toString()); + subscriptionBox.setName("Test Subscription Box"); + subscriptionBox.setPrice(100000); + // subscriptionBox.setRating(5); + + given(subscriptionBoxService.deleteBox(any(String.class))).willReturn(subscriptionBox); + + mockMvc.perform(get("/subscription-box/delete/" + subscriptionBox.getId())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(subscriptionBox.getId())) + .andExpect(jsonPath("$.name").value(subscriptionBox.getName())) + .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())) + // .andExpect(jsonPath("$.rating").value(subscriptionBox.getRating())) + ; + } + + @Test + public void testViewSubscriptionBox() throws Exception { + SubscriptionBox subscriptionBox = new SubscriptionBox(); + subscriptionBox.setId(UUID.randomUUID().toString()); + subscriptionBox.setName("Test Subscription Box"); + subscriptionBox.setPrice(100000); + // subscriptionBox.setRating(5); + + given(subscriptionBoxService.viewDetails(any(String.class))).willReturn(subscriptionBox.toString()); + + mockMvc.perform(get("/subscription-box/view/" + subscriptionBox.getId())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").value(subscriptionBox.toString())); + } + + @Test + public void testViewAllSubscriptionBox() throws Exception { + mockMvc.perform(get("/subscription-box/viewAll/")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()); + } + + @Test + public void testFilterByPrice() throws Exception { + mockMvc.perform(get("/subscription-box/filterByPrice/100000")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()); + } +} \ No newline at end of file From 6d892c9cbd79468c14bcd4fb8da70c68596c2ec1 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:13:03 +0700 Subject: [PATCH 20/28] [GREEN] implement new controller --- .../controller/SubscriptionBoxController.java | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index 2c2bae0..3b2bb0e 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -1,15 +1,59 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService; -@Controller -@RequestMapping("/") +import java.util.List; + +@RestController +@RequestMapping("/subscription-box") public class SubscriptionBoxController { - @GetMapping("/Subscription-Box-Admin") - public String homePage() { - return "SubscriptionBoxAdmin"; + @Autowired + private SubscriptionBoxService subscriptionBoxService; + String createHTML = "userCreate"; + @GetMapping("../") + public String createUserPage(Model model) { + return "

Subscription box and Item Management sudah berhasil!

"; + } + + @PostMapping("/create") + public ResponseEntity createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox, Model model) { + SubscriptionBox newBox = subscriptionBoxService.addBox(subscriptionBox); + return ResponseEntity.ok(newBox); + } + + @GetMapping("/viewAll") + public ResponseEntity> viewAll() { + List allBoxes = subscriptionBoxService.viewAll(); + return ResponseEntity.ok(allBoxes); + } + + @GetMapping("/viewDetails/{boxId}") + public ResponseEntity viewDetails(@PathVariable String boxId) { + String boxName = subscriptionBoxService.viewDetails(boxId); + return ResponseEntity.ok(boxName); + } + + @DeleteMapping("/delete/{boxId}") + public ResponseEntity deleteBox(@PathVariable String boxId) { + SubscriptionBox deletedBox = subscriptionBoxService.deleteBox(boxId); + return ResponseEntity.ok(deletedBox); + } + + @PutMapping("/edit/{boxId}") + public ResponseEntity editBox(@PathVariable String boxId, @RequestBody SubscriptionBox subscriptionBox) { + SubscriptionBox editedBox = subscriptionBoxService.editBox(boxId, subscriptionBox); + return ResponseEntity.ok(editedBox); + } + + @GetMapping("/filterByPrice/{price}") + public ResponseEntity> filterByPrice(@PathVariable int price) { + List filteredBoxes = subscriptionBoxService.filterByPrice(price); + return ResponseEntity.ok(filteredBoxes); } } \ No newline at end of file From 86cdb5ba9f17893ff0c7e762b6f4e1c4f1cf2ee3 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:40:34 +0700 Subject: [PATCH 21/28] [DEPLOYMENT] added cross origins --- .github/workflows/ci.yml | 2 +- .../controller/SubscriptionBoxController.java | 3 ++- .../snackscription_subscriptionbox/model/SubscriptionBox.java | 2 -- .../model/SubscriptionBoxTest.java | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7c39476..982f90f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,7 +88,7 @@ jobs: - name: check directory run: ls -al - name: Login to Docker Hub - run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin docker.io + run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin docker.io - name: Set Docker BuildKit run: export DOCKER_BUILDKIT=1 - name: Build Docker Image diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index 3b2bb0e..81e4e55 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -12,13 +12,14 @@ @RestController @RequestMapping("/subscription-box") +@CrossOrigin(origins = "*") public class SubscriptionBoxController { @Autowired private SubscriptionBoxService subscriptionBoxService; String createHTML = "userCreate"; @GetMapping("../") public String createUserPage(Model model) { - return "

Subscription box and Item Management sudah berhasil!

"; + return "

Subscription Box Management sudah berhasil!

"; } @PostMapping("/create") diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index a145751..5da995d 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -3,8 +3,6 @@ import lombok.Builder; import lombok.Getter; import lombok.Setter; -import java.util.ArrayList; -import java.util.UUID; import java.util.List; @Getter @Setter diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index 270d210..d0d712a 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -17,7 +17,7 @@ public void setUp() { this.subscriptionBox = new SubscriptionBox(); this.subscriptionBox.setId("b060669f-4047-47ee-9d0f-1b4a123a104a"); this.subscriptionBox.setName("BOX1"); - this.subscriptionBox.setType("MTH"); + this.subscriptionBox.setType("MONTHLY"); this.subscriptionBox.setPrice(100000); this.items = new ArrayList<>(); Items item1 = new Items(); @@ -40,7 +40,7 @@ public void testGetName() { @Test public void testGetType() { - assertEquals("MTH", subscriptionBox.getType()); + assertEquals("MONTHLY", subscriptionBox.getType()); } @Test From 7b5fdc1fcb005f336fa201144063548785bdb2d1 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:43:23 +0700 Subject: [PATCH 22/28] [BUILD] Added dependency of JPA and PostgreSQL --- build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 6d57da5..a02ee00 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,10 +23,12 @@ repositories { } dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-web") compileOnly("org.projectlombok:lombok") developmentOnly("org.springframework.boot:spring-boot-devtools") + runtimeOnly("org.postgresql:postgresql") annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") From a85acf6907683859dc1d25be88168992811d950b Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:46:09 +0700 Subject: [PATCH 23/28] [REFACTOR] remove factory pattern and reimplement builder pattern, added cross origins to controller --- .../controller/SubscriptionBoxController.java | 2 ++ .../factory/Factory.java | 7 ------- .../factory/SubscriptionBoxFactory.java | 19 ------------------- 3 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index 81e4e55..b012413 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -13,6 +13,8 @@ @RestController @RequestMapping("/subscription-box") @CrossOrigin(origins = "*") + + public class SubscriptionBoxController { @Autowired private SubscriptionBoxService subscriptionBoxService; 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 deleted file mode 100644 index 1290410..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java +++ /dev/null @@ -1,7 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; - -public abstract class Factory { - public abstract SubscriptionBox create(); -} 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 deleted file mode 100644 index 66cf108..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java +++ /dev/null @@ -1,19 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; - - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; - -import java.time.LocalDate; -import java.util.List; - -public class SubscriptionBoxFactory extends Factory { - @Override - public SubscriptionBox create(){ - return new SubscriptionBox(); - } - - public SubscriptionBox create(String id, String name, String category, String photo, List prices, List items, LocalDate dateCreated){ - return new SubscriptionBox(id,name, category, photo,prices, items, dateCreated); - } -} \ No newline at end of file From 5a7fbbaac962e4deaecb2388d1daf95aae5cbda7 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:53:14 +0700 Subject: [PATCH 24/28] [DEPLOYMENT] imported jakarta.persistence.*, havent implement --- .../model/SubscriptionBox.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 5da995d..8875ce1 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -1,16 +1,21 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import jakarta.persistence.*; import lombok.Builder; import lombok.Getter; import lombok.Setter; import java.util.List; + @Getter @Setter public class SubscriptionBox { + String id; String name; String type; int price; List items; + } \ No newline at end of file From 272f7c7ad029a082713cb95fed0e6b97c844fe0d Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 1 May 2024 20:54:46 +0700 Subject: [PATCH 25/28] [DOCS] added readme --- README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 From af4a43b796dc07759920fddee98298a5c1f33cf1 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Wed, 8 May 2024 20:31:43 +0700 Subject: [PATCH 26/28] [REFACTOR] FIX TESTS, ALL GREEN FINALLY, FINISH HIGH LEVEL NETWORKING --- .../controller/SubscriptionBoxController.java | 5 +- .../model/Item.java | 23 ++++ .../model/Items.java | 12 -- .../model/ItemsFactory.java | 12 -- .../model/SubscriptionBox.java | 19 ++- .../model/builder/ItemBuilder.java | 29 +++++ .../model/builder/SubscriptionBoxBuilder.java | 5 +- .../repository/ItemRepository.java | 41 ++++++ .../repository/ItemsRepository.java | 23 ---- .../repository/SubscriptionBoxRepository.java | 17 ++- .../service/ItemsService.java | 9 -- .../service/ItemsServiceImpl.java | 9 -- src/main/resources/application-dev.properties | 4 +- .../resources/application-test.properties | 6 +- ...iptionSubscriptionboxApplicationTests.java | 3 - .../SubscriptionBoxControllerTest.java | 45 ++----- .../model/SubscriptionBoxTest.java | 6 +- .../SubscriptionBoxRepositoryTest.java | 120 ++++++++--------- .../SubscriptionBoxServiceImplTest.java | 123 ++++++++++++++++++ .../service/SubscriptionBoxServiceTest.java | 72 ---------- 20 files changed, 333 insertions(+), 250 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java delete mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index b012413..7cb9ea8 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -36,7 +36,7 @@ public ResponseEntity> viewAll() { return ResponseEntity.ok(allBoxes); } - @GetMapping("/viewDetails/{boxId}") + @GetMapping("/view-details/{boxId}") public ResponseEntity viewDetails(@PathVariable String boxId) { String boxName = subscriptionBoxService.viewDetails(boxId); return ResponseEntity.ok(boxName); @@ -51,6 +51,9 @@ public ResponseEntity deleteBox(@PathVariable String boxId) { @PutMapping("/edit/{boxId}") public ResponseEntity editBox(@PathVariable String boxId, @RequestBody SubscriptionBox subscriptionBox) { SubscriptionBox editedBox = subscriptionBoxService.editBox(boxId, subscriptionBox); + if (editedBox == null) { + return ResponseEntity.notFound().build(); + } return ResponseEntity.ok(editedBox); } 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 new file mode 100644 index 0000000..d2b73c6 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.java @@ -0,0 +1,23 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Entity +@Getter @Setter +@Table (name = "Item") +public class Item { + @Id + private String id; + @Column(name = "item_name") + private String name; + + @Column(name = "item_quantity") + private int quantity; + + @ManyToMany(mappedBy = "items") + private List subscriptionBoxes; +} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java deleted file mode 100644 index 559c4cd..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Items.java +++ /dev/null @@ -1,12 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -import lombok.Getter; -import lombok.Setter; - - -@Getter @Setter -public class Items { - private String itemsId; - private String itemsName; - private int itemsQuantity; -} \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java deleted file mode 100644 index 4c27096..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/ItemsFactory.java +++ /dev/null @@ -1,12 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; - -public class ItemsFactory { - public static Items createItems(String type) { - if (type.equalsIgnoreCase("snack")) { - return new Snack(); - } else if (type.equalsIgnoreCase("drink")) { - return new Drink(); - } - return null; - } -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index 8875ce1..f0a499b 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -8,14 +8,27 @@ import java.util.List; +@Entity @Getter @Setter +@Table(name = "subscriptionbox") public class SubscriptionBox { - + @Id String id; + @Column(name = "box_name") String name; + + @Column(name = "box_type") String type; - int price; - List items; + @Column(name = "box_price") + int price; + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable( + name = "subscriptionbox_item", + joinColumns = @JoinColumn(name = "subscriptionbox_id", referencedColumnName = "id"), + inverseJoinColumns = @JoinColumn(name = "item_id", referencedColumnName = "id") + ) + List items; + // Rating rating; } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java new file mode 100644 index 0000000..9c46b1d --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java @@ -0,0 +1,29 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.builder; + +import org.springframework.stereotype.Component; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; + +@Component +public class ItemBuilder { + private Item item; + + private ItemBuilder() { + item = new Item(); + } + + public Item setName(String name) { + item.setName(name); + return item; + } + + public Item setQuantity(int quantity) { + item.setQuantity(quantity); + return item; + } + + public Item setId(String id) { + item.setId(id); + return item; + } + +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java index 235e502..9494137 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java @@ -7,6 +7,7 @@ import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; + public class SubscriptionBoxBuilder { private SubscriptionBox subscriptionBox; @@ -23,7 +24,9 @@ public SubscriptionBoxBuilder setName(String name) { } public SubscriptionBoxBuilder setType(String type) { - subscriptionBox.setType(type); + if (!type.equals("MONTHLY") | !type.equals("QUARTERLY") | !type.equals("SEMI-ANNUALLY")){ + throw new IllegalArgumentException("Invalid Type."); + } return this; } 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 new file mode 100644 index 0000000..1ee048c --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.java @@ -0,0 +1,41 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import lombok.Getter; +import org.springframework.stereotype.Repository; +import java.util.ArrayList; +import java.util.List; + + +@Repository +public class ItemRepository { + private List items = new ArrayList<>(); + public Item createItem(Item item) { + items.add(item); + return item; + } + + public Item getItemById(String id) { + for (Item item : items) { + if (item.getId().equals(id)) { + return item; + } + } + return null; + } + public Item deleteItem(Item item) { + items.remove(item); + return item; + } + public Item editItem(Item item) { + for (Item itemToEdit : items) { + if (item.getId().equals(item.getId())) { + itemToEdit = item; + return itemToEdit; + } + } + return null; + } +} + + diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java deleted file mode 100644 index 3937823..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemsRepository.java +++ /dev/null @@ -1,23 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription.repository; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -import org.springframework.stereotype.Repository; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -@Repository -public class ItemsRepository { - private List itemsData = new ArrayList<>(); - - public Items create(Items items) { - itemsData.add(items); - return items; - } - - public Iterator findAll() { - return itemsData.iterator(); - } - -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java index d52ef2a..fad78bf 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -13,7 +13,7 @@ public class SubscriptionBoxRepository { private List subscriptionBoxes = new ArrayList<>(); private List filteredBoxesByPrice = new ArrayList<>(); - private List filteredBoxesByRating = new ArrayList<>(); +// private List filteredBoxesByRating = new ArrayList<>(); public SubscriptionBox addBox(SubscriptionBox box) { subscriptionBoxes.add(box); @@ -30,16 +30,19 @@ public SubscriptionBox deleteBox(String id) { return null; } - public SubscriptionBox editBox(String id, SubscriptionBox box) { - for (SubscriptionBox subscriptionBox : subscriptionBoxes) { - if (subscriptionBox.getId().equals(id)) { - subscriptionBox = box; - return subscriptionBox; + public SubscriptionBox editBox(String id, SubscriptionBox updatedBox) { + for (SubscriptionBox box : subscriptionBoxes) { + if (box.getId().equals(id)) { + // Assuming the updatedBox object contains the updated fields + box.setName(updatedBox.getName()); + box.setPrice(updatedBox.getPrice()); + return box; // Return the updated box } } - return null; + return null; // Return null if no box with the given id was found } + public List viewAll() { return subscriptionBoxes; } diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java deleted file mode 100644 index d66a67c..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsService.java +++ /dev/null @@ -1,9 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription.service; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -import java.util.List; - -public interface ItemsService { - public Items create(Items items); - public List findAll(); -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java deleted file mode 100644 index 42f06c7..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemsServiceImpl.java +++ /dev/null @@ -1,9 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Items; -import java.util.List; - -public interface ItemsService { - public Items create(Items items); - public List findAll(); -} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 8715c4b..9999f38 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -1,5 +1,5 @@ -spring.datasource.url=jdbc:postgresql://aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres -spring.datasource.password=postgregaming123 +spring.datasource.url=jdbc:postgresql://localhost:5432/postgres +spring.datasource.password= ${LOCAL_PASSWORD} spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index bd0de3e..4963b8e 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -1,6 +1,6 @@ -spring.datasource.url=jdbc:postgresql://aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres -spring.datasource.username=postgregaming123 -spring.datasource.password=postgregaming123 +spring.datasource.url=jdbc:postgresql://aws-0-us-west-1.pooler.supabase.com:5432/postgres +spring.datasource.username=postgres.eihhrciqsxfomnqhrgle +spring.datasource.password=${TEST_PASSWORD} spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplicationTests.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplicationTests.java index 404d7c9..25b0158 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplicationTests.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/SnackscriptionSubscriptionboxApplicationTests.java @@ -6,8 +6,5 @@ @SpringBootTest class SnackscriptionSubscriptionboxApplicationTests { - @Test - void contextLoads() { - } } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java index d08c204..9441edc 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java @@ -19,6 +19,7 @@ import java.util.UUID; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -61,44 +62,26 @@ public void testCreateSubscriptionBox() throws Exception { .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())); } - @Test - public void testEditSubscriptionBox() throws Exception { - SubscriptionBox subscriptionBox = new SubscriptionBox(); - subscriptionBox.setId(UUID.randomUUID().toString()); - subscriptionBox.setName("Test Subscription Box"); - subscriptionBox.setPrice(100000); - // subscriptionBox.setRating(5); - - given(subscriptionBoxService.editBox(any(String.class), any(SubscriptionBox.class))).willReturn(subscriptionBox); - - mockMvc.perform(get("/subscription-box/edit/" + subscriptionBox.getId())) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.id").value(subscriptionBox.getId())) - .andExpect(jsonPath("$.name").value(subscriptionBox.getName())) - .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())) - // .andExpect(jsonPath("$.rating").value(subscriptionBox.getRating())) - ; - } - @Test public void testDeleteSubscriptionBox() throws Exception { + String boxId = UUID.randomUUID().toString(); SubscriptionBox subscriptionBox = new SubscriptionBox(); - subscriptionBox.setId(UUID.randomUUID().toString()); - subscriptionBox.setName("Test Subscription Box"); - subscriptionBox.setPrice(100000); - // subscriptionBox.setRating(5); + subscriptionBox.setId(boxId); + subscriptionBox.setType("MONTHLY"); + subscriptionBox.setName("Test Box"); + subscriptionBox.setPrice(20000); - given(subscriptionBoxService.deleteBox(any(String.class))).willReturn(subscriptionBox); + given(subscriptionBoxService.deleteBox(subscriptionBox.getId())).willReturn(subscriptionBox); - mockMvc.perform(get("/subscription-box/delete/" + subscriptionBox.getId())) + mockMvc.perform(delete("/subscription-box/delete/" + boxId)) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id").value(subscriptionBox.getId())) - .andExpect(jsonPath("$.name").value(subscriptionBox.getName())) - .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())) - // .andExpect(jsonPath("$.rating").value(subscriptionBox.getRating())) - ; + .andExpect(jsonPath("$.id").value(boxId)) + .andExpect(jsonPath("$.name").value("Test Box")) + .andExpect(jsonPath("$.type").value("MONTHLY")) + .andExpect(jsonPath("$.price").value(20000)); } + @Test public void testViewSubscriptionBox() throws Exception { SubscriptionBox subscriptionBox = new SubscriptionBox(); @@ -109,7 +92,7 @@ public void testViewSubscriptionBox() throws Exception { given(subscriptionBoxService.viewDetails(any(String.class))).willReturn(subscriptionBox.toString()); - mockMvc.perform(get("/subscription-box/view/" + subscriptionBox.getId())) + mockMvc.perform(get("/subscription-box/view-details/" + subscriptionBox.getId())) .andExpect(status().isOk()) .andExpect(jsonPath("$").value(subscriptionBox.toString())); } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index d0d712a..d385804 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -10,7 +10,7 @@ import java.util.UUID; public class SubscriptionBoxTest { - List items; + List items; SubscriptionBox subscriptionBox; @BeforeEach public void setUp() { @@ -20,10 +20,10 @@ public void setUp() { this.subscriptionBox.setType("MONTHLY"); this.subscriptionBox.setPrice(100000); this.items = new ArrayList<>(); - Items item1 = new Items(); + Item item1 = new Item(); items.add(item1); - Items item2 = new Items(); + Item item2 = new Item(); items.add(item2); this.subscriptionBox.setItems(items); } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java index 2a71ca4..f37992e 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java @@ -8,73 +8,75 @@ import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; +import org.mockito.Mock; public class SubscriptionBoxRepositoryTest { + @Mock + private SubscriptionBoxRepository subscriptionBoxRepository; - private SubscriptionBoxRepository subscriptionBoxRepository; - @BeforeEach - public void setUp() { - subscriptionBoxRepository = new SubscriptionBoxRepository(); - } + @BeforeEach + public void setUp() { + subscriptionBoxRepository = new SubscriptionBoxRepository(); + } - @Test - public void testAddBox() { - SubscriptionBox box = new SubscriptionBox(); - SubscriptionBox addedBox = subscriptionBoxRepository.addBox(box); - assertNotNull(addedBox); - } + @Test + public void testAddBox() { + SubscriptionBox box = new SubscriptionBox(); + SubscriptionBox addedBox = subscriptionBoxRepository.addBox(box); + assertNotNull(addedBox); + } - @Test - public void testDeleteBox() { - SubscriptionBox box = new SubscriptionBox(); - box.setId("1"); - subscriptionBoxRepository.addBox(box); - SubscriptionBox deletedBox = subscriptionBoxRepository.deleteBox(box.getId()); - assertNotNull(deletedBox); - } + @Test + public void testDeleteBox() { + SubscriptionBox box = new SubscriptionBox(); + box.setId("1"); + subscriptionBoxRepository.addBox(box); + SubscriptionBox deletedBox = subscriptionBoxRepository.deleteBox(box.getId()); + assertNotNull(deletedBox); + } - @Test - public void testEditBox() { - SubscriptionBox box = new SubscriptionBox(); - box.setId("1"); - String id = box.getId(); - box.setName("abc"); - subscriptionBoxRepository.addBox(box); - box.setName("def"); - SubscriptionBox editedBox = subscriptionBoxRepository.editBox(id, box); - assertEquals("def", editedBox.getName()); - } + @Test + public void testEditBox() { + SubscriptionBox box = new SubscriptionBox(); + box.setId("1"); + String id = box.getId(); + box.setName("abc"); + subscriptionBoxRepository.addBox(box); + box.setName("def"); + SubscriptionBox editedBox = subscriptionBoxRepository.editBox(id, box); + assertEquals("def", editedBox.getName()); + } - @Test - public void testViewAll() { - SubscriptionBox box1 = new SubscriptionBox(); - SubscriptionBox box2 = new SubscriptionBox(); - subscriptionBoxRepository.addBox(box1); - subscriptionBoxRepository.addBox(box2); - List allBoxes = subscriptionBoxRepository.viewAll(); - assertEquals(2, allBoxes.size()); - } + @Test + public void testViewAll() { + SubscriptionBox box1 = new SubscriptionBox(); + SubscriptionBox box2 = new SubscriptionBox(); + subscriptionBoxRepository.addBox(box1); + subscriptionBoxRepository.addBox(box2); + List allBoxes = subscriptionBoxRepository.viewAll(); + assertEquals(2, allBoxes.size()); + } - @Test - public void testViewDetails() { - SubscriptionBox box = new SubscriptionBox(); - box.setName("abc"); - box.setId("1"); - subscriptionBoxRepository.addBox(box); - String details = subscriptionBoxRepository.viewDetails(box.getId()); - assertEquals("abc", details); - } + @Test + public void testViewDetails() { + SubscriptionBox box = new SubscriptionBox(); + box.setName("abc"); + box.setId("1"); + subscriptionBoxRepository.addBox(box); + String details = subscriptionBoxRepository.viewDetails(box.getId()); + assertEquals("abc", details); + } - @Test - public void testFilterByPrice() { - SubscriptionBox box1 = new SubscriptionBox(); - box1.setPrice(15); - SubscriptionBox box2 = new SubscriptionBox(); - box2.setPrice(25); - subscriptionBoxRepository.addBox(box1); - subscriptionBoxRepository.addBox(box2); - List filteredBoxes = subscriptionBoxRepository.filterByPrice(15); - assertEquals(1, filteredBoxes.size()); - } + @Test + public void testFilterByPrice() { + SubscriptionBox box1 = new SubscriptionBox(); + box1.setPrice(15); + SubscriptionBox box2 = new SubscriptionBox(); + box2.setPrice(25); + subscriptionBoxRepository.addBox(box1); + subscriptionBoxRepository.addBox(box2); + List filteredBoxes = subscriptionBoxRepository.filterByPrice(15); + assertEquals(1, filteredBoxes.size()); + } } \ No newline at end of file diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java new file mode 100644 index 0000000..b208bb9 --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java @@ -0,0 +1,123 @@ +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.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.List; + +@ExtendWith(MockitoExtension.class) +public class SubscriptionBoxServiceImplTest { + + @Mock + private SubscriptionBoxRepository subscriptionBoxRepository; + + @InjectMocks + private SubscriptionBoxServiceImpl subscriptionBoxService; + + private SubscriptionBox box1, box2; + private Item item1, item2; + + @BeforeEach + void setUp() { + // Set up SubscriptionBox and Items + item1 = new Item(); + item1.setName("Chocolate Bar"); + + + item2 = new Item(); + item2.setName("Face Mask"); + + List items1 = new ArrayList<>(); + items1.add(item1); + List items2 = new ArrayList<>(); + items2.add(item2); + + box1 = new SubscriptionBox(); + box1.setId("1"); + box1.setName("Basic Box"); + box1.setType("MONTHLY"); + box1.setPrice(100); + box1.setItems(items1); + + box2 = new SubscriptionBox(); + box2.setId("2"); + box2.setName("Premium Box"); + box2.setType("QUARTERLY"); + box2.setPrice(200); + box2.setItems(items2); + } + + @Test + public void testAddBox() { + when(subscriptionBoxRepository.addBox(box1)).thenReturn(box1); + SubscriptionBox result = subscriptionBoxService.addBox(box1); + assertEquals(box1, result); + verify(subscriptionBoxRepository).addBox(box1); + } + + @Test + public void testEditBox() { + SubscriptionBox updatedBox = new SubscriptionBox(); + updatedBox.setId("1"); + updatedBox.setName("Updated Basic Box"); + updatedBox.setType("Monthly"); + updatedBox.setPrice(150); + updatedBox.setItems(new ArrayList<>(box1.getItems())); + + when(subscriptionBoxRepository.editBox("1", updatedBox)).thenReturn(updatedBox); + SubscriptionBox result = subscriptionBoxService.editBox("1", updatedBox); + assertEquals("Updated Basic Box", result.getName()); + assertEquals(150, result.getPrice()); + verify(subscriptionBoxRepository).editBox("1", updatedBox); + } + + @Test + public void testDeleteBox() { + when(subscriptionBoxRepository.deleteBox("1")).thenReturn(box1); + SubscriptionBox result = subscriptionBoxService.deleteBox("1"); + assertEquals(box1, result); + verify(subscriptionBoxRepository).deleteBox("1"); + } + + @Test + public void testViewAll() { + List boxes = new ArrayList<>(); + boxes.add(box1); + boxes.add(box2); + when(subscriptionBoxRepository.viewAll()).thenReturn(boxes); + List result = subscriptionBoxService.viewAll(); + assertEquals(2, result.size()); + verify(subscriptionBoxRepository).viewAll(); + } + + @Test + public void testViewDetails() { + when(subscriptionBoxRepository.viewDetails("1")).thenReturn("Basic Box"); + String result = subscriptionBoxService.viewDetails("1"); + assertEquals("Basic Box", result); + verify(subscriptionBoxRepository).viewDetails("1"); + } + + @Test + public void testFilterByPrice() { + List filteredBoxes = new ArrayList<>(); + filteredBoxes.add(box1); + when(subscriptionBoxRepository.filterByPrice(100)).thenReturn(filteredBoxes); + List result = subscriptionBoxService.filterByPrice(100); + assertEquals(1, result.size()); + assertTrue(result.contains(box1)); + verify(subscriptionBoxRepository).filterByPrice(100); + } +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java deleted file mode 100644 index 770c594..0000000 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.List; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; -public class SubscriptionBoxServiceTest { - - private SubscriptionBoxService subscriptionBoxService; - - @BeforeEach - public void setUp() { - subscriptionBoxService = new SubscriptionBoxServiceImpl(); - SubscriptionBox box1 = new SubscriptionBox(); - box1.setId("1"); - box1.setName("Box 1"); - box1.setPrice(100000); - box1.setType("MTH"); - subscriptionBoxService.addBox(box1); - } - - @Test - public void testAddBox() { - SubscriptionBox box2 = new SubscriptionBox(); - box2.setId("2"); - box2.setName("Box 2"); - box2.setPrice(200000); - box2.setType("MTH"); - subscriptionBoxService.addBox(box2); - assertEquals(2, subscriptionBoxService.viewAll().size()); - } - - @Test - public void testEditBox() { - SubscriptionBox box1 = new SubscriptionBox(); - box1.setId("1"); - box1.setName("Box 1"); - box1.setPrice(100000); - box1.setType("MTH"); - subscriptionBoxService.editBox("1", box1); - assertEquals("Box 1", subscriptionBoxService.viewDetails("1")); - } - - @Test - public void testDeleteBox() { - subscriptionBoxService.deleteBox("1"); - assertEquals(0, subscriptionBoxService.viewAll().size()); - } - - @Test - public void testViewAll() { - subscriptionBoxService.viewAll(); - assertEquals(1, subscriptionBoxService.viewAll().size()); - } - - @Test - public void testViewDetails() { - String name = subscriptionBoxService.viewDetails("1"); - assertEquals("Box 1", name); - } - - @Test - public void testFilterByPrice() { - List boxes = subscriptionBoxService.filterByPrice(100000); - assertEquals(1, boxes.size()); - } - - -} \ No newline at end of file From 0b5a2d63f6fec0d7c4e1ba219582272b1d0c9245 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Thu, 16 May 2024 13:11:34 +0700 Subject: [PATCH 27/28] [GREEN] [REFACTOR] Reimplement TDD with factory pattern on subscription box :D --- .../controller/SubscriptionBoxController.java | 121 +++++--- .../dto/DTOMapper.java | 39 +++ .../dto/SubscriptionBoxDTO.java | 17 ++ .../factory/Factory.java | 11 + .../factory/SubscriptionBoxFactory.java | 17 ++ .../model/SubscriptionBox.java | 41 ++- .../model/builder/SubscriptionBoxBuilder.java | 7 +- .../repository/SubscriptionBoxRepository.java | 166 ++++++++--- .../service/SubscriptionBoxService.java | 40 ++- .../service/SubscriptionBoxServiceImpl.java | 125 +++++++- src/main/resources/application-dev.properties | 12 +- .../resources/application-test.properties | 2 +- .../SubscriptionBoxControllerTest.java | 213 +++++++++----- .../factory/SubscriptionBoxFactoryTest.java | 39 +++ .../model/SubscriptionBoxTest.java | 20 +- .../SubscriptionBoxRepositoryTest.java | 269 +++++++++++++----- .../SubscriptionBoxServiceImplTest.java | 237 +++++++++------ 17 files changed, 1040 insertions(+), 336 deletions(-) create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/DTOMapper.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/SubscriptionBoxDTO.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java create mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java create mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactoryTest.java diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index 7cb9ea8..725c95b 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -1,65 +1,112 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService; - import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; @RestController @RequestMapping("/subscription-box") -@CrossOrigin(origins = "*") +@CrossOrigin(origins = "http://localhost:3000") // Change to specific origin if needed +public class SubscriptionBoxController { + private final SubscriptionBoxService subscriptionBoxService; -public class SubscriptionBoxController { @Autowired - private SubscriptionBoxService subscriptionBoxService; - String createHTML = "userCreate"; - @GetMapping("../") - public String createUserPage(Model model) { - return "

Subscription Box Management sudah berhasil!

"; + public SubscriptionBoxController(SubscriptionBoxService subscriptionBoxService) { + this.subscriptionBoxService = subscriptionBoxService; } @PostMapping("/create") - public ResponseEntity createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox, Model model) { - SubscriptionBox newBox = subscriptionBoxService.addBox(subscriptionBox); - return ResponseEntity.ok(newBox); + public CompletableFuture> createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox) { + return subscriptionBoxService.save(subscriptionBox) + .thenApply(ResponseEntity::ok) + .exceptionally(ex -> ResponseEntity.badRequest().build()); } - @GetMapping("/viewAll") - public ResponseEntity> viewAll() { - List allBoxes = subscriptionBoxService.viewAll(); - return ResponseEntity.ok(allBoxes); + @GetMapping("/list") + public CompletableFuture>> findAll() { + return subscriptionBoxService.findAll() + .thenApply(ResponseEntity::ok); } - @GetMapping("/view-details/{boxId}") - public ResponseEntity viewDetails(@PathVariable String boxId) { - String boxName = subscriptionBoxService.viewDetails(boxId); - return ResponseEntity.ok(boxName); + @GetMapping("/{id}") + public CompletableFuture> findById(@PathVariable String id) { + try { + UUID.fromString(id); + } catch (IllegalArgumentException e) { + return CompletableFuture.completedFuture(ResponseEntity.badRequest().build()); + } + + return subscriptionBoxService.findById(id) + .thenApply(optionalSubscriptionBox -> + optionalSubscriptionBox.map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build())); } - @DeleteMapping("/delete/{boxId}") - public ResponseEntity deleteBox(@PathVariable String boxId) { - SubscriptionBox deletedBox = subscriptionBoxService.deleteBox(boxId); - return ResponseEntity.ok(deletedBox); + @PatchMapping("/update") + public CompletableFuture> updateSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox) { + if (subscriptionBox.getId() == null || subscriptionBox.getId().isEmpty()) { + return CompletableFuture.completedFuture(ResponseEntity.badRequest().build()); + } + + return subscriptionBoxService.findById(subscriptionBox.getId()) + .thenCompose(optionalSubscriptionBox -> { + if (optionalSubscriptionBox.isEmpty()) { + return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); + } else { + return subscriptionBoxService.update(subscriptionBox) + .thenApply(ResponseEntity::ok); + } + }); } - @PutMapping("/edit/{boxId}") - public ResponseEntity editBox(@PathVariable String boxId, @RequestBody SubscriptionBox subscriptionBox) { - SubscriptionBox editedBox = subscriptionBoxService.editBox(boxId, subscriptionBox); - if (editedBox == null) { - return ResponseEntity.notFound().build(); + @DeleteMapping("/{id}") + public CompletableFuture> deleteSubscriptionBox(@PathVariable String id) { + try { + UUID.fromString(id); + } catch (IllegalArgumentException e) { + return CompletableFuture.completedFuture(ResponseEntity.badRequest().build()); } - return ResponseEntity.ok(editedBox); + + return subscriptionBoxService.delete(id) + .thenApply(result -> ResponseEntity.ok("DELETE SUCCESS")) + .exceptionally(ex -> ResponseEntity.notFound().build()); + } + + @GetMapping("/price/less-than/{price}") + public CompletableFuture>> findByPriceLessThan(@PathVariable int price) { + return subscriptionBoxService.findByPriceLessThan(price) + .thenApply(ResponseEntity::ok); + } + + @GetMapping("/price/greater-than/{price}") + public CompletableFuture>> findByPriceGreaterThan(@PathVariable int price) { + return subscriptionBoxService.findByPriceGreaterThan(price) + .thenApply(ResponseEntity::ok); + } + + @GetMapping("/price/equals/{price}") + public CompletableFuture>> findByPriceEquals(@PathVariable int price) { + return subscriptionBoxService.findByPriceEquals(price) + .thenApply(ResponseEntity::ok); + } + + @GetMapping("/name/{name}") + public CompletableFuture>>> findByName(@PathVariable String name) { + return subscriptionBoxService.findByName(name) + .thenApply(ResponseEntity::ok); } - @GetMapping("/filterByPrice/{price}") - public ResponseEntity> filterByPrice(@PathVariable int price) { - List filteredBoxes = subscriptionBoxService.filterByPrice(price); - return ResponseEntity.ok(filteredBoxes); + @GetMapping("/distinct-names") + public CompletableFuture>>> findDistinctNames() { + return subscriptionBoxService.findDistinctNames() + .thenApply(ResponseEntity::ok); } -} \ No newline at end of file +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/DTOMapper.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/DTOMapper.java new file mode 100644 index 0000000..710e0e6 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/DTOMapper.java @@ -0,0 +1,39 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import org.springframework.stereotype.Component; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory.SubscriptionBoxFactory; + +import java.util.List; +import java.util.Optional; + +@Component +public class DTOMapper { + public static SubscriptionBoxDTO convertModeltoDto (SubscriptionBox subscriptionBox){ + return new SubscriptionBoxDTO( + subscriptionBox.getId(), + subscriptionBox.getName(), + subscriptionBox.getType(), + subscriptionBox.getPrice(), + subscriptionBox.getItems() + ); + } + + public static SubscriptionBox convertDTOtoModel(SubscriptionBoxDTO subscriptionBoxDTO){ + String id = subscriptionBoxDTO.getId(); + String name = subscriptionBoxDTO.getName(); + String type = subscriptionBoxDTO.getType(); + int price = subscriptionBoxDTO.getPrice(); + List items = subscriptionBoxDTO.getItems(); + return new SubscriptionBoxFactory().create(id,name,type,price,items); + } + + public static SubscriptionBox updateSubscriptionBox(SubscriptionBox subscriptionBox, SubscriptionBoxDTO subscriptionBoxDTO){ + Optional.ofNullable(subscriptionBoxDTO.getItems()).ifPresent(subscriptionBox::setItems); + Optional.of( + subscriptionBoxDTO.getPrice()).ifPresent(subscriptionBox::setPrice); + return subscriptionBox; + + } +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/SubscriptionBoxDTO.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/SubscriptionBoxDTO.java new file mode 100644 index 0000000..e608fb1 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/SubscriptionBoxDTO.java @@ -0,0 +1,17 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto; + +import lombok.*; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; + +import java.util.List; + +@AllArgsConstructor +@NoArgsConstructor +@Getter @Setter +public class SubscriptionBoxDTO { + String id; + String name; + String type; + int price; + List items; +} 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 new file mode 100644 index 0000000..f5f4d16 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java @@ -0,0 +1,11 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; + +import java.util.List; + +public interface Factory { + T create(); + + 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/SubscriptionBoxFactory.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java new file mode 100644 index 0000000..c35f306 --- /dev/null +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java @@ -0,0 +1,17 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; + +import java.util.List; + +public class SubscriptionBoxFactory implements Factory { + @Override + public SubscriptionBox create(){ + return new SubscriptionBox(); + } + + public SubscriptionBox create(String id, String name, String type, int price, List items ){ + return new SubscriptionBox( name, type, price, items); + } +} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java index f0a499b..27b9af7 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.java @@ -6,6 +6,7 @@ import lombok.Getter; import lombok.Setter; import java.util.List; +import java.util.UUID; @Entity @@ -31,4 +32,42 @@ public class SubscriptionBox { ) List items; // Rating rating; -} \ No newline at end of file + + public SubscriptionBox(){ + this.id = UUID.randomUUID().toString(); + } + + public SubscriptionBox( String name, String type, int price, List items){ + this.id = UUID.randomUUID().toString(); + this.name = name; + this.setType(type); + this.setPrice(price); + this.items = items; + } + + public void setType(String type) { + if (type.equalsIgnoreCase("monthly") | + type.equalsIgnoreCase("quarterly") | + type.equalsIgnoreCase("semi-annual") + ){ + this.type = type; + } + else{ + throw new IllegalArgumentException("Invalid type"); + } + + + } + + public void setPrice(int price) { + if (price >0){ + this.price = price; + } + else{ + throw new IllegalArgumentException("Invalid Price, Please enter integer above 0"); + } + + + } +} +; diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java index 9494137..5555acb 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java @@ -24,10 +24,11 @@ public SubscriptionBoxBuilder setName(String name) { } public SubscriptionBoxBuilder setType(String type) { - if (!type.equals("MONTHLY") | !type.equals("QUARTERLY") | !type.equals("SEMI-ANNUALLY")){ - throw new IllegalArgumentException("Invalid Type."); + if (type.equals("MONTHLY") | type.equals("QUARTERLY") | type.equals("SEMI-ANNUALLY")){ + return this; + } - return this; + throw new IllegalArgumentException("Invalid Type."); } public SubscriptionBoxBuilder setPrice(int price) { diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java index fad78bf..b45df42 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -2,68 +2,150 @@ import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import jakarta.transaction.TransactionScoped; import org.springframework.stereotype.Repository; import java.util.ArrayList; +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; +import jakarta.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Optional; @Repository public class SubscriptionBoxRepository { + @Autowired + private EntityManager entityManager; - private List subscriptionBoxes = new ArrayList<>(); - private List filteredBoxesByPrice = new ArrayList<>(); -// private List filteredBoxesByRating = new ArrayList<>(); + @Transactional + public SubscriptionBox save(SubscriptionBox subscriptionBox) { + entityManager.persist(subscriptionBox); + return subscriptionBox; + } - public SubscriptionBox addBox(SubscriptionBox box) { - subscriptionBoxes.add(box); - return box; + @Transactional + public Optional findById(String id){ + SubscriptionBox subscription = entityManager.find(SubscriptionBox.class, id); + return Optional.ofNullable(subscription); + } + @Transactional + public List findAll(){ + String jpql = "SELECT sb FROM SubscriptionBox sb"; + TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); + return query.getResultList(); } - public SubscriptionBox deleteBox(String id) { - for (SubscriptionBox subscriptionBox : subscriptionBoxes) { - if (subscriptionBox.getId().equals(id)) { - subscriptionBoxes.remove(subscriptionBox); - return subscriptionBox; - } - } - return null; + @Transactional + public SubscriptionBox update(SubscriptionBox subscriptionBox){ + return entityManager.merge(subscriptionBox); } - public SubscriptionBox editBox(String id, SubscriptionBox updatedBox) { - for (SubscriptionBox box : subscriptionBoxes) { - if (box.getId().equals(id)) { - // Assuming the updatedBox object contains the updated fields - box.setName(updatedBox.getName()); - box.setPrice(updatedBox.getPrice()); - return box; // Return the updated box - } - } - return null; // Return null if no box with the given id was found + @Transactional + public void delete(String id){ + SubscriptionBox subscription = findById(id) + .orElseThrow(() -> new IllegalArgumentException("Subscription Box ID not found")); + entityManager.remove(subscription); } + @Transactional + public List findByPriceLessThan(int price) { + String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price"; + TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); + query.setParameter("price", price); + return query.getResultList(); + } + + @Transactional + public List findByPriceGreaterThan(int price) { + String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price"; + TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); + query.setParameter("price", price); + return query.getResultList(); + } - public List viewAll() { - return subscriptionBoxes; + @Transactional + public List findByPriceEquals(int price) { + String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price"; + TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); + query.setParameter("price", price); + return query.getResultList(); } - public String viewDetails(String boxId) { - for (SubscriptionBox subscriptionBox : subscriptionBoxes) { - if (subscriptionBox.getId().equals(boxId)) { - return subscriptionBox.getName(); - } - } - return null; + @Transactional + public Optional> findByName(String name) { + String jpql = "SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)"; + TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); + query.setParameter("name", "%" + name.toLowerCase() + "%"); // Convert input name to lowercase + List result = query.getResultList(); + return Optional.ofNullable(result.isEmpty() ? null : result); } - public List filterByPrice(int price) { - List filteredBoxes = new ArrayList<>(); - for (SubscriptionBox subscriptionBox : subscriptionBoxes) { - if (subscriptionBox.getPrice() == price) { - filteredBoxes.add(subscriptionBox); - } - } - filteredBoxesByPrice = filteredBoxes; - return filteredBoxesByPrice; + @Transactional + public Optional> findDistinctNames() { + String jpql = "SELECT DISTINCT sb.name FROM SubscriptionBox sb"; + TypedQuery query = entityManager.createQuery(jpql, String.class); + List result = query.getResultList(); + return Optional.ofNullable(result.isEmpty() ? null : result); } + +// private List subscriptionBoxes = new ArrayList<>(); +// private List filteredBoxesByPrice = new ArrayList<>(); +//// private List filteredBoxesByRating = new ArrayList<>(); +// +// public SubscriptionBox addBox(SubscriptionBox box) { +// subscriptionBoxes.add(box); +// return box; +// } +// +// public SubscriptionBox deleteBox(String id) { +// for (SubscriptionBox subscriptionBox : subscriptionBoxes) { +// if (subscriptionBox.getId().equals(id)) { +// subscriptionBoxes.remove(subscriptionBox); +// return subscriptionBox; +// } +// } +// return null; +// } +// +// public SubscriptionBox editBox(String id, SubscriptionBox updatedBox) { +// for (SubscriptionBox box : subscriptionBoxes) { +// if (box.getId().equals(id)) { +// // Assuming the updatedBox object contains the updated fields +// box.setName(updatedBox.getName()); +// box.setPrice(updatedBox.getPrice()); +// return box; // Return the updated box +// } +// } +// return null; // Return null if no box with the given id was found +// } +// +// +// public List viewAll() { +// return subscriptionBoxes; +// } +// +// public String viewDetails(String boxId) { +// for (SubscriptionBox subscriptionBox : subscriptionBoxes) { +// if (subscriptionBox.getId().equals(boxId)) { +// return subscriptionBox.getName(); +// } +// } +// return null; +// } +// +// public List filterByPrice(int price) { +// List filteredBoxes = new ArrayList<>(); +// for (SubscriptionBox subscriptionBox : subscriptionBoxes) { +// if (subscriptionBox.getPrice() == price) { +// filteredBoxes.add(subscriptionBox); +// } +// } +// filteredBoxesByPrice = filteredBoxes; +// return filteredBoxesByPrice; +// } } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java index 5596309..a8c52c7 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java @@ -3,13 +3,39 @@ import java.util.List; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import org.springframework.scheduling.annotation.Async; + +import java.util.Optional; +import java.util.concurrent.CompletableFuture; public interface SubscriptionBoxService { - public SubscriptionBox addBox(SubscriptionBox subscriptionBox); - public SubscriptionBox editBox(String id, SubscriptionBox subscriptionBox); - public SubscriptionBox deleteBox(String id); - public List viewAll(); - public String viewDetails(String boxId); - public List filterByPrice(int price); - // public List filterByRating(int rating); + CompletableFuture save(SubscriptionBox subscriptionBox); + + CompletableFuture> findById(String id); + + CompletableFuture> findAll(); + + CompletableFuture update(SubscriptionBox subscriptionBox); + + CompletableFuture delete(String id); + + CompletableFuture> findByPriceLessThan(int price); + + CompletableFuture> findByPriceGreaterThan(int price); + + CompletableFuture> findByPriceEquals(int price); + + CompletableFuture>> findByName(String name); + + CompletableFuture>> findDistinctNames(); + + + +// public SubscriptionBox addBox(SubscriptionBox subscriptionBox); +// public SubscriptionBox editBox(String id, SubscriptionBox subscriptionBox); +// public SubscriptionBox deleteBox(String id); +// public List viewAll(); +// public String viewDetails(String boxId); +// public List filterByPrice(int price); +// // public List filterByRating(int rating); } \ No newline at end of file diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java index f0624b1..1c64846 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java @@ -1,47 +1,144 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; + + import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; @Service -public class SubscriptionBoxServiceImpl implements SubscriptionBoxService{ +public class SubscriptionBoxServiceImpl implements SubscriptionBoxService { @Autowired private SubscriptionBoxRepository subscriptionBoxRepository; @Override - public SubscriptionBox addBox(SubscriptionBox box) { - return subscriptionBoxRepository.addBox(box); + @Async + public CompletableFuture save(SubscriptionBox subscriptionBox) { + return CompletableFuture.completedFuture(subscriptionBoxRepository.save(subscriptionBox)); + } + + @Override + @Async + public CompletableFuture> findById(String id) { + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("ID cannot be null or empty"); + } + return CompletableFuture.completedFuture(subscriptionBoxRepository.findById(id)); + } + + @Override + @Async + public CompletableFuture> findAll() { + List subscriptionBoxes = subscriptionBoxRepository.findAll(); + return CompletableFuture.completedFuture(subscriptionBoxes); } @Override - public SubscriptionBox deleteBox(String id) { - return subscriptionBoxRepository.deleteBox(id); + @Async + public CompletableFuture update(SubscriptionBox subscriptionBox) { + if (subscriptionBox == null) { + throw new IllegalArgumentException("SubscriptionBox cannot be null"); + } + return CompletableFuture.completedFuture(subscriptionBoxRepository.update(subscriptionBox)); } @Override - public SubscriptionBox editBox(String id, SubscriptionBox box) { - return subscriptionBoxRepository.editBox(id, box); + @Async + public CompletableFuture delete(String id) { + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("ID cannot be null or empty"); + } + Optional subscriptionBox = subscriptionBoxRepository.findById(id); + if (!subscriptionBox.isPresent()) { + throw new IllegalArgumentException("Subscription Box not found"); + } + subscriptionBoxRepository.delete(id); + return CompletableFuture.completedFuture(null); } @Override - public List viewAll() { - return subscriptionBoxRepository.viewAll(); + @Async + public CompletableFuture> findByPriceLessThan(int price) { + List result = subscriptionBoxRepository.findByPriceLessThan(price); + return CompletableFuture.completedFuture(result); } @Override - public String viewDetails(String boxId) { - return subscriptionBoxRepository.viewDetails(boxId); + @Async + public CompletableFuture> findByPriceGreaterThan(int price) { + List result = subscriptionBoxRepository.findByPriceGreaterThan(price); + return CompletableFuture.completedFuture(result); } @Override - public List filterByPrice(int price) { - return subscriptionBoxRepository.filterByPrice(price); + @Async + public CompletableFuture> findByPriceEquals(int price) { + List result = subscriptionBoxRepository.findByPriceEquals(price); + return CompletableFuture.completedFuture(result); } + @Override + @Async + public CompletableFuture>> findByName(String name) { + Optional> result = subscriptionBoxRepository.findByName(name); + return CompletableFuture.completedFuture(result); + } + + @Override + public CompletableFuture>> findDistinctNames() { + Optional> distinctNames = subscriptionBoxRepository.findDistinctNames(); + return CompletableFuture.completedFuture(distinctNames); + } + + +} -} \ No newline at end of file +// +//@Service +//public class SubscriptionBoxServiceImpl implements SubscriptionBoxService{ +// @Autowired +// private SubscriptionBoxRepository subscriptionBoxRepository; +// +// @Override +// @Async +// public CompletableFuture +// +// @Override +// public SubscriptionBox addBox(SubscriptionBox box) { +// return subscriptionBoxRepository.addBox(box); +// } +// +// @Override +// public SubscriptionBox deleteBox(String id) { +// return subscriptionBoxRepository.deleteBox(id); +// } +// +// @Override +// public SubscriptionBox editBox(String id, SubscriptionBox box) { +// return subscriptionBoxRepository.editBox(id, box); +// } +// +// @Override +// public List viewAll() { +// return subscriptionBoxRepository.viewAll(); +// } +// +// @Override +// public String viewDetails(String boxId) { +// return subscriptionBoxRepository.viewDetails(boxId); +// } +// +// @Override +// public List filterByPrice(int price) { +// return subscriptionBoxRepository.filterByPrice(price); +// } +// +// +//} \ No newline at end of file diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 9999f38..4176bef 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -1,6 +1,12 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/postgres -spring.datasource.password= ${LOCAL_PASSWORD} -spring.jpa.hibernate.ddl-auto=create-drop +#spring.datasource.password= ${LOCAL_PASSWORD} +spring.datasource.username= postgres +spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file +spring.jpa.properties.hibernate.format_sql=true + + + + +spring.datasource.password= nelwanfamily2004 \ No newline at end of file diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index 4963b8e..92d131d 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -1,7 +1,7 @@ spring.datasource.url=jdbc:postgresql://aws-0-us-west-1.pooler.supabase.com:5432/postgres spring.datasource.username=postgres.eihhrciqsxfomnqhrgle spring.datasource.password=${TEST_PASSWORD} -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java index 9441edc..303cc0a 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java @@ -1,33 +1,25 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; -import com.fasterxml.jackson.databind.ObjectMapper; - import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.http.ResponseEntity; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.mockito.Mockito.when; -@ExtendWith(SpringExtension.class) -public class SubscriptionBoxControllerTest { - - private MockMvc mockMvc; +class SubscriptionBoxControllerTest { @Mock private SubscriptionBoxService subscriptionBoxService; @@ -35,79 +27,158 @@ public class SubscriptionBoxControllerTest { @InjectMocks private SubscriptionBoxController subscriptionBoxController; - private ObjectMapper objectMapper = new ObjectMapper(); + private SubscriptionBox subscriptionBox; @BeforeEach - public void setUp() { - MockitoAnnotations.initMocks(this); - mockMvc = MockMvcBuilders.standaloneSetup(subscriptionBoxController).build(); + void setUp() { + MockitoAnnotations.openMocks(this); + + subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null); + subscriptionBox.setId(UUID.randomUUID().toString()); } @Test - public void testCreateSubscriptionBox() throws Exception { - SubscriptionBox subscriptionBox = new SubscriptionBox(); - subscriptionBox.setId(UUID.randomUUID().toString()); - subscriptionBox.setName("Test Subscription Box"); - subscriptionBox.setPrice(100000); - // subscriptionBox.setRating(5); - - given(subscriptionBoxService.addBox(any(SubscriptionBox.class))).willReturn(subscriptionBox); - - mockMvc.perform(post("/subscription-box/create") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(subscriptionBox))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.id").value(subscriptionBox.getId())) - .andExpect(jsonPath("$.name").value(subscriptionBox.getName())) - .andExpect(jsonPath("$.price").value(subscriptionBox.getPrice())); + void testCreateSubscriptionBox() { + when(subscriptionBoxService.save(any(SubscriptionBox.class))) + .thenReturn(CompletableFuture.completedFuture(subscriptionBox)); + + CompletableFuture> result = subscriptionBoxController.createSubscriptionBox(subscriptionBox); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBox), result.join()); } @Test - public void testDeleteSubscriptionBox() throws Exception { - String boxId = UUID.randomUUID().toString(); - SubscriptionBox subscriptionBox = new SubscriptionBox(); - subscriptionBox.setId(boxId); - subscriptionBox.setType("MONTHLY"); - subscriptionBox.setName("Test Box"); - subscriptionBox.setPrice(20000); - - given(subscriptionBoxService.deleteBox(subscriptionBox.getId())).willReturn(subscriptionBox); - - mockMvc.perform(delete("/subscription-box/delete/" + boxId)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.id").value(boxId)) - .andExpect(jsonPath("$.name").value("Test Box")) - .andExpect(jsonPath("$.type").value("MONTHLY")) - .andExpect(jsonPath("$.price").value(20000)); + void testFindAll() { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + + when(subscriptionBoxService.findAll()) + .thenReturn(CompletableFuture.completedFuture(subscriptionBoxes)); + + CompletableFuture>> result = subscriptionBoxController.findAll(); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBoxes), result.join()); } + @Test + void testFindById() { + when(subscriptionBoxService.findById(subscriptionBox.getId())) + .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBox))); + + CompletableFuture> result = subscriptionBoxController.findById(subscriptionBox.getId()); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBox), result.join()); + } @Test - public void testViewSubscriptionBox() throws Exception { - SubscriptionBox subscriptionBox = new SubscriptionBox(); - subscriptionBox.setId(UUID.randomUUID().toString()); - subscriptionBox.setName("Test Subscription Box"); - subscriptionBox.setPrice(100000); - // subscriptionBox.setRating(5); + void testFindByIdInvalidId() { + String invalidId = "invalid_id"; + + CompletableFuture> result = subscriptionBoxController.findById(invalidId); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.badRequest().build(), result.join()); + } + + @Test + void testUpdateSubscriptionBox() { + when(subscriptionBoxService.findById(subscriptionBox.getId())) + .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBox))); + when(subscriptionBoxService.update(any(SubscriptionBox.class))) + .thenReturn(CompletableFuture.completedFuture(subscriptionBox)); + + CompletableFuture> result = subscriptionBoxController.updateSubscriptionBox(subscriptionBox); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBox), result.join()); + } + + @Test + void testDeleteSubscriptionBox() { + when(subscriptionBoxService.delete(subscriptionBox.getId())) + .thenReturn(CompletableFuture.completedFuture(null)); - given(subscriptionBoxService.viewDetails(any(String.class))).willReturn(subscriptionBox.toString()); + CompletableFuture> result = subscriptionBoxController.deleteSubscriptionBox(subscriptionBox.getId()); - mockMvc.perform(get("/subscription-box/view-details/" + subscriptionBox.getId())) - .andExpect(status().isOk()) - .andExpect(jsonPath("$").value(subscriptionBox.toString())); + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok("DELETE SUCCESS"), result.join()); } @Test - public void testViewAllSubscriptionBox() throws Exception { - mockMvc.perform(get("/subscription-box/viewAll/")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$").isArray()); + void testFindByPriceLessThan() { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + + when(subscriptionBoxService.findByPriceLessThan(150)) + .thenReturn(CompletableFuture.completedFuture(subscriptionBoxes)); + + CompletableFuture>> result = subscriptionBoxController.findByPriceLessThan(150); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBoxes), result.join()); } @Test - public void testFilterByPrice() throws Exception { - mockMvc.perform(get("/subscription-box/filterByPrice/100000")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$").isArray()); + void testFindByPriceGreaterThan() { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + + when(subscriptionBoxService.findByPriceGreaterThan(50)) + .thenReturn(CompletableFuture.completedFuture(subscriptionBoxes)); + + CompletableFuture>> result = subscriptionBoxController.findByPriceGreaterThan(50); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBoxes), result.join()); + } + + @Test + void testFindByPriceEquals() { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + + when(subscriptionBoxService.findByPriceEquals(100)) + .thenReturn(CompletableFuture.completedFuture(subscriptionBoxes)); + + CompletableFuture>> result = subscriptionBoxController.findByPriceEquals(100); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(subscriptionBoxes), result.join()); + } + + @Test + void testFindByName() { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + + when(subscriptionBoxService.findByName("Basic")) + .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBoxes))); + + CompletableFuture>>> result = subscriptionBoxController.findByName("Basic"); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(Optional.of(subscriptionBoxes)), result.join()); + } + + @Test + void testFindDistinctNames() { + List names = Arrays.asList("Basic", "Premium"); + + when(subscriptionBoxService.findDistinctNames()) + .thenReturn(CompletableFuture.completedFuture(Optional.of(names))); + + CompletableFuture>>> result = subscriptionBoxController.findDistinctNames(); + + assertNotNull(result); + assertTrue(result.isDone()); + assertEquals(ResponseEntity.ok(Optional.of(names)), result.join()); } -} \ No newline at end of file +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactoryTest.java new file mode 100644 index 0000000..a8bea7e --- /dev/null +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactoryTest.java @@ -0,0 +1,39 @@ +package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; + +class SubscriptionBoxFactoryTest { + + @Test + void testCreateEmptySubscriptionBox() { + SubscriptionBoxFactory subscriptionBoxFactory = new SubscriptionBoxFactory(); + SubscriptionBox subscriptionBox = subscriptionBoxFactory.create(); + assertNotNull(subscriptionBox.getId()); // Check if ID is auto-generated + assertNull(subscriptionBox.getName()); // Expect null for name as none set + assertNull(subscriptionBox.getType()); // Expect null for type as none set + assertEquals(0, subscriptionBox.getPrice()); // Default price should be 0 + assertNull(subscriptionBox.getItems()); // Items should be null by default + } + + @Test + void testCreateSubscriptionBoxWithParameters() { + SubscriptionBoxFactory subscriptionBoxFactory = new SubscriptionBoxFactory(); + List items = new ArrayList<>(); + items.add(new Item()); // Assuming you have a constructor in Item class like this + items.add(new Item()); + + SubscriptionBox subscriptionBox = subscriptionBoxFactory.create("1", "Deluxe Box", "Monthly", 150, items); + + assertEquals("Deluxe Box", subscriptionBox.getName()); + assertEquals("Monthly", subscriptionBox.getType()); + assertEquals(150, subscriptionBox.getPrice()); + assertNotNull(subscriptionBox.getItems()); + assertEquals(2, subscriptionBox.getItems().size()); // Check that two items were added + } +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java index d385804..205f22d 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBoxTest.java @@ -1,13 +1,14 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; + +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.SubscriptionBoxDTO; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.util.ArrayList; import java.util.List; -import java.util.UUID; + public class SubscriptionBoxTest { List items; @@ -43,6 +44,21 @@ public void testGetType() { assertEquals("MONTHLY", subscriptionBox.getType()); } + @Test + public void testCreateInvalidType(){ + assertThrows(IllegalArgumentException.class, () -> { + SubscriptionBox subscriptionBoxTest = new SubscriptionBox(subscriptionBox.getName(), "Daily", subscriptionBox.getPrice(), subscriptionBox.getItems()); + }); + + } + + @Test + public void testInvalidPrice(){ + assertThrows(IllegalArgumentException.class, () -> { + subscriptionBox.setPrice(-1); + }); + + } @Test public void testGetPrice() { assertEquals(100000, subscriptionBox.getPrice()); diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java index f37992e..42067ef 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java @@ -1,82 +1,201 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; -import org.junit.jupiter.api.BeforeEach; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Arrays; import java.util.List; +import java.util.Optional; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; -import org.mockito.Mock; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class SubscriptionBoxRepositoryTest { + + @Mock + private EntityManager entityManager; + + @InjectMocks + private SubscriptionBoxRepository subscriptionBoxRepository; + + @Test + void testSave() { + SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null); + SubscriptionBox savedSubscriptionBox = subscriptionBoxRepository.save(subscriptionBox); + assertEquals(subscriptionBox, savedSubscriptionBox); + verify(entityManager, times(1)).persist(subscriptionBox); + } + + @Test + void testFindAll() { + SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null); + SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null); + + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb", SubscriptionBox.class)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1, subscriptionBox2)); + + List subscriptionBoxes = subscriptionBoxRepository.findAll(); + + assertEquals(2, subscriptionBoxes.size()); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb", SubscriptionBox.class); + verify(query, times(1)).getResultList(); + } + + @Test + void testFindById() { + SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null); + subscriptionBox.setId("1"); + + when(entityManager.find(SubscriptionBox.class, "1")).thenReturn(subscriptionBox); + + Optional optionalSubscriptionBox = subscriptionBoxRepository.findById("1"); + + assertEquals(Optional.of(subscriptionBox), optionalSubscriptionBox); + verify(entityManager, times(1)).find(SubscriptionBox.class, "1"); + } + + @Test + void testFindByIdSubscriptionNotFound() { + when(entityManager.find(SubscriptionBox.class, "nonexistentId")).thenReturn(null); + + assertNull(subscriptionBoxRepository.findById("nonexistentId").orElse(null)); + + verify(entityManager, times(1)).find(SubscriptionBox.class, "nonexistentId"); + } + + @Test + void testUpdate() { + SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null); + + when(entityManager.merge(subscriptionBox)).thenReturn(subscriptionBox); + + SubscriptionBox updatedSubscriptionBox = subscriptionBoxRepository.update(subscriptionBox); + + assertEquals(subscriptionBox, updatedSubscriptionBox); + verify(entityManager, times(1)).merge(subscriptionBox); + } + + @Test + void testDelete() { + SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null); + subscriptionBox.setId("1"); + + when(entityManager.find(SubscriptionBox.class, "1")).thenReturn(subscriptionBox); + doNothing().when(entityManager).remove(subscriptionBox); + + subscriptionBoxRepository.delete("1"); + + verify(entityManager, times(1)).find(SubscriptionBox.class, "1"); + verify(entityManager, times(1)).remove(subscriptionBox); + } + + @Test + void testDeleteSubscriptionNotFound() { + when(entityManager.find(SubscriptionBox.class, "1")).thenReturn(null); + + assertThrows(IllegalArgumentException.class, () -> subscriptionBoxRepository.delete("1")); + + verify(entityManager, times(1)).find(SubscriptionBox.class, "1"); + verify(entityManager, never()).remove(any()); + } + + @Test + void testFindByPriceLessThan() { + SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null); + SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null); + + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("price", 150)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1)); + + List result = subscriptionBoxRepository.findByPriceLessThan(150); + + assertEquals(1, result.size()); + assertEquals(subscriptionBox1, result.get(0)); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price", SubscriptionBox.class); + verify(query, times(1)).setParameter("price", 150); + verify(query, times(1)).getResultList(); + } + + @Test + void testFindByPriceGreaterThan() { + SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null); + SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null); + + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("price", 150)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox2)); + + List result = subscriptionBoxRepository.findByPriceGreaterThan(150); + + assertEquals(1, result.size()); + assertEquals(subscriptionBox2, result.get(0)); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price", SubscriptionBox.class); + verify(query, times(1)).setParameter("price", 150); + verify(query, times(1)).getResultList(); + } + + @Test + void testFindByPriceEquals() { + SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null); + SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null); + + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("price", 100)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1)); + + List result = subscriptionBoxRepository.findByPriceEquals(100); + + assertEquals(1, result.size()); + assertEquals(subscriptionBox1, result.get(0)); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price", SubscriptionBox.class); + verify(query, times(1)).setParameter("price", 100); + verify(query, times(1)).getResultList(); + } + + @Test + void testFindByName() { + SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic Box", "Monthly", 100, null); + SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium Box", "Monthly", 200, null); + + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("name", "%box%")).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1, subscriptionBox2)); + + Optional> result = subscriptionBoxRepository.findByName("box"); + + assertTrue(result.isPresent()); + assertEquals(2, result.get().size()); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)", SubscriptionBox.class); + verify(query, times(1)).setParameter("name", "%box%"); + verify(query, times(1)).getResultList(); + } + + @Test + void testFindDistinctNames() { + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT DISTINCT sb.name FROM SubscriptionBox sb", String.class)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList("Basic Box", "Premium Box")); + + Optional> result = subscriptionBoxRepository.findDistinctNames(); -public class SubscriptionBoxRepositoryTest { - @Mock - private SubscriptionBoxRepository subscriptionBoxRepository; - - - @BeforeEach - public void setUp() { - subscriptionBoxRepository = new SubscriptionBoxRepository(); - } - - @Test - public void testAddBox() { - SubscriptionBox box = new SubscriptionBox(); - SubscriptionBox addedBox = subscriptionBoxRepository.addBox(box); - assertNotNull(addedBox); - } - - @Test - public void testDeleteBox() { - SubscriptionBox box = new SubscriptionBox(); - box.setId("1"); - subscriptionBoxRepository.addBox(box); - SubscriptionBox deletedBox = subscriptionBoxRepository.deleteBox(box.getId()); - assertNotNull(deletedBox); - } - - @Test - public void testEditBox() { - SubscriptionBox box = new SubscriptionBox(); - box.setId("1"); - String id = box.getId(); - box.setName("abc"); - subscriptionBoxRepository.addBox(box); - box.setName("def"); - SubscriptionBox editedBox = subscriptionBoxRepository.editBox(id, box); - assertEquals("def", editedBox.getName()); - } - - @Test - public void testViewAll() { - SubscriptionBox box1 = new SubscriptionBox(); - SubscriptionBox box2 = new SubscriptionBox(); - subscriptionBoxRepository.addBox(box1); - subscriptionBoxRepository.addBox(box2); - List allBoxes = subscriptionBoxRepository.viewAll(); - assertEquals(2, allBoxes.size()); - } - - @Test - public void testViewDetails() { - SubscriptionBox box = new SubscriptionBox(); - box.setName("abc"); - box.setId("1"); - subscriptionBoxRepository.addBox(box); - String details = subscriptionBoxRepository.viewDetails(box.getId()); - assertEquals("abc", details); - } - - @Test - public void testFilterByPrice() { - SubscriptionBox box1 = new SubscriptionBox(); - box1.setPrice(15); - SubscriptionBox box2 = new SubscriptionBox(); - box2.setPrice(25); - subscriptionBoxRepository.addBox(box1); - subscriptionBoxRepository.addBox(box2); - List filteredBoxes = subscriptionBoxRepository.filterByPrice(15); - assertEquals(1, filteredBoxes.size()); - } -} \ No newline at end of file + assertTrue(result.isPresent()); + assertEquals(2, result.get().size()); + assertTrue(result.get().contains("Basic Box")); + assertTrue(result.get().contains("Premium Box")); + verify(entityManager, times(1)).createQuery("SELECT DISTINCT sb.name FROM SubscriptionBox sb", String.class); + verify(query, times(1)).getResultList(); + } +} diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java index b208bb9..aa05b32 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java @@ -1,24 +1,25 @@ 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.model.SubscriptionBox; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) -public class SubscriptionBoxServiceImplTest { +class SubscriptionBoxServiceImplTest { @Mock private SubscriptionBoxRepository subscriptionBoxRepository; @@ -26,98 +27,174 @@ public class SubscriptionBoxServiceImplTest { @InjectMocks private SubscriptionBoxServiceImpl subscriptionBoxService; - private SubscriptionBox box1, box2; - private Item item1, item2; + private SubscriptionBox subscriptionBox; @BeforeEach void setUp() { - // Set up SubscriptionBox and Items - item1 = new Item(); - item1.setName("Chocolate Bar"); - - - item2 = new Item(); - item2.setName("Face Mask"); - - List items1 = new ArrayList<>(); - items1.add(item1); - List items2 = new ArrayList<>(); - items2.add(item2); - - box1 = new SubscriptionBox(); - box1.setId("1"); - box1.setName("Basic Box"); - box1.setType("MONTHLY"); - box1.setPrice(100); - box1.setItems(items1); - - box2 = new SubscriptionBox(); - box2.setId("2"); - box2.setName("Premium Box"); - box2.setType("QUARTERLY"); - box2.setPrice(200); - box2.setItems(items2); + subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null); + subscriptionBox.setId("1"); + } + + @Test + void testSave() throws ExecutionException, InterruptedException { + when(subscriptionBoxRepository.save(subscriptionBox)).thenReturn(subscriptionBox); + + CompletableFuture future = subscriptionBoxService.save(subscriptionBox); + SubscriptionBox result = future.get(); + + assertEquals(subscriptionBox, result); + verify(subscriptionBoxRepository, times(1)).save(subscriptionBox); + } + + @Test + void testFindById() throws ExecutionException, InterruptedException { + when(subscriptionBoxRepository.findById("1")).thenReturn(Optional.of(subscriptionBox)); + + CompletableFuture> future = subscriptionBoxService.findById("1"); + Optional result = future.get(); + + assertTrue(result.isPresent()); + assertEquals(subscriptionBox, result.get()); + verify(subscriptionBoxRepository, times(1)).findById("1"); + } + + @Test + void testFindByIdInvalidId() { + assertThrows(IllegalArgumentException.class, () -> { + subscriptionBoxService.findById("").get(); + }); + verify(subscriptionBoxRepository, never()).findById(anyString()); + } + + @Test + void testFindAll() throws ExecutionException, InterruptedException { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + when(subscriptionBoxRepository.findAll()).thenReturn(subscriptionBoxes); + + CompletableFuture> future = subscriptionBoxService.findAll(); + List result = future.get(); + + assertEquals(1, result.size()); + assertEquals(subscriptionBox, result.get(0)); + verify(subscriptionBoxRepository, times(1)).findAll(); + } + + @Test + void testUpdate() throws ExecutionException, InterruptedException { + when(subscriptionBoxRepository.update(subscriptionBox)).thenReturn(subscriptionBox); + + CompletableFuture future = subscriptionBoxService.update(subscriptionBox); + SubscriptionBox result = future.get(); + + assertEquals(subscriptionBox, result); + verify(subscriptionBoxRepository, times(1)).update(subscriptionBox); } @Test - public void testAddBox() { - when(subscriptionBoxRepository.addBox(box1)).thenReturn(box1); - SubscriptionBox result = subscriptionBoxService.addBox(box1); - assertEquals(box1, result); - verify(subscriptionBoxRepository).addBox(box1); + void testUpdateInvalidBox() { + assertThrows(IllegalArgumentException.class, () -> { + subscriptionBoxService.update(null).get(); + }); + verify(subscriptionBoxRepository, never()).update(any()); } @Test - public void testEditBox() { - SubscriptionBox updatedBox = new SubscriptionBox(); - updatedBox.setId("1"); - updatedBox.setName("Updated Basic Box"); - updatedBox.setType("Monthly"); - updatedBox.setPrice(150); - updatedBox.setItems(new ArrayList<>(box1.getItems())); - - when(subscriptionBoxRepository.editBox("1", updatedBox)).thenReturn(updatedBox); - SubscriptionBox result = subscriptionBoxService.editBox("1", updatedBox); - assertEquals("Updated Basic Box", result.getName()); - assertEquals(150, result.getPrice()); - verify(subscriptionBoxRepository).editBox("1", updatedBox); + void testDelete() throws ExecutionException, InterruptedException { + when(subscriptionBoxRepository.findById("1")).thenReturn(Optional.of(subscriptionBox)); + doNothing().when(subscriptionBoxRepository).delete("1"); + + CompletableFuture future = subscriptionBoxService.delete("1"); + future.get(); + + verify(subscriptionBoxRepository, times(1)).findById("1"); + verify(subscriptionBoxRepository, times(1)).delete("1"); } @Test - public void testDeleteBox() { - when(subscriptionBoxRepository.deleteBox("1")).thenReturn(box1); - SubscriptionBox result = subscriptionBoxService.deleteBox("1"); - assertEquals(box1, result); - verify(subscriptionBoxRepository).deleteBox("1"); + void testDeleteInvalidId() { + assertThrows(IllegalArgumentException.class, () -> { + subscriptionBoxService.delete("").get(); + }); + verify(subscriptionBoxRepository, never()).delete(anyString()); } @Test - public void testViewAll() { - List boxes = new ArrayList<>(); - boxes.add(box1); - boxes.add(box2); - when(subscriptionBoxRepository.viewAll()).thenReturn(boxes); - List result = subscriptionBoxService.viewAll(); - assertEquals(2, result.size()); - verify(subscriptionBoxRepository).viewAll(); + void testDeleteSubscriptionNotFound() { + when(subscriptionBoxRepository.findById("1")).thenReturn(Optional.empty()); + + assertThrows(IllegalArgumentException.class, () -> { + subscriptionBoxService.delete("1").get(); + }); + + verify(subscriptionBoxRepository, times(1)).findById("1"); + verify(subscriptionBoxRepository, never()).delete(anyString()); } @Test - public void testViewDetails() { - when(subscriptionBoxRepository.viewDetails("1")).thenReturn("Basic Box"); - String result = subscriptionBoxService.viewDetails("1"); - assertEquals("Basic Box", result); - verify(subscriptionBoxRepository).viewDetails("1"); + void testFindByPriceLessThan() throws ExecutionException, InterruptedException { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + when(subscriptionBoxRepository.findByPriceLessThan(150)).thenReturn(subscriptionBoxes); + + CompletableFuture> future = subscriptionBoxService.findByPriceLessThan(150); + List result = future.get(); + + assertEquals(1, result.size()); + assertEquals(subscriptionBox, result.get(0)); + verify(subscriptionBoxRepository, times(1)).findByPriceLessThan(150); } @Test - public void testFilterByPrice() { - List filteredBoxes = new ArrayList<>(); - filteredBoxes.add(box1); - when(subscriptionBoxRepository.filterByPrice(100)).thenReturn(filteredBoxes); - List result = subscriptionBoxService.filterByPrice(100); + void testFindByPriceGreaterThan() throws ExecutionException, InterruptedException { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + when(subscriptionBoxRepository.findByPriceGreaterThan(50)).thenReturn(subscriptionBoxes); + + CompletableFuture> future = subscriptionBoxService.findByPriceGreaterThan(50); + List result = future.get(); + assertEquals(1, result.size()); - assertTrue(result.contains(box1)); - verify(subscriptionBoxRepository).filterByPrice(100); + assertEquals(subscriptionBox, result.get(0)); + verify(subscriptionBoxRepository, times(1)).findByPriceGreaterThan(50); + } + + @Test + void testFindByPriceEquals() throws ExecutionException, InterruptedException { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + when(subscriptionBoxRepository.findByPriceEquals(100)).thenReturn(subscriptionBoxes); + + CompletableFuture> future = subscriptionBoxService.findByPriceEquals(100); + List result = future.get(); + + assertEquals(1, result.size()); + assertEquals(subscriptionBox, result.get(0)); + verify(subscriptionBoxRepository, times(1)).findByPriceEquals(100); + } + + @Test + void testFindByName() throws ExecutionException, InterruptedException { + List subscriptionBoxes = Arrays.asList(subscriptionBox); + when(subscriptionBoxRepository.findByName("Basic")).thenReturn(Optional.of(subscriptionBoxes)); + + CompletableFuture>> future = subscriptionBoxService.findByName("Basic"); + Optional> result = future.get(); + + assertTrue(result.isPresent()); + assertEquals(1, result.get().size()); + assertEquals(subscriptionBox, result.get().get(0)); + verify(subscriptionBoxRepository, times(1)).findByName("Basic"); + } + + @Test + void testFindDistinctNames() throws ExecutionException, InterruptedException { + List names = Arrays.asList("Basic", "Premium"); + when(subscriptionBoxRepository.findDistinctNames()).thenReturn(Optional.of(names)); + + CompletableFuture>> future = subscriptionBoxService.findDistinctNames(); + Optional> result = future.get(); + + assertTrue(result.isPresent()); + assertEquals(2, result.get().size()); + assertTrue(result.get().contains("Basic")); + assertTrue(result.get().contains("Premium")); + verify(subscriptionBoxRepository, times(1)).findDistinctNames(); } } From 227764c135d7439df51ec72dab0d25dba4329546 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Thu, 16 May 2024 13:13:26 +0700 Subject: [PATCH 28/28] remove builder pattern --- .github/workflows/ci.yml | 2 +- .../model/builder/ItemBuilder.java | 29 ----------- .../model/builder/SubscriptionBoxBuilder.java | 49 ------------------- 3 files changed, 1 insertion(+), 79 deletions(-) delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java delete mode 100644 src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 982f90f..9a6d8c7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,7 @@ on: push: branches: - master - + - staging jobs: build: name: Build diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java deleted file mode 100644 index 9c46b1d..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/ItemBuilder.java +++ /dev/null @@ -1,29 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.builder; - -import org.springframework.stereotype.Component; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; - -@Component -public class ItemBuilder { - private Item item; - - private ItemBuilder() { - item = new Item(); - } - - public Item setName(String name) { - item.setName(name); - return item; - } - - public Item setQuantity(int quantity) { - item.setQuantity(quantity); - return item; - } - - public Item setId(String id) { - item.setId(id); - return item; - } - -} diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java deleted file mode 100644 index 5555acb..0000000 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/builder/SubscriptionBoxBuilder.java +++ /dev/null @@ -1,49 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.builder; - -import java.util.ArrayList; - -import org.springframework.beans.factory.annotation.Autowired; - -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; -import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository; - - -public class SubscriptionBoxBuilder { - private SubscriptionBox subscriptionBox; - - @Autowired - private SubscriptionBoxRepository subscriptionBoxRepository; - - public SubscriptionBoxBuilder() { - this.subscriptionBox = new SubscriptionBox(); - } - - public SubscriptionBoxBuilder setName(String name) { - subscriptionBox.setName(name); - return this; - } - - public SubscriptionBoxBuilder setType(String type) { - if (type.equals("MONTHLY") | type.equals("QUARTERLY") | type.equals("SEMI-ANNUALLY")){ - return this; - - } - throw new IllegalArgumentException("Invalid Type."); - } - - public SubscriptionBoxBuilder setPrice(int price) { - subscriptionBox.setPrice(price); - return this; - } - - public SubscriptionBoxBuilder setItems() { - subscriptionBox.setItems(new ArrayList<>()); - return this; - } - - public SubscriptionBoxBuilder setId(String id) { - subscriptionBox.setId(id); - return this; - } - -} \ No newline at end of file