diff --git a/fruitsWithLessThenZeroQuantity.csv b/fruitsWithLessThenZeroQuantity.csv new file mode 100644 index 0000000000..1a2d1a51cc --- /dev/null +++ b/fruitsWithLessThenZeroQuantity.csv @@ -0,0 +1,3 @@ +type,fruit,quantity +b,apple,-10 +b,banana,-10 \ No newline at end of file diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index ec71c8da6a..b40cdae55c 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -1,29 +1,39 @@ package core.basesyntax; -import core.basesyntax.service.CreateReportService; -import core.basesyntax.service.DataConvertService; -import core.basesyntax.service.DataProcessService; -import core.basesyntax.service.ReadFromCsvFileService; -import core.basesyntax.service.WriteToCsvFileService; -import core.basesyntax.service.impl.CreateReportServiceImpl; -import core.basesyntax.service.impl.DataConvertServiceImpl; -import core.basesyntax.service.impl.DataProcessServiceImpl; -import core.basesyntax.service.impl.ReadFromCsvFileServiceImpl; -import core.basesyntax.service.impl.WriteToCsvFileServiceImpl; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.*; +import core.basesyntax.service.impl.*; +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; +import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; +import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; +import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; + +import java.util.List; +import java.util.Map; public class Main { - private static final String FIRST_TEST_CSV = "fruits1.csv"; - private static final String SECOND_TEST_CSV = "fruits2.csv"; private static final String THIRD_TEST_CSV = "fruits3.csv"; + private static final String REPORT_FILENAME = "report.csv"; + private static final Map operationPicker = + Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), + Operation.PURCHASE, new PurchaseOperationHandlerImpl(), + Operation.RETURN, new ReturnOperationHandlerImpl(), + Operation.SUPPLY, new SupplyOperationHandlerImpl()); public static void main(String[] args) { ReadFromCsvFileService reader = new ReadFromCsvFileServiceImpl(); DataConvertService convertor = new DataConvertServiceImpl(); - DataProcessService processor = new DataProcessServiceImpl(); + DataProcessService processor = new DataProcessServiceImpl(operationPicker); CreateReportService reportCreator = new CreateReportServiceImpl(); - WriteToCsvFileService writer = new WriteToCsvFileServiceImpl(); + WriteToCsvFileService writer = new WriteToCsvFileServiceImpl(REPORT_FILENAME); + + List convertedFruitsFromFile = + convertor.convert(reader.readFile(THIRD_TEST_CSV)); + + processor.processFruits(convertedFruitsFromFile); - processor.processFruits(convertor.convert(reader.readFile(THIRD_TEST_CSV))); writer.write(reportCreator.createReport()); } } diff --git a/src/main/java/core/basesyntax/db/dao/StorageDaoImpl.java b/src/main/java/core/basesyntax/db/dao/StorageDaoImpl.java index 5819e6cb40..8e81d5ea3e 100644 --- a/src/main/java/core/basesyntax/db/dao/StorageDaoImpl.java +++ b/src/main/java/core/basesyntax/db/dao/StorageDaoImpl.java @@ -1,6 +1,7 @@ package core.basesyntax.db.dao; import core.basesyntax.db.Storage; + import java.util.Map; public class StorageDaoImpl implements StorageDao { diff --git a/src/main/java/core/basesyntax/model/Operation.java b/src/main/java/core/basesyntax/model/Operation.java index f938e8c82d..a80e9ab993 100644 --- a/src/main/java/core/basesyntax/model/Operation.java +++ b/src/main/java/core/basesyntax/model/Operation.java @@ -11,13 +11,17 @@ public enum Operation { this.code = code; } - public static Operation findByCode(String code) { - for (Operation operation : values()) { - if (operation.code.equals(code)) { - return operation; - } - } + public String getCode() { + return code; + } - throw new RuntimeException("Code is not exist " + code); + public static Operation findByCode(String code) { + return switch (code) { + case "b" -> BALANCE; + case "s" -> SUPPLY; + case "p" -> PURCHASE; + case "r" -> RETURN; + default -> throw new RuntimeException("Code is not exist " + code); + }; } } diff --git a/src/main/java/core/basesyntax/service/DataConvertService.java b/src/main/java/core/basesyntax/service/DataConvertService.java index 8f0caf4ff9..7783268075 100644 --- a/src/main/java/core/basesyntax/service/DataConvertService.java +++ b/src/main/java/core/basesyntax/service/DataConvertService.java @@ -1,6 +1,7 @@ package core.basesyntax.service; import core.basesyntax.model.FruitTransaction; + import java.util.List; public interface DataConvertService { diff --git a/src/main/java/core/basesyntax/service/DataProcessService.java b/src/main/java/core/basesyntax/service/DataProcessService.java index aad34827bb..494fc36c31 100644 --- a/src/main/java/core/basesyntax/service/DataProcessService.java +++ b/src/main/java/core/basesyntax/service/DataProcessService.java @@ -1,6 +1,7 @@ package core.basesyntax.service; import core.basesyntax.model.FruitTransaction; + import java.util.List; public interface DataProcessService { diff --git a/src/main/java/core/basesyntax/service/impl/CreateReportServiceImpl.java b/src/main/java/core/basesyntax/service/impl/CreateReportServiceImpl.java index 0c908749da..60b166a925 100644 --- a/src/main/java/core/basesyntax/service/impl/CreateReportServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/CreateReportServiceImpl.java @@ -3,6 +3,7 @@ import core.basesyntax.db.dao.StorageDao; import core.basesyntax.db.dao.StorageDaoImpl; import core.basesyntax.service.CreateReportService; + import java.util.Map; public class CreateReportServiceImpl implements CreateReportService { diff --git a/src/main/java/core/basesyntax/service/impl/DataConvertServiceImpl.java b/src/main/java/core/basesyntax/service/impl/DataConvertServiceImpl.java index 73b48e7fa4..7c05ecb227 100644 --- a/src/main/java/core/basesyntax/service/impl/DataConvertServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/DataConvertServiceImpl.java @@ -3,6 +3,7 @@ import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.service.DataConvertService; + import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/core/basesyntax/service/impl/DataProcessServiceImpl.java b/src/main/java/core/basesyntax/service/impl/DataProcessServiceImpl.java index 0f9ce7f41e..cfca1d2377 100644 --- a/src/main/java/core/basesyntax/service/impl/DataProcessServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/DataProcessServiceImpl.java @@ -1,33 +1,49 @@ package core.basesyntax.service.impl; +import core.basesyntax.db.dao.StorageDao; +import core.basesyntax.db.dao.StorageDaoImpl; import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.service.DataProcessService; import core.basesyntax.strategy.FruitOperationStrategy; import core.basesyntax.strategy.OperationHandler; -import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; import core.basesyntax.strategy.impl.FruitOperationStrategyImpl; -import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; -import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; -import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; + import java.util.List; import java.util.Map; public class DataProcessServiceImpl implements DataProcessService { + private final Map operationPicker; + private final StorageDao storageDao; + + public DataProcessServiceImpl(Map operationPicker) { + this.operationPicker = operationPicker; + storageDao = new StorageDaoImpl(); + } + @Override public void processFruits(List fruits) { - Map operationPicker = Map.of(Operation.BALANCE, - new BalanceOperationHandlerImpl(), - Operation.PURCHASE, - new PurchaseOperationHandlerImpl(), - Operation.RETURN, - new ReturnOperationHandlerImpl(), - Operation.SUPPLY, - new SupplyOperationHandlerImpl()); + List fruitNames = fruits.stream() + .map(FruitTransaction::getFruitName) + .distinct() + .toList(); FruitOperationStrategy fruitOperationStrategy = new FruitOperationStrategyImpl(operationPicker); - fruitOperationStrategy.countFruits(fruits); + for (String fruitName : fruitNames) { + int fruitCount = 0; + for (FruitTransaction fruitTransaction : fruits) { + if (fruitTransaction.getFruitName().equals(fruitName)) { + fruitCount += fruitOperationStrategy.countFruits(fruitTransaction); + } + } + + if (fruitCount < 0) { + throw new RuntimeException("Cannot be less then 0: " + fruitCount); + } + + storageDao.add(fruitName, fruitCount); + } } } diff --git a/src/main/java/core/basesyntax/service/impl/ReadFromCsvFileServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReadFromCsvFileServiceImpl.java index d9003dca93..a6954691b1 100644 --- a/src/main/java/core/basesyntax/service/impl/ReadFromCsvFileServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ReadFromCsvFileServiceImpl.java @@ -1,6 +1,7 @@ package core.basesyntax.service.impl; import core.basesyntax.service.ReadFromCsvFileService; + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -8,7 +9,6 @@ import java.util.List; public class ReadFromCsvFileServiceImpl implements ReadFromCsvFileService { - @Override public List readFile(String fromFileName) { List fruits = new ArrayList<>(); diff --git a/src/main/java/core/basesyntax/service/impl/WriteToCsvFileServiceImpl.java b/src/main/java/core/basesyntax/service/impl/WriteToCsvFileServiceImpl.java index 64ba88967a..bd39eb76bb 100644 --- a/src/main/java/core/basesyntax/service/impl/WriteToCsvFileServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/WriteToCsvFileServiceImpl.java @@ -1,24 +1,29 @@ package core.basesyntax.service.impl; import core.basesyntax.service.WriteToCsvFileService; + import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteToCsvFileServiceImpl implements WriteToCsvFileService { - private static final String FILE_NAME = "report.csv"; + private final String fileName; + + public WriteToCsvFileServiceImpl(String fileName) { + this.fileName = fileName; + } @Override public void write(String report) { - File file = new File(FILE_NAME); + File file = new File(fileName); try { file.createNewFile(); } catch (IOException e) { throw new RuntimeException("Cannot create a file ", e); } - try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) { writer.write(report); } catch (IOException e) { throw new RuntimeException("Cannot write data to file", e); diff --git a/src/main/java/core/basesyntax/strategy/FruitOperationStrategy.java b/src/main/java/core/basesyntax/strategy/FruitOperationStrategy.java index 9a09d8fc7a..e1bede394e 100644 --- a/src/main/java/core/basesyntax/strategy/FruitOperationStrategy.java +++ b/src/main/java/core/basesyntax/strategy/FruitOperationStrategy.java @@ -1,8 +1,7 @@ package core.basesyntax.strategy; import core.basesyntax.model.FruitTransaction; -import java.util.List; public interface FruitOperationStrategy { - void countFruits(List fruitTransactions); + int countFruits(FruitTransaction fruitTransactions); } diff --git a/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImpl.java b/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImpl.java index c5c03623bf..5497648571 100644 --- a/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImpl.java +++ b/src/main/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImpl.java @@ -6,6 +6,12 @@ public class BalanceOperationHandlerImpl implements OperationHandler { @Override public int count(FruitTransaction fruitTransaction) { - return fruitTransaction.getQuantity(); + int quantity = fruitTransaction.getQuantity(); + + if (quantity < 0) { + throw new RuntimeException("Quantity cannot be less then zero: " + quantity); + } + + return quantity; } } diff --git a/src/main/java/core/basesyntax/strategy/impl/FruitOperationStrategyImpl.java b/src/main/java/core/basesyntax/strategy/impl/FruitOperationStrategyImpl.java index a730d86d6a..6abbab7e85 100644 --- a/src/main/java/core/basesyntax/strategy/impl/FruitOperationStrategyImpl.java +++ b/src/main/java/core/basesyntax/strategy/impl/FruitOperationStrategyImpl.java @@ -1,43 +1,22 @@ package core.basesyntax.strategy.impl; -import core.basesyntax.db.dao.StorageDao; -import core.basesyntax.db.dao.StorageDaoImpl; import core.basesyntax.model.FruitTransaction; import core.basesyntax.model.Operation; import core.basesyntax.strategy.FruitOperationStrategy; import core.basesyntax.strategy.OperationHandler; -import java.util.List; + import java.util.Map; public class FruitOperationStrategyImpl implements FruitOperationStrategy { - private Map handlerOperationMap; - private final StorageDao storageDao = new StorageDaoImpl(); + private final Map handlerOperationMap; public FruitOperationStrategyImpl(Map handlerOperationMap) { this.handlerOperationMap = handlerOperationMap; } @Override - public void countFruits(List fruitTransactions) { - List fruitNames = fruitTransactions.stream() - .map(FruitTransaction::getFruitName) - .distinct() - .toList(); - - for (String fruitName : fruitNames) { - int fruitCount = 0; - for (FruitTransaction fruitTransaction : fruitTransactions) { - if (fruitTransaction.getFruitName().equals(fruitName)) { - fruitCount += handlerOperationMap.get(fruitTransaction.getOperation()) - .count(fruitTransaction); - } - } - - if (fruitCount < 0) { - throw new RuntimeException("Cannot be less then 0: " + fruitCount); - } - - storageDao.add(fruitName, fruitCount); - } + public int countFruits(FruitTransaction fruitTransaction) { + return handlerOperationMap.get(fruitTransaction.getOperation()) + .count(fruitTransaction); } } diff --git a/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImpl.java b/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImpl.java index 5b111d9e5b..e68dbf369c 100644 --- a/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImpl.java +++ b/src/main/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImpl.java @@ -6,6 +6,12 @@ public class PurchaseOperationHandlerImpl implements OperationHandler { @Override public int count(FruitTransaction fruitTransaction) { - return -fruitTransaction.getQuantity(); + int quantity = fruitTransaction.getQuantity(); + + if (quantity < 0) { + throw new RuntimeException("Quantity cannot be less then zero: " + quantity); + } + + return -quantity; } } diff --git a/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImpl.java b/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImpl.java index 813693e53f..7c405b700f 100644 --- a/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImpl.java +++ b/src/main/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImpl.java @@ -6,6 +6,12 @@ public class ReturnOperationHandlerImpl implements OperationHandler { @Override public int count(FruitTransaction fruitTransaction) { - return fruitTransaction.getQuantity(); + int quantity = fruitTransaction.getQuantity(); + + if (quantity < 0) { + throw new RuntimeException("Quantity cannot be less then zero: " + quantity); + } + + return quantity; } } diff --git a/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImpl.java b/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImpl.java index 063ab51315..ad0f3faae7 100644 --- a/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImpl.java +++ b/src/main/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImpl.java @@ -6,6 +6,12 @@ public class SupplyOperationHandlerImpl implements OperationHandler { @Override public int count(FruitTransaction fruitTransaction) { - return fruitTransaction.getQuantity(); + int quantity = fruitTransaction.getQuantity(); + + if (quantity < 0) { + throw new RuntimeException("Quantity cannot be less then zero: " + quantity); + } + + return quantity; } } diff --git a/src/test/java/core/basesyntax/db/dao/StorageDaoImplTest.java b/src/test/java/core/basesyntax/db/dao/StorageDaoImplTest.java index 805c61b94b..33bd702212 100644 --- a/src/test/java/core/basesyntax/db/dao/StorageDaoImplTest.java +++ b/src/test/java/core/basesyntax/db/dao/StorageDaoImplTest.java @@ -13,6 +13,8 @@ class StorageDaoImplTest { private static final String BANANA_NAME = "banana"; private static final Integer APPLE_COUNT = 15; private static final Integer BANANA_COUNT = 20; + private static final Map expected = Map.of(APPLE_NAME, + APPLE_COUNT, BANANA_NAME, BANANA_COUNT); private static StorageDao storageDao; @BeforeAll @@ -22,9 +24,6 @@ static void beforeAll() { @Test void storageDaoAdd_twoFruits_Ok() { - Map expected = Map.of(APPLE_NAME, APPLE_COUNT, - BANANA_NAME, BANANA_COUNT); - storageDao.add(APPLE_NAME, APPLE_COUNT); storageDao.add(BANANA_NAME, BANANA_COUNT); diff --git a/src/test/java/core/basesyntax/service/impl/CreateReportServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/CreateReportServiceImplTest.java index f5dd850b25..083b921ee3 100644 --- a/src/test/java/core/basesyntax/service/impl/CreateReportServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/CreateReportServiceImplTest.java @@ -5,11 +5,18 @@ import core.basesyntax.db.Storage; import core.basesyntax.db.dao.StorageDao; import core.basesyntax.db.dao.StorageDaoImpl; +import core.basesyntax.model.Operation; import core.basesyntax.service.CreateReportService; import core.basesyntax.service.DataConvertService; import core.basesyntax.service.DataProcessService; import core.basesyntax.service.ReadFromCsvFileService; import java.util.Map; + +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; +import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; +import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; +import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -25,12 +32,17 @@ class CreateReportServiceImplTest { private static DataConvertService dataConverter; private static ReadFromCsvFileService csvReader; private static DataProcessService dataProcessor; + private static final Map operationPicker = + Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), + Operation.PURCHASE, new PurchaseOperationHandlerImpl(), + Operation.RETURN, new ReturnOperationHandlerImpl(), + Operation.SUPPLY, new SupplyOperationHandlerImpl()); @BeforeAll static void beforeAll() { storageDao = new StorageDaoImpl(); dataConverter = new DataConvertServiceImpl(); - dataProcessor = new DataProcessServiceImpl(); + dataProcessor = new DataProcessServiceImpl(operationPicker); } @BeforeEach diff --git a/src/test/java/core/basesyntax/service/impl/DataConvertServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/DataConvertServiceImplTest.java index e9ef6cd4f7..3793b1e950 100644 --- a/src/test/java/core/basesyntax/service/impl/DataConvertServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/DataConvertServiceImplTest.java @@ -10,6 +10,13 @@ import core.basesyntax.service.DataProcessService; import core.basesyntax.service.ReadFromCsvFileService; import java.util.List; +import java.util.Map; + +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; +import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; +import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; +import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -24,6 +31,11 @@ class DataConvertServiceImplTest { private static DataConvertService dataConverter; private static DataProcessService dataProcessor; private static ReadFromCsvFileService csvReader; + private static final Map operationPicker = + Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), + Operation.PURCHASE, new PurchaseOperationHandlerImpl(), + Operation.RETURN, new ReturnOperationHandlerImpl(), + Operation.SUPPLY, new SupplyOperationHandlerImpl()); @BeforeEach void setUp() { @@ -33,7 +45,7 @@ void setUp() { @BeforeAll static void beforeAll() { dataConverter = new DataConvertServiceImpl(); - dataProcessor = new DataProcessServiceImpl(); + dataProcessor = new DataProcessServiceImpl(operationPicker); } @Test diff --git a/src/test/java/core/basesyntax/service/impl/DataProcessServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/DataProcessServiceImplTest.java new file mode 100644 index 0000000000..e7114c3d89 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/DataProcessServiceImplTest.java @@ -0,0 +1,90 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.Operation; +import core.basesyntax.service.DataConvertService; +import core.basesyntax.service.DataProcessService; +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; +import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; +import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; +import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class DataProcessServiceImplTest { + private static final String FIRST_FILENAME = "fruits1.csv"; + private static final String SECOND_FILENAME = "fruits2.csv"; + private static final String THIRD_FILENAME = "fruits3.csv"; + private static final String LESS_THEN_ZERO_FILENAME = "fruitsWithLessThenZeroQuantity.csv"; + private static final String ZERO_QUANTITY_FILENAME = "zeroFruits.csv"; + private static final Map operationPicker = + Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), + Operation.PURCHASE, new PurchaseOperationHandlerImpl(), + Operation.RETURN, new ReturnOperationHandlerImpl(), + Operation.SUPPLY, new SupplyOperationHandlerImpl()); + private static ReadFromCsvFileServiceImpl reader; + private static DataConvertService convertor; + private static DataProcessService processor; + + @BeforeAll + static void beforeAll() { + reader = new ReadFromCsvFileServiceImpl(); + convertor = new DataConvertServiceImpl(); + processor = new DataProcessServiceImpl(operationPicker); + } + + @Test + void dataProcessor_twoFruits_Ok() { + processor.processFruits(convertor.convert(reader.readFile(FIRST_FILENAME))); + Map expected = Map.of("banana", 152, "apple", 90); + Map actual = Storage.fruitsCount; + + assertEquals(expected, actual); + } + + @Test + void dataProcessor_threeFruits_Ok() { + processor.processFruits(convertor.convert(reader.readFile(SECOND_FILENAME))); + Map expected = Map.of("pineapple", 111, + "apple", 117, "banana", 60); + Map actual = Storage.fruitsCount; + + assertEquals(expected, actual); + } + + @Test + void dataProcessor_fourFruits_Ok() { + processor.processFruits(convertor.convert(reader.readFile(THIRD_FILENAME))); + Map expected = Map.of("melon", 10, + "apple", 100, "banana", 10, "grape", 150); + Map actual = Storage.fruitsCount; + + assertEquals(expected, actual); + } + + @Test + void dataProcessor_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + processor.processFruits(convertor.convert(reader.readFile(LESS_THEN_ZERO_FILENAME)))); + } + + @Test + void dataProcessor_zeroValues_Ok() { + processor.processFruits(convertor.convert(reader.readFile(ZERO_QUANTITY_FILENAME))); + Map expected = Map.of("apple", 0, "banana", 0); + Map actual = Storage.fruitsCount; + + assertEquals(expected, actual); + } + + @AfterEach + void tearDown() { + Storage.fruitsCount.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/WriteToCsvFileServiceImplTest.java b/src/test/java/core/basesyntax/service/impl/WriteToCsvFileServiceImplTest.java index 1b45d4f153..9be72197c5 100644 --- a/src/test/java/core/basesyntax/service/impl/WriteToCsvFileServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/WriteToCsvFileServiceImplTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import core.basesyntax.db.Storage; +import core.basesyntax.model.Operation; import core.basesyntax.service.CreateReportService; import core.basesyntax.service.DataConvertService; import core.basesyntax.service.DataProcessService; @@ -11,6 +12,13 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.util.Map; + +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; +import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; +import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; +import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -27,18 +35,23 @@ class WriteToCsvFileServiceImplTest { private static ReadFromCsvFileService csvReader; private static DataProcessService dataProcessor; private static WriteToCsvFileService csvWriter; + private static final Map operationPicker = + Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), + Operation.PURCHASE, new PurchaseOperationHandlerImpl(), + Operation.RETURN, new ReturnOperationHandlerImpl(), + Operation.SUPPLY, new SupplyOperationHandlerImpl()); @BeforeAll static void beforeAll() { dataConverter = new DataConvertServiceImpl(); - dataProcessor = new DataProcessServiceImpl(); + dataProcessor = new DataProcessServiceImpl(operationPicker); } @BeforeEach void setUp() { reportCreator = new CreateReportServiceImpl(); csvReader = new ReadFromCsvFileServiceImpl(); - csvWriter = new WriteToCsvFileServiceImpl(); + csvWriter = new WriteToCsvFileServiceImpl(REPORT_FILENAME); builder = new StringBuilder(); } diff --git a/src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImplTest.java b/src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImplTest.java new file mode 100644 index 0000000000..8be3aad3c3 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/BalanceOperationHandlerImplTest.java @@ -0,0 +1,49 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BalanceOperationHandlerImplTest { + private static final int NORMAL_VALUE = 10; + private static final int ZERO_VALUE = 0; + private static final int LESS_THEN_VALUE = -5; + private static final FruitTransaction NORMAL_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, LESS_THEN_VALUE); + private static OperationHandler balanceHandler; + + @BeforeAll + static void beforeAll() { + balanceHandler = new BalanceOperationHandlerImpl(); + } + + @Test + void balanceOperationHandler_validValues_Ok() { + int expected = NORMAL_VALUE; + int actual = balanceHandler.count(NORMAL_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void balanceOperationHandler_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = balanceHandler.count(ZERO_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void balanceOperationHandler_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + balanceHandler.count(LESS_THEN_ZERO_TRANSACTION)); + } +} \ No newline at end of file diff --git a/src/test/java/core/basesyntax/strategy/impl/FruitOperationStrategyImplTest.java b/src/test/java/core/basesyntax/strategy/impl/FruitOperationStrategyImplTest.java new file mode 100644 index 0000000000..aa5179aa3b --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/FruitOperationStrategyImplTest.java @@ -0,0 +1,141 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.strategy.FruitOperationStrategy; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class FruitOperationStrategyImplTest { + private static final int NORMAL_VALUE = 10; + private static final int ZERO_VALUE = 0; + private static final int LESS_THEN_VALUE = -5; + private static final FruitTransaction NORMAL_VALUE_BALANCE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_BALANCE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_BALANCE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, LESS_THEN_VALUE); + private static final FruitTransaction NORMAL_VALUE_SUPPLY_TRANSACTION = + new FruitTransaction(Operation.SUPPLY, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_SUPPLY_TRANSACTION = + new FruitTransaction(Operation.SUPPLY, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_SUPPLY_TRANSACTION = + new FruitTransaction(Operation.SUPPLY, null, LESS_THEN_VALUE); + private static final FruitTransaction NORMAL_VALUE_PURCHASE_TRANSACTION = + new FruitTransaction(Operation.PURCHASE, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_PURCHASE_TRANSACTION = + new FruitTransaction(Operation.PURCHASE, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_PURCHASE_TRANSACTION = + new FruitTransaction(Operation.PURCHASE, null, LESS_THEN_VALUE); + private static final FruitTransaction NORMAL_VALUE_RETURN_TRANSACTION = + new FruitTransaction(Operation.RETURN, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_RETURN_TRANSACTION = + new FruitTransaction(Operation.RETURN, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_RETURN_TRANSACTION = + new FruitTransaction(Operation.RETURN, null, LESS_THEN_VALUE); + private static final Map operationPicker = + Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), + Operation.PURCHASE, new PurchaseOperationHandlerImpl(), + Operation.RETURN, new ReturnOperationHandlerImpl(), + Operation.SUPPLY, new SupplyOperationHandlerImpl()); + private static FruitOperationStrategy fruitStrategy; + + @BeforeAll + static void beforeAll() { + fruitStrategy = new FruitOperationStrategyImpl(operationPicker); + } + + @Test + void strategyBalance_normalValue_Ok() { + int expected = NORMAL_VALUE; + int actual = fruitStrategy.countFruits(NORMAL_VALUE_BALANCE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategyBalance_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = fruitStrategy.countFruits(ZERO_VALUE_BALANCE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategyBalance_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + fruitStrategy.countFruits(LESS_THEN_ZERO_BALANCE_TRANSACTION)); + } + + @Test + void strategySupply_normalValue_Ok() { + int expected = NORMAL_VALUE; + int actual = fruitStrategy.countFruits(NORMAL_VALUE_SUPPLY_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategySupply_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = fruitStrategy.countFruits(ZERO_VALUE_SUPPLY_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategySupply_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + fruitStrategy.countFruits(LESS_THEN_ZERO_SUPPLY_TRANSACTION)); + } + + @Test + void strategyPurchase_normalValue_Ok() { + int expected = -NORMAL_VALUE; + int actual = fruitStrategy.countFruits(NORMAL_VALUE_PURCHASE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategyPurchase_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = fruitStrategy.countFruits(ZERO_VALUE_PURCHASE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategyPurchase_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + fruitStrategy.countFruits(LESS_THEN_ZERO_PURCHASE_TRANSACTION)); + } + + @Test + void strategyReturn_normalValue_Ok() { + int expected = NORMAL_VALUE; + int actual = fruitStrategy.countFruits(NORMAL_VALUE_RETURN_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategyReturn_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = fruitStrategy.countFruits(ZERO_VALUE_RETURN_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void strategyReturn_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + fruitStrategy.countFruits(LESS_THEN_ZERO_RETURN_TRANSACTION)); + } +} \ No newline at end of file diff --git a/src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImplTest.java b/src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImplTest.java new file mode 100644 index 0000000000..3e89469190 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/PurchaseOperationHandlerImplTest.java @@ -0,0 +1,49 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PurchaseOperationHandlerImplTest { + private static final int NORMAL_VALUE = 10; + private static final int ZERO_VALUE = 0; + private static final int LESS_THEN_VALUE = -5; + private static final FruitTransaction NORMAL_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, LESS_THEN_VALUE); + private static OperationHandler purchaseHandler; + + @BeforeAll + static void beforeAll() { + purchaseHandler = new PurchaseOperationHandlerImpl(); + } + + @Test + void purchaseOperationHandler_validValues_Ok() { + int expected = -NORMAL_VALUE; + int actual = purchaseHandler.count(NORMAL_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void purchaseOperationHandler_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = purchaseHandler.count(ZERO_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void purchaseOperationHandler_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + purchaseHandler.count(LESS_THEN_ZERO_TRANSACTION)); + } +} \ No newline at end of file diff --git a/src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImplTest.java b/src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImplTest.java new file mode 100644 index 0000000000..fa2806c2aa --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/ReturnOperationHandlerImplTest.java @@ -0,0 +1,49 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ReturnOperationHandlerImplTest { + private static final int NORMAL_VALUE = 10; + private static final int ZERO_VALUE = 0; + private static final int LESS_THEN_VALUE = -5; + private static final FruitTransaction NORMAL_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, LESS_THEN_VALUE); + private static OperationHandler returnHandler; + + @BeforeAll + static void beforeAll() { + returnHandler = new BalanceOperationHandlerImpl(); + } + + @Test + void balanceOperationHandler_validValues_Ok() { + int expected = NORMAL_VALUE; + int actual = returnHandler.count(NORMAL_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void balanceOperationHandler_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = returnHandler.count(ZERO_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void balanceOperationHandler_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + returnHandler.count(LESS_THEN_ZERO_TRANSACTION)); + } +} \ No newline at end of file diff --git a/src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImplTest.java b/src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImplTest.java new file mode 100644 index 0000000000..e4287f4c0d --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/SupplyOperationHandlerImplTest.java @@ -0,0 +1,49 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.strategy.OperationHandler; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SupplyOperationHandlerImplTest { + private static final int NORMAL_VALUE = 10; + private static final int ZERO_VALUE = 0; + private static final int LESS_THEN_VALUE = -5; + private static final FruitTransaction NORMAL_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, NORMAL_VALUE); + private static final FruitTransaction ZERO_VALUE_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, ZERO_VALUE); + private static final FruitTransaction LESS_THEN_ZERO_TRANSACTION = + new FruitTransaction(Operation.BALANCE, null, LESS_THEN_VALUE); + private static OperationHandler supplyHandler; + + @BeforeAll + static void beforeAll() { + supplyHandler = new SupplyOperationHandlerImpl(); + } + + @Test + void balanceOperationHandler_validValues_Ok() { + int expected = NORMAL_VALUE; + int actual = supplyHandler.count(NORMAL_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void balanceOperationHandler_zeroValue_Ok() { + int expected = ZERO_VALUE; + int actual = supplyHandler.count(ZERO_VALUE_TRANSACTION); + + assertEquals(expected, actual); + } + + @Test + void balanceOperationHandler_lessThenZero_NotOk() { + assertThrows(RuntimeException.class, () -> + supplyHandler.count(LESS_THEN_ZERO_TRANSACTION)); + } +} \ No newline at end of file diff --git a/zeroFruits.csv b/zeroFruits.csv new file mode 100644 index 0000000000..dfb0f54c71 --- /dev/null +++ b/zeroFruits.csv @@ -0,0 +1,9 @@ +type,fruit,quantity +b,apple,0 +b,banana,0 +s,apple,0 +s,banana,0 +r,apple,0 +r,banana,0 +p,apple,0 +p,banana,0 \ No newline at end of file