Skip to content

Commit

Permalink
Add a whole bunch of test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yijiano committed Nov 11, 2024
1 parent 1c021e5 commit 8a37ac8
Show file tree
Hide file tree
Showing 18 changed files with 1,161 additions and 559 deletions.
73 changes: 68 additions & 5 deletions src/test/java/seedu/pill/command/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import seedu.pill.exceptions.PillException;
import seedu.pill.exceptions.ExceptionMessages;
import seedu.pill.util.Item;
import seedu.pill.util.ItemMap;
import seedu.pill.util.Storage;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.time.LocalDate;

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

/**
* Unit tests for FindCommand
*/
public class FindCommandTest {
private ItemMap itemMap;
private Storage storage;
Expand All @@ -30,17 +37,73 @@ public void setUp() {
System.setOut(printStream);
}

// Existing test cases
@Test
public void listFindEmptyNotFoundPasses() {
FindCommand findCommand = new FindCommand("abc");
public void execute_emptyItemMap_throwsException() {
FindCommand findCommand = new FindCommand("panadol");

PillException exception = assertThrows(PillException.class, () -> {
findCommand.execute(itemMap, storage);
});

String expectedMessage = "Item not found...";
assertEquals(expectedMessage, exception.getMessage());
assertEquals("Item not found...", exception.getMessage());
}

@Test
public void execute_exactMatch_findsItem() throws PillException {
itemMap.addItemSilent(new Item("panadol", 5));
FindCommand findCommand = new FindCommand("panadol");

findCommand.execute(itemMap, storage);

String output = outputStream.toString().trim();
assertTrue(output.contains("panadol"));
}

@Test
public void execute_partialMatch_findsItem() throws PillException {
itemMap.addItemSilent(new Item("panadol extra", 5));
FindCommand findCommand = new FindCommand("panadol");

findCommand.execute(itemMap, storage);

String output = outputStream.toString().trim();
assertTrue(output.contains("panadol extra"));
}

@Test
public void execute_caseInsensitiveMatch_findsItem() throws PillException {
itemMap.addItemSilent(new Item("Panadol", 5));
FindCommand findCommand = new FindCommand("panadol");

findCommand.execute(itemMap, storage);

String output = outputStream.toString().trim();
assertTrue(output.contains("Panadol"));
}

@Test
public void execute_multipleMatches_findsAllItems() throws PillException {
itemMap.addItemSilent(new Item("panadol extra", 5));
itemMap.addItemSilent(new Item("panadol active", 3));
FindCommand findCommand = new FindCommand("panadol");

findCommand.execute(itemMap, storage);

String output = outputStream.toString().trim();
assertTrue(output.contains("panadol extra"));
assertTrue(output.contains("panadol active"));
}

@Test
public void execute_noMatch_throwsException() {
itemMap.addItemSilent(new Item("aspirin", 5));
FindCommand findCommand = new FindCommand("panadol");

PillException exception = assertThrows(PillException.class, () -> {
findCommand.execute(itemMap, storage);
});

assertEquals("Item not found...", exception.getMessage());
}

@Test
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/seedu/pill/command/FulfillCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package seedu.pill.command;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import seedu.pill.exceptions.PillException;
import seedu.pill.util.ItemMap;
import seedu.pill.util.Storage;
import seedu.pill.util.Order;
import seedu.pill.util.TransactionManager;
import seedu.pill.util.Item;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

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

/**
* Unit tests for FulfillCommand
*/
public class FulfillCommandTest {
private ItemMap itemMap;
private Storage storage;
private TransactionManager transactionManager;
private ByteArrayOutputStream outputStream;
private PrintStream printStream;
private final PrintStream standardOut = System.out;

@BeforeEach
public void setUp() {
itemMap = new ItemMap();
storage = new Storage();
transactionManager = new TransactionManager(itemMap);
outputStream = new ByteArrayOutputStream();
printStream = new PrintStream(outputStream);
System.setOut(printStream);
}

@Test
public void execute_insufficientStock_throwsException() {
// Create dispense order without sufficient stock
ItemMap orderItems = new ItemMap();
orderItems.addItemSilent(new Item("Paracetamol", 10));
Order order = transactionManager.createOrder(Order.OrderType.DISPENSE, orderItems, "Test failure");

FulfillCommand command = new FulfillCommand(order, transactionManager);

assertThrows(PillException.class, () -> command.execute(itemMap, storage));
}

@Test
public void isExit_returnsAlwaysFalse() {
ItemMap orderItems = new ItemMap();
Order order = transactionManager.createOrder(Order.OrderType.PURCHASE, orderItems, "Test");
FulfillCommand command = new FulfillCommand(order, transactionManager);
assertFalse(command.isExit());
}

@AfterEach
public void restoreSystemOut() {
System.setOut(standardOut);
}
}
76 changes: 76 additions & 0 deletions src/test/java/seedu/pill/command/OrderCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package seedu.pill.command;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import seedu.pill.exceptions.PillException;
import seedu.pill.util.ItemMap;
import seedu.pill.util.Storage;
import seedu.pill.util.TransactionManager;
import seedu.pill.util.Order;
import seedu.pill.util.Item;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

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

public class OrderCommandTest {
private ItemMap itemMap;
private Storage storage;
private TransactionManager transactionManager;
private ByteArrayOutputStream outputStream;
private PrintStream printStream;
private final PrintStream standardOut = System.out;

@BeforeEach
public void setUp() {
itemMap = new ItemMap();
storage = new Storage();
transactionManager = new TransactionManager(itemMap);
outputStream = new ByteArrayOutputStream();
printStream = new PrintStream(outputStream);
System.setOut(printStream);
}

@Test
public void execute_purchaseOrder_createsOrder() throws PillException {
ItemMap itemsToOrder = new ItemMap();
itemsToOrder.addItemSilent(new Item("Paracetamol", 10));

OrderCommand command = new OrderCommand(itemsToOrder, transactionManager,
Order.OrderType.PURCHASE);
command.execute(itemMap, storage);

assertEquals(1, transactionManager.getOrders().size());
Order createdOrder = transactionManager.getOrders().get(0);
assertEquals(Order.OrderType.PURCHASE, createdOrder.getType());
}

@Test
public void execute_dispenseOrder_createsOrder() throws PillException {
ItemMap itemsToOrder = new ItemMap();
itemsToOrder.addItemSilent(new Item("Paracetamol", 5));

OrderCommand command = new OrderCommand(itemsToOrder, transactionManager,
Order.OrderType.DISPENSE);
command.execute(itemMap, storage);

assertEquals(1, transactionManager.getOrders().size());
Order createdOrder = transactionManager.getOrders().get(0);
assertEquals(Order.OrderType.DISPENSE, createdOrder.getType());
}

@Test
public void isExit_returnsAlwaysFalse() {
OrderCommand command = new OrderCommand(new ItemMap(), transactionManager,
Order.OrderType.PURCHASE);
assertFalse(command.isExit());
}

@AfterEach
public void restoreSystemOut() {
System.setOut(standardOut);
}
}
70 changes: 70 additions & 0 deletions src/test/java/seedu/pill/command/RestockAllCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package seedu.pill.command;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import seedu.pill.exceptions.PillException;
import seedu.pill.util.ItemMap;
import seedu.pill.util.Storage;
import seedu.pill.util.Item;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.time.LocalDate;

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

public class RestockAllCommandTest {
private ItemMap itemMap;
private Storage storage;
private ByteArrayOutputStream outputStream;
private PrintStream printStream;
private final PrintStream standardOut = System.out;

@BeforeEach
public void setUp() {
itemMap = new ItemMap();
storage = new Storage();
outputStream = new ByteArrayOutputStream();
printStream = new PrintStream(outputStream);
System.setOut(printStream);
}

@Test
public void execute_noItemsBelowThreshold_printsNoChanges() throws PillException {
itemMap.addItemSilent(new Item("Paracetamol", 20, LocalDate.now(), 5.0, 7.0));
itemMap.addItemSilent(new Item("Aspirin", 15, LocalDate.now(), 4.0, 6.0));

RestockAllCommand command = new RestockAllCommand(10);
command.execute(itemMap, storage);

String output = outputStream.toString().trim();
assertTrue(output.contains("Total Restock Cost for all items below threshold 10: $0.00"));
}

@Test
public void execute_calculatesCorrectCosts() throws PillException {
itemMap.addItemSilent(new Item("Paracetamol", 5, LocalDate.now(), 5.0, 7.0));
// Need to restock 5 units at $5.0 each = $25.0

RestockAllCommand command = new RestockAllCommand(10);
command.execute(itemMap, storage);

String output = outputStream.toString().trim();
assertTrue(output.contains("Restock Cost: $25.00"));
assertTrue(output.contains("Total Restock Cost for all items below threshold 10: $25.00"));
}

@Test
public void isExit_returnsAlwaysFalse() {
RestockAllCommand command = new RestockAllCommand(10);
assertFalse(command.isExit());
}

@AfterEach
public void restoreSystemOut() {
System.setOut(standardOut);
}
}
58 changes: 58 additions & 0 deletions src/test/java/seedu/pill/command/RestockItemCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package seedu.pill.command;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import seedu.pill.exceptions.ExceptionMessages;
import seedu.pill.exceptions.PillException;
import seedu.pill.util.ItemMap;
import seedu.pill.util.Storage;
import seedu.pill.util.Item;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.time.LocalDate;
import java.util.Optional;

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

public class RestockItemCommandTest {
private ItemMap itemMap;
private Storage storage;
private ByteArrayOutputStream outputStream;
private PrintStream printStream;
private final PrintStream standardOut = System.out;

@BeforeEach
public void setUp() {
itemMap = new ItemMap();
storage = new Storage();
outputStream = new ByteArrayOutputStream();
printStream = new PrintStream(outputStream);
System.setOut(printStream);
}

@Test
public void execute_itemDoesNotExist_throwsException() {
RestockItemCommand command = new RestockItemCommand(
"NonexistentItem", Optional.empty(), 10);

assertThrows(PillException.class, () -> command.execute(itemMap, storage)
);
}

@Test
public void isExit_returnsAlwaysFalse() {
RestockItemCommand command = new RestockItemCommand(
"test", Optional.empty(), 10);
assertFalse(command.isExit());
}

@AfterEach
public void restoreSystemOut() {
System.setOut(standardOut);
}
}
Loading

0 comments on commit 8a37ac8

Please sign in to comment.