diff --git a/file.CSV b/file.CSV new file mode 100644 index 0000000000..cd89421949 --- /dev/null +++ b/file.CSV @@ -0,0 +1,7 @@ +type,fruit,quantity +b,apple,100 +b,banana,20 +s,apple,20 +p,apple,50 +r,banana,40 +p,banana,25 diff --git a/file1.CSV b/file1.CSV new file mode 100644 index 0000000000..79d20ab99b --- /dev/null +++ b/file1.CSV @@ -0,0 +1,3 @@ +fruit, quantity +apple,100 +banana,25 \ No newline at end of file diff --git a/newFIle.CSV b/newFIle.CSV new file mode 100644 index 0000000000..79d20ab99b --- /dev/null +++ b/newFIle.CSV @@ -0,0 +1,3 @@ +fruit, quantity +apple,100 +banana,25 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 651ebad0b7..586525cdeb 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,9 @@ check + + **/Main* + BUNDLE diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index e758b17650..0000000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,7 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { -} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..abfab6be04 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,60 @@ +package core.basesyntax; + +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.Operation; +import core.basesyntax.service.FruitService; +import core.basesyntax.service.amount.ActivityHandler; +import core.basesyntax.service.amount.BalanceActivityHandler; +import core.basesyntax.service.amount.PurchaseActivityHandler; +import core.basesyntax.service.amount.ReturnActivityHandler; +import core.basesyntax.service.amount.SupplyActivityHandler; +import core.basesyntax.service.impl.FruitServiceImpl; +import core.basesyntax.service.impl.ParserServiceImpl; +import core.basesyntax.service.impl.ReaderServiceImpl; +import core.basesyntax.service.impl.WriterServiceImpl; +import core.basesyntax.strategy.TypeActivityStrategy; +import core.basesyntax.strategy.TypeActivityStrategyImpl; +import java.util.HashMap; +import java.util.Map; + +/** + * Feel free to remove this class and create your own. + */ +public class Main { + private static final String FILE = "file.CSV"; + private static final String TO_FILE = "newFile.CSV"; + + public static void main(String[] args) { + Map + amountOfFruitsHandlersMap = new HashMap<>(); + + initializationMap(amountOfFruitsHandlersMap); + + TypeActivityStrategy typeActivityStrategy = + new TypeActivityStrategyImpl(amountOfFruitsHandlersMap); + + FruitService fruitService = + new FruitServiceImpl(new ReaderServiceImpl(new ParserServiceImpl()), + new WriterServiceImpl(), typeActivityStrategy); + fruitService.writeReport(FILE); + } + + private static void initializationMap(Map + amountOfFruitsHandlersMap) { + amountOfFruitsHandlersMap + .put(Operation.RETURN, + new ReturnActivityHandler(new FruitTransactionDaoImpl())); + + amountOfFruitsHandlersMap + .put(Operation.BALANCE, + new BalanceActivityHandler(new FruitTransactionDaoImpl())); + + amountOfFruitsHandlersMap + .put(Operation.PURCHASE, + new PurchaseActivityHandler(new FruitTransactionDaoImpl())); + + amountOfFruitsHandlersMap + .put(Operation.SUPPLY, + new SupplyActivityHandler(new FruitTransactionDaoImpl())); + } +} diff --git a/src/main/java/core/basesyntax/dao/FruitTransactionDao.java b/src/main/java/core/basesyntax/dao/FruitTransactionDao.java new file mode 100644 index 0000000000..01b82ecce5 --- /dev/null +++ b/src/main/java/core/basesyntax/dao/FruitTransactionDao.java @@ -0,0 +1,9 @@ +package core.basesyntax.dao; + +import core.basesyntax.model.FruitTransaction; + +public interface FruitTransactionDao { + FruitTransaction getFromStorage(FruitTransaction fruitTransaction); + + void addToStorage(FruitTransaction fruitTransaction); +} diff --git a/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java new file mode 100644 index 0000000000..772537432c --- /dev/null +++ b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java @@ -0,0 +1,24 @@ +package core.basesyntax.dao; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; + +public class FruitTransactionDaoImpl implements FruitTransactionDao { + @Override + public FruitTransaction getFromStorage(FruitTransaction fruitTransaction) { + for (FruitTransaction fruit : Storage.fruitTransactions) { + if (fruit.getFruit().equals(fruitTransaction.getFruit())) { + return fruit; + } + } + return null; + } + + @Override + public void addToStorage(FruitTransaction fruitTransaction) { + if (fruitTransaction == null) { + throw new IllegalArgumentException("Fruit cannot be null"); + } + Storage.fruitTransactions.add(fruitTransaction); + } +} diff --git a/src/main/java/core/basesyntax/db/Storage.java b/src/main/java/core/basesyntax/db/Storage.java new file mode 100644 index 0000000000..72bcabd559 --- /dev/null +++ b/src/main/java/core/basesyntax/db/Storage.java @@ -0,0 +1,9 @@ +package core.basesyntax.db; + +import core.basesyntax.model.FruitTransaction; +import java.util.ArrayList; +import java.util.List; + +public class Storage { + public static final List fruitTransactions = new ArrayList<>(); +} diff --git a/src/main/java/core/basesyntax/model/FruitTransaction.java b/src/main/java/core/basesyntax/model/FruitTransaction.java new file mode 100644 index 0000000000..18f49e3554 --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,79 @@ +package core.basesyntax.model; + +import java.util.Objects; + +public class FruitTransaction { + private String fruit; + private int quantity; + private Operation operation; + + private FruitTransaction(Operation operation, String fruit, int quantity) { + this.operation = operation; + this.fruit = fruit; + this.quantity = quantity; + } + + public static FruitTransaction of(Operation operation, String fruit, int quantity) { + if (quantity < 0) { + throw new IllegalArgumentException("Quantity cannot be less than 0"); + } + return new FruitTransaction(operation, fruit, quantity); + } + + public String getFruit() { + return fruit; + } + + public void subtract(int quantity) { + if (quantity > this.quantity) { + throw new IllegalArgumentException("Cannot sell fruits more than in stock"); + } + this.quantity -= quantity; + } + + public void add(int quantity) { + this.quantity += quantity; + } + + public void setFruit(String fruit) { + this.fruit = fruit; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + if (quantity > 0) { + this.quantity = quantity; + } else { + throw new IllegalArgumentException("Quantity cannot be less than 0"); + } + } + + public Operation getOperation() { + return operation; + } + + public void setOperation(Operation operation) { + this.operation = operation; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + FruitTransaction that = (FruitTransaction) o; + return quantity == that.quantity + && Objects.equals(fruit, that.fruit) && operation == that.operation; + } + + @Override + public int hashCode() { + return Objects.hash(fruit, quantity, operation); + } +} diff --git a/src/main/java/core/basesyntax/model/Operation.java b/src/main/java/core/basesyntax/model/Operation.java new file mode 100644 index 0000000000..3cc21f7e72 --- /dev/null +++ b/src/main/java/core/basesyntax/model/Operation.java @@ -0,0 +1,19 @@ +package core.basesyntax.model; + +public enum Operation { + BALANCE("b"), + SUPPLY("s"), + PURCHASE("p"), + RETURN("r"); + + private String code; + + Operation(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + +} diff --git a/src/main/java/core/basesyntax/service/FruitService.java b/src/main/java/core/basesyntax/service/FruitService.java new file mode 100644 index 0000000000..d73fed6a9f --- /dev/null +++ b/src/main/java/core/basesyntax/service/FruitService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface FruitService { + void writeReport(String file); +} diff --git a/src/main/java/core/basesyntax/service/ParserService.java b/src/main/java/core/basesyntax/service/ParserService.java new file mode 100644 index 0000000000..11dff2e59c --- /dev/null +++ b/src/main/java/core/basesyntax/service/ParserService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface ParserService { + List parseStringsIntoObjects(List strings); +} diff --git a/src/main/java/core/basesyntax/service/ReaderService.java b/src/main/java/core/basesyntax/service/ReaderService.java new file mode 100644 index 0000000000..da5c4a1988 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReaderService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface ReaderService { + List readFromFile(String fromFile); +} diff --git a/src/main/java/core/basesyntax/service/WriterService.java b/src/main/java/core/basesyntax/service/WriterService.java new file mode 100644 index 0000000000..9de965c930 --- /dev/null +++ b/src/main/java/core/basesyntax/service/WriterService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface WriterService { + void writeToFile(String toFile); +} diff --git a/src/main/java/core/basesyntax/service/amount/ActivityHandler.java b/src/main/java/core/basesyntax/service/amount/ActivityHandler.java new file mode 100644 index 0000000000..a009238de3 --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/ActivityHandler.java @@ -0,0 +1,7 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.model.FruitTransaction; + +public interface ActivityHandler { + void setAmountOfFruit(FruitTransaction fruitTransaction); +} diff --git a/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java new file mode 100644 index 0000000000..380be1526a --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.model.FruitTransaction; + +public class BalanceActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao; + + public BalanceActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } + + @Override + public void setAmountOfFruit(FruitTransaction fruitTransaction) { + fruitTransactionDao.addToStorage(fruitTransaction); + } +} diff --git a/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java new file mode 100644 index 0000000000..89e4c2abba --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java @@ -0,0 +1,19 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.model.FruitTransaction; + +public class PurchaseActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao; + + public PurchaseActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } + + @Override + public void setAmountOfFruit(FruitTransaction fruitTransaction) { + fruitTransactionDao.getFromStorage(fruitTransaction) + .subtract(fruitTransaction.getQuantity()); + + } +} diff --git a/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java b/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java new file mode 100644 index 0000000000..a940af8b01 --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.model.FruitTransaction; + +public class ReturnActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao; + + public ReturnActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } + + @Override + public void setAmountOfFruit(FruitTransaction fruitTransaction) { + fruitTransactionDao.getFromStorage(fruitTransaction).add(fruitTransaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java new file mode 100644 index 0000000000..eeec8e57cd --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.model.FruitTransaction; + +public class SupplyActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao; + + public SupplyActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } + + @Override + public void setAmountOfFruit(FruitTransaction fruitTransaction) { + fruitTransactionDao.getFromStorage(fruitTransaction).add(fruitTransaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java new file mode 100644 index 0000000000..f49f48a048 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java @@ -0,0 +1,36 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.FruitService; +import core.basesyntax.service.ReaderService; +import core.basesyntax.service.WriterService; +import core.basesyntax.strategy.TypeActivityStrategy; +import java.util.List; + +public class FruitServiceImpl implements FruitService { + private static final String COMA = ","; + private static final String TO_FILE = "newFile.CSV"; + + private ReaderService readerService; + + private WriterService writerService; + + private TypeActivityStrategy typeActivityStrategy; + + public FruitServiceImpl(ReaderService readerService, WriterService writerService, + TypeActivityStrategy typeActivityStrategy) { + this.readerService = readerService; + this.writerService = writerService; + this.typeActivityStrategy = typeActivityStrategy; + } + + @Override + public void writeReport(String file) { + List fruitTransactions = readerService.readFromFile(file); + fruitTransactions.stream() + .forEach(fruit -> typeActivityStrategy + .get(fruit.getOperation()).setAmountOfFruit(fruit)); + writerService.writeToFile(TO_FILE); + } + +} diff --git a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java new file mode 100644 index 0000000000..691a86b09d --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java @@ -0,0 +1,44 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.ParserService; +import java.util.List; +import java.util.stream.Collectors; + +public class ParserServiceImpl implements ParserService { + private static final String COMA = ","; + + @Override + public List parseStringsIntoObjects(List strings) { + if (strings == null) { + throw new IllegalArgumentException("Cannot parse null"); + } + return strings.stream().map(this::getFruitFromString).collect(Collectors.toList()); + } + + private FruitTransaction getFruitFromString(String string) { + String[] parts = string.split(COMA); + FruitTransaction fruitTransaction + = FruitTransaction.of(getOperationFromString(parts[0]), parts[1], + Integer.parseInt(parts[2])); + return fruitTransaction; + } + + private Operation getOperationFromString(String string) { + switch (string) { + case "b": { + return Operation.BALANCE; + } + case "s": { + return Operation.SUPPLY; + } + case "r": { + return Operation.RETURN; + } + default: { + return Operation.PURCHASE; + } + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java new file mode 100644 index 0000000000..9c63beaaf9 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java @@ -0,0 +1,32 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.ParserService; +import core.basesyntax.service.ReaderService; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class ReaderServiceImpl implements ReaderService { + private ParserService parserService; + + public ReaderServiceImpl(ParserService parserService) { + this.parserService = parserService; + } + + @Override + public List readFromFile(String fromFile) { + if (fromFile == null) { + throw new IllegalArgumentException("File cannot be null"); + } + List fruitTransactions; + try { + fruitTransactions = Files.readAllLines(Path.of(fromFile)); + } catch (IOException e) { + throw new RuntimeException("Can't read from file " + fromFile, e); + } + fruitTransactions.remove(0); + return parserService.parseStringsIntoObjects(fruitTransactions); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java new file mode 100644 index 0000000000..40d2037073 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java @@ -0,0 +1,33 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.service.WriterService; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.util.stream.Collectors; + +public class WriterServiceImpl implements WriterService { + private static final String HEADER = "fruit, quantity"; + + @Override + public void writeToFile(String toFile) { + if (toFile == null) { + throw new IllegalArgumentException("File cannot be null"); + } + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toFile))) { + bufferedWriter.write(HEADER); + bufferedWriter.newLine(); + bufferedWriter.write(getString()); + } catch (IOException ex) { + throw new RuntimeException("Can't write to file " + toFile, ex); + } + } + + private String getString() { + return Storage.fruitTransactions + .stream() + .map(fruit -> fruit.getFruit() + "," + fruit.getQuantity()) + .collect(Collectors.joining(System.lineSeparator())); + } +} diff --git a/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java b/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java new file mode 100644 index 0000000000..5ea593b79c --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java @@ -0,0 +1,8 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.Operation; +import core.basesyntax.service.amount.ActivityHandler; + +public interface TypeActivityStrategy { + ActivityHandler get(Operation operation); +} diff --git a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java new file mode 100644 index 0000000000..81a07f619a --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java @@ -0,0 +1,21 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.Operation; +import core.basesyntax.service.amount.ActivityHandler; +import java.util.Map; + +public class TypeActivityStrategyImpl implements TypeActivityStrategy { + private Map strategyMap; + + public TypeActivityStrategyImpl(Map strategyMap) { + this.strategyMap = strategyMap; + } + + @Override + public ActivityHandler get(Operation operation) { + if (operation == null) { + throw new IllegalArgumentException("Operation cannot be null"); + } + return strategyMap.get(operation); + } +} diff --git a/src/test/java/core/basesyntax/HelloWorldTest.java b/src/test/java/core/basesyntax/HelloWorldTest.java deleted file mode 100644 index c4a0df68d7..0000000000 --- a/src/test/java/core/basesyntax/HelloWorldTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorldTest { -} diff --git a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java new file mode 100644 index 0000000000..e4b8298c55 --- /dev/null +++ b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java @@ -0,0 +1,61 @@ +package core.basesyntax.dao; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class FruitTransactionDaoImplTest { + private static final String APPLE = "apple"; + private static FruitTransactionDao fruitTransactionDao; + + @BeforeAll + static void beforeAll() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + } + + @Test + void add_toStorage_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, APPLE, 75); + Storage.fruitTransactions.add(fruitTransaction); + assertEquals(fruitTransaction, fruitTransactionDao.getFromStorage(fruitTransaction)); + } + + @Test + void get_nullFromStorage_isNotOk() { + assertNull(fruitTransactionDao.getFromStorage(null)); + } + + @Test + void get_nonExistFruitFromStorage_isNotOk() { + assertNull(fruitTransactionDao + .getFromStorage(FruitTransaction.of(Operation.RETURN, APPLE, 20))); + } + + @Test + void add_toStorageByDao_isOk() { + fruitTransactionDao + .addToStorage(FruitTransaction.of(Operation.BALANCE, APPLE, 40)); + FruitTransaction actual = Storage.fruitTransactions.get(0); + FruitTransaction expected = FruitTransaction.of(Operation.BALANCE, APPLE, 40); + assertEquals(expected, actual); + } + + @Test + void add_nullToStorage_isNotOk() { + assertThrows(IllegalArgumentException.class, + () -> fruitTransactionDao.addToStorage(null)); + } + + @AfterEach + void afterEach() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/model/FruitTransactionTest.java b/src/test/java/core/basesyntax/model/FruitTransactionTest.java new file mode 100644 index 0000000000..8159f61d3d --- /dev/null +++ b/src/test/java/core/basesyntax/model/FruitTransactionTest.java @@ -0,0 +1,52 @@ +package core.basesyntax.model; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +class FruitTransactionTest { + private static final String BANANA = "banana"; + + @Test + void add_toQuantity_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, BANANA, 100); + fruitTransaction.add(55); + int expected = 155; + int actual = fruitTransaction.getQuantity(); + assertEquals(expected, actual); + } + + @Test + void subtract_correctQuantity_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.RETURN, BANANA, 50); + fruitTransaction.subtract(30); + int expected = 20; + int actual = fruitTransaction.getQuantity(); + assertEquals(expected, actual); + } + + @Test + void subtract_incorrectQuantity_isNotOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.RETURN, BANANA, 20); + assertThrows(IllegalArgumentException.class, + () -> fruitTransaction.subtract(30)); + } + + @Test + void creating_fruitWithNegativeQuantity_isNotOk() { + assertThrows(IllegalArgumentException.class, + () -> FruitTransaction.of(Operation.RETURN, BANANA, -5)); + } + + @Test + void set_fruitWithNegativeQuantity_isNotOk() { + FruitTransaction fruitTransaction = + FruitTransaction.of(Operation.RETURN, BANANA, 40); + assertThrows(IllegalArgumentException.class, + () -> fruitTransaction.setQuantity(-20)); + } +} diff --git a/src/test/java/core/basesyntax/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java new file mode 100644 index 0000000000..3ea4c14441 --- /dev/null +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -0,0 +1,81 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Operation; +import core.basesyntax.service.amount.ActivityHandler; +import core.basesyntax.service.amount.BalanceActivityHandler; +import core.basesyntax.service.amount.PurchaseActivityHandler; +import core.basesyntax.service.amount.ReturnActivityHandler; +import core.basesyntax.service.amount.SupplyActivityHandler; +import core.basesyntax.service.impl.FruitServiceImpl; +import core.basesyntax.service.impl.ParserServiceImpl; +import core.basesyntax.service.impl.ReaderServiceImpl; +import core.basesyntax.service.impl.WriterServiceImpl; +import core.basesyntax.strategy.TypeActivityStrategy; +import core.basesyntax.strategy.TypeActivityStrategyImpl; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class FruitServiceTest { + private static final String FROM_FILE = "file.CSV"; + private static final String TO_FILE = "newFile.CSV"; + private static Map activityHandlerMap; + private static FruitTransactionDao fruitTransactionDao; + + @BeforeAll + static void beforeAll() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + + activityHandlerMap = new HashMap<>(); + + activityHandlerMap.put(Operation.BALANCE, + new BalanceActivityHandler(fruitTransactionDao)); + + activityHandlerMap.put(Operation.SUPPLY, + new SupplyActivityHandler(fruitTransactionDao)); + + activityHandlerMap.put(Operation.PURCHASE, + new PurchaseActivityHandler(fruitTransactionDao)); + + activityHandlerMap.put(Operation.RETURN, + new ReturnActivityHandler(fruitTransactionDao)); + } + + @Test + void write_reportToCorrectFile_isOk() { + TypeActivityStrategy typeActivityStrategy + = new TypeActivityStrategyImpl(activityHandlerMap); + + ReaderService readerService + = new ReaderServiceImpl(new ParserServiceImpl()); + + FruitService fruitService = + new FruitServiceImpl(readerService, new WriterServiceImpl(), typeActivityStrategy); + + fruitService.writeReport(FROM_FILE); + List actual; + try { + actual = Files.readAllLines(Path.of(TO_FILE)); + } catch (IOException e) { + throw new RuntimeException("Can't read from file " + TO_FILE); + } + List expected = List.of("fruit, quantity", "apple,70", "banana,35"); + assertIterableEquals(expected, actual); + } + + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/ParserServiceTest.java b/src/test/java/core/basesyntax/service/ParserServiceTest.java new file mode 100644 index 0000000000..fc786b2121 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -0,0 +1,58 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertIterableEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.impl.ParserServiceImpl; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ParserServiceTest { + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + private static ParserService parserService; + + @BeforeAll + static void beforeAll() { + parserService = new ParserServiceImpl(); + } + + @Test + void parse_stringsIntoObjects_isOk() { + List strings = List.of("b,apple,100", "r,apple,50", + "b,banana,100", "p,banana,20", "s,apple,50"); + + List actual + = parserService.parseStringsIntoObjects(strings); + + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, APPLE, 100); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.RETURN, APPLE, 50); + + FruitTransaction fruitTransaction2 + = FruitTransaction.of(Operation.BALANCE, BANANA, 100); + + FruitTransaction fruitTransaction3 + = FruitTransaction.of(Operation.PURCHASE, BANANA, 20); + + FruitTransaction fruitTransaction4 + = FruitTransaction.of(Operation.SUPPLY, APPLE, 50); + + List expected + = List.of(fruitTransaction, fruitTransaction1, fruitTransaction2, + fruitTransaction3, fruitTransaction4); + + assertIterableEquals(expected, actual); + } + + @Test + void parse_nullInObjects_isNotOk() { + assertThrows(IllegalArgumentException.class, + () -> parserService.parseStringsIntoObjects(null)); + } +} diff --git a/src/test/java/core/basesyntax/service/ReaderServiceTest.java b/src/test/java/core/basesyntax/service/ReaderServiceTest.java new file mode 100644 index 0000000000..c5b64aa797 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -0,0 +1,73 @@ +package core.basesyntax.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.impl.ParserServiceImpl; +import core.basesyntax.service.impl.ReaderServiceImpl; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ReaderServiceTest { + private static final String EXIST_FILE = "file.CSV"; + private static final String NON_EXIST_FILE = "file4.CSV"; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + private static ReaderService readerService; + + @BeforeAll + static void beforeAll() { + readerService = new ReaderServiceImpl(new ParserServiceImpl()); + } + + @Test + void read_fromNull_isNotOk() { + Exception exception = assertThrows(IllegalArgumentException.class, + () -> readerService.readFromFile(null)); + String expected = "File cannot be null"; + String actual = exception.getMessage(); + assertEquals(expected, actual); + } + + @Test + void read_fromExistFile_isOk() { + List actual = readerService.readFromFile(EXIST_FILE); + + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, APPLE, 100); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.BALANCE, BANANA, 20); + + FruitTransaction fruitTransaction2 + = FruitTransaction.of(Operation.SUPPLY, APPLE, 20); + + FruitTransaction fruitTransaction3 + = FruitTransaction.of(Operation.PURCHASE, APPLE, 50); + + FruitTransaction fruitTransaction4 + = FruitTransaction.of(Operation.RETURN, BANANA, 40); + + FruitTransaction fruitTransaction5 + = FruitTransaction.of(Operation.PURCHASE, BANANA, 25); + + List expected + = List.of(fruitTransaction, fruitTransaction1, fruitTransaction2, + fruitTransaction3, fruitTransaction4, fruitTransaction5); + assertIterableEquals(expected, actual); + } + + @Test + void read_fromNonExistFile_isNotOk() { + Exception exception = assertThrows(RuntimeException.class, + () -> readerService.readFromFile(NON_EXIST_FILE)); + String actual = exception.getMessage(); + String expected = "Can't read"; + assertTrue(actual.contains(expected)); + } +} diff --git a/src/test/java/core/basesyntax/service/WriterServiceTest.java b/src/test/java/core/basesyntax/service/WriterServiceTest.java new file mode 100644 index 0000000000..f31b84783d --- /dev/null +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -0,0 +1,76 @@ +package core.basesyntax.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.impl.WriterServiceImpl; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class WriterServiceTest { + private static final String EXIST_FILE = "newFile.CSV"; + private static final String NON_EXIST_FILE = "file1.CSV"; + private static WriterService writerService; + + @BeforeAll + static void beforeAll() { + writerService = new WriterServiceImpl(); + } + + @Test + void write_toNull_isNotOk() { + Exception exception = assertThrows(IllegalArgumentException.class, + () -> writerService.writeToFile(null)); + String expected = "File cannot be null"; + String actual = exception.getMessage(); + assertEquals(expected, actual); + } + + @Test + void write_toNonExistFile_isOk() { + assertDoesNotThrow(() -> writerService.writeToFile(NON_EXIST_FILE)); + } + + @Test + void write_toExistFile_isOk() { + FruitTransactionDao fruitTransactionDao + = new FruitTransactionDaoImpl(); + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "apple", 100); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.BALANCE, "banana", 25); + + fruitTransactionDao.addToStorage(fruitTransaction); + + fruitTransactionDao.addToStorage(fruitTransaction1); + + writerService.writeToFile(EXIST_FILE); + + List expected = List.of("fruit, quantity", "apple,100", "banana,25"); + List actual; + try { + actual = Files.readAllLines(Path.of(EXIST_FILE)); + } catch (IOException e) { + throw new RuntimeException("Can't read from file " + EXIST_FILE); + } + assertIterableEquals(expected, actual); + } + + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java new file mode 100644 index 0000000000..a1f503fcce --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java @@ -0,0 +1,49 @@ +package core.basesyntax.service.amount; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class BalanceActivityHandlerTest { + private static FruitTransactionDao fruitTransactionDao; + private static BalanceActivityHandler balanceActivityHandler; + + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + balanceActivityHandler = new BalanceActivityHandler(fruitTransactionDao); + } + + @Test + void balance_activityHandlerForBanana_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "banana", 50); + balanceActivityHandler + .setAmountOfFruit(fruitTransaction); + FruitTransaction expected = fruitTransaction; + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + assertEquals(expected, actual); + } + + @Test + void balance_activityHandlerForApple_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "apple", 100); + balanceActivityHandler.setAmountOfFruit(fruitTransaction); + FruitTransaction expected = fruitTransaction; + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + assertEquals(expected, actual); + } + + @AfterEach + void afterEach() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java new file mode 100644 index 0000000000..98d43e3ea0 --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java @@ -0,0 +1,57 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class PurchaseActivityHandlerTest { + private static FruitTransactionDao fruitTransactionDao; + private static PurchaseActivityHandler purchaseActivityHandler; + + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + purchaseActivityHandler = new PurchaseActivityHandler(fruitTransactionDao); + } + + @Test + void purchase_activityHandlerForBanana_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "banana", 30); + fruitTransactionDao.addToStorage(fruitTransaction); + FruitTransaction fruitTransactionPurchase + = FruitTransaction.of(Operation.PURCHASE, "banana", 10); + purchaseActivityHandler + .setAmountOfFruit(fruitTransactionPurchase); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "banana", 20); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); + } + + @Test + void purchase_activityHandlerForApple_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "apple", 100); + fruitTransactionDao.addToStorage(fruitTransaction); + FruitTransaction fruitTransactionPurchase + = FruitTransaction.of(Operation.PURCHASE, "apple", 40); + purchaseActivityHandler + .setAmountOfFruit(fruitTransactionPurchase); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "apple", 60); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); + } + + @AfterEach + void afterEach() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java new file mode 100644 index 0000000000..335ff4c769 --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java @@ -0,0 +1,57 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ReturnActivityHandlerTest { + private static FruitTransactionDao fruitTransactionDao; + private static ReturnActivityHandler returnActivityHandler; + + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + returnActivityHandler = new ReturnActivityHandler(fruitTransactionDao); + } + + @Test + void return_activityHandlerForBanana_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "banana", 60); + fruitTransactionDao.addToStorage(fruitTransaction); + FruitTransaction fruitTransactionReturn + = FruitTransaction.of(Operation.RETURN, "banana", 30); + returnActivityHandler + .setAmountOfFruit(fruitTransactionReturn); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "banana", 90); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); + } + + @Test + void return_activityHandlerForApple_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "apple", 100); + fruitTransactionDao.addToStorage(fruitTransaction); + FruitTransaction fruitTransactionPurchase + = FruitTransaction.of(Operation.RETURN, "apple", 40); + returnActivityHandler + .setAmountOfFruit(fruitTransactionPurchase); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "apple", 140); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); + } + + @AfterEach + void afterEach() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java new file mode 100644 index 0000000000..5280e1ad06 --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java @@ -0,0 +1,57 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class SupplyActivityHandlerTest { + private static FruitTransactionDao fruitTransactionDao; + private static SupplyActivityHandler supplyActivityHandler; + + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + supplyActivityHandler = new SupplyActivityHandler(fruitTransactionDao); + } + + @Test + void supply_activityHandlerForBanana_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "banana", 65); + fruitTransactionDao.addToStorage(fruitTransaction); + FruitTransaction fruitTransactionSupply + = FruitTransaction.of(Operation.SUPPLY, "banana", 35); + supplyActivityHandler + .setAmountOfFruit(fruitTransactionSupply); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "banana", 100); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); + } + + @Test + void supply_activityHandlerForApple_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, "apple", 70); + fruitTransactionDao.addToStorage(fruitTransaction); + FruitTransaction fruitTransactionSupply + = FruitTransaction.of(Operation.SUPPLY, "apple", 40); + supplyActivityHandler + .setAmountOfFruit(fruitTransactionSupply); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "apple", 110); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); + } + + @AfterEach + void afterEach() { + Storage.fruitTransactions.clear(); + } +} diff --git a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java new file mode 100644 index 0000000000..462cdb94a9 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -0,0 +1,79 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.Operation; +import core.basesyntax.service.amount.ActivityHandler; +import core.basesyntax.service.amount.BalanceActivityHandler; +import core.basesyntax.service.amount.PurchaseActivityHandler; +import core.basesyntax.service.amount.ReturnActivityHandler; +import core.basesyntax.service.amount.SupplyActivityHandler; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class TypeActivityStrategyTest { + private static Map activityHandlerMap; + private static TypeActivityStrategy typeActivityStrategy; + private static FruitTransactionDao fruitTransactionDao; + + @BeforeAll + static void beforeAll() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + + activityHandlerMap = new HashMap<>(); + + activityHandlerMap.put(Operation.BALANCE, + new BalanceActivityHandler(fruitTransactionDao)); + + activityHandlerMap.put(Operation.SUPPLY, + new SupplyActivityHandler(fruitTransactionDao)); + + activityHandlerMap.put(Operation.PURCHASE, + new PurchaseActivityHandler(fruitTransactionDao)); + + activityHandlerMap.put(Operation.RETURN, + new ReturnActivityHandler(fruitTransactionDao)); + + typeActivityStrategy + = new TypeActivityStrategyImpl(activityHandlerMap); + } + + @Test + void get_handlerFromSupplyOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.SUPPLY); + ActivityHandler expected = new SupplyActivityHandler(fruitTransactionDao); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void get_handlerFromReturnOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.RETURN); + ActivityHandler expected = new ReturnActivityHandler(fruitTransactionDao); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void get_handlerFromPurchaseOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.PURCHASE); + ActivityHandler expected = new PurchaseActivityHandler(fruitTransactionDao); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void get_handlerFromBalanceOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.BALANCE); + ActivityHandler expected = new BalanceActivityHandler(fruitTransactionDao); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void get_handlerFromNull_isNotOk() { + assertThrows(IllegalArgumentException.class, + () -> typeActivityStrategy.get(null)); + } +}