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..c00f30e2d2 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,43 @@ +package core.basesyntax; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.ReaderService; +import core.basesyntax.service.WriterService; +import core.basesyntax.service.impl.FruitShopService; +import core.basesyntax.service.impl.OperationStrategyImpl; +import core.basesyntax.service.impl.ParserServiceImpl; +import core.basesyntax.service.impl.ReaderServiceImpl; +import core.basesyntax.service.impl.ReportServiceImpl; +import core.basesyntax.service.impl.WriterServiceImpl; +import core.basesyntax.service.impl.operationhadler.BalanceHandler; +import core.basesyntax.service.impl.operationhadler.PurchaseHandler; +import core.basesyntax.service.impl.operationhadler.ReturnHandler; +import core.basesyntax.service.impl.operationhadler.SupplyHandler; +import core.basesyntax.strategy.OperationHandler; +import java.util.List; +import java.util.Map; + +public class Main { + private static final String INPUT_FILE_PATH = "src/main/resources/FruitFiles.csv"; + private static final String REPORT_FILE_PATH = "src/main/resources/reportFiles/report.csv"; + + public static void main(String[] args) { + Map operationStrategyMap = + Map.of(FruitTransaction.Operation.BALANCE, new BalanceHandler(), + FruitTransaction.Operation.PURCHASE, new PurchaseHandler(), + FruitTransaction.Operation.SUPPLY, new SupplyHandler(), + FruitTransaction.Operation.RETURN, new ReturnHandler()); + ReaderService fileReader = new ReaderServiceImpl(); + List dataFromFile = fileReader.readFromCsvFile(INPUT_FILE_PATH); + ParserServiceImpl fileParser = new ParserServiceImpl(); + List operation = fileParser.parseData(dataFromFile); + FruitShopService fruitShop = + new FruitShopService(new OperationStrategyImpl( + operationStrategyMap)); + fruitShop.processOfOperations(operation); + ReportServiceImpl report = new ReportServiceImpl(); + String dataForReport = report.reportPreparation(); + WriterService writer = new WriterServiceImpl(); + writer.writeData(dataForReport, REPORT_FILE_PATH); + } +} 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..9d385af657 --- /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 fruitsStorage = new HashMap<>(); +} 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..98d04d835b --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,60 @@ +package core.basesyntax.model; + +public class FruitTransaction { + private Operation operation; + private String fruit; + private int quantity; + + public Operation getOperation() { + return operation; + } + + public void setOperation(Operation operation) { + this.operation = operation; + } + + public String getFruit() { + return fruit; + } + + public void setFruit(String fruit) { + this.fruit = fruit; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + @Override + public String toString() { + return "FruitsTransaction{" + "operation=" + operation + ", fruit='" + fruit + '\'' + ", " + + "quantity=" + quantity + '}'; + } + + 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; + } + + public static Operation fromString(String code) { + for (Operation operation : Operation.values()) { + if (operation.code.equals(code)) { + return operation; + } + } + throw new IllegalArgumentException("Unknown operation - " + code + "!!!"); + } + } +} 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..e189b03638 --- /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 parseData(List dataFromFile); +} 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..32c4d1f891 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReaderService.java @@ -0,0 +1,7 @@ +package core.basesyntax.service; + +import java.util.List; + +public interface ReaderService { + List readFromCsvFile(String path); +} 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..bdf288c6da --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface ReportService { + String reportPreparation(); +} 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..b7dce4fba3 --- /dev/null +++ b/src/main/java/core/basesyntax/service/WriterService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface WriterService { + void writeData(String data, String outputPath); +} diff --git a/src/main/java/core/basesyntax/service/impl/FruitShopService.java b/src/main/java/core/basesyntax/service/impl/FruitShopService.java new file mode 100644 index 0000000000..682d96f8a6 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/FruitShopService.java @@ -0,0 +1,20 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationStrategy; +import java.util.List; + +public class FruitShopService { + private final OperationStrategy strategy; + + public FruitShopService(OperationStrategy strategy) { + this.strategy = strategy; + } + + public void processOfOperations(List operations) { + for (FruitTransaction operation : operations) { + strategy.getOperationHandler(operation.getOperation()) + .handleTransaction(operation); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/OperationStrategyImpl.java b/src/main/java/core/basesyntax/service/impl/OperationStrategyImpl.java new file mode 100644 index 0000000000..a10c57a0f4 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/OperationStrategyImpl.java @@ -0,0 +1,19 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.OperationStrategy; +import java.util.Map; + +public class OperationStrategyImpl implements OperationStrategy { + private final Map fruitsMap; + + public OperationStrategyImpl(Map fruitsMap) { + this.fruitsMap = fruitsMap; + } + + @Override + public OperationHandler getOperationHandler(FruitTransaction.Operation operationType) { + return fruitsMap.get(operationType); + } +} 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..c92e77797e --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java @@ -0,0 +1,30 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.ParserService; +import java.util.ArrayList; +import java.util.List; + +public class ParserServiceImpl implements ParserService { + private static final String SEPARATOR = ","; + 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 TITLE_INDEX = 0; + + @Override + public List parseData(List dataFromFile) { + dataFromFile.remove(TITLE_INDEX); + List operationList = new ArrayList<>(); + for (String record : dataFromFile) { + String[] array = record.split(SEPARATOR); + FruitTransaction operation = new FruitTransaction(); + operation.setOperation( + FruitTransaction.Operation.fromString(array[OPERATION_INDEX])); + operation.setFruit(array[FRUIT_INDEX]); + operation.setQuantity(Integer.parseInt(array[QUANTITY_INDEX])); + operationList.add(operation); + } + return operationList; + } +} 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..b5343a903e --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java @@ -0,0 +1,18 @@ +package core.basesyntax.service.impl; + +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 { + @Override + public List readFromCsvFile(String filePath) { + try { + return Files.readAllLines(Path.of(filePath)); + } catch (IOException e) { + throw new RuntimeException("Can't read file " + filePath, e); + } + } +} 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..d2bdeb5a61 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java @@ -0,0 +1,21 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.service.ReportService; +import java.util.Map; + +public class ReportServiceImpl implements ReportService { + private static final String TITLE = "fruit,quantity\n"; + private static final String DELIMITER = ","; + + @Override + public String reportPreparation() { + Map reportMap = Storage.fruitsStorage; + StringBuilder reportString = new StringBuilder(TITLE); + for (Map.Entry entry : reportMap.entrySet()) { + reportString.append(entry.getKey()).append(DELIMITER).append(entry.getValue()) + .append(System.lineSeparator()); + } + return reportString.toString(); + } +} 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..338e578d76 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.service.WriterService; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class WriterServiceImpl implements WriterService { + @Override + public void writeData(String data, String outputPath) { + try { + Files.writeString(Path.of(outputPath), data); + } catch (IOException e) { + throw new RuntimeException("Can't write to file " + outputPath, e); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/operationhadler/BalanceHandler.java b/src/main/java/core/basesyntax/service/impl/operationhadler/BalanceHandler.java new file mode 100644 index 0000000000..3a6bf3a7f3 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/operationhadler/BalanceHandler.java @@ -0,0 +1,12 @@ +package core.basesyntax.service.impl.operationhadler; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class BalanceHandler implements OperationHandler { + @Override + public void handleTransaction(FruitTransaction transaction) { + Storage.fruitsStorage.put(transaction.getFruit(), transaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/operationhadler/PurchaseHandler.java b/src/main/java/core/basesyntax/service/impl/operationhadler/PurchaseHandler.java new file mode 100644 index 0000000000..5ead1c1114 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/operationhadler/PurchaseHandler.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.impl.operationhadler; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class PurchaseHandler implements OperationHandler { + @Override + public void handleTransaction(FruitTransaction transaction) { + int result = Storage.fruitsStorage.get(transaction.getFruit()) + - transaction.getQuantity(); + if (result < 0) { + throw new RuntimeException("Not enough fruits!"); + } + Storage.fruitsStorage.put(transaction.getFruit(), result); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/operationhadler/ReturnHandler.java b/src/main/java/core/basesyntax/service/impl/operationhadler/ReturnHandler.java new file mode 100644 index 0000000000..bd808d6e01 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/operationhadler/ReturnHandler.java @@ -0,0 +1,14 @@ +package core.basesyntax.service.impl.operationhadler; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class ReturnHandler implements OperationHandler { + @Override + public void handleTransaction(FruitTransaction transaction) { + int currentValue = Storage.fruitsStorage.get(transaction.getFruit()); + Storage.fruitsStorage.put(transaction.getFruit(), currentValue + + transaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/operationhadler/SupplyHandler.java b/src/main/java/core/basesyntax/service/impl/operationhadler/SupplyHandler.java new file mode 100644 index 0000000000..504a8df07a --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/operationhadler/SupplyHandler.java @@ -0,0 +1,14 @@ +package core.basesyntax.service.impl.operationhadler; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationHandler; + +public class SupplyHandler implements OperationHandler { + @Override + public void handleTransaction(FruitTransaction transaction) { + int currentQuantity = Storage.fruitsStorage.get(transaction.getFruit()); + Storage.fruitsStorage.put(transaction.getFruit(), currentQuantity + + transaction.getQuantity()); + } +} 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..741cda93d4 --- /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 handleTransaction(FruitTransaction transaction); +} 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..146e093f16 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationStrategy.java @@ -0,0 +1,7 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; + +public interface OperationStrategy { + OperationHandler getOperationHandler(FruitTransaction.Operation operation); +} diff --git a/src/main/resources/EmptyFile.csv b/src/main/resources/EmptyFile.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/resources/FruitFiles.csv b/src/main/resources/FruitFiles.csv new file mode 100644 index 0000000000..5ebf26646f --- /dev/null +++ b/src/main/resources/FruitFiles.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 diff --git a/src/main/resources/reportFiles/report.csv b/src/main/resources/reportFiles/report.csv new file mode 100644 index 0000000000..6158e4419c --- /dev/null +++ b/src/main/resources/reportFiles/report.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/OperationStrategyImplTest.java b/src/test/java/core/basesyntax/service/OperationStrategyImplTest.java new file mode 100644 index 0000000000..9a6072a01f --- /dev/null +++ b/src/test/java/core/basesyntax/service/OperationStrategyImplTest.java @@ -0,0 +1,24 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.model.FruitTransaction; +import org.junit.jupiter.api.Test; + +public class OperationStrategyImplTest { + @Test + public void fromString_getCorrectOperation_Ok() { + FruitTransaction.Operation actual = FruitTransaction.Operation.fromString("b"); + FruitTransaction.Operation expected = FruitTransaction.Operation.BALANCE; + assertEquals(expected, actual); + } + + @Test + public void fromString_getIncorrectOperationType_notOk() { + assertThrows(RuntimeException.class, () -> { + FruitTransaction.Operation.fromString( + "invalid_value"); + }); + } +} diff --git a/src/test/java/core/basesyntax/service/ParserServiceImplTest.java b/src/test/java/core/basesyntax/service/ParserServiceImplTest.java new file mode 100644 index 0000000000..9e5d051abe --- /dev/null +++ b/src/test/java/core/basesyntax/service/ParserServiceImplTest.java @@ -0,0 +1,51 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.ParserServiceImpl; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ParserServiceImplTest { + private List expected; + private ParserServiceImpl fileParser; + + @BeforeEach + public void setUp() { + fileParser = new ParserServiceImpl(); + expected = new ArrayList<>(); + expected.add("type,fruit,quantity"); + expected.add("b,banana,20"); + expected.add("b,apple,100"); + expected.add("s,banana,100"); + expected.add("p,banana,13"); + expected.add("r,apple,10"); + expected.add("p,apple,20"); + expected.add("p,banana,5"); + expected.add("s,banana,50"); + } + + @Test + public void parseData_deleteTitleFromList_Ok() { + int expectSize = 8; + List actual = fileParser.parseData(expected); + assertEquals(expectSize, actual.size()); + } + + @Test + public void parseData_correctLineParsing_Ok() { + List actual = fileParser.parseData(expected); + assertEquals(FruitTransaction.Operation.BALANCE, actual.get(0).getOperation()); + assertEquals("banana", actual.get(0).getFruit()); + assertEquals(20, actual.get(0).getQuantity()); + assertEquals(FruitTransaction.Operation.BALANCE, actual.get(1).getOperation()); + assertEquals("apple", actual.get(1).getFruit()); + assertEquals(100, actual.get(1).getQuantity()); + assertEquals(FruitTransaction.Operation.SUPPLY, actual.get(7).getOperation()); + assertEquals("banana", actual.get(7).getFruit()); + assertEquals(50, actual.get(7).getQuantity()); + } +} diff --git a/src/test/java/core/basesyntax/service/ReaderServiceImplTest.java b/src/test/java/core/basesyntax/service/ReaderServiceImplTest.java new file mode 100644 index 0000000000..44ce4a99b0 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ReaderServiceImplTest.java @@ -0,0 +1,70 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.service.impl.ReaderServiceImpl; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ReaderServiceImplTest { + private static final String VALID_FILE_PATH = "src/test/resources/FruitFiles.csv"; + private static final String WRONG_FILE_PATH = "src/test/resources/WrongPath.csv"; + private static final String EMPTY_FILE = "src/test/resources/EmptyFile.csv"; + private List expected; + private ReaderServiceImpl fileReader; + + @BeforeEach + public void setUp() { + fileReader = new ReaderServiceImpl(); + expected = new ArrayList<>(); + expected.add("type,fruit,quantity"); + expected.add("b,banana,20"); + expected.add("b,apple,100"); + expected.add("s,banana,100"); + expected.add("p,banana,13"); + expected.add("r,apple,10"); + expected.add("p,apple,20"); + expected.add("p,banana,5"); + expected.add("s,banana,50"); + } + + @Test + public void readFromCsvFile_validPath_Ok() { + List actual = fileReader.readFromCsvFile(VALID_FILE_PATH); + assertArrayEquals(expected.toArray(), actual.toArray()); + } + + @Test + void readFromCsvFile_notNull_Ok() { + List fromCsvFile = fileReader.readFromCsvFile(VALID_FILE_PATH); + assertNotNull(fromCsvFile); + } + + @Test + void readFromCsvFile_fileNotEmpty_Ok() { + List fromCsvFile = fileReader.readFromCsvFile(VALID_FILE_PATH); + assertFalse(fromCsvFile.isEmpty()); + } + + @Test + void readFromCsvFile_invalidPath_notOk() { + assertThrows(RuntimeException.class, () -> { + fileReader.readFromCsvFile(WRONG_FILE_PATH); + }); + } + + @Test + public void readFromCsvFile_emptyFile_notOk() { + assertThrows(RuntimeException.class, () -> { + fileReader.readFromCsvFile(EMPTY_FILE); + }); + } +} + + + diff --git a/src/test/java/core/basesyntax/service/ReportServiceImplTest.java b/src/test/java/core/basesyntax/service/ReportServiceImplTest.java new file mode 100644 index 0000000000..716707e92b --- /dev/null +++ b/src/test/java/core/basesyntax/service/ReportServiceImplTest.java @@ -0,0 +1,60 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.FruitShopService; +import core.basesyntax.service.impl.OperationStrategyImpl; +import core.basesyntax.service.impl.ParserServiceImpl; +import core.basesyntax.service.impl.ReportServiceImpl; +import core.basesyntax.service.impl.operationhadler.BalanceHandler; +import core.basesyntax.service.impl.operationhadler.PurchaseHandler; +import core.basesyntax.service.impl.operationhadler.ReturnHandler; +import core.basesyntax.service.impl.operationhadler.SupplyHandler; +import core.basesyntax.strategy.OperationHandler; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ReportServiceImplTest { + private static final String EXPECT = + "fruit,quantity\n" + "banana,152" + System.lineSeparator() + + "apple," + "90" + System.lineSeparator(); + private List expected; + private Map operationStrategy; + private ParserServiceImpl fileParser; + private ReportServiceImpl reportService; + private FruitShopService fruitShop; + + @BeforeEach + public void setUp() { + fileParser = new ParserServiceImpl(); + reportService = new ReportServiceImpl(); + expected = new ArrayList<>(); + expected.add("type,fruit,quantity"); + expected.add("b,banana,20"); + expected.add("b,apple,100"); + expected.add("s,banana,100"); + expected.add("p,banana,13"); + expected.add("r,apple,10"); + expected.add("p,apple,20"); + expected.add("p,banana,5"); + expected.add("s,banana,50"); + operationStrategy = Map.of(FruitTransaction.Operation.BALANCE, new BalanceHandler(), + FruitTransaction.Operation.PURCHASE, new PurchaseHandler(), + FruitTransaction.Operation.SUPPLY, new SupplyHandler(), + FruitTransaction.Operation.RETURN, new ReturnHandler()); + OperationStrategyImpl operationStrategyImpl = new OperationStrategyImpl(operationStrategy); + fruitShop = new FruitShopService(operationStrategyImpl); + } + + @Test + public void reportPreparation_correctStringRecords_ok() { + List transactions = fileParser.parseData(expected); + fruitShop.processOfOperations(transactions); + String actual = reportService.reportPreparation(); + assertEquals(EXPECT, actual); + } +} diff --git a/src/test/java/core/basesyntax/service/WriterServiceImplTest.java b/src/test/java/core/basesyntax/service/WriterServiceImplTest.java new file mode 100644 index 0000000000..74f8bded1d --- /dev/null +++ b/src/test/java/core/basesyntax/service/WriterServiceImplTest.java @@ -0,0 +1,50 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import core.basesyntax.service.impl.WriterServiceImpl; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class WriterServiceImplTest { + private static final String DATA = + "fruit,quantity\n" + "banana,152" + System.lineSeparator() + + "apple," + "90" + System.lineSeparator(); + private WriterServiceImpl writerService; + private Path tempDir; + private String outputPath; + + @BeforeEach + void setUp() throws IOException { + writerService = new WriterServiceImpl(); + tempDir = Files.createTempDirectory("tempDir"); + outputPath = tempDir.resolve("tempReport.csv").toString(); + } + + @Test + public void writeData_FileCreated_Ok() { + writerService.writeData(DATA, outputPath); + assertTrue(Files.exists(Path.of(outputPath))); + } + + @Test + public void writeData_CorrectContent_Ok() { + assertDoesNotThrow(() -> { + writerService.writeData(DATA, outputPath); + String fileContent = Files.readString(Path.of(outputPath)); + assertEquals(DATA, fileContent); + }, "Can't read \"tempReport.csv\""); + } + + @Test + void writeData_notExistFile_notOk() { + Assert.assertThrows(RuntimeException.class, () -> writerService.writeData("Test Data", + "nonexistent/test.csv")); + } +} diff --git a/src/test/java/core/basesyntax/service/operationhandler/BalanceHandlerTest.java b/src/test/java/core/basesyntax/service/operationhandler/BalanceHandlerTest.java new file mode 100644 index 0000000000..eac46affd2 --- /dev/null +++ b/src/test/java/core/basesyntax/service/operationhandler/BalanceHandlerTest.java @@ -0,0 +1,41 @@ +package core.basesyntax.service.operationhandler; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.operationhadler.BalanceHandler; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class BalanceHandlerTest { + private Storage fruitStorage; + private String testFruit = "banana"; + private OperationHandler balanceHandler; + private FruitTransaction transaction; + + @BeforeEach + public void setUp() { + fruitStorage = new Storage(); + Storage.fruitsStorage.put("banana", 20); + balanceHandler = new BalanceHandler(); + transaction = new FruitTransaction(); + } + + @AfterEach + public void afterEachTest() { + Storage.fruitsStorage.clear(); + } + + @Test + public void correctBalanceQuantity_ok() { + transaction.setFruit(testFruit); + transaction.setQuantity(20); + balanceHandler.handleTransaction(transaction); + int expectedQuantity = 20; + int actualQuantity = Storage.fruitsStorage.get(testFruit); + assertEquals(expectedQuantity, actualQuantity); + } +} diff --git a/src/test/java/core/basesyntax/service/operationhandler/PurchaseHandlerTest.java b/src/test/java/core/basesyntax/service/operationhandler/PurchaseHandlerTest.java new file mode 100644 index 0000000000..906465bf0b --- /dev/null +++ b/src/test/java/core/basesyntax/service/operationhandler/PurchaseHandlerTest.java @@ -0,0 +1,54 @@ +package core.basesyntax.service.operationhandler; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.operationhadler.PurchaseHandler; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class PurchaseHandlerTest { + private Storage fruitStorage; + private String testFruit = "banana"; + private OperationHandler purchaseHandler; + private FruitTransaction transaction; + + @BeforeEach + public void setUp() { + fruitStorage = new Storage(); + Storage.fruitsStorage.put("banana", 20); + purchaseHandler = new PurchaseHandler(); + transaction = new FruitTransaction(); + } + + @AfterEach + public void afterEachTest() { + Storage.fruitsStorage.clear(); + } + + @Test + public void removeCorrectPurchaseQuantity_ok() { + transaction.setFruit(testFruit); + transaction.setQuantity(7); + purchaseHandler.handleTransaction(transaction); + int expectedQuantity = 13; + int actualQuantity = Storage.fruitsStorage.get(testFruit); + assertEquals(expectedQuantity, actualQuantity); + } + + @Test + public void incorrectPurchaseQuantity_notOk() { + int quantityMoreThanBalance = 21; + OperationHandler purchaseHandler = new PurchaseHandler(); + FruitTransaction transaction = new FruitTransaction(); + transaction.setFruit(testFruit); + transaction.setQuantity(quantityMoreThanBalance); + assertThrows(RuntimeException.class, () -> { + purchaseHandler.handleTransaction(transaction); + }); + } +} diff --git a/src/test/java/core/basesyntax/service/operationhandler/ReturnHandlerTest.java b/src/test/java/core/basesyntax/service/operationhandler/ReturnHandlerTest.java new file mode 100644 index 0000000000..3c2af6401d --- /dev/null +++ b/src/test/java/core/basesyntax/service/operationhandler/ReturnHandlerTest.java @@ -0,0 +1,41 @@ +package core.basesyntax.service.operationhandler; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.operationhadler.ReturnHandler; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ReturnHandlerTest { + private Storage fruitStorage; + private String testFruit = "banana"; + private OperationHandler returnHandler; + private FruitTransaction transaction; + + @BeforeEach + public void setUp() { + fruitStorage = new Storage(); + Storage.fruitsStorage.put("banana", 20); + returnHandler = new ReturnHandler(); + transaction = new FruitTransaction(); + } + + @AfterEach + public void afterEachTest() { + Storage.fruitsStorage.clear(); + } + + @Test + public void correctQuantityAfterReturnOperation_ok() { + transaction.setFruit(testFruit); + transaction.setQuantity(2); + returnHandler.handleTransaction(transaction); + int expectedQuantity = 22; + int actualQuantity = Storage.fruitsStorage.get(testFruit); + assertEquals(expectedQuantity, actualQuantity); + } +} diff --git a/src/test/java/core/basesyntax/service/operationhandler/SupplyHandlerTest.java b/src/test/java/core/basesyntax/service/operationhandler/SupplyHandlerTest.java new file mode 100644 index 0000000000..10b63afc61 --- /dev/null +++ b/src/test/java/core/basesyntax/service/operationhandler/SupplyHandlerTest.java @@ -0,0 +1,41 @@ +package core.basesyntax.service.operationhandler; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.impl.operationhadler.SupplyHandler; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class SupplyHandlerTest { + private Storage fruitStorage; + private String testFruit = "banana"; + private OperationHandler supplyHandler; + private FruitTransaction transaction; + + @BeforeEach + public void setUp() { + fruitStorage = new Storage(); + Storage.fruitsStorage.put("banana", 20); + supplyHandler = new SupplyHandler(); + transaction = new FruitTransaction(); + } + + @AfterEach + public void afterEachTest() { + Storage.fruitsStorage.clear(); + } + + @Test + public void correctSupplyQuantity_ok() { + transaction.setFruit(testFruit); + transaction.setQuantity(100); + supplyHandler.handleTransaction(transaction); + int expectedQuantity = 120; + int actualQuantity = Storage.fruitsStorage.get(testFruit); + assertEquals(expectedQuantity, actualQuantity); + } +} diff --git a/src/test/resources/FruitFiles.csv b/src/test/resources/FruitFiles.csv new file mode 100644 index 0000000000..5ebf26646f --- /dev/null +++ b/src/test/resources/FruitFiles.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 diff --git a/src/test/resources/reportFiles/report.csv b/src/test/resources/reportFiles/report.csv new file mode 100644 index 0000000000..6158e4419c --- /dev/null +++ b/src/test/resources/reportFiles/report.csv @@ -0,0 +1,3 @@ +fruit,quantity +banana,152 +apple,90