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..1f0895e881 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,49 @@ +package core.basesyntax; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.DataConverterService; +import core.basesyntax.service.FileWriteService; +import core.basesyntax.service.ProccessService; +import core.basesyntax.service.ReaderService; +import core.basesyntax.service.ReportCreatorService; +import core.basesyntax.service.impl.DataConverterServiceImpl; +import core.basesyntax.service.impl.FileReaderServiceImpl; +import core.basesyntax.service.impl.FileWriteServiceImpl; +import core.basesyntax.service.impl.ProccesServiceImpl; +import core.basesyntax.service.impl.ReportCreatorServiceImpl; +import core.basesyntax.strategy.OperationService; +import core.basesyntax.strategy.impl.BalanceOperation; +import core.basesyntax.strategy.impl.PurchaceOperation; +import core.basesyntax.strategy.impl.ReturnOperation; +import core.basesyntax.strategy.impl.SupplyOperation; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Main { + private static final String FROM_FILE_NAME = "src/main/resources/text.txt"; + private static final String TO_FILE_NAME = "src/main/resources/report.txt"; + + public static void main(String[] args) { + ReaderService readerService = new FileReaderServiceImpl(); + DataConverterService dataConverterService = new DataConverterServiceImpl(); + ProccessService proccessService = new ProccesServiceImpl(getOperationMap()); + ReportCreatorService reportCreatorService = new ReportCreatorServiceImpl(); + FileWriteService fileWriteService = new FileWriteServiceImpl(); + List strings = readerService.readFromFile(FROM_FILE_NAME); + List fruitTransactions = dataConverterService.convertText(strings); + proccessService.proccessing(fruitTransactions); + String reportString = reportCreatorService.createReport(); + fileWriteService.writeToFile(reportString, TO_FILE_NAME); + } + + private static Map getOperationMap() { + Map operationMap = new HashMap<>(); + operationMap.put(Operation.BALANCE, new BalanceOperation()); + operationMap.put(Operation.RETURN,new ReturnOperation()); + operationMap.put(Operation.PURCHASE,new PurchaceOperation()); + operationMap.put(Operation.SUPPLY, new SupplyOperation()); + return operationMap; + } +} 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..6b93bbf0a6 --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,37 @@ +package core.basesyntax.model; + +public class FruitTransaction { + private Operation operation; + private String fruit; + private int quantity; + + public FruitTransaction(Operation operation, String fruit, int quantity) { + this.operation = operation; + this.fruit = fruit; + this.quantity = 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; + } +} diff --git a/src/main/java/core/basesyntax/model/Operation.java b/src/main/java/core/basesyntax/model/Operation.java new file mode 100644 index 0000000000..119c79293e --- /dev/null +++ b/src/main/java/core/basesyntax/model/Operation.java @@ -0,0 +1,27 @@ +package core.basesyntax.model; + +import java.util.Arrays; + +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 getOperationByCode(String code) { + return Arrays.stream(Operation.values()) + .filter(o -> o.getCode().equals(code)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Can`t find operation by code")); + } +} diff --git a/src/main/java/core/basesyntax/service/DataConverterService.java b/src/main/java/core/basesyntax/service/DataConverterService.java new file mode 100644 index 0000000000..251bb499d8 --- /dev/null +++ b/src/main/java/core/basesyntax/service/DataConverterService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface DataConverterService { + public List convertText(List strings); +} diff --git a/src/main/java/core/basesyntax/service/FileWriteService.java b/src/main/java/core/basesyntax/service/FileWriteService.java new file mode 100644 index 0000000000..a70ce2b283 --- /dev/null +++ b/src/main/java/core/basesyntax/service/FileWriteService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface FileWriteService { + void writeToFile(String report, String toFileName); +} diff --git a/src/main/java/core/basesyntax/service/ProccessService.java b/src/main/java/core/basesyntax/service/ProccessService.java new file mode 100644 index 0000000000..b29b1a6da9 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ProccessService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface ProccessService { + void proccessing(List fruitTransactionsList); +} 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..06f859ec98 --- /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 readFromFile(String pathToFile); +} diff --git a/src/main/java/core/basesyntax/service/ReportCreatorService.java b/src/main/java/core/basesyntax/service/ReportCreatorService.java new file mode 100644 index 0000000000..8ef54ba113 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportCreatorService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface ReportCreatorService { + String createReport(); +} diff --git a/src/main/java/core/basesyntax/service/impl/DataConverterServiceImpl.java b/src/main/java/core/basesyntax/service/impl/DataConverterServiceImpl.java new file mode 100644 index 0000000000..329a013361 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/DataConverterServiceImpl.java @@ -0,0 +1,29 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.DataConverterService; +import java.util.ArrayList; +import java.util.List; + +public class DataConverterServiceImpl implements DataConverterService { + private static final String SEPARATOR = ","; + private static final int INDEX_BY_TRANSACTION = 0; + private static final int INDEX_BY_FRUIT = 1; + private static final int INDEX_BY_AMOUNT = 2; + + @Override + public List convertText(List strings) { + List dataFruitTransaction = new ArrayList<>(); + for (int i = 0; i < strings.size(); i++) { + String[] data = strings.get(i).split(SEPARATOR); + dataFruitTransaction + .add(new FruitTransaction( + Operation.getOperationByCode(data[INDEX_BY_TRANSACTION]), + data[INDEX_BY_FRUIT], + Integer.parseInt(data[INDEX_BY_AMOUNT]) + )); + } + return dataFruitTransaction; + } +} diff --git a/src/main/java/core/basesyntax/service/impl/FileReaderServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FileReaderServiceImpl.java new file mode 100644 index 0000000000..9d6b4748e8 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/FileReaderServiceImpl.java @@ -0,0 +1,25 @@ +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.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class FileReaderServiceImpl implements ReaderService { + private static final int FIRST_LINE = 1; + + @Override + public List readFromFile(String pathToFile) { + List linesFromFile = new ArrayList<>(); + Path path = Paths.get(pathToFile); + try { + Files.lines(path).skip(FIRST_LINE).forEach(linesFromFile::add); + } catch (IOException e) { + throw new RuntimeException("Can`t read data from file" + pathToFile); + } + return linesFromFile; + } +} diff --git a/src/main/java/core/basesyntax/service/impl/FileWriteServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FileWriteServiceImpl.java new file mode 100644 index 0000000000..fdd90f16c1 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/FileWriteServiceImpl.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.service.FileWriteService; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + +public class FileWriteServiceImpl implements FileWriteService { + @Override + public void writeToFile(String report, String toFileName) { + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toFileName))) { + bufferedWriter.write(report); + } catch (IOException e) { + throw new RuntimeException("Can't write to file " + toFileName); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ProccesServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ProccesServiceImpl.java new file mode 100644 index 0000000000..0e4289edda --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ProccesServiceImpl.java @@ -0,0 +1,23 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.ProccessService; +import core.basesyntax.strategy.OperationService; +import java.util.List; +import java.util.Map; + +public class ProccesServiceImpl implements ProccessService { + private final Map processedStoreHanler; + + public ProccesServiceImpl(Map processedStoreHanler) { + this.processedStoreHanler = processedStoreHanler; + } + + @Override + public void proccessing(List fruitTransactionsList) { + for (FruitTransaction fruitTransaction : fruitTransactionsList) { + processedStoreHanler.get(fruitTransaction.getOperation()).doOperation(fruitTransaction); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ReportCreatorServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ReportCreatorServiceImpl.java new file mode 100644 index 0000000000..7eaba86964 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReportCreatorServiceImpl.java @@ -0,0 +1,21 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.service.ReportCreatorService; +import core.basesyntax.storage.Storage; +import java.util.Map; + +public class ReportCreatorServiceImpl implements ReportCreatorService { + private static final String FIRST_STRING = "fruit,quantity" + System.lineSeparator(); + private static final String COMMA = ","; + + @Override + public String createReport() { + StringBuilder builder = new StringBuilder(FIRST_STRING); + builder.append(System.lineSeparator()); + for (Map.Entry fruits : Storage.getStorage().entrySet()) { + builder.append(fruits.getKey()).append(COMMA).append(fruits.getValue()) + .append(System.lineSeparator()); + } + return builder.toString().trim(); + } +} diff --git a/src/main/java/core/basesyntax/storage/Storage.java b/src/main/java/core/basesyntax/storage/Storage.java new file mode 100644 index 0000000000..6d8df240ad --- /dev/null +++ b/src/main/java/core/basesyntax/storage/Storage.java @@ -0,0 +1,12 @@ +package core.basesyntax.storage; + +import java.util.HashMap; +import java.util.Map; + +public class Storage { + private static Map storage = new HashMap<>(); + + public static Map getStorage() { + return storage; + } +} diff --git a/src/main/java/core/basesyntax/strategy/OperationService.java b/src/main/java/core/basesyntax/strategy/OperationService.java new file mode 100644 index 0000000000..c24ba4ec0b --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationService.java @@ -0,0 +1,7 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; + +public interface OperationService { + void doOperation(FruitTransaction fruitTransaction); +} diff --git a/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java b/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java new file mode 100644 index 0000000000..11bd392bf2 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java @@ -0,0 +1,15 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.OperationService; + +public class BalanceOperation implements OperationService { + @Override + public void doOperation(FruitTransaction fruitTransaction) { + if (fruitTransaction.getQuantity() < 0 || fruitTransaction.getFruit() == null) { + throw new RuntimeException("Not correct value"); + } + Storage.getStorage().put(fruitTransaction.getFruit(),fruitTransaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/PurchaceOperation.java b/src/main/java/core/basesyntax/strategy/impl/PurchaceOperation.java new file mode 100644 index 0000000000..08ac943f5e --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/PurchaceOperation.java @@ -0,0 +1,17 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.OperationService; + +public class PurchaceOperation implements OperationService { + @Override + public void doOperation(FruitTransaction fruitTransaction) { + int balance = Storage.getStorage().get(fruitTransaction.getFruit()); + int purchase = balance - fruitTransaction.getQuantity(); + if (purchase < 0) { + throw new RuntimeException("Not enough quantity in shop"); + } + Storage.getStorage().put(fruitTransaction.getFruit(),purchase); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/ReturnOperation.java b/src/main/java/core/basesyntax/strategy/impl/ReturnOperation.java new file mode 100644 index 0000000000..201ae0ef08 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/ReturnOperation.java @@ -0,0 +1,14 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.OperationService; + +public class ReturnOperation implements OperationService { + @Override + public void doOperation(FruitTransaction fruitTransaction) { + int balance = Storage.getStorage().get(fruitTransaction.getFruit()); + int retOperation = balance + fruitTransaction.getQuantity(); + Storage.getStorage().put((fruitTransaction.getFruit()),retOperation); + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/SupplyOperation.java b/src/main/java/core/basesyntax/strategy/impl/SupplyOperation.java new file mode 100644 index 0000000000..cec13af9ec --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/SupplyOperation.java @@ -0,0 +1,14 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.OperationService; + +public class SupplyOperation implements OperationService { + @Override + public void doOperation(FruitTransaction fruitTransaction) { + int balance = Storage.getStorage().get(fruitTransaction.getFruit()); + int supply = balance + fruitTransaction.getQuantity(); + Storage.getStorage().put(fruitTransaction.getFruit(), supply); + } +} diff --git a/src/main/resources/report.txt b/src/main/resources/report.txt new file mode 100644 index 0000000000..6be177349d --- /dev/null +++ b/src/main/resources/report.txt @@ -0,0 +1,2 @@ +fruit,quantity +banana,40 diff --git a/src/main/resources/text.txt b/src/main/resources/text.txt new file mode 100644 index 0000000000..c771f259d3 --- /dev/null +++ b/src/main/resources/text.txt @@ -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/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/DataConverterServiceImplTest.java b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java new file mode 100644 index 0000000000..663fd8c747 --- /dev/null +++ b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java @@ -0,0 +1,31 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.service.impl.DataConverterServiceImpl; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class DataConverterServiceImplTest { + private static DataConverterService dataConverterService; + + @BeforeAll + static void beforeAll() { + dataConverterService = new DataConverterServiceImpl(); + } + + @Test + void convertData_invalidOperation_notOk() { + List data = List.of("b,banana,10", "b,apple,10", "q,banana,20"); + assertThrows(RuntimeException.class, + () -> dataConverterService.convertText(data)); + } + + @Test + void convertData_notNumericQuantity_notOk() { + List data = List.of("b,banana,10", "b,apple,10", "s,banana,2a0"); + assertThrows(RuntimeException.class, + () -> dataConverterService.convertText(data)); + } +} diff --git a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java new file mode 100644 index 0000000000..a6ff0bc086 --- /dev/null +++ b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java @@ -0,0 +1,35 @@ +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.assertThrows; + +import core.basesyntax.service.impl.FileReaderServiceImpl; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class FileReaderServiceImplTest { + private static ReaderService fileReaderService; + + @BeforeAll + static void beforeAll() { + fileReaderService = new FileReaderServiceImpl(); + } + + @Test + void readFromFileFromNonExistingDirectory_notOk() { + assertThrows(RuntimeException.class, + () -> fileReaderService.readFromFile("src/main/resources/wrongFile.txt")); + } + + @Test + void readFromFileFromExistingDirectory_Ok() { + assertDoesNotThrow( + () -> fileReaderService.readFromFile("src/main/resources/text.txt")); + List actual = fileReaderService.readFromFile("src/main/resources/text.txt"); + List expected = List.of("b,banana,20", "b,apple,100", "s,banana,100", + "p,banana,13","r,apple,10", "p,apple,20","p,banana,5","s,banana,50"); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java new file mode 100644 index 0000000000..3a6b790c5f --- /dev/null +++ b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java @@ -0,0 +1,52 @@ +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.assertThrows; + +import core.basesyntax.service.impl.FileWriteServiceImpl; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class FileWriteServiceImplTest { + private static FileWriteService fileWriteService; + + @BeforeAll + static void beforeAll() { + fileWriteService = new FileWriteServiceImpl(); + } + + @Test + void writeToFile_inInvalidDirectory_notOk() { + String report = "fruit,quantity" + System.lineSeparator() + + "banana,40"; + assertThrows(RuntimeException.class, + () -> fileWriteService.writeToFile(report, "src/resources/text.txt")); + } + + @Test + void writeToFile_validDirectory_ok() { + String report = "fruit,quantity" + System.lineSeparator() + + "banana,40" + + System.lineSeparator(); + assertDoesNotThrow( + () -> fileWriteService.writeToFile(report, "src/main/resources/report.txt")); + assertEquals(report, readFromTestFile("src/main/resources/report.txt")); + } + + private String readFromTestFile(String fromFileName) { + StringBuilder builder = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fromFileName))) { + String line; + while ((line = bufferedReader.readLine()) != null) { + builder.append(line).append(System.lineSeparator()); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return builder.toString(); + } +} diff --git a/src/test/java/core/basesyntax/service/ProccesServiceImplTest.java b/src/test/java/core/basesyntax/service/ProccesServiceImplTest.java new file mode 100644 index 0000000000..7a44088720 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ProccesServiceImplTest.java @@ -0,0 +1,54 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.impl.ProccesServiceImpl; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.impl.BalanceOperation; +import core.basesyntax.strategy.impl.PurchaceOperation; +import core.basesyntax.strategy.impl.ReturnOperation; +import core.basesyntax.strategy.impl.SupplyOperation; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class ProccesServiceImplTest { + private static ProccessService proccessService; + + @BeforeAll + static void beforeAll() { + proccessService = new ProccesServiceImpl(Map.of( + Operation.BALANCE,new BalanceOperation(), + Operation.RETURN,new ReturnOperation(), + Operation.SUPPLY,new SupplyOperation(), + Operation.PURCHASE, new PurchaceOperation() + )); + } + + @AfterEach + void tearDown() { + Storage.getStorage().clear(); + } + + @Test + void operationServiceProccess_empty_ok() { + List fruitTransactions = new ArrayList<>(); + assertDoesNotThrow(() -> + proccessService.proccessing(fruitTransactions)); + } + + @Test + void operationServiceProccess_nonEmptyList_ok() { + List transactions = List.of( + new FruitTransaction(Operation.BALANCE, "banana", 10), + new FruitTransaction(Operation.BALANCE, "apple", 20), + new FruitTransaction(Operation.SUPPLY, "banana", 10) + ); + assertDoesNotThrow(() -> proccessService.proccessing(transactions)); + } +} diff --git a/src/test/java/core/basesyntax/service/ReportCreatorServiceImplTest.java b/src/test/java/core/basesyntax/service/ReportCreatorServiceImplTest.java new file mode 100644 index 0000000000..c6fe0e9df6 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ReportCreatorServiceImplTest.java @@ -0,0 +1,46 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.service.impl.ReportCreatorServiceImpl; +import core.basesyntax.storage.Storage; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class ReportCreatorServiceImplTest { + private static ReportCreatorService reportCreatorService; + + @BeforeAll + static void beforeAll() { + reportCreatorService = new ReportCreatorServiceImpl(); + } + + @AfterEach + void tearDown() { + Storage.getStorage().clear(); + } + + @Test + void createReport_emptyData_ok() { + String expected = "fruit,quantity"; + assertEquals(expected, reportCreatorService.createReport()); + } + + @Test + void createReport_existingData_ok() { + Storage.getStorage().put("banana", 10); + Storage.getStorage().put("apple", 20); + String expectedReport = "fruit,quantity" + System.lineSeparator() + System.lineSeparator() + + "banana,10" + System.lineSeparator() + "apple,20"; + assertEquals(expectedReport, reportCreatorService.createReport()); + } + + @Test + void createReport_OneItem_Ok() { + Storage.getStorage().put("banana", 10); + String expectedReport = "fruit,quantity" + System.lineSeparator() + System.lineSeparator() + + "banana,10"; + assertEquals(expectedReport,reportCreatorService.createReport()); + } +} diff --git a/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java new file mode 100644 index 0000000000..616a363e2e --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java @@ -0,0 +1,59 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.impl.BalanceOperation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class BalanceOperationTest { + private static OperationService balanceOperation; + + @BeforeAll + static void beforeAll() { + balanceOperation = new BalanceOperation(); + } + + @Test + void processBalanceOperation_OK() { + FruitTransaction appleTransaction = new FruitTransaction( + Operation.BALANCE, "apple", 120); + FruitTransaction bananaTransaction = new FruitTransaction( + Operation.BALANCE, "banana", 100); + balanceOperation.doOperation(appleTransaction); + balanceOperation.doOperation(bananaTransaction); + int appleActual = Storage.getStorage().get("apple"); + assertEquals(120, appleActual); + int bananaActual = Storage.getStorage().get("banana"); + assertEquals(100, bananaActual); + balanceOperation.doOperation(appleTransaction); + int secondAppleActual = Storage.getStorage().get("apple"); + assertEquals(120, secondAppleActual); + } + + @Test + void processBalanceOperationWithNegativeValue_notOK() { + FruitTransaction appleTransaction = new FruitTransaction( + Operation.BALANCE, "apple", -120); + assertThrows(RuntimeException.class, + () -> balanceOperation.doOperation(appleTransaction)); + } + + @Test + void processBalanceOperationWithValueNull_notOK() { + FruitTransaction appleTransaction = new FruitTransaction( + Operation.BALANCE, null, -120); + assertThrows(RuntimeException.class, + () -> balanceOperation.doOperation(appleTransaction)); + } + + @AfterEach + void tearDown() { + Storage.getStorage().clear(); + } +} diff --git a/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java new file mode 100644 index 0000000000..b082434779 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java @@ -0,0 +1,46 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.impl.PurchaceOperation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class PurchaceOperationTest { + private static OperationService purchaseOperation; + + @BeforeAll + static void beforeAll() { + purchaseOperation = new PurchaceOperation(); + } + + @Test + void processPurchaseOperation_quantityLessThanAmount_notOk() { + Storage.getStorage().put("apple", 15); + FruitTransaction fruitTransaction = new FruitTransaction( + Operation.PURCHASE, "apple", 30); + assertThrows(RuntimeException.class, + () -> purchaseOperation.doOperation(fruitTransaction)); + } + + @Test + void processPurchaseOperation_ok() { + Storage.getStorage().put("apple", 50); + FruitTransaction fruitTransaction = new FruitTransaction( + Operation.PURCHASE, "apple", 30); + assertDoesNotThrow(() -> purchaseOperation.doOperation(fruitTransaction)); + int actual = Storage.getStorage().get("apple"); + assertEquals(20, actual); + } + + @AfterEach + void tearDown() { + Storage.getStorage().clear(); + } +} diff --git a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java new file mode 100644 index 0000000000..3fa9ccf12b --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java @@ -0,0 +1,30 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.impl.ReturnOperation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +public class ReturnOperationTest { + private final OperationService returnOperation = new ReturnOperation(); + + @Test + void processReturnOperation_ok() { + Storage.getStorage().put("apple", 100); + FruitTransaction fruitTransaction = new FruitTransaction( + Operation.RETURN, "apple", 15); + assertDoesNotThrow(() -> returnOperation.doOperation(fruitTransaction)); + int actual = Storage.getStorage().get("apple"); + assertEquals(115, actual); + } + + @AfterEach + void tearDown() { + Storage.getStorage().clear(); + } +} diff --git a/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java b/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java new file mode 100644 index 0000000000..ae9568a3ca --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java @@ -0,0 +1,30 @@ +package core.basesyntax.strategy; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.storage.Storage; +import core.basesyntax.strategy.impl.SupplyOperation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +public class SupplyOperationTest { + private final OperationService supplyOperation = new SupplyOperation(); + + @Test + void processReturnOperation_ok() { + Storage.getStorage().put("apple", 100); + FruitTransaction fruitTransaction = new FruitTransaction( + Operation.SUPPLY, "apple", 15); + assertDoesNotThrow(() -> supplyOperation.doOperation(fruitTransaction)); + int actual = Storage.getStorage().get("apple"); + assertEquals(115, actual); + } + + @AfterEach + void tearDown() { + Storage.getStorage().clear(); + } +}