From 7f3b8c44ef6d4969dc4307e4e177fc72200ad99b Mon Sep 17 00:00:00 2001 From: Volodymyr Hryhoryk Date: Tue, 19 Sep 2023 13:21:02 +0300 Subject: [PATCH 1/5] created tests for FruitShop --- src/main/java/core/basesyntax/HelloWorld.java | 7 -- src/main/java/core/basesyntax/Main.java | 59 ++++++++++++++++ src/main/java/core/basesyntax/db/Storage.java | 8 +++ .../exceptions/FruitShopException.java | 7 ++ .../basesyntax/model/FruitTransaction.java | 59 ++++++++++++++++ .../basesyntax/service/OperationService.java | 19 ++++++ .../core/basesyntax/service/ParseService.java | 8 +++ .../core/basesyntax/service/ReadService.java | 7 ++ .../basesyntax/service/ReportService.java | 5 ++ .../basesyntax/service/WriterService.java | 5 ++ .../service/impl/ParseServiceImpl.java | 39 +++++++++++ .../service/impl/ReadServiceImpl.java | 20 ++++++ .../service/impl/ReportServiceImpl.java | 26 +++++++ .../service/impl/WriteServiceImpl.java | 19 ++++++ .../basesyntax/strategy/OperationHandler.java | 7 ++ .../strategy/OperationStrategy.java | 17 +++++ .../impl/BalanceOperationHandler.java | 16 +++++ .../impl/PurchaseOperationHandler.java | 24 +++++++ .../strategy/impl/ReturnOperationHandler.java | 20 ++++++ .../strategy/impl/SupplyOperationHandler.java | 20 ++++++ src/main/java/resources/input.csv | 9 +++ src/main/java/resources/output.csv | 3 + .../java/core/basesyntax/HelloWorldTest.java | 7 -- .../service/OperationServiceTest.java | 59 ++++++++++++++++ .../service/impl/ParseServiceImplTest.java | 67 +++++++++++++++++++ .../service/impl/ReadServiceImplTest.java | 42 ++++++++++++ .../service/impl/ReportServiceImplTest.java | 36 ++++++++++ .../service/impl/WriteServiceImplTest.java | 33 +++++++++ .../strategy/OperationStrategyTest.java | 60 +++++++++++++++++ .../impl/BalanceOperationHandlerTest.java | 41 ++++++++++++ .../impl/PurchaseOperationHandlerTest.java | 61 +++++++++++++++++ .../impl/ReturnOperationHandlerTest.java | 54 +++++++++++++++ .../impl/SupplyOperationHandlerTest.java | 53 +++++++++++++++ src/test/resources/input.csv | 9 +++ src/test/resources/output.csv | 3 + 35 files changed, 915 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/db/Storage.java create mode 100644 src/main/java/core/basesyntax/exceptions/FruitShopException.java create mode 100644 src/main/java/core/basesyntax/model/FruitTransaction.java create mode 100644 src/main/java/core/basesyntax/service/OperationService.java create mode 100644 src/main/java/core/basesyntax/service/ParseService.java create mode 100644 src/main/java/core/basesyntax/service/ReadService.java create mode 100644 src/main/java/core/basesyntax/service/ReportService.java create mode 100644 src/main/java/core/basesyntax/service/WriterService.java create mode 100644 src/main/java/core/basesyntax/service/impl/ParseServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ReadServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/WriteServiceImpl.java create mode 100644 src/main/java/core/basesyntax/strategy/OperationHandler.java create mode 100644 src/main/java/core/basesyntax/strategy/OperationStrategy.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandler.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandler.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandler.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandler.java create mode 100644 src/main/java/resources/input.csv create mode 100644 src/main/java/resources/output.csv delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java create mode 100644 src/test/java/core/basesyntax/service/OperationServiceTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/strategy/OperationStrategyTest.java create mode 100644 src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerTest.java create mode 100644 src/test/resources/input.csv create mode 100644 src/test/resources/output.csv 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..0617e44f75 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,59 @@ +package core.basesyntax; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.OperationService; +import core.basesyntax.service.ParseService; +import core.basesyntax.service.ReadService; +import core.basesyntax.service.WriterService; +import core.basesyntax.service.impl.ParseServiceImpl; +import core.basesyntax.service.impl.ReadServiceImpl; +import core.basesyntax.service.impl.ReportServiceImpl; +import core.basesyntax.service.impl.WriteServiceImpl; +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.OperationStrategy; +import core.basesyntax.strategy.impl.BalanceOperationHandler; +import core.basesyntax.strategy.impl.PurchaseOperationHandler; +import core.basesyntax.strategy.impl.ReturnOperationHandler; +import core.basesyntax.strategy.impl.SupplyOperationHandler; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Main { + private static final String INPUT_PATH = "src/main/java/resources/input.csv"; + private static final String OUTPUT_PATH = "src/main/java/resources/output.csv"; + + public static void main(String[] args) { + Map + operationHandlerMap = initializeOperationHandlers(); + + ReadService readService = new ReadServiceImpl(); + List inputData = readService.readInputData(INPUT_PATH); + + ParseService parseService = new ParseServiceImpl(); + List parsedInputData = parseService.parseInputData(inputData); + + OperationStrategy strategy = new OperationStrategy(operationHandlerMap); + OperationService service = new OperationService(strategy); + service.processOperation(parsedInputData); + + String report = new ReportServiceImpl().createReport(); + System.out.println(report); + + WriterService writerService = new WriteServiceImpl(); + writerService.writeToFile(OUTPUT_PATH, report); + } + + private static Map initializeOperationHandlers() { + Map operationHandlerMap = new HashMap<>(); + operationHandlerMap.put( + FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.RETURN, new ReturnOperationHandler()); + return operationHandlerMap; + } +} 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..cc38189c14 --- /dev/null +++ b/src/main/java/core/basesyntax/db/Storage.java @@ -0,0 +1,8 @@ +package core.basesyntax.db; + +import java.util.HashMap; +import java.util.Map; + +public class Storage { + public static final Map STORAGE = new HashMap<>(); +} diff --git a/src/main/java/core/basesyntax/exceptions/FruitShopException.java b/src/main/java/core/basesyntax/exceptions/FruitShopException.java new file mode 100644 index 0000000000..9119b2faae --- /dev/null +++ b/src/main/java/core/basesyntax/exceptions/FruitShopException.java @@ -0,0 +1,7 @@ +package core.basesyntax.exceptions; + +public class FruitShopException extends RuntimeException { + public FruitShopException(String message) { + super(message); + } +} 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..94bc45885d --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,59 @@ +package core.basesyntax.model; + +import core.basesyntax.exceptions.FruitShopException; + +public class FruitTransaction { + private Operation operation; + private String fruit; + private int quantity; + + public Operation getOperation() { + return operation; + } + + public String getFruit() { + return fruit; + } + + public int getQuantity() { + return quantity; + } + + public void setOperation(Operation operation) { + this.operation = operation; + } + + public void setFruit(String fruit) { + this.fruit = fruit; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public enum Operation { + BALANCE("b"), + SUPPLY("s"), + PURCHASE("p"), + RETURN("r"); + + private final String code; + + Operation(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static Operation getOperationByCode(String code) { + for (Operation operation : Operation.values()) { + if (operation.getCode().equals(code)) { + return operation; + } + } + throw new FruitShopException("This operation is unavailable " + code); + } + } +} diff --git a/src/main/java/core/basesyntax/service/OperationService.java b/src/main/java/core/basesyntax/service/OperationService.java new file mode 100644 index 0000000000..761d5dfd80 --- /dev/null +++ b/src/main/java/core/basesyntax/service/OperationService.java @@ -0,0 +1,19 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationStrategy; +import java.util.List; + +public class OperationService { + private final OperationStrategy operationStrategy; + + public OperationService(OperationStrategy operationStrategy) { + this.operationStrategy = operationStrategy; + } + + public void processOperation(List transactions) { + for (FruitTransaction transaction : transactions) { + operationStrategy.get(transaction.getOperation()).handle(transaction); + } + } +} diff --git a/src/main/java/core/basesyntax/service/ParseService.java b/src/main/java/core/basesyntax/service/ParseService.java new file mode 100644 index 0000000000..e73c8d8b8b --- /dev/null +++ b/src/main/java/core/basesyntax/service/ParseService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface ParseService { + List parseInputData(List inputData); +} diff --git a/src/main/java/core/basesyntax/service/ReadService.java b/src/main/java/core/basesyntax/service/ReadService.java new file mode 100644 index 0000000000..26bacdd3ff --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReadService.java @@ -0,0 +1,7 @@ +package core.basesyntax.service; + +import java.util.List; + +public interface ReadService { + List readInputData(String inputFilePath); +} diff --git a/src/main/java/core/basesyntax/service/ReportService.java b/src/main/java/core/basesyntax/service/ReportService.java new file mode 100644 index 0000000000..8d2487b298 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface ReportService { + String createReport(); +} 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..b1b4f82c2f --- /dev/null +++ b/src/main/java/core/basesyntax/service/WriterService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface WriterService { + String writeToFile(String filePath, String report); +} diff --git a/src/main/java/core/basesyntax/service/impl/ParseServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ParseServiceImpl.java new file mode 100644 index 0000000000..327cabf0fc --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ParseServiceImpl.java @@ -0,0 +1,39 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.ParseService; +import java.util.ArrayList; +import java.util.List; + +public class ParseServiceImpl implements ParseService { + private static final String DELIMITER = ","; + private static final int OPERATION_INDEX = 0; + private static final int FRUIT_INDEX = 1; + private static final int QUANTITY_INDEX = 2; + private static final int START_POINT = 1; + + @Override + public List parseInputData(List inputData) { + if (inputData.isEmpty()) { + throw new FruitShopException("Input data list is empty!"); + } + List parsedData = new ArrayList<>(); + for (int i = START_POINT; i < inputData.size(); i++) { + parsedData.add(parseEachLine(inputData.get(i))); + } + return parsedData; + } + + private FruitTransaction parseEachLine(String line) { + FruitTransaction fruitTransaction = new FruitTransaction(); + String[] splitLines = line.split(DELIMITER); + fruitTransaction.setOperation( + FruitTransaction.Operation.getOperationByCode(splitLines[OPERATION_INDEX])); + fruitTransaction.setFruit( + splitLines[FRUIT_INDEX]); + fruitTransaction.setQuantity( + Integer.parseInt(splitLines[QUANTITY_INDEX])); + return fruitTransaction; + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ReadServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReadServiceImpl.java new file mode 100644 index 0000000000..56e6928f48 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReadServiceImpl.java @@ -0,0 +1,20 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.ReadService; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class ReadServiceImpl implements ReadService { + @Override + public List readInputData(String inputFilePath) { + try { + return Files.readAllLines(Path.of(inputFilePath)); + } catch (IOException e) { + throw new FruitShopException("Can't read this file " + inputFilePath); + } + + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java new file mode 100644 index 0000000000..37ea2c4a8e --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java @@ -0,0 +1,26 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.ReportService; +import java.util.Map; + +public class ReportServiceImpl implements ReportService { + private static final String FIRST_ROW = "fruit,quantity\n"; + private static final String DELIMITER = ","; + + @Override + public String createReport() { + if (Storage.STORAGE.isEmpty()) { + throw new FruitShopException("Can't create report"); + } + StringBuilder builder = new StringBuilder(FIRST_ROW); + for (Map.Entry entry : Storage.STORAGE.entrySet()) { + builder.append(entry.getKey()) + .append(DELIMITER) + .append(entry.getValue()) + .append(System.lineSeparator()); + } + return builder.toString(); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/WriteServiceImpl.java b/src/main/java/core/basesyntax/service/impl/WriteServiceImpl.java new file mode 100644 index 0000000000..86a3029d16 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/WriteServiceImpl.java @@ -0,0 +1,19 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.WriterService; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class WriteServiceImpl implements WriterService { + @Override + public String writeToFile(String filePath, String report) { + try { + Files.writeString(Path.of(filePath), report); + } catch (IOException e) { + throw new FruitShopException("Can't write to file " + filePath); + } + return report; + } +} diff --git a/src/main/java/core/basesyntax/strategy/OperationHandler.java b/src/main/java/core/basesyntax/strategy/OperationHandler.java new file mode 100644 index 0000000000..a9137546f3 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationHandler.java @@ -0,0 +1,7 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; + +public interface OperationHandler { + void handle(FruitTransaction fruitTransaction); +} diff --git a/src/main/java/core/basesyntax/strategy/OperationStrategy.java b/src/main/java/core/basesyntax/strategy/OperationStrategy.java new file mode 100644 index 0000000000..798e525db4 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationStrategy.java @@ -0,0 +1,17 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; +import java.util.Map; + +public class OperationStrategy { + private final Map operationHandlerMap; + + public OperationStrategy(Map + operationHandlerMap) { + this.operationHandlerMap = operationHandlerMap; + } + + public OperationHandler get(FruitTransaction.Operation operation) { + return operationHandlerMap.get(operation); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandler.java b/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandler.java new file mode 100644 index 0000000000..6a3edf4079 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandler.java @@ -0,0 +1,16 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class BalanceOperationHandler implements OperationHandler { + @Override + public void handle(FruitTransaction fruitTransaction) { + if (fruitTransaction.getQuantity() <= 0) { + throw new FruitShopException("Quantity of fruits must be greater than zero"); + } + Storage.STORAGE.put(fruitTransaction.getFruit(), fruitTransaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandler.java b/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandler.java new file mode 100644 index 0000000000..f6d25b2408 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandler.java @@ -0,0 +1,24 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class PurchaseOperationHandler implements OperationHandler { + + @Override + public void handle(FruitTransaction fruitTransaction) { + if (fruitTransaction.getQuantity() <= 0) { + throw new FruitShopException("Quantity must be greater than zero"); + } + String fruitName = fruitTransaction.getFruit(); + int transactionQuantity = fruitTransaction.getQuantity(); + int initialQuantity = Storage.STORAGE.get(fruitName); + if (initialQuantity < transactionQuantity) { + throw new FruitShopException("Unfortunately, we don't have this amount "); + } + Storage.STORAGE.put( + fruitTransaction.getFruit(), initialQuantity - transactionQuantity); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandler.java b/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandler.java new file mode 100644 index 0000000000..ca130c278a --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandler.java @@ -0,0 +1,20 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class ReturnOperationHandler implements OperationHandler { + @Override + public void handle(FruitTransaction fruitTransaction) { + if (fruitTransaction.getQuantity() <= 0) { + throw new FruitShopException("Quantity of fruits must be greater than zero"); + } + String fruitName = fruitTransaction.getFruit(); + int transactionQuantity = fruitTransaction.getQuantity(); + int initialQuantity = Storage.STORAGE.get(fruitName); + Storage.STORAGE.put( + fruitName, initialQuantity + transactionQuantity); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandler.java b/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandler.java new file mode 100644 index 0000000000..468ba9b0b1 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandler.java @@ -0,0 +1,20 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class SupplyOperationHandler implements OperationHandler { + @Override + public void handle(FruitTransaction fruitTransaction) { + if (fruitTransaction.getQuantity() <= 0) { + throw new FruitShopException("Quantity must be greater than zero"); + } + String fruitName = fruitTransaction.getFruit(); + int transactionQuantity = fruitTransaction.getQuantity(); + int initialQuantity = Storage.STORAGE.get(fruitName); + Storage.STORAGE.put( + fruitName, initialQuantity + transactionQuantity); + } +} diff --git a/src/main/java/resources/input.csv b/src/main/java/resources/input.csv new file mode 100644 index 0000000000..c771f259d3 --- /dev/null +++ b/src/main/java/resources/input.csv @@ -0,0 +1,9 @@ +type,fruit,quantity +b,banana,20 +b,apple,100 +s,banana,100 +p,banana,13 +r,apple,10 +p,apple,20 +p,banana,5 +s,banana,50 \ No newline at end of file diff --git a/src/main/java/resources/output.csv b/src/main/java/resources/output.csv new file mode 100644 index 0000000000..6158e4419c --- /dev/null +++ b/src/main/java/resources/output.csv @@ -0,0 +1,3 @@ +fruit,quantity +banana,152 +apple,90 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/OperationServiceTest.java b/src/test/java/core/basesyntax/service/OperationServiceTest.java new file mode 100644 index 0000000000..6d930bc4a4 --- /dev/null +++ b/src/test/java/core/basesyntax/service/OperationServiceTest.java @@ -0,0 +1,59 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.ParseServiceImpl; +import core.basesyntax.service.impl.ReadServiceImpl; +import core.basesyntax.service.impl.ReportServiceImpl; +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.OperationStrategy; +import core.basesyntax.strategy.impl.BalanceOperationHandler; +import core.basesyntax.strategy.impl.PurchaseOperationHandler; +import core.basesyntax.strategy.impl.ReturnOperationHandler; +import core.basesyntax.strategy.impl.SupplyOperationHandler; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class OperationServiceTest { + private static final String INPUT_PATH = "src/test/resources/input.csv"; + private static OperationStrategy operationStrategy; + private static OperationService operationService; + + @BeforeAll + static void beforeAll() { + Map operationHandlerMap = new HashMap<>(); + operationHandlerMap.put( + FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.RETURN, new ReturnOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); + operationStrategy = new OperationStrategy(operationHandlerMap); + } + + @AfterAll + static void afterAll() { + Storage.STORAGE.clear(); + } + + @Test + void validResult_Ok() { + String expected = "fruit,quantity\n" + + "banana,152\r\n" + + "apple,90\r\n"; + List inputData = new ReadServiceImpl().readInputData(INPUT_PATH); + List parseData = new ParseServiceImpl().parseInputData(inputData); + operationService = new OperationService(operationStrategy); + operationService.processOperation(parseData); + String actual = new ReportServiceImpl().createReport(); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java new file mode 100644 index 0000000000..cb5546ef84 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java @@ -0,0 +1,67 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.ParseService; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ParseServiceImplTest { + private static final int BALANCE_INDEX = 0; + private static final int SUPPLY_INDEX = 1; + private static final int PURCHASE_INDEX = 2; + private static final int RETURN_INDEX = 3; + private static final String FRUIT = "banana"; + private static final int QUANTITY = 100; + private static ParseService parseService; + private static List inputData; + private static List emptyList; + private static List parseData; + + @BeforeAll + static void beforeAll() { + parseService = new ParseServiceImpl(); + emptyList = new ArrayList<>(); + inputData = new ArrayList<>(Arrays.asList( + "type,fruit,quantity", + "b,banana,100", + "s,banana,100", + "p,banana,100", + "r,banana,100")); + parseData = parseService.parseInputData(inputData); + } + + @Test + void parseNull_NotOk() { + assertThrows(FruitShopException.class, () -> parseService.parseInputData(emptyList)); + } + + @Test + void correctParseData_Ok() { + FruitTransaction balanceTransaction = parseData.get(BALANCE_INDEX); + assertEquals(FruitTransaction.Operation.BALANCE, balanceTransaction.getOperation()); + assertEquals(FRUIT, balanceTransaction.getFruit()); + assertEquals(QUANTITY, balanceTransaction.getQuantity()); + + FruitTransaction supplyTransaction = parseData.get(SUPPLY_INDEX); + assertEquals(FruitTransaction.Operation.SUPPLY, supplyTransaction.getOperation()); + assertEquals(FRUIT, supplyTransaction.getFruit()); + assertEquals(QUANTITY, supplyTransaction.getQuantity()); + + FruitTransaction purchaseTransaction = parseData.get(PURCHASE_INDEX); + assertEquals(FruitTransaction.Operation.PURCHASE, purchaseTransaction.getOperation()); + assertEquals(FRUIT, purchaseTransaction.getFruit()); + assertEquals(QUANTITY, purchaseTransaction.getQuantity()); + + FruitTransaction returnTransaction = parseData.get(RETURN_INDEX); + assertEquals(FruitTransaction.Operation.RETURN, returnTransaction.getOperation()); + assertEquals(FRUIT, returnTransaction.getFruit()); + assertEquals(QUANTITY, returnTransaction.getQuantity()); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java new file mode 100644 index 0000000000..32daf7d57f --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java @@ -0,0 +1,42 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.ReadService; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ReadServiceImplTest { + private static final String INVALID_PATH = "src/main/java/resources/invalid.csv"; + private static final String VALID_PATH = "src/main/java/resources/input.csv"; + private static ReadService readService; + private static List expectedList; + + @BeforeAll + static void beforeAll() { + readService = new ReadServiceImpl(); + expectedList = List.of("type,fruit,quantity", + "b,banana,20", + "b,apple,100", + "s,banana,100", + "p,banana,13", + "r,apple,10", + "p,apple,20", + "p,banana,5", + "s,banana,50"); + } + + @Test + void readInputData_Ok() { + List actualList = readService.readInputData(VALID_PATH); + assertEquals(expectedList, actualList); + } + + @Test + void invalidPath_notOk() { + assertThrows(FruitShopException.class, () -> readService.readInputData(INVALID_PATH)); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java new file mode 100644 index 0000000000..678066cc21 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java @@ -0,0 +1,36 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.ReportService; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ReportServiceImplTest { + private static final String FIRST_ROW = "fruit,quantity\n"; + private static ReportService reportService; + + @BeforeAll + static void beforeAll() { + reportService = new ReportServiceImpl(); + } + + @Test + void validReport_Ok() { + Storage.STORAGE.put("banana", 20); + Storage.STORAGE.put("apple", 15); + String expected = FIRST_ROW + + "banana,20\r\n" + + "apple,15\r\n"; + String actual = reportService.createReport(); + assertEquals(expected, actual); + } + + @Test + void emptyReport_NotOk() { + assertThrows(FruitShopException.class, () -> reportService.createReport()); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java new file mode 100644 index 0000000000..dddf862f44 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java @@ -0,0 +1,33 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.WriterService; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class WriteServiceImplTest { + private static final String OUTPUT_PATH = "src/main/java/resources/output.csv"; + private static final String REPORT = "fruit,quantity\n" + + "banana,152\r\n" + + "apple,90\r\n"; + private static WriterService writerService; + + @BeforeAll + static void beforeAll() { + writerService = new WriteServiceImpl(); + } + + @Test + void invalidPath_NotOk() { + assertThrows(FruitShopException.class, () -> writerService.writeToFile("", REPORT)); + } + + @Test + void writeReport_Ok() { + String actual = writerService.writeToFile(OUTPUT_PATH, REPORT); + assertEquals(actual, REPORT); + } +} diff --git a/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java b/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java new file mode 100644 index 0000000000..a6c142e89d --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java @@ -0,0 +1,60 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.impl.BalanceOperationHandler; +import core.basesyntax.strategy.impl.PurchaseOperationHandler; +import core.basesyntax.strategy.impl.ReturnOperationHandler; +import core.basesyntax.strategy.impl.SupplyOperationHandler; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class OperationStrategyTest { + private static OperationHandler operationHandler; + private static OperationStrategy operationStrategy; + + @BeforeAll + static void beforeAll() { + Map operationHandlerMap = new HashMap<>(); + operationHandlerMap.put( + FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.RETURN, new ReturnOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler()); + operationHandlerMap.put( + FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); + operationStrategy = new OperationStrategy(operationHandlerMap); + } + + @Test + void getSuppleHandler_Ok() { + operationHandler = new SupplyOperationHandler(); + assertEquals(operationHandler.getClass(), + operationStrategy.get(FruitTransaction.Operation.SUPPLY).getClass()); + } + + @Test + void getReturnHandler_Ok() { + operationHandler = new ReturnOperationHandler(); + assertEquals(operationHandler.getClass(), + operationStrategy.get(FruitTransaction.Operation.RETURN).getClass()); + } + + @Test + void getPurchaseHandler_Ok() { + operationHandler = new PurchaseOperationHandler(); + assertEquals(operationHandler.getClass(), + operationStrategy.get(FruitTransaction.Operation.PURCHASE).getClass()); + } + + @Test + void getBalanceHandler_Ok() { + operationHandler = new BalanceOperationHandler(); + assertEquals(operationHandler.getClass(), + operationStrategy.get(FruitTransaction.Operation.BALANCE).getClass()); + } +} diff --git a/src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerTest.java b/src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerTest.java new file mode 100644 index 0000000000..a57ede5d24 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerTest.java @@ -0,0 +1,41 @@ +package core.basesyntax.strategy.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class BalanceOperationHandlerTest { + private static final int ZERO_QUANTITY = 0; + private static final int VALID_QUANTITY = 10; + private static final String FRUIT = "banana"; + private static OperationHandler operationHandler; + private static FruitTransaction fruitTransaction; + + @BeforeAll + static void beforeAll() { + operationHandler = new BalanceOperationHandler(); + fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation(FruitTransaction.Operation.BALANCE); + fruitTransaction.setFruit(FRUIT); + fruitTransaction.setQuantity(ZERO_QUANTITY); + } + + @Test + void quantityZeroOrLess_notOk() { + assertThrows(FruitShopException.class, () -> operationHandler.handle(fruitTransaction)); + } + + @Test + void correctTrasaction_Ok() { + fruitTransaction.setQuantity(VALID_QUANTITY); + operationHandler.handle(fruitTransaction); + int actual = Storage.STORAGE.get(FRUIT); + assertEquals(VALID_QUANTITY, actual); + } +} diff --git a/src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerTest.java b/src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerTest.java new file mode 100644 index 0000000000..eb4dafee62 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerTest.java @@ -0,0 +1,61 @@ +package core.basesyntax.strategy.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PurchaseOperationHandlerTest { + private static final int ZERO_QUANTITY = 0; + private static final int BIGGER_THAN_EXPECTED_QUANTITY = 30; + private static final int VALID_QUANTITY = 10; + private static final String FRUIT = "banana"; + private static OperationHandler operationHandler; + private static FruitTransaction fruitTransaction; + + @BeforeAll + static void beforeAll() { + operationHandler = new PurchaseOperationHandler(); + fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation(FruitTransaction.Operation.PURCHASE); + fruitTransaction.setFruit(FRUIT); + fruitTransaction.setQuantity(ZERO_QUANTITY); + } + + @BeforeEach + void setUp() { + Storage.STORAGE.put(FRUIT, VALID_QUANTITY); + } + + @AfterEach + void tearDown() { + Storage.STORAGE.clear(); + } + + @Test + void quantityZeroOrLess_NotOk() { + fruitTransaction.setQuantity(ZERO_QUANTITY); + assertThrows(FruitShopException.class, () -> operationHandler.handle(fruitTransaction)); + } + + @Test + void tooMuchQuantity_NotOk() { + fruitTransaction.setQuantity(BIGGER_THAN_EXPECTED_QUANTITY); + assertThrows(FruitShopException.class, () -> operationHandler.handle(fruitTransaction)); + } + + @Test + void correctQuantity_Ok() { + fruitTransaction.setQuantity(VALID_QUANTITY); + operationHandler.handle(fruitTransaction); + int actual = Storage.STORAGE.get(FRUIT); + assertEquals(ZERO_QUANTITY, actual); + } +} diff --git a/src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerTest.java b/src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerTest.java new file mode 100644 index 0000000000..78ef618b2b --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerTest.java @@ -0,0 +1,54 @@ +package core.basesyntax.strategy.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ReturnOperationHandlerTest { + private static final int ZERO_QUANTITY = 0; + private static final int VALID_QUANTITY = 10; + private static final String FRUIT = "banana"; + private static OperationHandler operationHandler; + private static FruitTransaction fruitTransaction; + + @BeforeAll + static void beforeAll() { + operationHandler = new ReturnOperationHandler(); + fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation(FruitTransaction.Operation.BALANCE); + fruitTransaction.setFruit(FRUIT); + fruitTransaction.setQuantity(ZERO_QUANTITY); + } + + @BeforeEach + void setUp() { + Storage.STORAGE.put(FRUIT, ZERO_QUANTITY); + } + + @AfterEach + void tearDown() { + Storage.STORAGE.clear(); + } + + @Test + void quantityZeroOrLess_NotOk() { + fruitTransaction.setQuantity(ZERO_QUANTITY); + assertThrows(FruitShopException.class, () -> operationHandler.handle(fruitTransaction)); + } + + @Test + void correctQuantity_Ok() { + fruitTransaction.setQuantity(VALID_QUANTITY); + operationHandler.handle(fruitTransaction); + int actual = Storage.STORAGE.get(FRUIT); + assertEquals(VALID_QUANTITY, actual); + } +} diff --git a/src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerTest.java b/src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerTest.java new file mode 100644 index 0000000000..30aa9da33f --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerTest.java @@ -0,0 +1,53 @@ +package core.basesyntax.strategy.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SupplyOperationHandlerTest { + private static final int ZERO_QUANTITY = 0; + private static final int VALID_QUANTITY = 10; + private static final String FRUIT = "banana"; + private static OperationHandler operationHandler; + private static FruitTransaction fruitTransaction; + + @BeforeAll + static void beforeAll() { + operationHandler = new SupplyOperationHandler(); + fruitTransaction = new FruitTransaction(); + fruitTransaction.setOperation(FruitTransaction.Operation.BALANCE); + fruitTransaction.setFruit(FRUIT); + fruitTransaction.setQuantity(ZERO_QUANTITY); + } + + @BeforeEach + void setUp() { + Storage.STORAGE.put(FRUIT, ZERO_QUANTITY); + } + + @AfterEach + void tearDown() { + Storage.STORAGE.clear(); + } + + @Test + void qualityZeroOrLess_NotOk() { + assertThrows(FruitShopException.class, () -> operationHandler.handle(fruitTransaction)); + } + + @Test + void validQuality_Ok() { + fruitTransaction.setQuantity(VALID_QUANTITY); + operationHandler.handle(fruitTransaction); + int actual = Storage.STORAGE.get(FRUIT); + assertEquals(VALID_QUANTITY, actual); + } +} diff --git a/src/test/resources/input.csv b/src/test/resources/input.csv new file mode 100644 index 0000000000..c771f259d3 --- /dev/null +++ b/src/test/resources/input.csv @@ -0,0 +1,9 @@ +type,fruit,quantity +b,banana,20 +b,apple,100 +s,banana,100 +p,banana,13 +r,apple,10 +p,apple,20 +p,banana,5 +s,banana,50 \ No newline at end of file diff --git a/src/test/resources/output.csv b/src/test/resources/output.csv new file mode 100644 index 0000000000..6158e4419c --- /dev/null +++ b/src/test/resources/output.csv @@ -0,0 +1,3 @@ +fruit,quantity +banana,152 +apple,90 From 1dc1023e588261a7d4fa0285bada68e51cf733a0 Mon Sep 17 00:00:00 2001 From: Volodymyr Hryhoryk Date: Tue, 19 Sep 2023 16:21:13 +0300 Subject: [PATCH 2/5] added fixes --- .../service/impl/ReportServiceImpl.java | 2 +- .../basesyntax/strategy/OperationStrategy.java | 4 ++++ .../basesyntax/service/OperationServiceTest.java | 9 ++++++--- .../service/impl/ParseServiceImplTest.java | 16 ++++++++-------- .../service/impl/ReportServiceImplTest.java | 8 +++++--- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java index 37ea2c4a8e..4277020962 100644 --- a/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java @@ -6,7 +6,7 @@ import java.util.Map; public class ReportServiceImpl implements ReportService { - private static final String FIRST_ROW = "fruit,quantity\n"; + private static final String FIRST_ROW = "fruit,quantity" + System.lineSeparator(); private static final String DELIMITER = ","; @Override diff --git a/src/main/java/core/basesyntax/strategy/OperationStrategy.java b/src/main/java/core/basesyntax/strategy/OperationStrategy.java index 798e525db4..a5b29754fe 100644 --- a/src/main/java/core/basesyntax/strategy/OperationStrategy.java +++ b/src/main/java/core/basesyntax/strategy/OperationStrategy.java @@ -1,5 +1,6 @@ package core.basesyntax.strategy; +import core.basesyntax.exceptions.FruitShopException; import core.basesyntax.model.FruitTransaction; import java.util.Map; @@ -12,6 +13,9 @@ public OperationStrategy(Map } public OperationHandler get(FruitTransaction.Operation operation) { + if (operation == null) { + throw new FruitShopException("Operation can't be null!"); + } return operationHandlerMap.get(operation); } } diff --git a/src/test/java/core/basesyntax/service/OperationServiceTest.java b/src/test/java/core/basesyntax/service/OperationServiceTest.java index 6d930bc4a4..250f79e8d9 100644 --- a/src/test/java/core/basesyntax/service/OperationServiceTest.java +++ b/src/test/java/core/basesyntax/service/OperationServiceTest.java @@ -46,9 +46,12 @@ static void afterAll() { @Test void validResult_Ok() { - String expected = "fruit,quantity\n" - + "banana,152\r\n" - + "apple,90\r\n"; + String expected = "fruit,quantity" + + System.lineSeparator() + + "banana,152" + + System.lineSeparator() + + "apple,90" + + System.lineSeparator(); List inputData = new ReadServiceImpl().readInputData(INPUT_PATH); List parseData = new ParseServiceImpl().parseInputData(inputData); operationService = new OperationService(operationStrategy); diff --git a/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java index cb5546ef84..4aefd8d842 100644 --- a/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java @@ -27,23 +27,23 @@ class ParseServiceImplTest { @BeforeAll static void beforeAll() { parseService = new ParseServiceImpl(); - emptyList = new ArrayList<>(); - inputData = new ArrayList<>(Arrays.asList( - "type,fruit,quantity", - "b,banana,100", - "s,banana,100", - "p,banana,100", - "r,banana,100")); - parseData = parseService.parseInputData(inputData); } @Test void parseNull_NotOk() { + emptyList = new ArrayList<>(); assertThrows(FruitShopException.class, () -> parseService.parseInputData(emptyList)); } @Test void correctParseData_Ok() { + inputData = new ArrayList<>(Arrays.asList( + "type,fruit,quantity", + "b,banana,100", + "s,banana,100", + "p,banana,100", + "r,banana,100")); + parseData = parseService.parseInputData(inputData); FruitTransaction balanceTransaction = parseData.get(BALANCE_INDEX); assertEquals(FruitTransaction.Operation.BALANCE, balanceTransaction.getOperation()); assertEquals(FRUIT, balanceTransaction.getFruit()); diff --git a/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java index 678066cc21..4ccf6e189a 100644 --- a/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test; class ReportServiceImplTest { - private static final String FIRST_ROW = "fruit,quantity\n"; + private static final String FIRST_ROW = "fruit,quantity" + System.lineSeparator(); private static ReportService reportService; @BeforeAll @@ -23,8 +23,10 @@ void validReport_Ok() { Storage.STORAGE.put("banana", 20); Storage.STORAGE.put("apple", 15); String expected = FIRST_ROW - + "banana,20\r\n" - + "apple,15\r\n"; + + "banana,20" + + System.lineSeparator() + + "apple,15" + + System.lineSeparator(); String actual = reportService.createReport(); assertEquals(expected, actual); } From 13a07d1f5a09b6d32bb518e991baf6e9c4fd18d2 Mon Sep 17 00:00:00 2001 From: Volodymyr Hryhoryk Date: Tue, 19 Sep 2023 16:23:40 +0300 Subject: [PATCH 3/5] added fixes --- .../java/core/basesyntax/service/OperationServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/core/basesyntax/service/OperationServiceTest.java b/src/test/java/core/basesyntax/service/OperationServiceTest.java index 250f79e8d9..5254f711cc 100644 --- a/src/test/java/core/basesyntax/service/OperationServiceTest.java +++ b/src/test/java/core/basesyntax/service/OperationServiceTest.java @@ -56,7 +56,7 @@ void validResult_Ok() { List parseData = new ParseServiceImpl().parseInputData(inputData); operationService = new OperationService(operationStrategy); operationService.processOperation(parseData); - String actual = new ReportServiceImpl().createReport(); - assertEquals(expected, actual); + String actuall = new ReportServiceImpl().createReport(); + assertEquals(expected, actuall); } } From d317b527777825307c79bd9a5498c4795cba9924 Mon Sep 17 00:00:00 2001 From: Volodymyr Hryhoryk Date: Tue, 19 Sep 2023 16:27:12 +0300 Subject: [PATCH 4/5] minor fix --- .../java/core/basesyntax/service/OperationServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/core/basesyntax/service/OperationServiceTest.java b/src/test/java/core/basesyntax/service/OperationServiceTest.java index 5254f711cc..250f79e8d9 100644 --- a/src/test/java/core/basesyntax/service/OperationServiceTest.java +++ b/src/test/java/core/basesyntax/service/OperationServiceTest.java @@ -56,7 +56,7 @@ void validResult_Ok() { List parseData = new ParseServiceImpl().parseInputData(inputData); operationService = new OperationService(operationStrategy); operationService.processOperation(parseData); - String actuall = new ReportServiceImpl().createReport(); - assertEquals(expected, actuall); + String actual = new ReportServiceImpl().createReport(); + assertEquals(expected, actual); } } From 938caddded3c705f775979ef563e8965933f98e9 Mon Sep 17 00:00:00 2001 From: Volodymyr Hryhoryk Date: Sun, 24 Sep 2023 16:11:51 +0300 Subject: [PATCH 5/5] added fixes --- .../service/OperationServiceTest.java | 62 ----------------- .../service/ParseServiceImplTest.java | 58 ++++++++++++++++ .../{impl => }/ReadServiceImplTest.java | 17 +++-- .../{impl => }/ReportServiceImplTest.java | 24 ++++--- .../service/WriteServiceImplTest.java | 45 +++++++++++++ .../service/impl/ParseServiceImplTest.java | 67 ------------------- .../service/impl/WriteServiceImplTest.java | 33 --------- .../strategy/OperationStrategyTest.java | 2 +- 8 files changed, 125 insertions(+), 183 deletions(-) delete mode 100644 src/test/java/core/basesyntax/service/OperationServiceTest.java create mode 100644 src/test/java/core/basesyntax/service/ParseServiceImplTest.java rename src/test/java/core/basesyntax/service/{impl => }/ReadServiceImplTest.java (64%) rename src/test/java/core/basesyntax/service/{impl => }/ReportServiceImplTest.java (54%) create mode 100644 src/test/java/core/basesyntax/service/WriteServiceImplTest.java delete mode 100644 src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java delete mode 100644 src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java diff --git a/src/test/java/core/basesyntax/service/OperationServiceTest.java b/src/test/java/core/basesyntax/service/OperationServiceTest.java deleted file mode 100644 index 250f79e8d9..0000000000 --- a/src/test/java/core/basesyntax/service/OperationServiceTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package core.basesyntax.service; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import core.basesyntax.db.Storage; -import core.basesyntax.model.FruitTransaction; -import core.basesyntax.service.impl.ParseServiceImpl; -import core.basesyntax.service.impl.ReadServiceImpl; -import core.basesyntax.service.impl.ReportServiceImpl; -import core.basesyntax.strategy.OperationHandler; -import core.basesyntax.strategy.OperationStrategy; -import core.basesyntax.strategy.impl.BalanceOperationHandler; -import core.basesyntax.strategy.impl.PurchaseOperationHandler; -import core.basesyntax.strategy.impl.ReturnOperationHandler; -import core.basesyntax.strategy.impl.SupplyOperationHandler; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class OperationServiceTest { - private static final String INPUT_PATH = "src/test/resources/input.csv"; - private static OperationStrategy operationStrategy; - private static OperationService operationService; - - @BeforeAll - static void beforeAll() { - Map operationHandlerMap = new HashMap<>(); - operationHandlerMap.put( - FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler()); - operationHandlerMap.put( - FruitTransaction.Operation.RETURN, new ReturnOperationHandler()); - operationHandlerMap.put( - FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler()); - operationHandlerMap.put( - FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); - operationStrategy = new OperationStrategy(operationHandlerMap); - } - - @AfterAll - static void afterAll() { - Storage.STORAGE.clear(); - } - - @Test - void validResult_Ok() { - String expected = "fruit,quantity" - + System.lineSeparator() - + "banana,152" - + System.lineSeparator() - + "apple,90" - + System.lineSeparator(); - List inputData = new ReadServiceImpl().readInputData(INPUT_PATH); - List parseData = new ParseServiceImpl().parseInputData(inputData); - operationService = new OperationService(operationStrategy); - operationService.processOperation(parseData); - String actual = new ReportServiceImpl().createReport(); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/core/basesyntax/service/ParseServiceImplTest.java b/src/test/java/core/basesyntax/service/ParseServiceImplTest.java new file mode 100644 index 0000000000..6f1b54722d --- /dev/null +++ b/src/test/java/core/basesyntax/service/ParseServiceImplTest.java @@ -0,0 +1,58 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.ParseServiceImpl; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ParseServiceImplTest { + private static ParseService parseService; + + @BeforeAll + static void beforeAll() { + parseService = new ParseServiceImpl(); + } + + @Test + void parseData_null_NotOk() { + List emptyList = new ArrayList<>(); + assertThrows(FruitShopException.class, () -> parseService.parseInputData(emptyList)); + } + + @Test + void parseData_correct_Ok() { + List inputData = new ArrayList<>(Arrays.asList( + "type,fruit,quantity", + "b,banana,100", + "s,banana,100", + "p,banana,100", + "r,banana,100")); + List parseData = parseService.parseInputData(inputData); + FruitTransaction balanceTransaction = parseData.get(0); + assertEquals(FruitTransaction.Operation.BALANCE, balanceTransaction.getOperation()); + assertEquals("banana", balanceTransaction.getFruit()); + assertEquals(100, balanceTransaction.getQuantity()); + + FruitTransaction supplyTransaction = parseData.get(1); + assertEquals(FruitTransaction.Operation.SUPPLY, supplyTransaction.getOperation()); + assertEquals("banana", supplyTransaction.getFruit()); + assertEquals(100, supplyTransaction.getQuantity()); + + FruitTransaction purchaseTransaction = parseData.get(2); + assertEquals(FruitTransaction.Operation.PURCHASE, purchaseTransaction.getOperation()); + assertEquals("banana", purchaseTransaction.getFruit()); + assertEquals(100, purchaseTransaction.getQuantity()); + + FruitTransaction returnTransaction = parseData.get(3); + assertEquals(FruitTransaction.Operation.RETURN, returnTransaction.getOperation()); + assertEquals("banana", returnTransaction.getFruit()); + assertEquals(100, returnTransaction.getQuantity()); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java b/src/test/java/core/basesyntax/service/ReadServiceImplTest.java similarity index 64% rename from src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java rename to src/test/java/core/basesyntax/service/ReadServiceImplTest.java index 32daf7d57f..83fb5b68fe 100644 --- a/src/test/java/core/basesyntax/service/impl/ReadServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/ReadServiceImplTest.java @@ -1,19 +1,17 @@ -package core.basesyntax.service.impl; +package core.basesyntax.service; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import core.basesyntax.exceptions.FruitShopException; -import core.basesyntax.service.ReadService; +import core.basesyntax.service.impl.ReadServiceImpl; import java.util.List; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class ReadServiceImplTest { - private static final String INVALID_PATH = "src/main/java/resources/invalid.csv"; - private static final String VALID_PATH = "src/main/java/resources/input.csv"; - private static ReadService readService; private static List expectedList; + private static ReadService readService; @BeforeAll static void beforeAll() { @@ -30,13 +28,14 @@ static void beforeAll() { } @Test - void readInputData_Ok() { - List actualList = readService.readInputData(VALID_PATH); + void readData_input_Ok() { + List actualList = readService.readInputData("src/main/java/resources/input.csv"); assertEquals(expectedList, actualList); } @Test - void invalidPath_notOk() { - assertThrows(FruitShopException.class, () -> readService.readInputData(INVALID_PATH)); + void readData_invalidPath_notOk() { + assertThrows(FruitShopException.class, () -> + readService.readInputData("src/main/java/resources/invalid.csv")); } } diff --git a/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java b/src/test/java/core/basesyntax/service/ReportServiceImplTest.java similarity index 54% rename from src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java rename to src/test/java/core/basesyntax/service/ReportServiceImplTest.java index 4ccf6e189a..64020e565d 100644 --- a/src/test/java/core/basesyntax/service/impl/ReportServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/ReportServiceImplTest.java @@ -1,16 +1,16 @@ -package core.basesyntax.service.impl; +package core.basesyntax.service; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import core.basesyntax.db.Storage; import core.basesyntax.exceptions.FruitShopException; -import core.basesyntax.service.ReportService; +import core.basesyntax.service.impl.ReportServiceImpl; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class ReportServiceImplTest { - private static final String FIRST_ROW = "fruit,quantity" + System.lineSeparator(); + //private static final String FIRST_ROW = "fruit,quantity" + System.lineSeparator(); private static ReportService reportService; @BeforeAll @@ -19,20 +19,22 @@ static void beforeAll() { } @Test - void validReport_Ok() { + void create_validReport_Ok() { Storage.STORAGE.put("banana", 20); Storage.STORAGE.put("apple", 15); - String expected = FIRST_ROW - + "banana,20" - + System.lineSeparator() - + "apple,15" - + System.lineSeparator(); + StringBuilder expected = new StringBuilder(); + expected.append("fruit,quantity") + .append(System.lineSeparator()) + .append("banana,20") + .append(System.lineSeparator()) + .append("apple,15") + .append(System.lineSeparator()); String actual = reportService.createReport(); - assertEquals(expected, actual); + assertEquals(expected.toString(), actual); } @Test - void emptyReport_NotOk() { + void create_emptyReport_NotOk() { assertThrows(FruitShopException.class, () -> reportService.createReport()); } } diff --git a/src/test/java/core/basesyntax/service/WriteServiceImplTest.java b/src/test/java/core/basesyntax/service/WriteServiceImplTest.java new file mode 100644 index 0000000000..eb1263cc16 --- /dev/null +++ b/src/test/java/core/basesyntax/service/WriteServiceImplTest.java @@ -0,0 +1,45 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.exceptions.FruitShopException; +import core.basesyntax.service.impl.WriteServiceImpl; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class WriteServiceImplTest { + private static WriterService writerService; + + @BeforeAll + static void beforeAll() { + writerService = new WriteServiceImpl(); + } + + @Test + void writeTo_invalidPath_NotOk() { + StringBuilder report = new StringBuilder(); + report.append("fruit,quantity") + .append(System.lineSeparator()) + .append("banana,152") + .append(System.lineSeparator()) + .append("apple,90") + .append(System.lineSeparator()); + assertThrows(FruitShopException.class, () -> + writerService.writeToFile("", report.toString())); + } + + @Test + void writeTo_validPath_Ok() { + StringBuilder report = new StringBuilder(); + report.append("fruit,quantity") + .append(System.lineSeparator()) + .append("banana,152") + .append(System.lineSeparator()) + .append("apple,90") + .append(System.lineSeparator()); + String actual = writerService.writeToFile( + "src/main/java/resources/output.csv", report.toString()); + assertEquals(actual, report.toString()); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java deleted file mode 100644 index 4aefd8d842..0000000000 --- a/src/test/java/core/basesyntax/service/impl/ParseServiceImplTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package core.basesyntax.service.impl; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import core.basesyntax.exceptions.FruitShopException; -import core.basesyntax.model.FruitTransaction; -import core.basesyntax.service.ParseService; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class ParseServiceImplTest { - private static final int BALANCE_INDEX = 0; - private static final int SUPPLY_INDEX = 1; - private static final int PURCHASE_INDEX = 2; - private static final int RETURN_INDEX = 3; - private static final String FRUIT = "banana"; - private static final int QUANTITY = 100; - private static ParseService parseService; - private static List inputData; - private static List emptyList; - private static List parseData; - - @BeforeAll - static void beforeAll() { - parseService = new ParseServiceImpl(); - } - - @Test - void parseNull_NotOk() { - emptyList = new ArrayList<>(); - assertThrows(FruitShopException.class, () -> parseService.parseInputData(emptyList)); - } - - @Test - void correctParseData_Ok() { - inputData = new ArrayList<>(Arrays.asList( - "type,fruit,quantity", - "b,banana,100", - "s,banana,100", - "p,banana,100", - "r,banana,100")); - parseData = parseService.parseInputData(inputData); - FruitTransaction balanceTransaction = parseData.get(BALANCE_INDEX); - assertEquals(FruitTransaction.Operation.BALANCE, balanceTransaction.getOperation()); - assertEquals(FRUIT, balanceTransaction.getFruit()); - assertEquals(QUANTITY, balanceTransaction.getQuantity()); - - FruitTransaction supplyTransaction = parseData.get(SUPPLY_INDEX); - assertEquals(FruitTransaction.Operation.SUPPLY, supplyTransaction.getOperation()); - assertEquals(FRUIT, supplyTransaction.getFruit()); - assertEquals(QUANTITY, supplyTransaction.getQuantity()); - - FruitTransaction purchaseTransaction = parseData.get(PURCHASE_INDEX); - assertEquals(FruitTransaction.Operation.PURCHASE, purchaseTransaction.getOperation()); - assertEquals(FRUIT, purchaseTransaction.getFruit()); - assertEquals(QUANTITY, purchaseTransaction.getQuantity()); - - FruitTransaction returnTransaction = parseData.get(RETURN_INDEX); - assertEquals(FruitTransaction.Operation.RETURN, returnTransaction.getOperation()); - assertEquals(FRUIT, returnTransaction.getFruit()); - assertEquals(QUANTITY, returnTransaction.getQuantity()); - } -} diff --git a/src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java deleted file mode 100644 index dddf862f44..0000000000 --- a/src/test/java/core/basesyntax/service/impl/WriteServiceImplTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package core.basesyntax.service.impl; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import core.basesyntax.exceptions.FruitShopException; -import core.basesyntax.service.WriterService; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class WriteServiceImplTest { - private static final String OUTPUT_PATH = "src/main/java/resources/output.csv"; - private static final String REPORT = "fruit,quantity\n" - + "banana,152\r\n" - + "apple,90\r\n"; - private static WriterService writerService; - - @BeforeAll - static void beforeAll() { - writerService = new WriteServiceImpl(); - } - - @Test - void invalidPath_NotOk() { - assertThrows(FruitShopException.class, () -> writerService.writeToFile("", REPORT)); - } - - @Test - void writeReport_Ok() { - String actual = writerService.writeToFile(OUTPUT_PATH, REPORT); - assertEquals(actual, REPORT); - } -} diff --git a/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java b/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java index a6c142e89d..645c1d3a36 100644 --- a/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java +++ b/src/test/java/core/basesyntax/strategy/OperationStrategyTest.java @@ -28,11 +28,11 @@ static void beforeAll() { operationHandlerMap.put( FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); operationStrategy = new OperationStrategy(operationHandlerMap); + operationHandler = new SupplyOperationHandler(); } @Test void getSuppleHandler_Ok() { - operationHandler = new SupplyOperationHandler(); assertEquals(operationHandler.getClass(), operationStrategy.get(FruitTransaction.Operation.SUPPLY).getClass()); }