From ff1d7b408a895c6d94521e25c1577c81c85501a5 Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Thu, 19 Oct 2023 11:25:24 +0300 Subject: [PATCH 1/8] test version --- pom.xml | 3 + src/main/java/core/basesyntax/HelloWorld.java | 7 -- src/main/java/core/basesyntax/Main.java | 53 +++++++++++ .../basesyntax/dao/FruitTransactionDao.java | 9 ++ .../dao/FruitTransactionDaoImpl.java | 21 ++++ src/main/java/core/basesyntax/db/Storage.java | 9 ++ .../basesyntax/model/FruitTransaction.java | 69 ++++++++++++++ .../core/basesyntax/service/FruitService.java | 10 ++ .../basesyntax/service/ParserService.java | 8 ++ .../basesyntax/service/ReaderService.java | 8 ++ .../basesyntax/service/WriterService.java | 5 + .../service/amount/ActivityHandler.java | 7 ++ .../amount/BalanceActivityHandler.java | 14 +++ .../amount/PurchaseActivityHandler.java | 15 +++ .../service/amount/ReturnActivityHandler.java | 14 +++ .../service/amount/SupplyActivityHandler.java | 14 +++ .../service/impl/FruitServiceImpl.java | 46 +++++++++ .../service/impl/ParserServiceImpl.java | 25 +++++ .../service/impl/ReaderServiceImpl.java | 24 +++++ .../service/impl/WriterServiceImpl.java | 30 ++++++ .../strategy/TypeActivityStrategy.java | 8 ++ .../strategy/TypeActivityStrategyImpl.java | 18 ++++ .../java/core/basesyntax/HelloWorldTest.java | 7 -- .../basesyntax/service/FruitServiceTest.java | 95 +++++++++++++++++++ .../basesyntax/service/ParserServiceTest.java | 11 +++ .../basesyntax/service/ReaderServiceTest.java | 7 ++ .../basesyntax/service/WriterServiceTest.java | 7 ++ .../strategy/TypeActivityStrategyTest.java | 7 ++ 28 files changed, 537 insertions(+), 14 deletions(-) delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/Main.java create mode 100644 src/main/java/core/basesyntax/dao/FruitTransactionDao.java create mode 100644 src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java create mode 100644 src/main/java/core/basesyntax/db/Storage.java create mode 100644 src/main/java/core/basesyntax/model/FruitTransaction.java create mode 100644 src/main/java/core/basesyntax/service/FruitService.java create mode 100644 src/main/java/core/basesyntax/service/ParserService.java create mode 100644 src/main/java/core/basesyntax/service/ReaderService.java create mode 100644 src/main/java/core/basesyntax/service/WriterService.java create mode 100644 src/main/java/core/basesyntax/service/amount/ActivityHandler.java create mode 100644 src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java create mode 100644 src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java create mode 100644 src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java create mode 100644 src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java create mode 100644 src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java create mode 100644 src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java create mode 100644 src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java create mode 100644 src/test/java/core/basesyntax/service/FruitServiceTest.java create mode 100644 src/test/java/core/basesyntax/service/ParserServiceTest.java create mode 100644 src/test/java/core/basesyntax/service/ReaderServiceTest.java create mode 100644 src/test/java/core/basesyntax/service/WriterServiceTest.java create mode 100644 src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java 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..656c29a859 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,53 @@ +package core.basesyntax; + +import core.basesyntax.model.FruitTransaction; +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.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<>(); + + amountOfFruitsHandlersMap + .put(FruitTransaction.Operation.RETURN, + new ReturnActivityHandler()); + + amountOfFruitsHandlersMap + .put(FruitTransaction.Operation.BALANCE, + new BalanceActivityHandler()); + + amountOfFruitsHandlersMap + .put(FruitTransaction.Operation.PURCHASE, + new PurchaseActivityHandler()); + + amountOfFruitsHandlersMap + .put(FruitTransaction.Operation.SUPPLY, + new SupplyActivityHandler()); + + TypeActivityStrategy typeActivityStrategy = + new TypeActivityStrategyImpl(amountOfFruitsHandlersMap); + + FruitService fruitService = + new FruitServiceImpl(new ReaderServiceImpl(), + new WriterServiceImpl(), typeActivityStrategy); + fruitService.writeReport(FILE, TO_FILE); + } +} 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..267f446e97 --- /dev/null +++ b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java @@ -0,0 +1,21 @@ +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) { + 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..f4fc74ded4 --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,69 @@ +package core.basesyntax.model; + +public class FruitTransaction { + private String fruit; + private int quantity; + private Operation operation; + + public String getFruit() { + return fruit; + } + + public void subtract(int quantity) { + 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 RuntimeException("Quantity cannot be less than 0"); + } + } + + public Operation getOperation() { + return operation; + } + + public void setOperation(String operation) { + this.operation = Operation.getOperationFromString(operation); + } + + public enum Operation { + BALANCE("b"), + SUPPLY("s"), + PURCHASE("p"), + RETURN("r"); + private String code; + + Operation(String code) { + this.code = code; + } + + public static Operation getOperationFromString(String code) { + for (Operation op : Operation.values()) { + if (code.equals(op)) { + return op; + } + } + return null; + } + + 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..bc18b68941 --- /dev/null +++ b/src/main/java/core/basesyntax/service/FruitService.java @@ -0,0 +1,10 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; + +import java.util.List; + +public interface FruitService { + void writeReport(String file, String toFile); + String getReport(List transactions); +} 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..c4796151e0 --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java @@ -0,0 +1,14 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.FruitTransaction; + +public class BalanceActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + + @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..0de03160d4 --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java @@ -0,0 +1,15 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.FruitTransaction; + +public class PurchaseActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + + @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..c800df8cb7 --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java @@ -0,0 +1,14 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.FruitTransaction; + +public class ReturnActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + + @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..ca5809ed29 --- /dev/null +++ b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java @@ -0,0 +1,14 @@ +package core.basesyntax.service.amount; + +import core.basesyntax.dao.FruitTransactionDao; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.FruitTransaction; + +public class SupplyActivityHandler implements ActivityHandler { + private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + + @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..b947e5d6b6 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java @@ -0,0 +1,46 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.db.Storage; +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; +import java.util.Map; +import java.util.stream.Collectors; + +public class FruitServiceImpl implements FruitService { + + 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, String toFile) { + List fruitTransactions = readerService.readFromFile(file); + fruitTransactions.stream() + .forEach(fruit -> typeActivityStrategy + .get(fruit.getOperation()).setAmountOfFruit(fruit)); + writerService.writeToFile(toFile); + } + + @Override + public String getReport(List fruitTransactions) { + fruitTransactions.stream() + .forEach(fruit -> typeActivityStrategy + .get(fruit.getOperation()).setAmountOfFruit(fruit)); + return Storage.fruitTransactions.stream() + .map(f -> f.getFruit() + "," + f.getQuantity()) + .collect(Collectors.joining(System.lineSeparator())); + } +} 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..8cd3c71082 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java @@ -0,0 +1,25 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +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) { + return strings.stream().map(this::getFruitFromString).collect(Collectors.toList()); + } + + private FruitTransaction getFruitFromString(String string) { + String [] parts = string.split(COMA); + FruitTransaction fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation(parts[0]); + fruitTransaction.setFruit(parts[1]); + fruitTransaction.setQuantity(Integer.parseInt(parts[2])); + fruitTransaction.setQuantity(Integer.parseInt(parts[2])); + return fruitTransaction; + } +} 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..58dd222318 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java @@ -0,0 +1,24 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +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 ParserServiceImpl parserService = new ParserServiceImpl(); + + @Override + public List readFromFile(String fromFile) { + List fruitTransactions; + try { + fruitTransactions = Files.readAllLines(Path.of(fromFile)); + } catch (IOException e) { + throw new RuntimeException("Can't read from file " + fromFile); + } + 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..5361c29c1e --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java @@ -0,0 +1,30 @@ +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) { + 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); + } + } + + 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..5595db3dfe --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java @@ -0,0 +1,8 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.amount.ActivityHandler; + +public interface TypeActivityStrategy { + ActivityHandler get(FruitTransaction.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..5410e042c6 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java @@ -0,0 +1,18 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; +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(FruitTransaction.Operation operation) { + 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/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java new file mode 100644 index 0000000000..f3b735d0af --- /dev/null +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -0,0 +1,95 @@ +package core.basesyntax.service; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.amount.ActivityHandler; +import core.basesyntax.service.amount.BalanceActivityHandler; +import core.basesyntax.service.amount.SupplyActivityHandler; +import core.basesyntax.service.impl.FruitServiceImpl; +import core.basesyntax.service.impl.ReaderServiceImpl; +import core.basesyntax.service.impl.WriterServiceImpl; +import core.basesyntax.strategy.TypeActivityStrategy; +import core.basesyntax.strategy.TypeActivityStrategyImpl; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class FruitServiceTest { + private static FruitService fruitService; + private static final String EXIST_FILE = "file.CSV"; + private static final String EXIST_FILE_2 = "file2.CSV"; + private static final String NON_EXIST_FILE = "newFile.CSV"; + private static Map activityHandlerMap; + private static TypeActivityStrategy typeActivityStrategy; + + @BeforeAll + static void beforeAll() { + activityHandlerMap = new HashMap<>(); + activityHandlerMap.put(FruitTransaction.Operation.BALANCE, new BalanceActivityHandler()); + activityHandlerMap.put(FruitTransaction.Operation.SUPPLY, new SupplyActivityHandler()); + typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); + fruitService = new FruitServiceImpl(new ReaderServiceImpl(), + new WriterServiceImpl(), typeActivityStrategy); + } + + @Test + void getReport_is0k() { + FruitTransaction fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation("b"); + fruitTransaction.setFruit("banana"); + fruitTransaction.setQuantity(100); + FruitTransaction fruitTransaction1 = new FruitTransaction(); + fruitTransaction1.setOperation("p"); + fruitTransaction1.setFruit("banana"); + fruitTransaction1.setQuantity(20); + List fruitTransactions = new ArrayList<>(); + fruitTransactions.add(fruitTransaction); + fruitTransactions.add(fruitTransaction1); + String expected = "banana,80"; + String actual = fruitService.getReport(fruitTransactions); + assertEquals(expected, actual); + } + + @Test + void getReportWithSupply_isOk() { + FruitTransaction fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation("b"); + fruitTransaction.setFruit("apple"); + fruitTransaction.setQuantity(100); + FruitTransaction fruitTransaction1 = new FruitTransaction(); + fruitTransaction1.setOperation("s"); + fruitTransaction1.setFruit("apple"); + fruitTransaction1.setQuantity(20); + Storage.fruitTransactions.add(fruitTransaction); + Storage.fruitTransactions.add(fruitTransaction1); + List fruitTransactions = new ArrayList<>(); + fruitTransactions.add(fruitTransaction); + fruitTransactions.add(fruitTransaction1); + String expected = "apple,120"; + String actual = fruitService.getReport(fruitTransactions); + assertEquals(expected, actual); + } + + @Test + void writeReportToNonExistFile_isNotOk() { + FruitTransaction fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation("b"); + fruitTransaction.setFruit("apple"); + fruitTransaction.setQuantity(80); + FruitTransaction fruitTransaction1 = new FruitTransaction(); + fruitTransaction1.setOperation("s"); + fruitTransaction1.setFruit("apple"); + fruitTransaction1.setQuantity(20); + Storage.fruitTransactions.add(fruitTransaction); + Storage.fruitTransactions.add(fruitTransaction1); + assertThrows(RuntimeException.class, () -> + fruitService.writeReport(EXIST_FILE, NON_EXIST_FILE)); + } +} \ No newline at end of file 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..9777cec990 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -0,0 +1,11 @@ +package core.basesyntax.service; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ParserServiceTest { + @Test + void getObjectFromString() { + } +} \ No newline at end of file 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..0939e5273a --- /dev/null +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -0,0 +1,7 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.*; + +class ReaderServiceTest { + +} \ No newline at end of file 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..79d700c5fa --- /dev/null +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -0,0 +1,7 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.*; + +class WriterServiceTest { + +} \ No newline at end of file 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..cc4b734d29 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -0,0 +1,7 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.*; + +class TypeActivityStrategyTest { + +} \ No newline at end of file From 14ea8f6b64f73b239a7ffe35ad350fff378b38f6 Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Thu, 19 Oct 2023 13:46:09 +0300 Subject: [PATCH 2/8] arised unexpected exception --- file.CSV | 2 + newFIle.CSV | 0 src/main/java/core/basesyntax/Main.java | 2 +- .../core/basesyntax/service/FruitService.java | 2 +- .../service/impl/FruitServiceImpl.java | 6 ++- .../basesyntax/service/FruitServiceTest.java | 45 ++----------------- 6 files changed, 11 insertions(+), 46 deletions(-) create mode 100644 file.CSV create mode 100644 newFIle.CSV diff --git a/file.CSV b/file.CSV new file mode 100644 index 0000000000..bcaec9780d --- /dev/null +++ b/file.CSV @@ -0,0 +1,2 @@ +b,banana,100 +s,banana,20 \ No newline at end of file diff --git a/newFIle.CSV b/newFIle.CSV new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 656c29a859..38252948e6 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -48,6 +48,6 @@ public static void main(String[] args) { FruitService fruitService = new FruitServiceImpl(new ReaderServiceImpl(), new WriterServiceImpl(), typeActivityStrategy); - fruitService.writeReport(FILE, TO_FILE); + fruitService.writeReport(FILE); } } diff --git a/src/main/java/core/basesyntax/service/FruitService.java b/src/main/java/core/basesyntax/service/FruitService.java index bc18b68941..1f0066cd88 100644 --- a/src/main/java/core/basesyntax/service/FruitService.java +++ b/src/main/java/core/basesyntax/service/FruitService.java @@ -5,6 +5,6 @@ import java.util.List; public interface FruitService { - void writeReport(String file, String toFile); + void writeReport(String file); String getReport(List transactions); } diff --git a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java index b947e5d6b6..ae6252167f 100644 --- a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java @@ -11,6 +11,8 @@ import java.util.stream.Collectors; public class FruitServiceImpl implements FruitService { + private static final String COMA = ","; + private static final String TO_FILE = "newFile.CSV"; private ReaderService readerService; @@ -26,12 +28,12 @@ public FruitServiceImpl(ReaderService readerService, WriterService writerService } @Override - public void writeReport(String file, String toFile) { + public void writeReport(String file) { List fruitTransactions = readerService.readFromFile(file); fruitTransactions.stream() .forEach(fruit -> typeActivityStrategy .get(fruit.getOperation()).setAmountOfFruit(fruit)); - writerService.writeToFile(toFile); + writerService.writeToFile(TO_FILE); } @Override diff --git a/src/test/java/core/basesyntax/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java index f3b735d0af..348298fdd3 100644 --- a/src/test/java/core/basesyntax/service/FruitServiceTest.java +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -41,55 +41,16 @@ static void beforeAll() { @Test void getReport_is0k() { - FruitTransaction fruitTransaction = new FruitTransaction(); - fruitTransaction.setOperation("b"); - fruitTransaction.setFruit("banana"); - fruitTransaction.setQuantity(100); - FruitTransaction fruitTransaction1 = new FruitTransaction(); - fruitTransaction1.setOperation("p"); - fruitTransaction1.setFruit("banana"); - fruitTransaction1.setQuantity(20); - List fruitTransactions = new ArrayList<>(); - fruitTransactions.add(fruitTransaction); - fruitTransactions.add(fruitTransaction1); - String expected = "banana,80"; - String actual = fruitService.getReport(fruitTransactions); - assertEquals(expected, actual); + } @Test void getReportWithSupply_isOk() { - FruitTransaction fruitTransaction = new FruitTransaction(); - fruitTransaction.setOperation("b"); - fruitTransaction.setFruit("apple"); - fruitTransaction.setQuantity(100); - FruitTransaction fruitTransaction1 = new FruitTransaction(); - fruitTransaction1.setOperation("s"); - fruitTransaction1.setFruit("apple"); - fruitTransaction1.setQuantity(20); - Storage.fruitTransactions.add(fruitTransaction); - Storage.fruitTransactions.add(fruitTransaction1); - List fruitTransactions = new ArrayList<>(); - fruitTransactions.add(fruitTransaction); - fruitTransactions.add(fruitTransaction1); - String expected = "apple,120"; - String actual = fruitService.getReport(fruitTransactions); - assertEquals(expected, actual); + } @Test void writeReportToNonExistFile_isNotOk() { - FruitTransaction fruitTransaction = new FruitTransaction(); - fruitTransaction.setOperation("b"); - fruitTransaction.setFruit("apple"); - fruitTransaction.setQuantity(80); - FruitTransaction fruitTransaction1 = new FruitTransaction(); - fruitTransaction1.setOperation("s"); - fruitTransaction1.setFruit("apple"); - fruitTransaction1.setQuantity(20); - Storage.fruitTransactions.add(fruitTransaction); - Storage.fruitTransactions.add(fruitTransaction1); - assertThrows(RuntimeException.class, () -> - fruitService.writeReport(EXIST_FILE, NON_EXIST_FILE)); + } } \ No newline at end of file From 217982b0175bd519efd6600a564cc84652a2f7ce Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Fri, 20 Oct 2023 13:37:06 +0300 Subject: [PATCH 3/8] test version --- file.CSV | 9 ++- newFIle.CSV | 3 + src/main/java/core/basesyntax/Main.java | 23 +++--- .../dao/FruitTransactionDaoImpl.java | 3 + .../basesyntax/model/FruitTransaction.java | 53 +++++++------- .../core/basesyntax/service/FruitService.java | 1 - .../amount/BalanceActivityHandler.java | 6 +- .../amount/PurchaseActivityHandler.java | 7 +- .../service/amount/ReturnActivityHandler.java | 6 +- .../service/amount/SupplyActivityHandler.java | 6 +- .../service/impl/FruitServiceImpl.java | 11 +-- .../service/impl/ParserServiceImpl.java | 32 +++++++-- .../service/impl/ReaderServiceImpl.java | 8 ++- .../service/impl/WriterServiceImpl.java | 3 +- .../strategy/TypeActivityStrategy.java | 3 +- .../strategy/TypeActivityStrategyImpl.java | 10 ++- .../basesyntax/service/FruitServiceTest.java | 72 +++++++++++++------ .../basesyntax/service/ParserServiceTest.java | 52 +++++++++++++- .../basesyntax/service/ReaderServiceTest.java | 61 ++++++++++++++++ .../basesyntax/service/WriterServiceTest.java | 65 +++++++++++++++++ .../strategy/TypeActivityStrategyTest.java | 67 +++++++++++++++++ 21 files changed, 415 insertions(+), 86 deletions(-) diff --git a/file.CSV b/file.CSV index bcaec9780d..cd89421949 100644 --- a/file.CSV +++ b/file.CSV @@ -1,2 +1,7 @@ -b,banana,100 -s,banana,20 \ No newline at end of file +type,fruit,quantity +b,apple,100 +b,banana,20 +s,apple,20 +p,apple,50 +r,banana,40 +p,banana,25 diff --git a/newFIle.CSV b/newFIle.CSV index e69de29bb2..79d20ab99b 100644 --- a/newFIle.CSV +++ b/newFIle.CSV @@ -0,0 +1,3 @@ +fruit, quantity +apple,100 +banana,25 \ No newline at end of file diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 38252948e6..f9e8274567 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -1,6 +1,8 @@ package core.basesyntax; +import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; import core.basesyntax.service.FruitService; import core.basesyntax.service.amount.ActivityHandler; import core.basesyntax.service.amount.BalanceActivityHandler; @@ -8,6 +10,7 @@ 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; @@ -23,30 +26,30 @@ public class Main { private static final String TO_FILE = "newFile.CSV"; public static void main(String[] args) { - Map + Map amountOfFruitsHandlersMap = new HashMap<>(); amountOfFruitsHandlersMap - .put(FruitTransaction.Operation.RETURN, - new ReturnActivityHandler()); + .put(Operation.RETURN, + new ReturnActivityHandler(new FruitTransactionDaoImpl())); amountOfFruitsHandlersMap - .put(FruitTransaction.Operation.BALANCE, - new BalanceActivityHandler()); + .put(Operation.BALANCE, + new BalanceActivityHandler(new FruitTransactionDaoImpl())); amountOfFruitsHandlersMap - .put(FruitTransaction.Operation.PURCHASE, - new PurchaseActivityHandler()); + .put(Operation.PURCHASE, + new PurchaseActivityHandler(new FruitTransactionDaoImpl())); amountOfFruitsHandlersMap - .put(FruitTransaction.Operation.SUPPLY, - new SupplyActivityHandler()); + .put(Operation.SUPPLY, + new SupplyActivityHandler(new FruitTransactionDaoImpl())); TypeActivityStrategy typeActivityStrategy = new TypeActivityStrategyImpl(amountOfFruitsHandlersMap); FruitService fruitService = - new FruitServiceImpl(new ReaderServiceImpl(), + new FruitServiceImpl(new ReaderServiceImpl(new ParserServiceImpl()), new WriterServiceImpl(), typeActivityStrategy); fruitService.writeReport(FILE); } diff --git a/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java index 267f446e97..4db361dc32 100644 --- a/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java @@ -16,6 +16,9 @@ public FruitTransaction getFromStorage(FruitTransaction fruitTransaction) { @Override public void addToStorage(FruitTransaction fruitTransaction) { + if (fruitTransaction == null) { + throw new RuntimeException("Fruit cannot be null"); + } Storage.fruitTransactions.add(fruitTransaction); } } diff --git a/src/main/java/core/basesyntax/model/FruitTransaction.java b/src/main/java/core/basesyntax/model/FruitTransaction.java index f4fc74ded4..5c3834d2df 100644 --- a/src/main/java/core/basesyntax/model/FruitTransaction.java +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -1,15 +1,31 @@ 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 RuntimeException("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 RuntimeException("Cannot sell fruits more than in stock"); + } this.quantity -= quantity; } @@ -37,33 +53,20 @@ public Operation getOperation() { return operation; } - public void setOperation(String operation) { - this.operation = Operation.getOperationFromString(operation); + public void setOperation(Operation operation) { + this.operation = operation; } - public enum Operation { - BALANCE("b"), - SUPPLY("s"), - PURCHASE("p"), - RETURN("r"); - private String code; - - Operation(String code) { - this.code = code; - } - - public static Operation getOperationFromString(String code) { - for (Operation op : Operation.values()) { - if (code.equals(op)) { - return op; - } - } - return null; - } - - public String getCode() { - return code; - } + @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/service/FruitService.java b/src/main/java/core/basesyntax/service/FruitService.java index 1f0066cd88..fcc59bad6e 100644 --- a/src/main/java/core/basesyntax/service/FruitService.java +++ b/src/main/java/core/basesyntax/service/FruitService.java @@ -6,5 +6,4 @@ public interface FruitService { void writeReport(String file); - String getReport(List transactions); } diff --git a/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java index c4796151e0..56461f4ff4 100644 --- a/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java @@ -5,7 +5,11 @@ import core.basesyntax.model.FruitTransaction; public class BalanceActivityHandler implements ActivityHandler { - private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + private final FruitTransactionDao fruitTransactionDao; + + public BalanceActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } @Override public void setAmountOfFruit(FruitTransaction fruitTransaction) { diff --git a/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java index 0de03160d4..56117ec482 100644 --- a/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java @@ -5,11 +5,16 @@ import core.basesyntax.model.FruitTransaction; public class PurchaseActivityHandler implements ActivityHandler { - private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + 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 index c800df8cb7..f7e1801a51 100644 --- a/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java @@ -5,7 +5,11 @@ import core.basesyntax.model.FruitTransaction; public class ReturnActivityHandler implements ActivityHandler { - private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + private final FruitTransactionDao fruitTransactionDao; + + public ReturnActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } @Override public void setAmountOfFruit(FruitTransaction fruitTransaction) { diff --git a/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java index ca5809ed29..f2bbecb2a1 100644 --- a/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java @@ -5,7 +5,11 @@ import core.basesyntax.model.FruitTransaction; public class SupplyActivityHandler implements ActivityHandler { - private final FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); + private final FruitTransactionDao fruitTransactionDao; + + public SupplyActivityHandler(FruitTransactionDao fruitTransactionDao) { + this.fruitTransactionDao = fruitTransactionDao; + } @Override public void setAmountOfFruit(FruitTransaction fruitTransaction) { diff --git a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java index ae6252167f..dd0d029d49 100644 --- a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java @@ -2,10 +2,12 @@ import core.basesyntax.db.Storage; import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; import core.basesyntax.service.FruitService; import core.basesyntax.service.ReaderService; import core.basesyntax.service.WriterService; import core.basesyntax.strategy.TypeActivityStrategy; + import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -36,13 +38,4 @@ public void writeReport(String file) { writerService.writeToFile(TO_FILE); } - @Override - public String getReport(List fruitTransactions) { - fruitTransactions.stream() - .forEach(fruit -> typeActivityStrategy - .get(fruit.getOperation()).setAmountOfFruit(fruit)); - return Storage.fruitTransactions.stream() - .map(f -> f.getFruit() + "," + f.getQuantity()) - .collect(Collectors.joining(System.lineSeparator())); - } } diff --git a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java index 8cd3c71082..71eec241db 100644 --- a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java @@ -1,7 +1,9 @@ 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; @@ -10,16 +12,34 @@ public class ParserServiceImpl implements ParserService { @Override public List parseStringsIntoObjects(List strings) { + if (strings == null) { + throw new RuntimeException("Cannot parse null"); + } return strings.stream().map(this::getFruitFromString).collect(Collectors.toList()); } private FruitTransaction getFruitFromString(String string) { - String [] parts = string.split(COMA); - FruitTransaction fruitTransaction = new FruitTransaction(); - fruitTransaction.setOperation(parts[0]); - fruitTransaction.setFruit(parts[1]); - fruitTransaction.setQuantity(Integer.parseInt(parts[2])); - fruitTransaction.setQuantity(Integer.parseInt(parts[2])); + 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 index 58dd222318..7232ceba3e 100644 --- a/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java @@ -1,14 +1,20 @@ 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 ParserServiceImpl parserService = new ParserServiceImpl(); + private ParserService parserService; + + public ReaderServiceImpl(ParserService parserService) { + this.parserService = parserService; + } @Override public List readFromFile(String fromFile) { diff --git a/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java index 5361c29c1e..ffa6ff86af 100644 --- a/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java @@ -2,6 +2,7 @@ import core.basesyntax.db.Storage; import core.basesyntax.service.WriterService; + import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; @@ -22,7 +23,7 @@ public void writeToFile(String toFile) { } private String getString() { - return Storage.fruitTransactions + 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 index 5595db3dfe..113e2edbef 100644 --- a/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java @@ -1,8 +1,9 @@ package core.basesyntax.strategy; import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; import core.basesyntax.service.amount.ActivityHandler; public interface TypeActivityStrategy { - ActivityHandler get(FruitTransaction.Operation operation); + ActivityHandler get(Operation operation); } diff --git a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java index 5410e042c6..28073c3e5d 100644 --- a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java @@ -1,18 +1,22 @@ package core.basesyntax.strategy; import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; import core.basesyntax.service.amount.ActivityHandler; import java.util.Map; public class TypeActivityStrategyImpl implements TypeActivityStrategy { - private Map strategyMap; + private Map strategyMap; - public TypeActivityStrategyImpl(Map strategyMap) { + public TypeActivityStrategyImpl(Map strategyMap) { this.strategyMap = strategyMap; } @Override - public ActivityHandler get(FruitTransaction.Operation operation) { + public ActivityHandler get(Operation operation) { + if (operation == null) { + throw new NullPointerException("Operation cannot be null"); + } return strategyMap.get(operation); } } diff --git a/src/test/java/core/basesyntax/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java index 348298fdd3..8e1d908184 100644 --- a/src/test/java/core/basesyntax/service/FruitServiceTest.java +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -1,19 +1,24 @@ package core.basesyntax.service; +import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.db.Storage; import core.basesyntax.model.FruitTransaction; -import core.basesyntax.service.amount.ActivityHandler; -import core.basesyntax.service.amount.BalanceActivityHandler; -import core.basesyntax.service.amount.SupplyActivityHandler; +import core.basesyntax.model.Operation; +import core.basesyntax.service.amount.*; 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 org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.IOException; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -22,35 +27,58 @@ import static org.junit.jupiter.api.Assertions.*; class FruitServiceTest { - private static FruitService fruitService; - private static final String EXIST_FILE = "file.CSV"; - private static final String EXIST_FILE_2 = "file2.CSV"; - private static final String NON_EXIST_FILE = "newFile.CSV"; - private static Map activityHandlerMap; - private static TypeActivityStrategy typeActivityStrategy; + private ReaderService readerService; + private FruitService fruitService; + private static final String FROM_FILE = "file.CSV"; + private static final String TO_FILE = "newFile.CSV"; + private static Map activityHandlerMap; + private TypeActivityStrategy typeActivityStrategy; + + @BeforeEach + void beforeEach() { + readerService = new ReaderServiceImpl(new ParserServiceImpl()); - @BeforeAll - static void beforeAll() { - activityHandlerMap = new HashMap<>(); - activityHandlerMap.put(FruitTransaction.Operation.BALANCE, new BalanceActivityHandler()); - activityHandlerMap.put(FruitTransaction.Operation.SUPPLY, new SupplyActivityHandler()); typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); - fruitService = new FruitServiceImpl(new ReaderServiceImpl(), + + fruitService = new FruitServiceImpl(readerService, new WriterServiceImpl(), typeActivityStrategy); } - @Test - void getReport_is0k() { + @BeforeAll + static void beforeAll() { + activityHandlerMap = new HashMap<>(); - } + activityHandlerMap.put(Operation.BALANCE, + new BalanceActivityHandler(new FruitTransactionDaoImpl())); - @Test - void getReportWithSupply_isOk() { + activityHandlerMap.put(Operation.SUPPLY, + new SupplyActivityHandler(new FruitTransactionDaoImpl())); + + activityHandlerMap.put(Operation.PURCHASE, + new PurchaseActivityHandler(new FruitTransactionDaoImpl())); + activityHandlerMap.put(Operation.RETURN, + new ReturnActivityHandler(new FruitTransactionDaoImpl())); } @Test - void writeReportToNonExistFile_isNotOk() { + void writeReport_isOk() { + 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 = new ArrayList<>(); + expected.add("fruit, quantity"); + expected.add("apple,70"); + expected.add("banana,35"); + assertIterableEquals(expected, actual); + } + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); } } \ No newline at end of file diff --git a/src/test/java/core/basesyntax/service/ParserServiceTest.java b/src/test/java/core/basesyntax/service/ParserServiceTest.java index 9777cec990..ea3916725e 100644 --- a/src/test/java/core/basesyntax/service/ParserServiceTest.java +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -1,11 +1,61 @@ package core.basesyntax.service; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.impl.ParserServiceImpl; +import org.junit.Before; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; class ParserServiceTest { + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + private ParserService parserService; + + @BeforeEach + void beforeEach() { + parserService = new ParserServiceImpl(); + } + + @Test + void parseStringsIntoObjects_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 getObjectFromString() { + void parseNullInObjects_isNotOk() { + assertThrows(RuntimeException.class, + () -> parserService.parseStringsIntoObjects(null)); } } \ No newline at end of file diff --git a/src/test/java/core/basesyntax/service/ReaderServiceTest.java b/src/test/java/core/basesyntax/service/ReaderServiceTest.java index 0939e5273a..05a5c56b6a 100644 --- a/src/test/java/core/basesyntax/service/ReaderServiceTest.java +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -1,7 +1,68 @@ package core.basesyntax.service; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.impl.ParserServiceImpl; +import core.basesyntax.service.impl.ReaderServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; class ReaderServiceTest { + private static final String EXIST_FILE = "file.CSV"; + private static final String NON_EXIST_FILE = "file1.CSV"; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + private ReaderService readerService; + + @BeforeEach + void beforeEach() { + readerService = new ReaderServiceImpl(new ParserServiceImpl()); + } + + @Test + void readFromNull_isNotOk() { + assertThrows(RuntimeException.class, + () -> readerService.readFromFile(null)); + } + + @Test + void readFromExistFile_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 readFromNonExistFile_isNotOk() { + assertThrows(RuntimeException.class, + () -> readerService.readFromFile(NON_EXIST_FILE)); + } } \ No newline at end of file diff --git a/src/test/java/core/basesyntax/service/WriterServiceTest.java b/src/test/java/core/basesyntax/service/WriterServiceTest.java index 79d700c5fa..b16c285a72 100644 --- a/src/test/java/core/basesyntax/service/WriterServiceTest.java +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -1,7 +1,72 @@ package core.basesyntax.service; +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 org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; class WriterServiceTest { + private WriterService writerService; + private static final String EXIST_FILE = "newFile.CSV"; + private static final String NON_EXIST_FILE = "file1.CSV"; + private FruitTransactionDao fruitTransactionDao; + + @BeforeEach + void beforeEach() { + writerService = new WriterServiceImpl(); + fruitTransactionDao = new FruitTransactionDaoImpl(); + } + + @Test + void writeToNull_isNotOk() { + assertThrows(RuntimeException.class, + () -> writerService.writeToFile(null)); + } + + @Test + void writeToNonExistFile_isOk() { + assertDoesNotThrow(() -> writerService.writeToFile(NON_EXIST_FILE)); + } + + @Test + void writeToExistFile_isOk() { + 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(e); + } + assertIterableEquals(expected, actual); + } + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } } \ No newline at end of file diff --git a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java index cc4b734d29..4078c949f4 100644 --- a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -1,7 +1,74 @@ package core.basesyntax.strategy; +import core.basesyntax.dao.FruitTransactionDaoImpl; +import core.basesyntax.model.Operation; +import core.basesyntax.service.amount.*; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + import static org.junit.jupiter.api.Assertions.*; class TypeActivityStrategyTest { + private TypeActivityStrategy typeActivityStrategy; + private static Map activityHandlerMap; + + @BeforeAll + static void beforeAll() { + activityHandlerMap = new HashMap<>(); + + activityHandlerMap.put(Operation.BALANCE, + new BalanceActivityHandler(new FruitTransactionDaoImpl())); + + activityHandlerMap.put(Operation.SUPPLY, + new SupplyActivityHandler(new FruitTransactionDaoImpl())); + + activityHandlerMap.put(Operation.PURCHASE, + new PurchaseActivityHandler(new FruitTransactionDaoImpl())); + + activityHandlerMap.put(Operation.RETURN, + new ReturnActivityHandler(new FruitTransactionDaoImpl())); + } + + @BeforeEach + void beforeEach() { + typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); + } + + @Test + void getHandlerFromSupplyOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.SUPPLY); + ActivityHandler expected = new SupplyActivityHandler(new FruitTransactionDaoImpl()); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void getHandlerFromReturnOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.RETURN); + ActivityHandler expected = new ReturnActivityHandler(new FruitTransactionDaoImpl()); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void getHandlerFromPurchaseOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.PURCHASE); + ActivityHandler expected = new PurchaseActivityHandler(new FruitTransactionDaoImpl()); + assertEquals(actual.getClass(), expected.getClass()); + } + + @Test + void getHandlerFromBalanceOperation_isOk() { + ActivityHandler actual = typeActivityStrategy.get(Operation.BALANCE); + ActivityHandler expected = new BalanceActivityHandler(new FruitTransactionDaoImpl()); + assertEquals(actual.getClass(), expected.getClass()); + } + @Test + void getHandlerFromNull_isNotOk() { + assertThrows(NullPointerException.class, + () -> typeActivityStrategy.get(null)); + } } \ No newline at end of file From 99643d5b636e00dbc6b1df17a6a746328008cd3a Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Fri, 20 Oct 2023 14:06:32 +0300 Subject: [PATCH 4/8] implemented the code --- file1.CSV | 1 + .../java/core/basesyntax/model/Operation.java | 19 ++++++ .../dao/FruitTransactionDaoImplTest.java | 58 ++++++++++++++++ .../model/FruitTransactionTest.java | 52 +++++++++++++++ .../amount/BalanceActivityHandlerTest.java | 56 ++++++++++++++++ .../amount/PurchaseActivityHandlerTest.java | 66 +++++++++++++++++++ .../amount/ReturnActivityHandlerTest.java | 65 ++++++++++++++++++ .../amount/SupplyActivityHandlerTest.java | 66 +++++++++++++++++++ 8 files changed, 383 insertions(+) create mode 100644 file1.CSV create mode 100644 src/main/java/core/basesyntax/model/Operation.java create mode 100644 src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java create mode 100644 src/test/java/core/basesyntax/model/FruitTransactionTest.java create mode 100644 src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java diff --git a/file1.CSV b/file1.CSV new file mode 100644 index 0000000000..a26bdccdd8 --- /dev/null +++ b/file1.CSV @@ -0,0 +1 @@ +fruit, quantity 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..40e8abf7d9 --- /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; + } + +} \ No newline at end of file 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..efe52dda7e --- /dev/null +++ b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java @@ -0,0 +1,58 @@ +package core.basesyntax.dao; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import org.junit.jupiter.api.*; + +import static org.junit.jupiter.api.Assertions.*; + +class FruitTransactionDaoImplTest { + private FruitTransactionDao fruitTransactionDao; + private static final String APPLE = "apple"; + + @BeforeEach + void beforeEach() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + } + + @Test + void getFromStorage_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.RETURN, APPLE, 50); + fruitTransactionDao + .addToStorage(fruitTransaction); + assertNotNull(fruitTransactionDao.getFromStorage(fruitTransaction)); + } + + @Test + void getNullFromStorage_isNotOk() { + assertNull(fruitTransactionDao.getFromStorage(null)); + } + + @Test + void getNonExistFruitFromStorage_isNotOk() { + assertNull(fruitTransactionDao + .getFromStorage(FruitTransaction.of(Operation.RETURN, APPLE, 20))); + } + + @Test + void addExistFileToStorage_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 addNullToStorage_isNotOk() { + assertThrows(RuntimeException.class, + () -> fruitTransactionDao.addToStorage(null)); + } + + @AfterEach + void afterEach() { + Storage.fruitTransactions.clear(); + } +} \ No newline at end of file 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..6e91d4141f --- /dev/null +++ b/src/test/java/core/basesyntax/model/FruitTransactionTest.java @@ -0,0 +1,52 @@ +package core.basesyntax.model; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FruitTransactionTest { + private static final String BANANA = "banana"; + @Test + void addToQuantity_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, BANANA, 100); + fruitTransaction.add(55); + int expected = 155; + int actual = fruitTransaction.getQuantity(); + assertEquals(expected, actual); + } + + @Test + void subtractCorrectQuantity_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.RETURN, BANANA, 50); + fruitTransaction.subtract(30); + int expected = 20; + int actual = fruitTransaction.getQuantity(); + assertEquals(expected, actual); + } + + @Test + void subtractIncorrectQuantity_isNotOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.RETURN, BANANA, 20); + assertThrows(RuntimeException.class, + () -> fruitTransaction.subtract(30)); + } + + @Test + void creatingFruitWithNegativeQuantity_isNotOk() { + assertThrows(RuntimeException.class, + () -> FruitTransaction.of(Operation.RETURN, BANANA, -5)); + } + + @Test + void setFruitWithNegativeQuantity_isNotOk() { + FruitTransaction fruitTransaction = + FruitTransaction.of(Operation.RETURN, BANANA, 40); + assertThrows(RuntimeException.class, + () -> fruitTransaction.setQuantity(-20)); + } +} \ No newline at end of file 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..509385dbe5 --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java @@ -0,0 +1,56 @@ +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.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class BalanceActivityHandlerTest { + private BalanceActivityHandler balanceActivityHandler; + private FruitTransactionDao fruitTransactionDao; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + + @BeforeEach + void beforeEach() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + balanceActivityHandler = new BalanceActivityHandler(fruitTransactionDao); + } + + + @Test + void balanceActivityHandler_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, BANANA, 50); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.BALANCE, APPLE, 100); + + balanceActivityHandler + .setAmountOfFruit(fruitTransaction); + + balanceActivityHandler + .setAmountOfFruit(fruitTransaction1); + + List expected = List.of(fruitTransaction, fruitTransaction1); + + List actual + = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); + + assertIterableEquals(expected, actual); + } + + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } +} \ No newline at end of file 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..db2b83179e --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java @@ -0,0 +1,66 @@ +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.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class PurchaseActivityHandlerTest { + private PurchaseActivityHandler purchaseActivityHandler; + private FruitTransactionDao fruitTransactionDao; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + + @BeforeEach + void beforeEach() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + purchaseActivityHandler = new PurchaseActivityHandler(fruitTransactionDao); + } + + @Test + void purchaseActivityHandler_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, APPLE, 100); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.BALANCE, BANANA, 50); + + fruitTransactionDao.addToStorage(fruitTransaction); + + fruitTransactionDao.addToStorage(fruitTransaction1); + + purchaseActivityHandler + .setAmountOfFruit(FruitTransaction.of(Operation.PURCHASE, BANANA, 10)); + + purchaseActivityHandler + .setAmountOfFruit(FruitTransaction.of(Operation.PURCHASE, APPLE, 40)); + + FruitTransaction fruitTransaction2 + = FruitTransaction.of(Operation.BALANCE, BANANA, 40); + + FruitTransaction fruitTransaction3 + = FruitTransaction.of(Operation.BALANCE, APPLE, 60); + + List expected + = List.of(fruitTransaction3, fruitTransaction2); + + List actual + = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); + + assertEquals(expected, actual); + } + + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } +} \ No newline at end of file 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..e1148ce4db --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java @@ -0,0 +1,65 @@ +package core.basesyntax.service.amount; + +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.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class ReturnActivityHandlerTest { + private ReturnActivityHandler returnActivityHandler; + private FruitTransactionDaoImpl fruitTransactionDao; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + + @BeforeEach + void beforeEach() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + returnActivityHandler = new ReturnActivityHandler(fruitTransactionDao); + } + + @Test + void returnActivityHandler_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, APPLE, 30); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.BALANCE, BANANA, 50); + + fruitTransactionDao.addToStorage(fruitTransaction); + + fruitTransactionDao.addToStorage(fruitTransaction1); + + returnActivityHandler + .setAmountOfFruit(FruitTransaction.of(Operation.RETURN, BANANA, 15)); + + returnActivityHandler + .setAmountOfFruit(FruitTransaction.of(Operation.RETURN, APPLE, 15)); + + FruitTransaction fruitTransaction2 + = FruitTransaction.of(Operation.BALANCE, APPLE, 45); + + FruitTransaction fruitTransaction3 + = FruitTransaction.of(Operation.BALANCE, BANANA, 65); + + List expected + = List.of(fruitTransaction2, fruitTransaction3); + + List actual + = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); + + assertEquals(expected, actual); + } + + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } +} \ No newline at end of file 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..4d437f3d8f --- /dev/null +++ b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java @@ -0,0 +1,66 @@ +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.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class SupplyActivityHandlerTest { + private SupplyActivityHandler supplyActivityHandler; + private FruitTransactionDao fruitTransactionDao; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + + @BeforeEach + void beforeEach() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + supplyActivityHandler = new SupplyActivityHandler(fruitTransactionDao); + } + + @Test + void supplyActivityHandler_isOk() { + FruitTransaction fruitTransaction + = FruitTransaction.of(Operation.BALANCE, APPLE, 80); + + FruitTransaction fruitTransaction1 + = FruitTransaction.of(Operation.BALANCE, BANANA, 55); + + fruitTransactionDao.addToStorage(fruitTransaction); + + fruitTransactionDao.addToStorage(fruitTransaction1); + + supplyActivityHandler + .setAmountOfFruit(FruitTransaction.of(Operation.SUPPLY, APPLE, 40)); + + supplyActivityHandler + .setAmountOfFruit(FruitTransaction.of(Operation.SUPPLY, BANANA, 20)); + + FruitTransaction fruitTransaction2 + = FruitTransaction.of(Operation.BALANCE, APPLE, 120); + + FruitTransaction fruitTransaction3 + = FruitTransaction.of(Operation.BALANCE, BANANA, 75); + + List expected + = List.of(fruitTransaction2, fruitTransaction3); + + List actual + = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); + + assertEquals(expected, actual); + } + + @AfterAll + static void afterAll() { + Storage.fruitTransactions.clear(); + } +} \ No newline at end of file From 07611a03a73398e946983a3b423a8190cad8eff7 Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Fri, 20 Oct 2023 15:30:41 +0300 Subject: [PATCH 5/8] fixed the mistake with mvn clean package --- src/main/java/core/basesyntax/Main.java | 1 - .../basesyntax/model/FruitTransaction.java | 13 +++++++-- .../java/core/basesyntax/model/Operation.java | 2 +- .../core/basesyntax/service/FruitService.java | 4 --- .../amount/BalanceActivityHandler.java | 1 - .../amount/PurchaseActivityHandler.java | 1 - .../service/amount/ReturnActivityHandler.java | 1 - .../service/amount/SupplyActivityHandler.java | 1 - .../service/impl/FruitServiceImpl.java | 5 ---- .../service/impl/ParserServiceImpl.java | 1 - .../service/impl/ReaderServiceImpl.java | 1 - .../service/impl/WriterServiceImpl.java | 1 - .../strategy/TypeActivityStrategy.java | 1 - .../strategy/TypeActivityStrategyImpl.java | 1 - .../dao/FruitTransactionDaoImplTest.java | 17 +++++++---- .../model/FruitTransactionTest.java | 14 +++++----- .../basesyntax/service/FruitServiceTest.java | 28 ++++++++++--------- .../basesyntax/service/ParserServiceTest.java | 21 ++++++-------- .../basesyntax/service/ReaderServiceTest.java | 18 ++++++------ .../basesyntax/service/WriterServiceTest.java | 18 ++++++------ .../amount/BalanceActivityHandlerTest.java | 19 ++++++------- .../amount/PurchaseActivityHandlerTest.java | 14 ++++------ .../amount/ReturnActivityHandlerTest.java | 14 ++++------ .../amount/SupplyActivityHandlerTest.java | 14 ++++------ .../strategy/TypeActivityStrategyTest.java | 20 +++++++------ 25 files changed, 108 insertions(+), 123 deletions(-) diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index f9e8274567..f18558b3b0 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -1,7 +1,6 @@ package core.basesyntax; import core.basesyntax.dao.FruitTransactionDaoImpl; -import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.service.FruitService; import core.basesyntax.service.amount.ActivityHandler; diff --git a/src/main/java/core/basesyntax/model/FruitTransaction.java b/src/main/java/core/basesyntax/model/FruitTransaction.java index 5c3834d2df..5c5c210104 100644 --- a/src/main/java/core/basesyntax/model/FruitTransaction.java +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -6,11 +6,13 @@ 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 RuntimeException("Quantity cannot be less than 0"); @@ -59,10 +61,15 @@ public void setOperation(Operation operation) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + 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; + return quantity == that.quantity + && Objects.equals(fruit, that.fruit) && operation == that.operation; } @Override diff --git a/src/main/java/core/basesyntax/model/Operation.java b/src/main/java/core/basesyntax/model/Operation.java index 40e8abf7d9..3cc21f7e72 100644 --- a/src/main/java/core/basesyntax/model/Operation.java +++ b/src/main/java/core/basesyntax/model/Operation.java @@ -16,4 +16,4 @@ public String getCode() { return code; } -} \ No newline at end of file +} diff --git a/src/main/java/core/basesyntax/service/FruitService.java b/src/main/java/core/basesyntax/service/FruitService.java index fcc59bad6e..d73fed6a9f 100644 --- a/src/main/java/core/basesyntax/service/FruitService.java +++ b/src/main/java/core/basesyntax/service/FruitService.java @@ -1,9 +1,5 @@ package core.basesyntax.service; -import core.basesyntax.model.FruitTransaction; - -import java.util.List; - public interface FruitService { void writeReport(String file); } diff --git a/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java index 56461f4ff4..380be1526a 100644 --- a/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/BalanceActivityHandler.java @@ -1,7 +1,6 @@ package core.basesyntax.service.amount; import core.basesyntax.dao.FruitTransactionDao; -import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.model.FruitTransaction; public class BalanceActivityHandler implements ActivityHandler { diff --git a/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java index 56117ec482..89e4c2abba 100644 --- a/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/PurchaseActivityHandler.java @@ -1,7 +1,6 @@ package core.basesyntax.service.amount; import core.basesyntax.dao.FruitTransactionDao; -import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.model.FruitTransaction; public class PurchaseActivityHandler implements ActivityHandler { diff --git a/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java b/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java index f7e1801a51..a940af8b01 100644 --- a/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/ReturnActivityHandler.java @@ -1,7 +1,6 @@ package core.basesyntax.service.amount; import core.basesyntax.dao.FruitTransactionDao; -import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.model.FruitTransaction; public class ReturnActivityHandler implements ActivityHandler { diff --git a/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java index f2bbecb2a1..eeec8e57cd 100644 --- a/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java +++ b/src/main/java/core/basesyntax/service/amount/SupplyActivityHandler.java @@ -1,7 +1,6 @@ package core.basesyntax.service.amount; import core.basesyntax.dao.FruitTransactionDao; -import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.model.FruitTransaction; public class SupplyActivityHandler implements ActivityHandler { diff --git a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java index dd0d029d49..f49f48a048 100644 --- a/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java @@ -1,16 +1,11 @@ package core.basesyntax.service.impl; -import core.basesyntax.db.Storage; import core.basesyntax.model.FruitTransaction; -import core.basesyntax.model.Operation; import core.basesyntax.service.FruitService; import core.basesyntax.service.ReaderService; import core.basesyntax.service.WriterService; import core.basesyntax.strategy.TypeActivityStrategy; - import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; public class FruitServiceImpl implements FruitService { private static final String COMA = ","; diff --git a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java index 71eec241db..9b03137cc2 100644 --- a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java @@ -3,7 +3,6 @@ import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.service.ParserService; - import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java index 7232ceba3e..8b4955a6be 100644 --- a/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java @@ -3,7 +3,6 @@ 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; diff --git a/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java index ffa6ff86af..01d200df9b 100644 --- a/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java @@ -2,7 +2,6 @@ import core.basesyntax.db.Storage; import core.basesyntax.service.WriterService; - import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; diff --git a/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java b/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java index 113e2edbef..5ea593b79c 100644 --- a/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategy.java @@ -1,6 +1,5 @@ package core.basesyntax.strategy; -import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.service.amount.ActivityHandler; diff --git a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java index 28073c3e5d..0a42d83dc4 100644 --- a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java @@ -1,6 +1,5 @@ package core.basesyntax.strategy; -import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.service.amount.ActivityHandler; import java.util.Map; diff --git a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java index efe52dda7e..2edde1421d 100644 --- a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java +++ b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java @@ -1,15 +1,20 @@ package core.basesyntax.dao; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +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.*; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class FruitTransactionDaoImplTest { - private FruitTransactionDao fruitTransactionDao; - private static final String APPLE = "apple"; + private static final String APPLE = "apple"; + private FruitTransactionDao fruitTransactionDao; @BeforeEach void beforeEach() { @@ -55,4 +60,4 @@ void addNullToStorage_isNotOk() { void afterEach() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/model/FruitTransactionTest.java b/src/test/java/core/basesyntax/model/FruitTransactionTest.java index 6e91d4141f..28215698b4 100644 --- a/src/test/java/core/basesyntax/model/FruitTransactionTest.java +++ b/src/test/java/core/basesyntax/model/FruitTransactionTest.java @@ -1,13 +1,13 @@ package core.basesyntax.model; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; class FruitTransactionTest { private static final String BANANA = "banana"; + @Test void addToQuantity_isOk() { FruitTransaction fruitTransaction @@ -38,8 +38,8 @@ void subtractIncorrectQuantity_isNotOk() { @Test void creatingFruitWithNegativeQuantity_isNotOk() { - assertThrows(RuntimeException.class, - () -> FruitTransaction.of(Operation.RETURN, BANANA, -5)); + assertThrows(RuntimeException.class, + () -> FruitTransaction.of(Operation.RETURN, BANANA, -5)); } @Test @@ -49,4 +49,4 @@ void setFruitWithNegativeQuantity_isNotOk() { assertThrows(RuntimeException.class, () -> fruitTransaction.setQuantity(-20)); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java index 8e1d908184..4cbcfe491f 100644 --- a/src/test/java/core/basesyntax/service/FruitServiceTest.java +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -1,37 +1,39 @@ package core.basesyntax.service; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.db.Storage; -import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; -import core.basesyntax.service.amount.*; +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 org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.io.*; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class FruitServiceTest { - private ReaderService readerService; - private FruitService fruitService; private static final String FROM_FILE = "file.CSV"; private static final String TO_FILE = "newFile.CSV"; private static Map activityHandlerMap; + private ReaderService readerService; + private FruitService fruitService; private TypeActivityStrategy typeActivityStrategy; @BeforeEach @@ -81,4 +83,4 @@ void writeReport_isOk() { static void afterAll() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/ParserServiceTest.java b/src/test/java/core/basesyntax/service/ParserServiceTest.java index ea3916725e..df977f3961 100644 --- a/src/test/java/core/basesyntax/service/ParserServiceTest.java +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -1,18 +1,15 @@ 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 org.junit.Before; -import org.junit.jupiter.api.BeforeAll; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class ParserServiceTest { private static final String BANANA = "banana"; private static final String APPLE = "apple"; @@ -25,8 +22,8 @@ void beforeEach() { @Test void parseStringsIntoObjects_isOk() { - List strings = List.of("b,apple,100", "r,apple,50" - , "b,banana,100", "p,banana,20", "s,apple,50"); + List strings = List.of("b,apple,100", "r,apple,50", + "b,banana,100", "p,banana,20", "s,apple,50"); List actual = parserService.parseStringsIntoObjects(strings); @@ -47,8 +44,8 @@ void parseStringsIntoObjects_isOk() { = FruitTransaction.of(Operation.SUPPLY, APPLE, 50); List expected - = List.of(fruitTransaction, fruitTransaction1, fruitTransaction2 - , fruitTransaction3, fruitTransaction4); + = List.of(fruitTransaction, fruitTransaction1, fruitTransaction2, + fruitTransaction3, fruitTransaction4); assertIterableEquals(expected, actual); } @@ -58,4 +55,4 @@ void parseNullInObjects_isNotOk() { assertThrows(RuntimeException.class, () -> parserService.parseStringsIntoObjects(null)); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/ReaderServiceTest.java b/src/test/java/core/basesyntax/service/ReaderServiceTest.java index 05a5c56b6a..b76b43b5e1 100644 --- a/src/test/java/core/basesyntax/service/ReaderServiceTest.java +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -1,21 +1,19 @@ 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 core.basesyntax.service.impl.ReaderServiceImpl; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class ReaderServiceTest { private static final String EXIST_FILE = "file.CSV"; - private static final String NON_EXIST_FILE = "file1.CSV"; + private static final String NON_EXIST_FILE = "file4.CSV"; private static final String BANANA = "banana"; private static final String APPLE = "apple"; private ReaderService readerService; @@ -55,8 +53,8 @@ void readFromExistFile_isOk() { = FruitTransaction.of(Operation.PURCHASE, BANANA, 25); List expected - = List.of(fruitTransaction, fruitTransaction1, fruitTransaction2 - , fruitTransaction3, fruitTransaction4, fruitTransaction5); + = List.of(fruitTransaction, fruitTransaction1, fruitTransaction2, + fruitTransaction3, fruitTransaction4, fruitTransaction5); assertIterableEquals(expected, actual); } @@ -65,4 +63,4 @@ void readFromNonExistFile_isNotOk() { assertThrows(RuntimeException.class, () -> readerService.readFromFile(NON_EXIST_FILE)); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/WriterServiceTest.java b/src/test/java/core/basesyntax/service/WriterServiceTest.java index b16c285a72..f0c284f1e0 100644 --- a/src/test/java/core/basesyntax/service/WriterServiceTest.java +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -1,27 +1,27 @@ package core.basesyntax.service; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + 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 org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class WriterServiceTest { - private WriterService writerService; private static final String EXIST_FILE = "newFile.CSV"; private static final String NON_EXIST_FILE = "file1.CSV"; + private WriterService writerService; private FruitTransactionDao fruitTransactionDao; @BeforeEach @@ -69,4 +69,4 @@ void writeToExistFile_isOk() { static void afterAll() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java index 509385dbe5..819c0752b5 100644 --- a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java @@ -1,24 +1,22 @@ package core.basesyntax.service.amount; +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 java.util.List; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class BalanceActivityHandlerTest { - private BalanceActivityHandler balanceActivityHandler; - private FruitTransactionDao fruitTransactionDao; - private static final String BANANA = "banana"; - private static final String APPLE = "apple"; + private static final String BANANA = "banana"; + private static final String APPLE = "apple"; + private BalanceActivityHandler balanceActivityHandler; + private FruitTransactionDao fruitTransactionDao; @BeforeEach void beforeEach() { @@ -26,7 +24,6 @@ void beforeEach() { balanceActivityHandler = new BalanceActivityHandler(fruitTransactionDao); } - @Test void balanceActivityHandler_isOk() { FruitTransaction fruitTransaction @@ -53,4 +50,4 @@ void balanceActivityHandler_isOk() { static void afterAll() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java index db2b83179e..e5a674c341 100644 --- a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java @@ -1,24 +1,22 @@ package core.basesyntax.service.amount; +import static org.junit.Assert.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 java.util.List; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class PurchaseActivityHandlerTest { - private PurchaseActivityHandler purchaseActivityHandler; - private FruitTransactionDao fruitTransactionDao; private static final String BANANA = "banana"; private static final String APPLE = "apple"; + private PurchaseActivityHandler purchaseActivityHandler; + private FruitTransactionDao fruitTransactionDao; @BeforeEach void beforeEach() { @@ -63,4 +61,4 @@ void purchaseActivityHandler_isOk() { static void afterAll() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java index e1148ce4db..f5abd4df9e 100644 --- a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java @@ -1,23 +1,21 @@ package core.basesyntax.service.amount; +import static org.junit.Assert.assertEquals; + import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.db.Storage; import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; +import java.util.List; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class ReturnActivityHandlerTest { - private ReturnActivityHandler returnActivityHandler; - private FruitTransactionDaoImpl fruitTransactionDao; private static final String BANANA = "banana"; private static final String APPLE = "apple"; + private ReturnActivityHandler returnActivityHandler; + private FruitTransactionDaoImpl fruitTransactionDao; @BeforeEach void beforeEach() { @@ -62,4 +60,4 @@ void returnActivityHandler_isOk() { static void afterAll() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java index 4d437f3d8f..7f45fea1f2 100644 --- a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java @@ -1,24 +1,22 @@ package core.basesyntax.service.amount; +import static org.junit.Assert.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 java.util.List; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class SupplyActivityHandlerTest { - private SupplyActivityHandler supplyActivityHandler; - private FruitTransactionDao fruitTransactionDao; private static final String BANANA = "banana"; private static final String APPLE = "apple"; + private SupplyActivityHandler supplyActivityHandler; + private FruitTransactionDao fruitTransactionDao; @BeforeEach void beforeEach() { @@ -63,4 +61,4 @@ void supplyActivityHandler_isOk() { static void afterAll() { Storage.fruitTransactions.clear(); } -} \ No newline at end of file +} diff --git a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java index 4078c949f4..b4da2b192d 100644 --- a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -1,20 +1,24 @@ package core.basesyntax.strategy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.model.Operation; -import core.basesyntax.service.amount.*; +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.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.*; - class TypeActivityStrategyTest { - private TypeActivityStrategy typeActivityStrategy; private static Map activityHandlerMap; + private TypeActivityStrategy typeActivityStrategy; @BeforeAll static void beforeAll() { @@ -71,4 +75,4 @@ void getHandlerFromNull_isNotOk() { assertThrows(NullPointerException.class, () -> typeActivityStrategy.get(null)); } -} \ No newline at end of file +} From 0acf792949c322f97558dd6365c043c15a14a82e Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Mon, 23 Oct 2023 11:59:32 +0300 Subject: [PATCH 6/8] improved the readability of the code --- src/main/java/core/basesyntax/Main.java | 21 +++++++++----- .../dao/FruitTransactionDaoImplTest.java | 8 ++--- .../basesyntax/service/FruitServiceTest.java | 29 +++++++------------ .../basesyntax/service/ParserServiceTest.java | 8 ++--- .../basesyntax/service/ReaderServiceTest.java | 9 +++--- .../basesyntax/service/WriterServiceTest.java | 12 ++++---- .../amount/BalanceActivityHandlerTest.java | 18 ++++++------ .../amount/PurchaseActivityHandlerTest.java | 11 ++++--- .../amount/ReturnActivityHandlerTest.java | 12 +++++--- .../amount/SupplyActivityHandlerTest.java | 11 ++++--- .../strategy/TypeActivityStrategyTest.java | 9 ++---- 11 files changed, 75 insertions(+), 73 deletions(-) diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index f18558b3b0..abfab6be04 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -28,6 +28,19 @@ 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())); @@ -43,13 +56,5 @@ public static void main(String[] args) { amountOfFruitsHandlersMap .put(Operation.SUPPLY, new SupplyActivityHandler(new FruitTransactionDaoImpl())); - - TypeActivityStrategy typeActivityStrategy = - new TypeActivityStrategyImpl(amountOfFruitsHandlersMap); - - FruitService fruitService = - new FruitServiceImpl(new ReaderServiceImpl(new ParserServiceImpl()), - new WriterServiceImpl(), typeActivityStrategy); - fruitService.writeReport(FILE); } } diff --git a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java index 2edde1421d..f0a1ebccbe 100644 --- a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java +++ b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java @@ -9,15 +9,15 @@ import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class FruitTransactionDaoImplTest { private static final String APPLE = "apple"; - private FruitTransactionDao fruitTransactionDao; + private static FruitTransactionDao fruitTransactionDao; - @BeforeEach - void beforeEach() { + @BeforeAll + static void beforeAll() { fruitTransactionDao = new FruitTransactionDaoImpl(); } diff --git a/src/test/java/core/basesyntax/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java index 4cbcfe491f..e4dd961816 100644 --- a/src/test/java/core/basesyntax/service/FruitServiceTest.java +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -19,32 +19,17 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; 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.BeforeEach; 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 ReaderService readerService; - private FruitService fruitService; - private TypeActivityStrategy typeActivityStrategy; - - @BeforeEach - void beforeEach() { - readerService = new ReaderServiceImpl(new ParserServiceImpl()); - - typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); - - fruitService = new FruitServiceImpl(readerService, - new WriterServiceImpl(), typeActivityStrategy); - } @BeforeAll static void beforeAll() { @@ -65,6 +50,15 @@ static void beforeAll() { @Test void writeReport_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 { @@ -72,10 +66,7 @@ void writeReport_isOk() { } catch (IOException e) { throw new RuntimeException("Can't read from file " + TO_FILE); } - List expected = new ArrayList<>(); - expected.add("fruit, quantity"); - expected.add("apple,70"); - expected.add("banana,35"); + List expected = List.of("fruit, quantity", "apple,70", "banana,35"); assertIterableEquals(expected, actual); } diff --git a/src/test/java/core/basesyntax/service/ParserServiceTest.java b/src/test/java/core/basesyntax/service/ParserServiceTest.java index df977f3961..ca844ca912 100644 --- a/src/test/java/core/basesyntax/service/ParserServiceTest.java +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -7,16 +7,16 @@ import core.basesyntax.model.Operation; import core.basesyntax.service.impl.ParserServiceImpl; import java.util.List; -import org.junit.jupiter.api.BeforeEach; +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 ParserService parserService; + private static ParserService parserService; - @BeforeEach - void beforeEach() { + @BeforeAll + static void beforeAll() { parserService = new ParserServiceImpl(); } diff --git a/src/test/java/core/basesyntax/service/ReaderServiceTest.java b/src/test/java/core/basesyntax/service/ReaderServiceTest.java index b76b43b5e1..462aa31588 100644 --- a/src/test/java/core/basesyntax/service/ReaderServiceTest.java +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -8,7 +8,7 @@ import core.basesyntax.service.impl.ParserServiceImpl; import core.basesyntax.service.impl.ReaderServiceImpl; import java.util.List; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class ReaderServiceTest { @@ -16,10 +16,10 @@ class ReaderServiceTest { private static final String NON_EXIST_FILE = "file4.CSV"; private static final String BANANA = "banana"; private static final String APPLE = "apple"; - private ReaderService readerService; + private static ReaderService readerService; - @BeforeEach - void beforeEach() { + @BeforeAll + static void beforeAll() { readerService = new ReaderServiceImpl(new ParserServiceImpl()); } @@ -31,7 +31,6 @@ void readFromNull_isNotOk() { @Test void readFromExistFile_isOk() { - List actual = readerService.readFromFile(EXIST_FILE); FruitTransaction fruitTransaction diff --git a/src/test/java/core/basesyntax/service/WriterServiceTest.java b/src/test/java/core/basesyntax/service/WriterServiceTest.java index f0c284f1e0..2ba1eedffa 100644 --- a/src/test/java/core/basesyntax/service/WriterServiceTest.java +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -15,19 +15,17 @@ import java.nio.file.Path; import java.util.List; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; +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 WriterService writerService; - private FruitTransactionDao fruitTransactionDao; + private static WriterService writerService; - @BeforeEach - void beforeEach() { + @BeforeAll + static void beforeAll() { writerService = new WriterServiceImpl(); - fruitTransactionDao = new FruitTransactionDaoImpl(); } @Test @@ -43,6 +41,8 @@ void writeToNonExistFile_isOk() { @Test void writeToExistFile_isOk() { + FruitTransactionDao fruitTransactionDao + = new FruitTransactionDaoImpl(); FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, "apple", 100); diff --git a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java index 819c0752b5..91b116ec7c 100644 --- a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java @@ -13,24 +13,24 @@ import org.junit.jupiter.api.Test; class BalanceActivityHandlerTest { - private static final String BANANA = "banana"; - private static final String APPLE = "apple"; - private BalanceActivityHandler balanceActivityHandler; - private FruitTransactionDao fruitTransactionDao; - @BeforeEach void beforeEach() { - fruitTransactionDao = new FruitTransactionDaoImpl(); - balanceActivityHandler = new BalanceActivityHandler(fruitTransactionDao); + } @Test void balanceActivityHandler_isOk() { + FruitTransactionDao fruitTransactionDao + = new FruitTransactionDaoImpl(); + + BalanceActivityHandler balanceActivityHandler + = new BalanceActivityHandler(fruitTransactionDao); + FruitTransaction fruitTransaction - = FruitTransaction.of(Operation.BALANCE, BANANA, 50); + = FruitTransaction.of(Operation.BALANCE, "banana", 50); FruitTransaction fruitTransaction1 - = FruitTransaction.of(Operation.BALANCE, APPLE, 100); + = FruitTransaction.of(Operation.BALANCE, "apple", 100); balanceActivityHandler .setAmountOfFruit(fruitTransaction); diff --git a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java index e5a674c341..118ec9d9a1 100644 --- a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java @@ -15,17 +15,20 @@ class PurchaseActivityHandlerTest { private static final String BANANA = "banana"; private static final String APPLE = "apple"; - private PurchaseActivityHandler purchaseActivityHandler; - private FruitTransactionDao fruitTransactionDao; @BeforeEach void beforeEach() { - fruitTransactionDao = new FruitTransactionDaoImpl(); - purchaseActivityHandler = new PurchaseActivityHandler(fruitTransactionDao); + } @Test void purchaseActivityHandler_isOk() { + FruitTransactionDao fruitTransactionDao + = new FruitTransactionDaoImpl(); + + PurchaseActivityHandler purchaseActivityHandler + = new PurchaseActivityHandler(fruitTransactionDao); + FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, APPLE, 100); diff --git a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java index f5abd4df9e..7da460be51 100644 --- a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; +import core.basesyntax.dao.FruitTransactionDao; import core.basesyntax.dao.FruitTransactionDaoImpl; import core.basesyntax.db.Storage; import core.basesyntax.model.FruitTransaction; @@ -14,17 +15,20 @@ class ReturnActivityHandlerTest { private static final String BANANA = "banana"; private static final String APPLE = "apple"; - private ReturnActivityHandler returnActivityHandler; - private FruitTransactionDaoImpl fruitTransactionDao; @BeforeEach void beforeEach() { - fruitTransactionDao = new FruitTransactionDaoImpl(); - returnActivityHandler = new ReturnActivityHandler(fruitTransactionDao); + } @Test void returnActivityHandler_isOk() { + FruitTransactionDao fruitTransactionDao + = new FruitTransactionDaoImpl(); + + ReturnActivityHandler returnActivityHandler + = new ReturnActivityHandler(fruitTransactionDao); + FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, APPLE, 30); diff --git a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java index 7f45fea1f2..6b0ba73e7a 100644 --- a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java @@ -15,17 +15,20 @@ class SupplyActivityHandlerTest { private static final String BANANA = "banana"; private static final String APPLE = "apple"; - private SupplyActivityHandler supplyActivityHandler; - private FruitTransactionDao fruitTransactionDao; @BeforeEach void beforeEach() { - fruitTransactionDao = new FruitTransactionDaoImpl(); - supplyActivityHandler = new SupplyActivityHandler(fruitTransactionDao); + } @Test void supplyActivityHandler_isOk() { + FruitTransactionDao fruitTransactionDao + = new FruitTransactionDaoImpl(); + + SupplyActivityHandler supplyActivityHandler + = new SupplyActivityHandler(fruitTransactionDao); + FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, APPLE, 80); diff --git a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java index b4da2b192d..30a491c86d 100644 --- a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -13,12 +13,11 @@ import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class TypeActivityStrategyTest { private static Map activityHandlerMap; - private TypeActivityStrategy typeActivityStrategy; + private static TypeActivityStrategy typeActivityStrategy; @BeforeAll static void beforeAll() { @@ -35,11 +34,9 @@ static void beforeAll() { activityHandlerMap.put(Operation.RETURN, new ReturnActivityHandler(new FruitTransactionDaoImpl())); - } - @BeforeEach - void beforeEach() { - typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); + typeActivityStrategy + = new TypeActivityStrategyImpl(activityHandlerMap); } @Test From 54766b5cd15c45e69bd8229f6ef66b886cfb265e Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Mon, 23 Oct 2023 14:10:00 +0300 Subject: [PATCH 7/8] improved the methods' names --- .../basesyntax/dao/FruitTransactionDaoImplTest.java | 10 +++++----- .../core/basesyntax/model/FruitTransactionTest.java | 10 +++++----- .../java/core/basesyntax/service/FruitServiceTest.java | 2 +- .../core/basesyntax/service/ParserServiceTest.java | 4 ++-- .../core/basesyntax/service/ReaderServiceTest.java | 6 +++--- .../core/basesyntax/service/WriterServiceTest.java | 6 +++--- .../service/amount/BalanceActivityHandlerTest.java | 2 +- .../service/amount/PurchaseActivityHandlerTest.java | 2 +- .../service/amount/ReturnActivityHandlerTest.java | 2 +- .../service/amount/SupplyActivityHandlerTest.java | 2 +- .../basesyntax/strategy/TypeActivityStrategyTest.java | 10 +++++----- 11 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java index f0a1ebccbe..d1bdc3a87c 100644 --- a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java +++ b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java @@ -22,7 +22,7 @@ static void beforeAll() { } @Test - void getFromStorage_isOk() { + void get_FromStorage_isOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, APPLE, 50); fruitTransactionDao @@ -31,18 +31,18 @@ void getFromStorage_isOk() { } @Test - void getNullFromStorage_isNotOk() { + void get_NullFromStorage_isNotOk() { assertNull(fruitTransactionDao.getFromStorage(null)); } @Test - void getNonExistFruitFromStorage_isNotOk() { + void get_NonExistFruitFromStorage_isNotOk() { assertNull(fruitTransactionDao .getFromStorage(FruitTransaction.of(Operation.RETURN, APPLE, 20))); } @Test - void addExistFileToStorage_isOk() { + void add_ExistFileToStorage_isOk() { fruitTransactionDao .addToStorage(FruitTransaction.of(Operation.BALANCE, APPLE, 40)); FruitTransaction actual = Storage.fruitTransactions.get(0); @@ -51,7 +51,7 @@ void addExistFileToStorage_isOk() { } @Test - void addNullToStorage_isNotOk() { + void add_NullToStorage_isNotOk() { assertThrows(RuntimeException.class, () -> fruitTransactionDao.addToStorage(null)); } diff --git a/src/test/java/core/basesyntax/model/FruitTransactionTest.java b/src/test/java/core/basesyntax/model/FruitTransactionTest.java index 28215698b4..268701030e 100644 --- a/src/test/java/core/basesyntax/model/FruitTransactionTest.java +++ b/src/test/java/core/basesyntax/model/FruitTransactionTest.java @@ -9,7 +9,7 @@ class FruitTransactionTest { private static final String BANANA = "banana"; @Test - void addToQuantity_isOk() { + void add_ToQuantity_isOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, BANANA, 100); fruitTransaction.add(55); @@ -19,7 +19,7 @@ void addToQuantity_isOk() { } @Test - void subtractCorrectQuantity_isOk() { + void subtract_CorrectQuantity_isOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, BANANA, 50); fruitTransaction.subtract(30); @@ -29,7 +29,7 @@ void subtractCorrectQuantity_isOk() { } @Test - void subtractIncorrectQuantity_isNotOk() { + void subtract_IncorrectQuantity_isNotOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, BANANA, 20); assertThrows(RuntimeException.class, @@ -37,13 +37,13 @@ void subtractIncorrectQuantity_isNotOk() { } @Test - void creatingFruitWithNegativeQuantity_isNotOk() { + void creating_FruitWithNegativeQuantity_isNotOk() { assertThrows(RuntimeException.class, () -> FruitTransaction.of(Operation.RETURN, BANANA, -5)); } @Test - void setFruitWithNegativeQuantity_isNotOk() { + void set_FruitWithNegativeQuantity_isNotOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, BANANA, 40); assertThrows(RuntimeException.class, diff --git a/src/test/java/core/basesyntax/service/FruitServiceTest.java b/src/test/java/core/basesyntax/service/FruitServiceTest.java index e4dd961816..8a12363763 100644 --- a/src/test/java/core/basesyntax/service/FruitServiceTest.java +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -49,7 +49,7 @@ static void beforeAll() { } @Test - void writeReport_isOk() { + void write_Report_isOk() { TypeActivityStrategy typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); diff --git a/src/test/java/core/basesyntax/service/ParserServiceTest.java b/src/test/java/core/basesyntax/service/ParserServiceTest.java index ca844ca912..a9b2df800b 100644 --- a/src/test/java/core/basesyntax/service/ParserServiceTest.java +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -21,7 +21,7 @@ static void beforeAll() { } @Test - void parseStringsIntoObjects_isOk() { + void parse_StringsIntoObjects_isOk() { List strings = List.of("b,apple,100", "r,apple,50", "b,banana,100", "p,banana,20", "s,apple,50"); @@ -51,7 +51,7 @@ void parseStringsIntoObjects_isOk() { } @Test - void parseNullInObjects_isNotOk() { + void parse_NullInObjects_isNotOk() { assertThrows(RuntimeException.class, () -> parserService.parseStringsIntoObjects(null)); } diff --git a/src/test/java/core/basesyntax/service/ReaderServiceTest.java b/src/test/java/core/basesyntax/service/ReaderServiceTest.java index 462aa31588..8f2b21aecf 100644 --- a/src/test/java/core/basesyntax/service/ReaderServiceTest.java +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -24,13 +24,13 @@ static void beforeAll() { } @Test - void readFromNull_isNotOk() { + void read_FromNull_isNotOk() { assertThrows(RuntimeException.class, () -> readerService.readFromFile(null)); } @Test - void readFromExistFile_isOk() { + void read_FromExistFile_isOk() { List actual = readerService.readFromFile(EXIST_FILE); FruitTransaction fruitTransaction @@ -58,7 +58,7 @@ void readFromExistFile_isOk() { } @Test - void readFromNonExistFile_isNotOk() { + void read_FromNonExistFile_isNotOk() { assertThrows(RuntimeException.class, () -> readerService.readFromFile(NON_EXIST_FILE)); } diff --git a/src/test/java/core/basesyntax/service/WriterServiceTest.java b/src/test/java/core/basesyntax/service/WriterServiceTest.java index 2ba1eedffa..93931addcc 100644 --- a/src/test/java/core/basesyntax/service/WriterServiceTest.java +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -29,18 +29,18 @@ static void beforeAll() { } @Test - void writeToNull_isNotOk() { + void write_ToNull_isNotOk() { assertThrows(RuntimeException.class, () -> writerService.writeToFile(null)); } @Test - void writeToNonExistFile_isOk() { + void write_ToNonExistFile_isOk() { assertDoesNotThrow(() -> writerService.writeToFile(NON_EXIST_FILE)); } @Test - void writeToExistFile_isOk() { + void write_ToExistFile_isOk() { FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); FruitTransaction fruitTransaction diff --git a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java index 91b116ec7c..37432e624c 100644 --- a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java @@ -19,7 +19,7 @@ void beforeEach() { } @Test - void balanceActivityHandler_isOk() { + void balance_ActivityHandler_isOk() { FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); diff --git a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java index 118ec9d9a1..810b2011e5 100644 --- a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java @@ -22,7 +22,7 @@ void beforeEach() { } @Test - void purchaseActivityHandler_isOk() { + void purchase_ActivityHandler_isOk() { FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); diff --git a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java index 7da460be51..7add41e8fb 100644 --- a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java @@ -22,7 +22,7 @@ void beforeEach() { } @Test - void returnActivityHandler_isOk() { + void return_ActivityHandler_isOk() { FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); diff --git a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java index 6b0ba73e7a..e6a09748fc 100644 --- a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java @@ -22,7 +22,7 @@ void beforeEach() { } @Test - void supplyActivityHandler_isOk() { + void supply_ActivityHandler_isOk() { FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); diff --git a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java index 30a491c86d..860490a37f 100644 --- a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -40,35 +40,35 @@ static void beforeAll() { } @Test - void getHandlerFromSupplyOperation_isOk() { + void get_HandlerFromSupplyOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.SUPPLY); ActivityHandler expected = new SupplyActivityHandler(new FruitTransactionDaoImpl()); assertEquals(actual.getClass(), expected.getClass()); } @Test - void getHandlerFromReturnOperation_isOk() { + void get_HandlerFromReturnOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.RETURN); ActivityHandler expected = new ReturnActivityHandler(new FruitTransactionDaoImpl()); assertEquals(actual.getClass(), expected.getClass()); } @Test - void getHandlerFromPurchaseOperation_isOk() { + void get_HandlerFromPurchaseOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.PURCHASE); ActivityHandler expected = new PurchaseActivityHandler(new FruitTransactionDaoImpl()); assertEquals(actual.getClass(), expected.getClass()); } @Test - void getHandlerFromBalanceOperation_isOk() { + void get_HandlerFromBalanceOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.BALANCE); ActivityHandler expected = new BalanceActivityHandler(new FruitTransactionDaoImpl()); assertEquals(actual.getClass(), expected.getClass()); } @Test - void getHandlerFromNull_isNotOk() { + void get_HandlerFromNull_isNotOk() { assertThrows(NullPointerException.class, () -> typeActivityStrategy.get(null)); } From e9f0e98473ccd4e6a858e836c91f87f0e102a981 Mon Sep 17 00:00:00 2001 From: Rostyslav Kuzmych Date: Thu, 26 Oct 2023 13:27:41 +0300 Subject: [PATCH 8/8] fixed a few mistakes --- file1.CSV | 2 + .../dao/FruitTransactionDaoImpl.java | 2 +- .../basesyntax/model/FruitTransaction.java | 6 +- .../service/impl/ParserServiceImpl.java | 2 +- .../service/impl/ReaderServiceImpl.java | 5 +- .../service/impl/WriterServiceImpl.java | 5 +- .../strategy/TypeActivityStrategyImpl.java | 2 +- .../dao/FruitTransactionDaoImplTest.java | 20 +++-- .../model/FruitTransactionTest.java | 16 ++-- .../basesyntax/service/FruitServiceTest.java | 14 ++-- .../basesyntax/service/ParserServiceTest.java | 6 +- .../basesyntax/service/ReaderServiceTest.java | 20 +++-- .../basesyntax/service/WriterServiceTest.java | 16 ++-- .../amount/BalanceActivityHandlerTest.java | 52 ++++++------- .../amount/PurchaseActivityHandlerTest.java | 76 ++++++++----------- .../amount/ReturnActivityHandlerTest.java | 76 ++++++++----------- .../amount/SupplyActivityHandlerTest.java | 76 ++++++++----------- .../strategy/TypeActivityStrategyTest.java | 32 ++++---- 18 files changed, 210 insertions(+), 218 deletions(-) diff --git a/file1.CSV b/file1.CSV index a26bdccdd8..79d20ab99b 100644 --- a/file1.CSV +++ b/file1.CSV @@ -1 +1,3 @@ fruit, quantity +apple,100 +banana,25 \ No newline at end of file diff --git a/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java index 4db361dc32..772537432c 100644 --- a/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java @@ -17,7 +17,7 @@ public FruitTransaction getFromStorage(FruitTransaction fruitTransaction) { @Override public void addToStorage(FruitTransaction fruitTransaction) { if (fruitTransaction == null) { - throw new RuntimeException("Fruit cannot be null"); + throw new IllegalArgumentException("Fruit cannot be null"); } Storage.fruitTransactions.add(fruitTransaction); } diff --git a/src/main/java/core/basesyntax/model/FruitTransaction.java b/src/main/java/core/basesyntax/model/FruitTransaction.java index 5c5c210104..18f49e3554 100644 --- a/src/main/java/core/basesyntax/model/FruitTransaction.java +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -15,7 +15,7 @@ private FruitTransaction(Operation operation, String fruit, int quantity) { public static FruitTransaction of(Operation operation, String fruit, int quantity) { if (quantity < 0) { - throw new RuntimeException("Quantity cannot be less than 0"); + throw new IllegalArgumentException("Quantity cannot be less than 0"); } return new FruitTransaction(operation, fruit, quantity); } @@ -26,7 +26,7 @@ public String getFruit() { public void subtract(int quantity) { if (quantity > this.quantity) { - throw new RuntimeException("Cannot sell fruits more than in stock"); + throw new IllegalArgumentException("Cannot sell fruits more than in stock"); } this.quantity -= quantity; } @@ -47,7 +47,7 @@ public void setQuantity(int quantity) { if (quantity > 0) { this.quantity = quantity; } else { - throw new RuntimeException("Quantity cannot be less than 0"); + throw new IllegalArgumentException("Quantity cannot be less than 0"); } } diff --git a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java index 9b03137cc2..691a86b09d 100644 --- a/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java @@ -12,7 +12,7 @@ public class ParserServiceImpl implements ParserService { @Override public List parseStringsIntoObjects(List strings) { if (strings == null) { - throw new RuntimeException("Cannot parse null"); + throw new IllegalArgumentException("Cannot parse null"); } return strings.stream().map(this::getFruitFromString).collect(Collectors.toList()); } diff --git a/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java index 8b4955a6be..9c63beaaf9 100644 --- a/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java @@ -17,11 +17,14 @@ public ReaderServiceImpl(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); + 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 index 01d200df9b..40d2037073 100644 --- a/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java @@ -12,12 +12,15 @@ public class WriterServiceImpl implements WriterService { @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); + throw new RuntimeException("Can't write to file " + toFile, ex); } } diff --git a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java index 0a42d83dc4..81a07f619a 100644 --- a/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java +++ b/src/main/java/core/basesyntax/strategy/TypeActivityStrategyImpl.java @@ -14,7 +14,7 @@ public TypeActivityStrategyImpl(Map strategyMap) { @Override public ActivityHandler get(Operation operation) { if (operation == null) { - throw new NullPointerException("Operation cannot be null"); + throw new IllegalArgumentException("Operation cannot be null"); } return strategyMap.get(operation); } diff --git a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java index d1bdc3a87c..e4b8298c55 100644 --- a/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java +++ b/src/test/java/core/basesyntax/dao/FruitTransactionDaoImplTest.java @@ -1,7 +1,6 @@ package core.basesyntax.dao; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; @@ -22,27 +21,26 @@ static void beforeAll() { } @Test - void get_FromStorage_isOk() { + void add_toStorage_isOk() { FruitTransaction fruitTransaction - = FruitTransaction.of(Operation.RETURN, APPLE, 50); - fruitTransactionDao - .addToStorage(fruitTransaction); - assertNotNull(fruitTransactionDao.getFromStorage(fruitTransaction)); + = FruitTransaction.of(Operation.BALANCE, APPLE, 75); + Storage.fruitTransactions.add(fruitTransaction); + assertEquals(fruitTransaction, fruitTransactionDao.getFromStorage(fruitTransaction)); } @Test - void get_NullFromStorage_isNotOk() { + void get_nullFromStorage_isNotOk() { assertNull(fruitTransactionDao.getFromStorage(null)); } @Test - void get_NonExistFruitFromStorage_isNotOk() { + void get_nonExistFruitFromStorage_isNotOk() { assertNull(fruitTransactionDao .getFromStorage(FruitTransaction.of(Operation.RETURN, APPLE, 20))); } @Test - void add_ExistFileToStorage_isOk() { + void add_toStorageByDao_isOk() { fruitTransactionDao .addToStorage(FruitTransaction.of(Operation.BALANCE, APPLE, 40)); FruitTransaction actual = Storage.fruitTransactions.get(0); @@ -51,8 +49,8 @@ void add_ExistFileToStorage_isOk() { } @Test - void add_NullToStorage_isNotOk() { - assertThrows(RuntimeException.class, + void add_nullToStorage_isNotOk() { + assertThrows(IllegalArgumentException.class, () -> fruitTransactionDao.addToStorage(null)); } diff --git a/src/test/java/core/basesyntax/model/FruitTransactionTest.java b/src/test/java/core/basesyntax/model/FruitTransactionTest.java index 268701030e..8159f61d3d 100644 --- a/src/test/java/core/basesyntax/model/FruitTransactionTest.java +++ b/src/test/java/core/basesyntax/model/FruitTransactionTest.java @@ -9,7 +9,7 @@ class FruitTransactionTest { private static final String BANANA = "banana"; @Test - void add_ToQuantity_isOk() { + void add_toQuantity_isOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, BANANA, 100); fruitTransaction.add(55); @@ -19,7 +19,7 @@ void add_ToQuantity_isOk() { } @Test - void subtract_CorrectQuantity_isOk() { + void subtract_correctQuantity_isOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, BANANA, 50); fruitTransaction.subtract(30); @@ -29,24 +29,24 @@ void subtract_CorrectQuantity_isOk() { } @Test - void subtract_IncorrectQuantity_isNotOk() { + void subtract_incorrectQuantity_isNotOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, BANANA, 20); - assertThrows(RuntimeException.class, + assertThrows(IllegalArgumentException.class, () -> fruitTransaction.subtract(30)); } @Test - void creating_FruitWithNegativeQuantity_isNotOk() { - assertThrows(RuntimeException.class, + void creating_fruitWithNegativeQuantity_isNotOk() { + assertThrows(IllegalArgumentException.class, () -> FruitTransaction.of(Operation.RETURN, BANANA, -5)); } @Test - void set_FruitWithNegativeQuantity_isNotOk() { + void set_fruitWithNegativeQuantity_isNotOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.RETURN, BANANA, 40); - assertThrows(RuntimeException.class, + 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 index 8a12363763..3ea4c14441 100644 --- a/src/test/java/core/basesyntax/service/FruitServiceTest.java +++ b/src/test/java/core/basesyntax/service/FruitServiceTest.java @@ -2,6 +2,7 @@ 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; @@ -30,26 +31,29 @@ 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(new FruitTransactionDaoImpl())); + new BalanceActivityHandler(fruitTransactionDao)); activityHandlerMap.put(Operation.SUPPLY, - new SupplyActivityHandler(new FruitTransactionDaoImpl())); + new SupplyActivityHandler(fruitTransactionDao)); activityHandlerMap.put(Operation.PURCHASE, - new PurchaseActivityHandler(new FruitTransactionDaoImpl())); + new PurchaseActivityHandler(fruitTransactionDao)); activityHandlerMap.put(Operation.RETURN, - new ReturnActivityHandler(new FruitTransactionDaoImpl())); + new ReturnActivityHandler(fruitTransactionDao)); } @Test - void write_Report_isOk() { + void write_reportToCorrectFile_isOk() { TypeActivityStrategy typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); diff --git a/src/test/java/core/basesyntax/service/ParserServiceTest.java b/src/test/java/core/basesyntax/service/ParserServiceTest.java index a9b2df800b..fc786b2121 100644 --- a/src/test/java/core/basesyntax/service/ParserServiceTest.java +++ b/src/test/java/core/basesyntax/service/ParserServiceTest.java @@ -21,7 +21,7 @@ static void beforeAll() { } @Test - void parse_StringsIntoObjects_isOk() { + void parse_stringsIntoObjects_isOk() { List strings = List.of("b,apple,100", "r,apple,50", "b,banana,100", "p,banana,20", "s,apple,50"); @@ -51,8 +51,8 @@ void parse_StringsIntoObjects_isOk() { } @Test - void parse_NullInObjects_isNotOk() { - assertThrows(RuntimeException.class, + 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 index 8f2b21aecf..c5b64aa797 100644 --- a/src/test/java/core/basesyntax/service/ReaderServiceTest.java +++ b/src/test/java/core/basesyntax/service/ReaderServiceTest.java @@ -1,7 +1,9 @@ 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 static org.junit.jupiter.api.Assertions.assertThrows; import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; @@ -24,13 +26,16 @@ static void beforeAll() { } @Test - void read_FromNull_isNotOk() { - assertThrows(RuntimeException.class, + 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() { + void read_fromExistFile_isOk() { List actual = readerService.readFromFile(EXIST_FILE); FruitTransaction fruitTransaction @@ -58,8 +63,11 @@ void read_FromExistFile_isOk() { } @Test - void read_FromNonExistFile_isNotOk() { - assertThrows(RuntimeException.class, + 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 index 93931addcc..f31b84783d 100644 --- a/src/test/java/core/basesyntax/service/WriterServiceTest.java +++ b/src/test/java/core/basesyntax/service/WriterServiceTest.java @@ -1,8 +1,9 @@ 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 static org.junit.jupiter.api.Assertions.assertThrows; import core.basesyntax.dao.FruitTransactionDao; import core.basesyntax.dao.FruitTransactionDaoImpl; @@ -29,18 +30,21 @@ static void beforeAll() { } @Test - void write_ToNull_isNotOk() { - assertThrows(RuntimeException.class, + 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() { + void write_toNonExistFile_isOk() { assertDoesNotThrow(() -> writerService.writeToFile(NON_EXIST_FILE)); } @Test - void write_ToExistFile_isOk() { + void write_toExistFile_isOk() { FruitTransactionDao fruitTransactionDao = new FruitTransactionDaoImpl(); FruitTransaction fruitTransaction @@ -60,7 +64,7 @@ void write_ToExistFile_isOk() { try { actual = Files.readAllLines(Path.of(EXIST_FILE)); } catch (IOException e) { - throw new RuntimeException(e); + throw new RuntimeException("Can't read from file " + EXIST_FILE); } assertIterableEquals(expected, actual); } diff --git a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java index 37432e624c..a1f503fcce 100644 --- a/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/BalanceActivityHandlerTest.java @@ -1,53 +1,49 @@ package core.basesyntax.service.amount; -import static org.junit.jupiter.api.Assertions.assertIterableEquals; +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 java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class BalanceActivityHandlerTest { - @BeforeEach - void beforeEach() { + private static FruitTransactionDao fruitTransactionDao; + private static BalanceActivityHandler balanceActivityHandler; + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + balanceActivityHandler = new BalanceActivityHandler(fruitTransactionDao); } @Test - void balance_ActivityHandler_isOk() { - FruitTransactionDao fruitTransactionDao - = new FruitTransactionDaoImpl(); - - BalanceActivityHandler balanceActivityHandler - = new BalanceActivityHandler(fruitTransactionDao); - + void balance_activityHandlerForBanana_isOk() { FruitTransaction fruitTransaction = FruitTransaction.of(Operation.BALANCE, "banana", 50); - - FruitTransaction fruitTransaction1 - = FruitTransaction.of(Operation.BALANCE, "apple", 100); - balanceActivityHandler .setAmountOfFruit(fruitTransaction); + FruitTransaction expected = fruitTransaction; + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + assertEquals(expected, actual); + } - balanceActivityHandler - .setAmountOfFruit(fruitTransaction1); - - List expected = List.of(fruitTransaction, fruitTransaction1); - - List actual - = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); - - assertIterableEquals(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); } - @AfterAll - static void afterAll() { + @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 index 810b2011e5..98d43e3ea0 100644 --- a/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/PurchaseActivityHandlerTest.java @@ -1,67 +1,57 @@ package core.basesyntax.service.amount; -import static org.junit.Assert.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 java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; +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 final String BANANA = "banana"; - private static final String APPLE = "apple"; - - @BeforeEach - void beforeEach() { + private static FruitTransactionDao fruitTransactionDao; + private static PurchaseActivityHandler purchaseActivityHandler; + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + purchaseActivityHandler = new PurchaseActivityHandler(fruitTransactionDao); } @Test - void purchase_ActivityHandler_isOk() { - FruitTransactionDao fruitTransactionDao - = new FruitTransactionDaoImpl(); - - PurchaseActivityHandler purchaseActivityHandler - = new PurchaseActivityHandler(fruitTransactionDao); - + void purchase_activityHandlerForBanana_isOk() { FruitTransaction fruitTransaction - = FruitTransaction.of(Operation.BALANCE, APPLE, 100); - - FruitTransaction fruitTransaction1 - = FruitTransaction.of(Operation.BALANCE, BANANA, 50); - + = FruitTransaction.of(Operation.BALANCE, "banana", 30); fruitTransactionDao.addToStorage(fruitTransaction); - - fruitTransactionDao.addToStorage(fruitTransaction1); - + FruitTransaction fruitTransactionPurchase + = FruitTransaction.of(Operation.PURCHASE, "banana", 10); purchaseActivityHandler - .setAmountOfFruit(FruitTransaction.of(Operation.PURCHASE, BANANA, 10)); + .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(FruitTransaction.of(Operation.PURCHASE, APPLE, 40)); - - FruitTransaction fruitTransaction2 - = FruitTransaction.of(Operation.BALANCE, BANANA, 40); - - FruitTransaction fruitTransaction3 - = FruitTransaction.of(Operation.BALANCE, APPLE, 60); - - List expected - = List.of(fruitTransaction3, fruitTransaction2); - - List actual - = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); - - assertEquals(expected, actual); + .setAmountOfFruit(fruitTransactionPurchase); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "apple", 60); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); } - @AfterAll - static void afterAll() { + @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 index 7add41e8fb..335ff4c769 100644 --- a/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/ReturnActivityHandlerTest.java @@ -1,67 +1,57 @@ package core.basesyntax.service.amount; -import static org.junit.Assert.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 java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; +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 final String BANANA = "banana"; - private static final String APPLE = "apple"; - - @BeforeEach - void beforeEach() { + private static FruitTransactionDao fruitTransactionDao; + private static ReturnActivityHandler returnActivityHandler; + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + returnActivityHandler = new ReturnActivityHandler(fruitTransactionDao); } @Test - void return_ActivityHandler_isOk() { - FruitTransactionDao fruitTransactionDao - = new FruitTransactionDaoImpl(); - - ReturnActivityHandler returnActivityHandler - = new ReturnActivityHandler(fruitTransactionDao); - + void return_activityHandlerForBanana_isOk() { FruitTransaction fruitTransaction - = FruitTransaction.of(Operation.BALANCE, APPLE, 30); - - FruitTransaction fruitTransaction1 - = FruitTransaction.of(Operation.BALANCE, BANANA, 50); - + = FruitTransaction.of(Operation.BALANCE, "banana", 60); fruitTransactionDao.addToStorage(fruitTransaction); - - fruitTransactionDao.addToStorage(fruitTransaction1); - + FruitTransaction fruitTransactionReturn + = FruitTransaction.of(Operation.RETURN, "banana", 30); returnActivityHandler - .setAmountOfFruit(FruitTransaction.of(Operation.RETURN, BANANA, 15)); + .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(FruitTransaction.of(Operation.RETURN, APPLE, 15)); - - FruitTransaction fruitTransaction2 - = FruitTransaction.of(Operation.BALANCE, APPLE, 45); - - FruitTransaction fruitTransaction3 - = FruitTransaction.of(Operation.BALANCE, BANANA, 65); - - List expected - = List.of(fruitTransaction2, fruitTransaction3); - - List actual - = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); - - assertEquals(expected, actual); + .setAmountOfFruit(fruitTransactionPurchase); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "apple", 140); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); } - @AfterAll - static void afterAll() { + @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 index e6a09748fc..5280e1ad06 100644 --- a/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java +++ b/src/test/java/core/basesyntax/service/amount/SupplyActivityHandlerTest.java @@ -1,67 +1,57 @@ package core.basesyntax.service.amount; -import static org.junit.Assert.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 java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; +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 final String BANANA = "banana"; - private static final String APPLE = "apple"; - - @BeforeEach - void beforeEach() { + private static FruitTransactionDao fruitTransactionDao; + private static SupplyActivityHandler supplyActivityHandler; + @BeforeAll + static void setUp() { + fruitTransactionDao = new FruitTransactionDaoImpl(); + supplyActivityHandler = new SupplyActivityHandler(fruitTransactionDao); } @Test - void supply_ActivityHandler_isOk() { - FruitTransactionDao fruitTransactionDao - = new FruitTransactionDaoImpl(); - - SupplyActivityHandler supplyActivityHandler - = new SupplyActivityHandler(fruitTransactionDao); - + void supply_activityHandlerForBanana_isOk() { FruitTransaction fruitTransaction - = FruitTransaction.of(Operation.BALANCE, APPLE, 80); - - FruitTransaction fruitTransaction1 - = FruitTransaction.of(Operation.BALANCE, BANANA, 55); - + = FruitTransaction.of(Operation.BALANCE, "banana", 65); fruitTransactionDao.addToStorage(fruitTransaction); - - fruitTransactionDao.addToStorage(fruitTransaction1); - + FruitTransaction fruitTransactionSupply + = FruitTransaction.of(Operation.SUPPLY, "banana", 35); supplyActivityHandler - .setAmountOfFruit(FruitTransaction.of(Operation.SUPPLY, APPLE, 40)); + .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(FruitTransaction.of(Operation.SUPPLY, BANANA, 20)); - - FruitTransaction fruitTransaction2 - = FruitTransaction.of(Operation.BALANCE, APPLE, 120); - - FruitTransaction fruitTransaction3 - = FruitTransaction.of(Operation.BALANCE, BANANA, 75); - - List expected - = List.of(fruitTransaction2, fruitTransaction3); - - List actual - = List.of(Storage.fruitTransactions.get(0), Storage.fruitTransactions.get(1)); - - assertEquals(expected, actual); + .setAmountOfFruit(fruitTransactionSupply); + FruitTransaction expected + = FruitTransaction.of(Operation.BALANCE, "apple", 110); + FruitTransaction actual = fruitTransactionDao.getFromStorage(fruitTransaction); + Assertions.assertEquals(expected, actual); } - @AfterAll - static void afterAll() { + @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 index 860490a37f..462cdb94a9 100644 --- a/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java +++ b/src/test/java/core/basesyntax/strategy/TypeActivityStrategyTest.java @@ -3,6 +3,7 @@ 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; @@ -18,58 +19,61 @@ 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(new FruitTransactionDaoImpl())); + new BalanceActivityHandler(fruitTransactionDao)); activityHandlerMap.put(Operation.SUPPLY, - new SupplyActivityHandler(new FruitTransactionDaoImpl())); + new SupplyActivityHandler(fruitTransactionDao)); activityHandlerMap.put(Operation.PURCHASE, - new PurchaseActivityHandler(new FruitTransactionDaoImpl())); + new PurchaseActivityHandler(fruitTransactionDao)); activityHandlerMap.put(Operation.RETURN, - new ReturnActivityHandler(new FruitTransactionDaoImpl())); + new ReturnActivityHandler(fruitTransactionDao)); typeActivityStrategy = new TypeActivityStrategyImpl(activityHandlerMap); } @Test - void get_HandlerFromSupplyOperation_isOk() { + void get_handlerFromSupplyOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.SUPPLY); - ActivityHandler expected = new SupplyActivityHandler(new FruitTransactionDaoImpl()); + ActivityHandler expected = new SupplyActivityHandler(fruitTransactionDao); assertEquals(actual.getClass(), expected.getClass()); } @Test - void get_HandlerFromReturnOperation_isOk() { + void get_handlerFromReturnOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.RETURN); - ActivityHandler expected = new ReturnActivityHandler(new FruitTransactionDaoImpl()); + ActivityHandler expected = new ReturnActivityHandler(fruitTransactionDao); assertEquals(actual.getClass(), expected.getClass()); } @Test - void get_HandlerFromPurchaseOperation_isOk() { + void get_handlerFromPurchaseOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.PURCHASE); - ActivityHandler expected = new PurchaseActivityHandler(new FruitTransactionDaoImpl()); + ActivityHandler expected = new PurchaseActivityHandler(fruitTransactionDao); assertEquals(actual.getClass(), expected.getClass()); } @Test - void get_HandlerFromBalanceOperation_isOk() { + void get_handlerFromBalanceOperation_isOk() { ActivityHandler actual = typeActivityStrategy.get(Operation.BALANCE); - ActivityHandler expected = new BalanceActivityHandler(new FruitTransactionDaoImpl()); + ActivityHandler expected = new BalanceActivityHandler(fruitTransactionDao); assertEquals(actual.getClass(), expected.getClass()); } @Test - void get_HandlerFromNull_isNotOk() { - assertThrows(NullPointerException.class, + void get_handlerFromNull_isNotOk() { + assertThrows(IllegalArgumentException.class, () -> typeActivityStrategy.get(null)); } }