From 45ccbc9b7dfe54b1012b937d336b8d6c27f5cdb4 Mon Sep 17 00:00:00 2001 From: evgenkolner Date: Sat, 23 Sep 2023 17:44:54 +0300 Subject: [PATCH 1/4] add solution1 --- src/main/java/core/basesyntax/HelloWorld.java | 7 --- src/main/java/core/basesyntax/Main.java | 49 +++++++++++++++++ .../basesyntax/model/FruitTransaction.java | 37 +++++++++++++ .../java/core/basesyntax/model/Operation.java | 27 ++++++++++ .../service/DataConverterService.java | 8 +++ .../basesyntax/service/FileWriteService.java | 5 ++ .../basesyntax/service/ProccessService.java | 8 +++ .../basesyntax/service/ReaderService.java | 7 +++ .../service/ReportCreatorService.java | 5 ++ .../impl/DataConverterServiceImpl.java | 29 ++++++++++ .../service/impl/FileReaderServiceImpl.java | 25 +++++++++ .../service/impl/FileWriteServiceImpl.java | 17 ++++++ .../service/impl/ProccesServiceImpl.java | 23 ++++++++ .../impl/ReportCreatorServiceImpl.java | 21 ++++++++ .../java/core/basesyntax/storage/Storage.java | 12 +++++ .../basesyntax/strategy/OperationService.java | 7 +++ .../strategy/impl/BalanceOperation.java | 12 +++++ .../strategy/impl/PurchaceOperation.java | 17 ++++++ .../strategy/impl/ReturnOperation.java | 14 +++++ .../strategy/impl/SupplyOperation.java | 14 +++++ src/main/resources/report.txt | 2 + src/main/resources/text.txt | 9 ++++ .../java/core/basesyntax/HelloWorldTest.java | 7 --- src/test/java/core/basesyntax/MainTest.java | 4 ++ .../service/DataConverterServiceImplTest.java | 25 +++++++++ .../service/FileReaderServiceImplTest.java | 29 ++++++++++ .../service/FileWriteServiceImplTest.java | 49 +++++++++++++++++ .../service/ProccesServiceImplTest.java | 54 +++++++++++++++++++ .../service/ReportCreatorServiceImplTest.java | 46 ++++++++++++++++ .../strategy/BalanceOperationTest.java | 36 +++++++++++++ .../strategy/PurchaceOperationTest.java | 40 ++++++++++++++ .../strategy/ReturnOperationTest.java | 30 +++++++++++ .../strategy/SupplyOperationTest.java | 30 +++++++++++ 33 files changed, 691 insertions(+), 14 deletions(-) delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/Main.java create mode 100644 src/main/java/core/basesyntax/model/FruitTransaction.java create mode 100644 src/main/java/core/basesyntax/model/Operation.java create mode 100644 src/main/java/core/basesyntax/service/DataConverterService.java create mode 100644 src/main/java/core/basesyntax/service/FileWriteService.java create mode 100644 src/main/java/core/basesyntax/service/ProccessService.java create mode 100644 src/main/java/core/basesyntax/service/ReaderService.java create mode 100644 src/main/java/core/basesyntax/service/ReportCreatorService.java create mode 100644 src/main/java/core/basesyntax/service/impl/DataConverterServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/FileReaderServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/FileWriteServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ProccesServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ReportCreatorServiceImpl.java create mode 100644 src/main/java/core/basesyntax/storage/Storage.java create mode 100644 src/main/java/core/basesyntax/strategy/OperationService.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/PurchaceOperation.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/ReturnOperation.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/SupplyOperation.java create mode 100644 src/main/resources/report.txt create mode 100644 src/main/resources/text.txt delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java create mode 100644 src/test/java/core/basesyntax/MainTest.java create mode 100644 src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/ProccesServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/service/ReportCreatorServiceImplTest.java create mode 100644 src/test/java/core/basesyntax/strategy/BalanceOperationTest.java create mode 100644 src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java create mode 100644 src/test/java/core/basesyntax/strategy/ReturnOperationTest.java create mode 100644 src/test/java/core/basesyntax/strategy/SupplyOperationTest.java 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..8e39dcf9d5 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java @@ -0,0 +1,12 @@ +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) { + 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/MainTest.java b/src/test/java/core/basesyntax/MainTest.java new file mode 100644 index 0000000000..51bbcfae5c --- /dev/null +++ b/src/test/java/core/basesyntax/MainTest.java @@ -0,0 +1,4 @@ +package core.basesyntax; + +class MainTest { +} 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..079090f2c7 --- /dev/null +++ b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java @@ -0,0 +1,25 @@ +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.Test; + +public class DataConverterServiceImplTest { + private DataConverterService 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..808d9a3706 --- /dev/null +++ b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java @@ -0,0 +1,29 @@ +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.Test; + +public class FileReaderServiceImplTest { + private ReaderService fileReaderService = new FileReaderServiceImpl(); + + @Test + void readFromNonExistingDirectory_notOk() { + assertThrows(RuntimeException.class, + () -> fileReaderService.readFromFile("src/main/resources/wrongFile.txt")); + } + + @Test + void readFromExistingDirectory_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..f1a061ff27 --- /dev/null +++ b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java @@ -0,0 +1,49 @@ +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.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import org.junit.jupiter.api.Test; + +public class FileWriteServiceImplTest { + private FileWriteService 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 (FileNotFoundException e) { + throw new RuntimeException(e); + } 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..4198e24279 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java @@ -0,0 +1,36 @@ +package core.basesyntax.strategy; + +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.BalanceOperation; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +public class BalanceOperationTest { + private OperationService 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); + } + + @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..4f535f09e9 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java @@ -0,0 +1,40 @@ +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.Test; + +public class PurchaceOperationTest { + private OperationService 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..7a165547de --- /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 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..663ec6c17c --- /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 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(); + } +} From 712c876984819c677cbeb6518d20be05490f7271 Mon Sep 17 00:00:00 2001 From: evgenkolner Date: Mon, 25 Sep 2023 08:50:24 +0300 Subject: [PATCH 2/4] add solution2 --- src/test/java/core/basesyntax/MainTest.java | 4 ---- .../basesyntax/service/DataConverterServiceImplTest.java | 2 +- .../core/basesyntax/service/FileReaderServiceImplTest.java | 6 +++--- .../core/basesyntax/service/FileWriteServiceImplTest.java | 2 +- .../java/core/basesyntax/strategy/BalanceOperationTest.java | 2 +- .../core/basesyntax/strategy/PurchaceOperationTest.java | 2 +- .../java/core/basesyntax/strategy/ReturnOperationTest.java | 2 +- .../java/core/basesyntax/strategy/SupplyOperationTest.java | 2 +- 8 files changed, 9 insertions(+), 13 deletions(-) delete mode 100644 src/test/java/core/basesyntax/MainTest.java diff --git a/src/test/java/core/basesyntax/MainTest.java b/src/test/java/core/basesyntax/MainTest.java deleted file mode 100644 index 51bbcfae5c..0000000000 --- a/src/test/java/core/basesyntax/MainTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package core.basesyntax; - -class MainTest { -} diff --git a/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java index 079090f2c7..07cb28483c 100644 --- a/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test; public class DataConverterServiceImplTest { - private DataConverterService dataConverterService = new DataConverterServiceImpl(); + private final DataConverterService dataConverterService = new DataConverterServiceImpl(); @Test void convertData_invalidOperation_notOk() { diff --git a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java index 808d9a3706..22a5af7cf4 100644 --- a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java @@ -9,16 +9,16 @@ import org.junit.jupiter.api.Test; public class FileReaderServiceImplTest { - private ReaderService fileReaderService = new FileReaderServiceImpl(); + private final ReaderService fileReaderService = new FileReaderServiceImpl(); @Test - void readFromNonExistingDirectory_notOk() { + void readFromFileFromNonExistingDirectory_notOk() { assertThrows(RuntimeException.class, () -> fileReaderService.readFromFile("src/main/resources/wrongFile.txt")); } @Test - void readFromExistingDirectory_Ok() { + void readFromFileFromExistingDirectory_Ok() { assertDoesNotThrow( () -> fileReaderService.readFromFile("src/main/resources/text.txt")); List actual = fileReaderService.readFromFile("src/main/resources/text.txt"); diff --git a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java index f1a061ff27..e92ee1bc4f 100644 --- a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java @@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test; public class FileWriteServiceImplTest { - private FileWriteService fileWriteService = new FileWriteServiceImpl(); + private final FileWriteService fileWriteService = new FileWriteServiceImpl(); @Test void writeToFile_inInvalidDirectory_notOk() { diff --git a/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java index 4198e24279..891f2e7a73 100644 --- a/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test; public class BalanceOperationTest { - private OperationService balanceOperation = new BalanceOperation(); + private final OperationService balanceOperation = new BalanceOperation(); @Test void processBalanceOperation_OK() { diff --git a/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java index 4f535f09e9..ac8e22d501 100644 --- a/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java @@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test; public class PurchaceOperationTest { - private OperationService purchaseOperation = new PurchaceOperation(); + private final OperationService purchaseOperation = new PurchaceOperation(); @Test void processPurchaseOperation_quantityLessThanAmount_notOk() { diff --git a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java index 7a165547de..3fa9ccf12b 100644 --- a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java @@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test; public class ReturnOperationTest { - private OperationService returnOperation = new ReturnOperation(); + private final OperationService returnOperation = new ReturnOperation(); @Test void processReturnOperation_ok() { diff --git a/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java b/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java index 663ec6c17c..ae9568a3ca 100644 --- a/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/SupplyOperationTest.java @@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test; public class SupplyOperationTest { - private OperationService supplyOperation = new SupplyOperation(); + private final OperationService supplyOperation = new SupplyOperation(); @Test void processReturnOperation_ok() { From 5999c1407f2b4998a6d23ca662ac966db2385ba1 Mon Sep 17 00:00:00 2001 From: evgenkolner Date: Tue, 10 Oct 2023 19:07:37 +0300 Subject: [PATCH 3/4] add additional tests and @BeforeAll --- .../strategy/impl/BalanceOperation.java | 3 +++ .../service/DataConverterServiceImplTest.java | 11 +++++++- .../service/FileReaderServiceImplTest.java | 9 ++++++- .../service/FileWriteServiceImplTest.java | 9 ++++++- .../strategy/BalanceOperationTest.java | 25 ++++++++++++++++++- .../strategy/PurchaceOperationTest.java | 9 ++++++- .../strategy/ReturnOperationTest.java | 5 +++- 7 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java b/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java index 8e39dcf9d5..11bd392bf2 100644 --- a/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java +++ b/src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java @@ -7,6 +7,9 @@ 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/test/java/core/basesyntax/service/DataConverterServiceImplTest.java b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java index 07cb28483c..5fdac2f6f7 100644 --- a/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java @@ -3,11 +3,20 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import core.basesyntax.service.impl.DataConverterServiceImpl; + +import java.io.IOException; import java.util.List; + +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class DataConverterServiceImplTest { - private final DataConverterService dataConverterService = new DataConverterServiceImpl(); + private static DataConverterService dataConverterService; + + @BeforeAll + static void beforeAll() { + dataConverterService = new DataConverterServiceImpl(); + } @Test void convertData_invalidOperation_notOk() { diff --git a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java index 22a5af7cf4..f5051d51b7 100644 --- a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java @@ -6,10 +6,17 @@ 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 final ReaderService fileReaderService = new FileReaderServiceImpl(); + private static ReaderService fileReaderService; + + @BeforeAll + static void beforeAll() { + fileReaderService = new FileReaderServiceImpl(); + } @Test void readFromFileFromNonExistingDirectory_notOk() { diff --git a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java index e92ee1bc4f..6e4527f646 100644 --- a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java @@ -9,10 +9,17 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; + +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class FileWriteServiceImplTest { - private final FileWriteService fileWriteService = new FileWriteServiceImpl(); + private static FileWriteService fileWriteService; + + @BeforeAll + static void beforeAll() { + fileWriteService = new FileWriteServiceImpl(); + } @Test void writeToFile_inInvalidDirectory_notOk() { diff --git a/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java index 891f2e7a73..616a363e2e 100644 --- a/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/BalanceOperationTest.java @@ -1,16 +1,23 @@ 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 final OperationService balanceOperation = new BalanceOperation(); + private static OperationService balanceOperation; + + @BeforeAll + static void beforeAll() { + balanceOperation = new BalanceOperation(); + } @Test void processBalanceOperation_OK() { @@ -29,6 +36,22 @@ void processBalanceOperation_OK() { 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 index ac8e22d501..c513b3da0d 100644 --- a/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java @@ -9,10 +9,17 @@ 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.BeforeEach; import org.junit.jupiter.api.Test; public class PurchaceOperationTest { - private final OperationService purchaseOperation = new PurchaceOperation(); + private static OperationService purchaseOperation; + + @BeforeAll + static void beforeAll() { + purchaseOperation = new PurchaceOperation(); + } @Test void processPurchaseOperation_quantityLessThanAmount_notOk() { diff --git a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java index 3fa9ccf12b..a447c98609 100644 --- a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java @@ -8,10 +8,13 @@ import core.basesyntax.storage.Storage; import core.basesyntax.strategy.impl.ReturnOperation; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class ReturnOperationTest { - private final OperationService returnOperation = new ReturnOperation(); + private final OperationService returnOperation = new ReturnOperation();; + @Test void processReturnOperation_ok() { From a28a59837b8383e3a9ffcd80691528071161615d Mon Sep 17 00:00:00 2001 From: evgenkolner Date: Wed, 11 Oct 2023 08:47:39 +0300 Subject: [PATCH 4/4] add additional tests and @BeforeAll --- .../basesyntax/service/DataConverterServiceImplTest.java | 3 --- .../core/basesyntax/service/FileReaderServiceImplTest.java | 1 - .../core/basesyntax/service/FileWriteServiceImplTest.java | 4 ---- .../java/core/basesyntax/strategy/PurchaceOperationTest.java | 1 - .../java/core/basesyntax/strategy/ReturnOperationTest.java | 5 +---- 5 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java index 5fdac2f6f7..663fd8c747 100644 --- a/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/DataConverterServiceImplTest.java @@ -3,10 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import core.basesyntax.service.impl.DataConverterServiceImpl; - -import java.io.IOException; import java.util.List; - import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java index f5051d51b7..a6ff0bc086 100644 --- a/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/FileReaderServiceImplTest.java @@ -6,7 +6,6 @@ import core.basesyntax.service.impl.FileReaderServiceImpl; import java.util.List; - import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java index 6e4527f646..3a6b790c5f 100644 --- a/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java +++ b/src/test/java/core/basesyntax/service/FileWriteServiceImplTest.java @@ -6,10 +6,8 @@ import core.basesyntax.service.impl.FileWriteServiceImpl; import java.io.BufferedReader; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; - import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -46,8 +44,6 @@ private String readFromTestFile(String fromFileName) { while ((line = bufferedReader.readLine()) != null) { builder.append(line).append(System.lineSeparator()); } - } catch (FileNotFoundException e) { - throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java index c513b3da0d..b082434779 100644 --- a/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/PurchaceOperationTest.java @@ -10,7 +10,6 @@ import core.basesyntax.strategy.impl.PurchaceOperation; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class PurchaceOperationTest { diff --git a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java index a447c98609..3fa9ccf12b 100644 --- a/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java +++ b/src/test/java/core/basesyntax/strategy/ReturnOperationTest.java @@ -8,13 +8,10 @@ import core.basesyntax.storage.Storage; import core.basesyntax.strategy.impl.ReturnOperation; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class ReturnOperationTest { - private final OperationService returnOperation = new ReturnOperation();; - + private final OperationService returnOperation = new ReturnOperation(); @Test void processReturnOperation_ok() {