Skip to content

Commit

Permalink
add order repository test
Browse files Browse the repository at this point in the history
  • Loading branch information
sitaamirasyarifah committed Mar 6, 2024
1 parent 0cd32fc commit a6e41b6
Showing 1 changed file with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package id.ac.ui.cs.advprog.eshop.repository;

import id.ac.ui.cs.advprog.eshop.model.Order;
import id.ac.ui.cs.advprog.eshop.enums.OrderStatus;
import id.ac.ui.cs.advprog.eshop.model.Product;
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.junit.jupiter.MockitoExtension;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;

public class OrderRepositoryTest {
OrderRepository orderRepository;
List<Order> orders;

@BeforeEach
void setUp() {
orderRepository = new OrderRepository();

List<Product> products = new ArrayList<>();
Product product1 = new Product();
product1.setProductId("eb558e9f-1c39-460e-8860-71af6af63bd6");
product1.setProductName("Sampo Cap Bambang");
product1.setProductQuantity(2);
products.add(product1);

orders = new ArrayList<>();
Order order1 = new Order("13652556-012a-407-b546-54eb1396d79b",
products, 1708560000L, "Safina Sudrajat");
orders.add(order1);
Order order2 = new Order("7f9e15bb-4b15-42f4-aebc-c3af385fb078",
products, 1708570000L, "Safira Sudrajat");
orders.add(order2);
Order order3 = new Order("e334ef40-9eff-4da8-9487-8ee697ecbfle",
products, 1708570080L, "Bambang Sudrajat");
orders.add(order3);
}

@Test
void testSaveCreate() {
Order order = orders.get(1);
Order result = orderRepository.save(order);

Order findResult = orderRepository.findById(orders.get(1).getId());
assertEquals(order.getId(), result.getId());
assertEquals(order.getId(), findResult.getId());
assertEquals(order.getOrderTime(), findResult.getOrderTime());
assertEquals(order.getAuthor(), findResult.getAuthor());
assertEquals(order.getStatus(), findResult.getStatus());
}

@Test
void testSaveUpdate() {
Order order = orders.get(1);
orderRepository.save(order);
Order newOrder = new Order(order.getId(), order.getProducts(), order.getOrderTime(),
order.getAuthor(), OrderStatus.SUCCESS.getValue());
Order result = orderRepository.save(newOrder);

Order findResult = orderRepository.findById(orders.get(1).getId());
assertEquals(order.getId(), result.getId());
assertEquals(order.getId(), findResult.getId());
assertEquals(order.getOrderTime(), findResult.getOrderTime());
assertEquals(order.getAuthor(), findResult.getAuthor());
assertEquals(OrderStatus.SUCCESS.getValue(), findResult.getStatus());
}

@Test
void testFindByIdIfIdFound() {
for (Order order : orders) {
orderRepository.save(order);
}

Order findResult = orderRepository.findById(orders.get(1).getId());
assertEquals(orders.get (1).getId(), findResult.getId());
assertEquals (orders.get(1).getOrderTime(), findResult.getOrderTime());
assertEquals(orders.get(1).getAuthor(), findResult.getAuthor());
assertEquals(orders.get (1).getStatus(), findResult.getStatus());
}

@Test
void testFindByIdIfIdNotFound() {
for (Order order : orders) {
orderRepository.save(order);
}

Order findResult = orderRepository.findById("zczc");
assertNull(findResult);
}

@Test
void testFindAllByAuthorIfAuthorCorrect() {
for (Order order : orders) {
orderRepository.save(order);
}

List<Order> orderList = orderRepository.findAllByAuthor(
orders.get(1).getAuthor());
assertEquals(2, orderList.size());
}

@Test
void testFindAllByAuthorIfAllLowercase() {
orderRepository.save(orders.get(1));

List<Order> orderList = orderRepository.findAllByAuthor(
orders.get(1).getAuthor().toLowerCase());
assertTrue(orderList.isEmpty());
}
}

0 comments on commit a6e41b6

Please sign in to comment.