From cdcc324cb1f675c807005bd49041ece15ae1e507 Mon Sep 17 00:00:00 2001 From: Dima Semeniuk Date: Tue, 24 Oct 2023 20:02:24 +0300 Subject: [PATCH 1/5] covered solution with tests --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 6148 bytes src/main/.DS_Store | Bin 0 -> 6148 bytes src/main/java/.DS_Store | Bin 0 -> 6148 bytes src/main/java/core/.DS_Store | Bin 0 -> 6148 bytes src/main/java/core/basesyntax/.DS_Store | Bin 0 -> 6148 bytes src/main/java/core/basesyntax/HelloWorld.java | 7 -- src/main/java/core/basesyntax/Main.java | 49 +++++++++ .../java/core/basesyntax/dao/StorageDao.java | 14 +++ .../core/basesyntax/dao/StorageDaoImpl.java | 32 ++++++ src/main/java/core/basesyntax/db/Storage.java | 9 ++ .../java/core/basesyntax/model/Fruit.java | 41 ++++++++ .../basesyntax/model/FruitTransaction.java | 31 ++++++ .../java/core/basesyntax/model/Operation.java | 28 +++++ .../basesyntax/service/DataProcessing.java | 7 ++ .../core/basesyntax/service/DataReader.java | 8 ++ .../basesyntax/service/OperationHandler.java | 16 +++ .../basesyntax/service/ReportCreator.java | 5 + .../java/core/basesyntax/service/Writer.java | 5 + .../handler/impl/BalanceOperationHandler.java | 22 ++++ .../impl/PurchaseOperationHandler.java | 36 +++++++ .../handler/impl/ReturnOperationHandler.java | 28 +++++ .../handler/impl/SupplyOperationHandler.java | 28 +++++ .../basesyntax/service/impl/CsvWriter.java | 17 +++ .../service/impl/DataProcessingImpl.java | 47 +++++++++ .../service/impl/DataReaderFromCsv.java | 22 ++++ .../service/impl/FruitShopServiceImpl.java | 30 ++++++ .../service/impl/ReportCreatorImpl.java | 24 +++++ .../strategy/OperationStrategy.java | 8 ++ .../strategy/impl/OperationStrategyImpl.java | 23 ++++ src/main/resources/data.csv | 9 ++ src/main/resources/report.csv | 3 + src/test/.DS_Store | Bin 0 -> 6148 bytes .../java/core/basesyntax/HelloWorldTest.java | 7 -- .../basesyntax/dao/StorageDaoImplTest.java | 71 +++++++++++++ .../impl/BalanceOperationHandlerTest.java | 74 +++++++++++++ .../impl/PurchaseOperationHandlerTest.java | 98 ++++++++++++++++++ .../impl/ReturnOperationHandlerTest.java | 76 ++++++++++++++ .../impl/SupplyOperationHandlerTest.java | 76 ++++++++++++++ .../service/impl/CsvWriterTest.java | 47 +++++++++ .../service/impl/DataProcessingImplTest.java | 83 +++++++++++++++ .../service/impl/DataReaderFromCsvTest.java | 51 +++++++++ .../service/impl/ReportCreatorImplTest.java | 49 +++++++++ .../impl/OperationStrategyImplTest.java | 72 +++++++++++++ src/test/resources/.DS_Store | Bin 0 -> 6148 bytes src/test/resources/data.csv | 9 ++ src/test/resources/report.csv | 3 + 47 files changed, 1251 insertions(+), 14 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/main/.DS_Store create mode 100644 src/main/java/.DS_Store create mode 100644 src/main/java/core/.DS_Store create mode 100644 src/main/java/core/basesyntax/.DS_Store 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/dao/StorageDao.java create mode 100644 src/main/java/core/basesyntax/dao/StorageDaoImpl.java create mode 100644 src/main/java/core/basesyntax/db/Storage.java create mode 100644 src/main/java/core/basesyntax/model/Fruit.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/DataProcessing.java create mode 100644 src/main/java/core/basesyntax/service/DataReader.java create mode 100644 src/main/java/core/basesyntax/service/OperationHandler.java create mode 100644 src/main/java/core/basesyntax/service/ReportCreator.java create mode 100644 src/main/java/core/basesyntax/service/Writer.java create mode 100644 src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java create mode 100644 src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java create mode 100644 src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java create mode 100644 src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java create mode 100644 src/main/java/core/basesyntax/service/impl/CsvWriter.java create mode 100644 src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java create mode 100644 src/main/java/core/basesyntax/service/impl/FruitShopServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java create mode 100644 src/main/java/core/basesyntax/strategy/OperationStrategy.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java create mode 100644 src/main/resources/data.csv create mode 100644 src/main/resources/report.csv create mode 100644 src/test/.DS_Store delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java create mode 100644 src/test/java/core/basesyntax/dao/StorageDaoImplTest.java create mode 100644 src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/CsvWriterTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java create mode 100644 src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java create mode 100644 src/test/java/core/basesyntax/strategy/impl/OperationStrategyImplTest.java create mode 100644 src/test/resources/.DS_Store create mode 100644 src/test/resources/data.csv create mode 100644 src/test/resources/report.csv diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..16f848fcce0423ae57598958404de587c339d2c6 GIT binary patch literal 6148 zcmeHK!D`z;5S_K%SRyU>&_XT&L9W4Z6B8)CsE%{Up_eGMhc>v9WfBotYb?1g7=zBq z$Mo7S$-nf}zS-TBSWd~Uly(PZ-t6qmO8ZvQYKchnX7Nj+77;m6#+nQBfN($Sn$%3B z2^4CKD@rM$oC<2MW!u2t$N;b1QyS7GjbQJu-hCQV4v z7;F6nHmziTtz^4&O2-(ZPj7*3R8+QZ5j z7($mL?wLH_V5w1;lQ4%5VXiF92}P)@V|-7glkhcKYZ6GA_B2L N0*VG}ECYX(fu9`+hh+c& literal 0 HcmV?d00001 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8877e45dd44061f594040ab1d37c88cbfaa61cfa GIT binary patch literal 6148 zcmeHK!EVz)5S?vJ>rf%(08)=Dag88NOjN|hP0N8RMsNTWY!ZQm>#bs^IYg0sE+50S zU((-!H@nkHN#Oz#plC;$eY3MOYt55(*F!{VI2#`jwTUQ%G6r`s{6u)2^_FajX9uWw zj~9jcm=z|&oHz918So7J-wg2C4d|3o%BfsCzeiYm^xcXr;wCPeS7&n0FSD~Euj+31 zN^NZicXoEe-Ec2_pL{m+q)zKuHB6^(dG^YfBD=QJ>~%hwkJ|T-OO}ChLX-fxpb_@65lv<>7?wJv5$ONNr$4O@le`^lZqYs(KFx~ zSY=?_P22qbKgSvVMgPB=i|u@xcZ08)>ZdX1o=X%vZz3GIPvjo<(%wUdY%jJJxN<`6~l89t_0 zzNEiX-t2B7(!w>O)r>UzW@l$??^|nkhltd0KIsy*iO7R8)_gR75guo~BQ@t~02M#u zZKlUbrW5q}fg=Au1H5((`bsffQhaOuUSsU}jS(}EOaUdCxYotcDmGDXOz4{1Kp zi=m$9lfvXicEIyJf7ISvE_;W)PVn+*u<8WMLAT!tdWZeh%J-h_zj$>z`j&mq^$$zE z!hdPxiNiJggt3{=Wi-n(oqvIk<9(XZlnT-`hc#yV7Bg3f`jZ)SMT7xiKp41347k0~ zYu%%7mQNK1gn`{K!23f0Wegow7R}ay#*_d+zD62>ZLa>nfE~clVPz2>h_b0bo2p!i zp7>k?@f`PNSa8jW~X@vn{V4H!u?Y8;+Klyk4znvsK zVL%voPz@Id4u NU}%s=7}zNTzX31MVyFNB literal 0 HcmV?d00001 diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..44a62117e45da6be16da50ec5549decd6f45e362 GIT binary patch literal 6148 zcmeHK!AiqG5Z!H~O({YT3OxqA7Hw@U1uwDIgI6PZP^n21G#ImzwB}F>IqMJkCH{`i z>~0ELJqaRZ24>%8c4ou84Lcde7#~diHe+?hm1M%;5zD(l(eM`$Z?O8NQ}Km zc<_uHlKg)L@azmWW`ZRw_;P-Su(tCqMJBTF^Ro0&pYyBt7=`Ji)q3Nlvaz;aF)L=x zymszH=1knlG#$9fB|e=A5qZVj_0Gc}8`U?DL_BfBI7nnb=m+3(brHtC$Od8>`>Bj| z)Ph+xtD|~jHtTk}O>1wzKW|#Ie!JJSx}Dy9UNyJ2cMncS_t8Tvo)z^RzDp?^8Vh)V z$s(U8Cy65w--501FZ>9J0b+m{SVac(U1!!-F*(YK7$63I#Q@$97AT^vF;^(B4j5z$ z0BC?&3e4rNKcIyMKwD$35G){Eg#xNju3HSQI|r`9VcpjLxk44rxO_A0qu*^6&wHsn?zvM^`h7zpeT~h@Q3g|Ywdc7NOgw2O`;|d1#re%1I-i0$uy3W!?Zu@Hb3r}yo}SlKd^+f z*GJ09QJVM6sAGnCudv+64Frv#(QU4d$L+0lOMm{dGimAZ&gOPYx3{(@lSZ)eY5nW> z?$7L3Ztm>tN#T37w(M~V_XwWU<2oATnaM9OKIKk1f}(&ZAPT%S1^hl1EWNdf%X~!v zQQ*HT!25%SGlm{3hj!~gV@m*F9o^Ot^Y0>aw8zk6H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 operationHandlerMap = new HashMap<>(); + operationHandlerMap.put(Operation.BALANCE, + new BalanceOperationHandler(storageDao)); + operationHandlerMap.put(Operation.SUPPLY, + new SupplyOperationHandler(storageDao)); + operationHandlerMap.put(Operation.PURCHASE, + new PurchaseOperationHandler(storageDao)); + operationHandlerMap.put(Operation.RETURN, + new ReturnOperationHandler(storageDao)); + OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlerMap); + DataReader dataReader = new DataReaderFromCsv(); + DataProcessing dataProcessing = new DataProcessingImpl(operationStrategy); + ReportCreator reportCreator = new ReportCreatorImpl(storageDao); + Writer writer = new CsvWriter(); + FruitShopServiceImpl fruitShopService = new FruitShopServiceImpl(dataReader, + dataProcessing, reportCreator, writer); + fruitShopService.createDailyReport(FILE_FROM, FILE_TO); + } +} diff --git a/src/main/java/core/basesyntax/dao/StorageDao.java b/src/main/java/core/basesyntax/dao/StorageDao.java new file mode 100644 index 0000000000..4ef360f96c --- /dev/null +++ b/src/main/java/core/basesyntax/dao/StorageDao.java @@ -0,0 +1,14 @@ +package core.basesyntax.dao; + +import core.basesyntax.model.Fruit; +import java.util.Map; + +public interface StorageDao { + void add(Fruit fruit, Integer amount); + + Map.Entry get(String name); + + boolean isInStorage(String name); + + Map getALl(); +} diff --git a/src/main/java/core/basesyntax/dao/StorageDaoImpl.java b/src/main/java/core/basesyntax/dao/StorageDaoImpl.java new file mode 100644 index 0000000000..d4261fe032 --- /dev/null +++ b/src/main/java/core/basesyntax/dao/StorageDaoImpl.java @@ -0,0 +1,32 @@ +package core.basesyntax.dao; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import java.util.HashMap; +import java.util.Map; + +public class StorageDaoImpl implements StorageDao { + @Override + public void add(Fruit fruit, Integer amount) { + Storage.fruits.put(fruit, amount); + } + + @Override + public Map.Entry get(String name) { + for (Map.Entry entry : Storage.fruits.entrySet()) { + if (entry.getKey().getName().equals(name)) { + return entry; + } + } + return null; + } + + public boolean isInStorage(String name) { + return (get(name) != null); + } + + @Override + public Map getALl() { + return new HashMap<>(Storage.fruits); + } +} diff --git a/src/main/java/core/basesyntax/db/Storage.java b/src/main/java/core/basesyntax/db/Storage.java new file mode 100644 index 0000000000..94c4799415 --- /dev/null +++ b/src/main/java/core/basesyntax/db/Storage.java @@ -0,0 +1,9 @@ +package core.basesyntax.db; + +import core.basesyntax.model.Fruit; +import java.util.HashMap; +import java.util.Map; + +public class Storage { + public static final Map fruits = new HashMap<>(); +} diff --git a/src/main/java/core/basesyntax/model/Fruit.java b/src/main/java/core/basesyntax/model/Fruit.java new file mode 100644 index 0000000000..1ccbedec9a --- /dev/null +++ b/src/main/java/core/basesyntax/model/Fruit.java @@ -0,0 +1,41 @@ +package core.basesyntax.model; + +import java.util.Objects; + +public class Fruit { + private String name; + + public Fruit(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Fruit fruit = (Fruit) o; + return Objects.equals(name, fruit.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + + @Override + public String toString() { + return "Fruit{" + "name='" + name + '\'' + '}'; + } +} 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..b04c9cf1b6 --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,31 @@ +package core.basesyntax.model; + +public class FruitTransaction { + private Operation operation; + private String fruitName; + private int amount; + + public Operation getOperation() { + return operation; + } + + public void setOperation(Operation operation) { + this.operation = operation; + } + + public String getFruitName() { + return fruitName; + } + + public void setFruitName(String fruit) { + this.fruitName = fruit; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } +} 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..bf8bd8a614 --- /dev/null +++ b/src/main/java/core/basesyntax/model/Operation.java @@ -0,0 +1,28 @@ +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 getCodeOfOperation() { + return code; + } + + public static Operation getOperationOf(String letter) { + return Arrays.stream(Operation.values()) + .filter(o -> o.getCodeOfOperation().equals(letter)) + .findFirst() + .orElseThrow(() -> new RuntimeException("There is no such operation by letter " + + "'" + letter + "'")); + } +} diff --git a/src/main/java/core/basesyntax/service/DataProcessing.java b/src/main/java/core/basesyntax/service/DataProcessing.java new file mode 100644 index 0000000000..1375df05ff --- /dev/null +++ b/src/main/java/core/basesyntax/service/DataProcessing.java @@ -0,0 +1,7 @@ +package core.basesyntax.service; + +import java.util.List; + +public interface DataProcessing { + void processTransaction(List data); +} diff --git a/src/main/java/core/basesyntax/service/DataReader.java b/src/main/java/core/basesyntax/service/DataReader.java new file mode 100644 index 0000000000..e09492732d --- /dev/null +++ b/src/main/java/core/basesyntax/service/DataReader.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import java.util.List; + +public interface DataReader { + List readData(String file); + +} diff --git a/src/main/java/core/basesyntax/service/OperationHandler.java b/src/main/java/core/basesyntax/service/OperationHandler.java new file mode 100644 index 0000000000..8898a2d365 --- /dev/null +++ b/src/main/java/core/basesyntax/service/OperationHandler.java @@ -0,0 +1,16 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; + +public interface OperationHandler { + int MIN_AMOUNT = 0; + + void updateStorage(FruitTransaction transaction); + + default void validAmount(FruitTransaction transaction) { + if (transaction.getAmount() < MIN_AMOUNT) { + throw new RuntimeException("OPERATION " + transaction.getOperation() + + "! Amount is less then zero!!!"); + } + } +} diff --git a/src/main/java/core/basesyntax/service/ReportCreator.java b/src/main/java/core/basesyntax/service/ReportCreator.java new file mode 100644 index 0000000000..54e05395c7 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportCreator.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface ReportCreator { + String createReport(); +} diff --git a/src/main/java/core/basesyntax/service/Writer.java b/src/main/java/core/basesyntax/service/Writer.java new file mode 100644 index 0000000000..2c12c7dfef --- /dev/null +++ b/src/main/java/core/basesyntax/service/Writer.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface Writer { + void writeReportToFile(String report, String toFile); +} diff --git a/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java new file mode 100644 index 0000000000..6f370797fb --- /dev/null +++ b/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java @@ -0,0 +1,22 @@ +package core.basesyntax.service.handler.impl; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.OperationHandler; + +public class BalanceOperationHandler implements OperationHandler { + private final StorageDao storageDao; + + public BalanceOperationHandler(StorageDao storageDao) { + this.storageDao = storageDao; + } + + @Override + public void updateStorage(FruitTransaction transaction) { + validAmount(transaction); + if (!storageDao.isInStorage(transaction.getFruitName())) { + storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); + } + } +} diff --git a/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java new file mode 100644 index 0000000000..c7f22ad222 --- /dev/null +++ b/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java @@ -0,0 +1,36 @@ +package core.basesyntax.service.handler.impl; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.OperationHandler; +import java.util.Map; + +public class PurchaseOperationHandler implements OperationHandler { + private final StorageDao storageDao; + + public PurchaseOperationHandler(StorageDao storageDao) { + this.storageDao = storageDao; + } + + @Override + public void updateStorage(FruitTransaction transaction) { + validAmountOfFruit(transaction); + Map.Entry fruitAndAmount = storageDao.get(transaction.getFruitName()); + Integer fruitAmount = fruitAndAmount.getValue(); + Integer fruitAmountFromTransaction = transaction.getAmount(); + fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction); + + } + + private void validAmountOfFruit(FruitTransaction transaction) { + validAmount(transaction); + if (!storageDao.isInStorage(transaction.getFruitName())) { + throw new RuntimeException("There is no such fruit!!!"); + } + if (storageDao.get(transaction.getFruitName()).getValue() < transaction.getAmount()) { + throw new RuntimeException("Amount of " + transaction.getFruitName() + + " isn't enough!!!"); + } + } +} diff --git a/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java new file mode 100644 index 0000000000..6f8dc53fee --- /dev/null +++ b/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java @@ -0,0 +1,28 @@ +package core.basesyntax.service.handler.impl; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.OperationHandler; +import java.util.Map; + +public class ReturnOperationHandler implements OperationHandler { + private final StorageDao storageDao; + + public ReturnOperationHandler(StorageDao storageDao) { + this.storageDao = storageDao; + } + + @Override + public void updateStorage(FruitTransaction transaction) { + validAmount(transaction); + if (!storageDao.isInStorage(transaction.getFruitName())) { + storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); + } else { + Map.Entry fruitAndAmount = storageDao.get(transaction.getFruitName()); + Integer fruitAmount = fruitAndAmount.getValue(); + Integer fruitAmountFromTransaction = transaction.getAmount(); + fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); + } + } +} diff --git a/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java new file mode 100644 index 0000000000..ac3874141a --- /dev/null +++ b/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java @@ -0,0 +1,28 @@ +package core.basesyntax.service.handler.impl; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.OperationHandler; +import java.util.Map; + +public class SupplyOperationHandler implements OperationHandler { + private final StorageDao storageDao; + + public SupplyOperationHandler(StorageDao storageDao) { + this.storageDao = storageDao; + } + + @Override + public void updateStorage(FruitTransaction transaction) { + validAmount(transaction); + if (!storageDao.isInStorage(transaction.getFruitName())) { + storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); + } else { + Map.Entry fruitAndAmount = storageDao.get(transaction.getFruitName()); + Integer fruitAmount = fruitAndAmount.getValue(); + Integer fruitAmountFromTransaction = transaction.getAmount(); + fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/CsvWriter.java b/src/main/java/core/basesyntax/service/impl/CsvWriter.java new file mode 100644 index 0000000000..57d0b3251d --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/CsvWriter.java @@ -0,0 +1,17 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.service.Writer; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class CsvWriter implements Writer { + @Override + public void writeReportToFile(String report, String toFile) { + try { + Files.write(Path.of(toFile), report.getBytes()); + } catch (IOException e) { + throw new RuntimeException("Can't write data to file: " + toFile); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java b/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java new file mode 100644 index 0000000000..c048dcda5d --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java @@ -0,0 +1,47 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.DataProcessing; +import core.basesyntax.strategy.OperationStrategy; +import java.util.List; + +public class DataProcessingImpl implements DataProcessing { + private static final String SPLITERATOR = ","; + private static final int OPERATION_POSITION = 0; + private static final int FRUIT_NAME_POSITION = 1; + private static final int AMOUNT_OF_FRUIT_POSITION = 2; + private final OperationStrategy operationStrategy; + + public DataProcessingImpl(OperationStrategy operationStrategy) { + this.operationStrategy = operationStrategy; + } + + @Override + public void processTransaction(List data) { + if (data == null) { + throw new RuntimeException("Data is null!!!"); + } + List fruitTransactions = getFruitTransactions(data); + fruitTransactions + .forEach(t -> operationStrategy.getOperationHandler(t.getOperation()) + .updateStorage(t)); + } + + private List getFruitTransactions(List data) { + return data.stream() + .map(this::getTransactionFromRow) + .toList(); + } + + private FruitTransaction getTransactionFromRow(String line) { + FruitTransaction fruitTransaction = new FruitTransaction(); + String[] rowWithTransaction = line.split(SPLITERATOR); + fruitTransaction.setOperation(Operation.getOperationOf( + rowWithTransaction[OPERATION_POSITION].trim())); + fruitTransaction.setFruitName(rowWithTransaction[FRUIT_NAME_POSITION].trim()); + fruitTransaction.setAmount(Integer + .parseInt(rowWithTransaction[AMOUNT_OF_FRUIT_POSITION].trim())); + return fruitTransaction; + } +} diff --git a/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java b/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java new file mode 100644 index 0000000000..d41a0af177 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java @@ -0,0 +1,22 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.service.DataReader; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; + +public class DataReaderFromCsv implements DataReader { + private static final int ROW_TO_SKIP = 1; + + @Override + public List readData(String file) { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + return reader.lines() + .skip(ROW_TO_SKIP) + .toList(); + } catch (IOException e) { + throw new RuntimeException("Can't read data from file: " + file); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/FruitShopServiceImpl.java b/src/main/java/core/basesyntax/service/impl/FruitShopServiceImpl.java new file mode 100644 index 0000000000..89b4cc5f89 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/FruitShopServiceImpl.java @@ -0,0 +1,30 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.service.DataProcessing; +import core.basesyntax.service.DataReader; +import core.basesyntax.service.ReportCreator; +import core.basesyntax.service.Writer; +import java.util.List; + +public class FruitShopServiceImpl { + private final DataReader dataReader; + private final DataProcessing dataProcessing; + private final ReportCreator reportCreator; + private final Writer writer; + + public FruitShopServiceImpl(DataReader dataReader, DataProcessing dataProcessing, + ReportCreator reportCreator, Writer writer) { + this.dataReader = dataReader; + this.dataProcessing = dataProcessing; + this.reportCreator = reportCreator; + this.writer = writer; + } + + public void createDailyReport(String fromFile, String toFile) { + List dataFromFile = dataReader.readData(fromFile); + dataProcessing.processTransaction(dataFromFile); + String report = reportCreator.createReport(); + writer.writeReportToFile(report, toFile); + } + +} diff --git a/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java b/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java new file mode 100644 index 0000000000..fb3a06adb9 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java @@ -0,0 +1,24 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.service.ReportCreator; +import java.util.stream.Collectors; + +public class ReportCreatorImpl implements ReportCreator { + private static final String SEPARATOR = ","; + private static final String REPORT_HEADER = "fruit,quantity"; + private static final String JOINING_SUFFIX = ""; + private final StorageDao storageDao; + + public ReportCreatorImpl(StorageDao storageDao) { + this.storageDao = storageDao; + } + + @Override + public String createReport() { + return storageDao.getALl().entrySet().stream() + .map(e -> e.getKey().getName() + SEPARATOR + e.getValue()) + .collect(Collectors.joining(System.lineSeparator(), + REPORT_HEADER + System.lineSeparator(), JOINING_SUFFIX)); + } +} diff --git a/src/main/java/core/basesyntax/strategy/OperationStrategy.java b/src/main/java/core/basesyntax/strategy/OperationStrategy.java new file mode 100644 index 0000000000..b7b18caeab --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationStrategy.java @@ -0,0 +1,8 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; + +public interface OperationStrategy { + OperationHandler getOperationHandler(Operation operation); +} diff --git a/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java b/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java new file mode 100644 index 0000000000..5491cad412 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java @@ -0,0 +1,23 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; +import core.basesyntax.strategy.OperationStrategy; +import java.util.Map; + +public class OperationStrategyImpl implements OperationStrategy { + private final Map operationHandlerMap; + + public OperationStrategyImpl(Map operationHandlerMap) { + this.operationHandlerMap = operationHandlerMap; + } + + @Override + public OperationHandler getOperationHandler(Operation operation) { + if (operationHandlerMap == null) { + throw new RuntimeException("Map with handlers is NULL!!!"); + } + return operationHandlerMap.get(operation); + } +} diff --git a/src/main/resources/data.csv b/src/main/resources/data.csv new file mode 100644 index 0000000000..c1e2881838 --- /dev/null +++ b/src/main/resources/data.csv @@ -0,0 +1,9 @@ +type,fruit,quantity +b,banana,20 +b,apple,100 +s,banana,100 +p,banana,13 +r,apple,10 +p,apple, 20 +p,banana,5 +s,banana,50 \ No newline at end of file diff --git a/src/main/resources/report.csv b/src/main/resources/report.csv new file mode 100644 index 0000000000..d7ee69b6d1 --- /dev/null +++ b/src/main/resources/report.csv @@ -0,0 +1,3 @@ +fruit,quantity +banana,152 +apple,90 \ No newline at end of file diff --git a/src/test/.DS_Store b/src/test/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..17dcd28ee1600ffd4ef90dd6aa6ddca1d49e8eb3 GIT binary patch literal 6148 zcmeHKO-sW-5S?wSO(;SS3OxqA7Ok}i;w8p<@M=U4Dm7_~Mq^f*ejG|6XZE^_(8%4*RnS|#h+zmp@sAM^*Q7xXTvb|z&UPU9dvkGi8)dHqNx{UA!ZJw=E* zT^w?C5hWct^5h`tq>Ah74$HReR=F}B*Y|2wXSdOqRGsmD9plFC-eh818=Kn)r>%$h zF_F(|wIJ}HR<>qv3NILZqs7qgC9zCyv3JUxG8kC_R)7^)E(OdkWtEoewfJpV0ajoM z6rlaVK__$#W*XJjfrUx|5a}AJ3}bcmM|7wG(KVQ9#1=H6sfd~?Oo|~)<{&g3`&~0W z)2Qhn)RS=@^JHN%6k!q%;i%ytT#a0^0<6HS0(o66)A@h=bN)X|;u`V5SjU(D)*tWZ;4o_)`Tw E0hG0882|tP literal 0 HcmV?d00001 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/dao/StorageDaoImplTest.java b/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java new file mode 100644 index 0000000000..8e984077c3 --- /dev/null +++ b/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java @@ -0,0 +1,71 @@ +package core.basesyntax.dao; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StorageDaoImplTest { + private static final Integer AMOUNT = 100; + private static final String FRUIT_NAME = "banana"; + private static final String NAME_OF_NOT_EXISTING_FRUIT = "grape"; + private static StorageDao storageDao; + private static Map expectedMap; + private Fruit fruit; + + @BeforeAll + static void beforeAll() { + storageDao = new StorageDaoImpl(); + expectedMap = new HashMap<>(); + + } + + @BeforeEach + void setUp() { + fruit = new Fruit(FRUIT_NAME); + expectedMap.put(fruit, AMOUNT); + } + + @Test + void storageDao_add_isOk() { + storageDao.add(fruit, AMOUNT); + Map actualMap = Storage.fruits; + assertEquals(expectedMap, actualMap); + } + + @Test + void storageDao_getExistingFruit_isOk() { + Storage.fruits.put(fruit, AMOUNT); + Map.Entry actualEntry = storageDao.get(fruit.getName()); + Fruit actualFruit = actualEntry.getKey(); + Integer actualAmount = actualEntry.getValue(); + assertEquals(fruit, actualFruit); + assertEquals(AMOUNT, actualAmount); + } + + @Test + void storageDao_getNullIfNotExist_isOk() { + Storage.fruits.put(fruit, AMOUNT); + Map.Entry actualEntry = storageDao.get(NAME_OF_NOT_EXISTING_FRUIT); + assertNull(actualEntry); + } + + @Test + void storageDao_getAll_isOk() { + Storage.fruits.put(fruit, AMOUNT); + Map actualMap = storageDao.getALl(); + assertEquals(expectedMap, actualMap); + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java new file mode 100644 index 0000000000..19d843484f --- /dev/null +++ b/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java @@ -0,0 +1,74 @@ +package core.basesyntax.service.handler.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BalanceOperationHandlerTest { + private static final String FRUIT_NAME = "apple"; + private static final int FRUIT_AMOUNT = 50; + private static final int FRUIT_NEGATIVE_AMOUNT = -50; + private static OperationHandler operationHandler; + private static FruitTransaction transaction; + private static FruitTransaction transactionWithNegativeAmount; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + operationHandler = new BalanceOperationHandler(storageDao); + transaction = new FruitTransaction(); + transactionWithNegativeAmount = new FruitTransaction(); + } + + @BeforeEach + void setUp() { + transaction.setOperation(Operation.BALANCE); + transaction.setFruitName(FRUIT_NAME); + transaction.setAmount(FRUIT_AMOUNT); + transactionWithNegativeAmount.setOperation(Operation.BALANCE); + transactionWithNegativeAmount.setFruitName(FRUIT_NAME); + transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); + + } + + @Test + void updateStorage_isOk() { + operationHandler.updateStorage(transaction); + Integer expectedFruitAmount = FRUIT_AMOUNT; + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + } + + @Test + void updateStorage_fruitInStorage_isOk() { + operationHandler.updateStorage(transaction); + operationHandler.updateStorage(transaction); + Integer expectedFruitAmount = FRUIT_AMOUNT; + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + + } + + @Test + void updateStorage_fruitAmountIsNegative_isNotOk() { + assertThrows(RuntimeException.class, () -> { + operationHandler.updateStorage(transactionWithNegativeAmount); + }, "If amount is negative it should throw RuntimeException!"); + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java new file mode 100644 index 0000000000..3a214c5973 --- /dev/null +++ b/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java @@ -0,0 +1,98 @@ +package core.basesyntax.service.handler.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PurchaseOperationHandlerTest { + private static final String FRUIT_NAME = "banana"; + private static final int FRUIT_AMOUNT = 120; + private static final int FRUIT_NEGATIVE_AMOUNT = -50; + private static final int FRUIT_GREATER_AMOUNT = 140; + private static final int FRUIT_LESS_AMOUNT = 100; + private static OperationHandler operationHandler; + private static FruitTransaction transaction; + private static FruitTransaction transactionWithNegativeAmount; + private static FruitTransaction transactionWithGreaterAmount; + private static FruitTransaction transactionWithLessAmount; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + operationHandler = new PurchaseOperationHandler(storageDao); + transaction = new FruitTransaction(); + transactionWithNegativeAmount = new FruitTransaction(); + transactionWithGreaterAmount = new FruitTransaction(); + transactionWithLessAmount = new FruitTransaction(); + } + + @BeforeEach + void setUp() { + transaction.setOperation(Operation.PURCHASE); + transaction.setFruitName(FRUIT_NAME); + transaction.setAmount(FRUIT_AMOUNT); + transactionWithNegativeAmount.setOperation(Operation.PURCHASE); + transactionWithNegativeAmount.setFruitName(FRUIT_NAME); + transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); + transactionWithGreaterAmount.setOperation(Operation.PURCHASE); + transactionWithGreaterAmount.setFruitName(FRUIT_NAME); + transactionWithGreaterAmount.setAmount(FRUIT_GREATER_AMOUNT); + transactionWithLessAmount.setOperation(Operation.PURCHASE); + transactionWithLessAmount.setFruitName(FRUIT_NAME); + transactionWithLessAmount.setAmount(FRUIT_LESS_AMOUNT); + + } + + @Test + void updateStorage_emptyStorage_isNotOk() { + assertThrows(RuntimeException.class, () -> { + operationHandler.updateStorage(transaction); + }, "If storage does not have such fruit it should throw Runtime Exception"); + + } + + @Test + void updateStorage_fruitInStorage_isOk() { + Storage.fruits.put(new Fruit(FRUIT_NAME), FRUIT_AMOUNT); + operationHandler.updateStorage(transaction); + Integer expectedFruitAmount = FRUIT_AMOUNT - transaction.getAmount(); + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + Storage.fruits.put(new Fruit(FRUIT_NAME), FRUIT_AMOUNT); + operationHandler.updateStorage(transactionWithLessAmount); + Integer expectedFruitAmount2 = FRUIT_AMOUNT - transactionWithLessAmount.getAmount(); + Integer actualFruitAmount2 = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount2, actualFruitAmount2); + } + + @Test + void updateStorage_fruitAmountIsGreaterThanInStorage_isNotOk() { + Storage.fruits.put(new Fruit(FRUIT_NAME), FRUIT_AMOUNT); + assertThrows(RuntimeException.class, () -> { + operationHandler.updateStorage(transactionWithGreaterAmount); + }, "If amount is greater than in Storage it should throw RuntimeException!"); + } + + @Test + void updateStorage_fruitAmountIsNegative_isNotOk() { + assertThrows(RuntimeException.class, () -> { + operationHandler.updateStorage(transactionWithNegativeAmount); + }, "If amount is negative it should throw RuntimeException!"); + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java new file mode 100644 index 0000000000..06d1d6d068 --- /dev/null +++ b/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java @@ -0,0 +1,76 @@ +package core.basesyntax.service.handler.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ReturnOperationHandlerTest { + private static final String FRUIT_NAME = "banana"; + private static final int FRUIT_AMOUNT = 120; + private static final int FRUIT_NEGATIVE_AMOUNT = -50; + private static final int NUMBER_OF_OPERATION = 2; + private static OperationHandler operationHandler; + private static FruitTransaction transaction; + private static FruitTransaction transactionWithNegativeAmount; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + operationHandler = new ReturnOperationHandler(storageDao); + transaction = new FruitTransaction(); + transactionWithNegativeAmount = new FruitTransaction(); + } + + @BeforeEach + void setUp() { + transaction.setOperation(Operation.RETURN); + transaction.setFruitName(FRUIT_NAME); + transaction.setAmount(FRUIT_AMOUNT); + transactionWithNegativeAmount.setOperation(Operation.RETURN); + transactionWithNegativeAmount.setFruitName(FRUIT_NAME); + transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); + + } + + @Test + void updateStorage_isOk() { + operationHandler.updateStorage(transaction); + Integer expectedFruitAmount = FRUIT_AMOUNT; + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + } + + @Test + void updateStorage_fruitInStorage_isOk() { + for (int count = 0; count < NUMBER_OF_OPERATION; count++) { + operationHandler.updateStorage(transaction); + } + Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + + } + + @Test + void updateStorage_fruitAmountIsNegative_isNotOk() { + assertThrows(RuntimeException.class, () -> { + operationHandler.updateStorage(transactionWithNegativeAmount); + }, "If amount is negative it should throw RuntimeException!"); + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java new file mode 100644 index 0000000000..de5e8d3bbb --- /dev/null +++ b/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java @@ -0,0 +1,76 @@ +package core.basesyntax.service.handler.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SupplyOperationHandlerTest { + private static final String FRUIT_NAME = "banana"; + private static final int FRUIT_AMOUNT = 120; + private static final int FRUIT_NEGATIVE_AMOUNT = -50; + private static final int NUMBER_OF_OPERATION = 2; + private static OperationHandler operationHandler; + private static FruitTransaction transaction; + private static FruitTransaction transactionWithNegativeAmount; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + operationHandler = new SupplyOperationHandler(storageDao); + transaction = new FruitTransaction(); + transactionWithNegativeAmount = new FruitTransaction(); + } + + @BeforeEach + void setUp() { + transaction.setOperation(Operation.SUPPLY); + transaction.setFruitName(FRUIT_NAME); + transaction.setAmount(FRUIT_AMOUNT); + transactionWithNegativeAmount.setOperation(Operation.SUPPLY); + transactionWithNegativeAmount.setFruitName(FRUIT_NAME); + transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); + + } + + @Test + void updateStorage_isOk() { + operationHandler.updateStorage(transaction); + Integer expectedFruitAmount = FRUIT_AMOUNT; + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + } + + @Test + void updateStorage_fruitInStorage_isOk() { + for (int count = 0; count < NUMBER_OF_OPERATION; count++) { + operationHandler.updateStorage(transaction); + } + Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; + Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + + } + + @Test + void updateStorage_fruitAmountIsNegative_isNotOk() { + assertThrows(RuntimeException.class, () -> { + operationHandler.updateStorage(transactionWithNegativeAmount); + }, "If amount is negative it should throw RuntimeException!"); + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java b/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java new file mode 100644 index 0000000000..033745a0a3 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java @@ -0,0 +1,47 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class CsvWriterTest { + private static final String FILE = "src/test/resources/report.csv"; + private static final String WRONG_FILE = "src/test/badResources/report.csv"; + private static final List EXPECTED_LIST = List.of("fruit,quantity", + "apple,90", "banana,132"); + private static final String DATA = """ + fruit,quantity + apple,90 + banana,132"""; + private static CsvWriter writer; + + @BeforeAll + static void beforeAll() { + writer = new CsvWriter(); + } + + @Test + void writeReportToFile_isOk() { + writer.writeReportToFile(DATA, FILE); + List actualList; + try { + actualList = Files.readAllLines(Path.of(FILE)); + } catch (IOException e) { + throw new RuntimeException("Cant read data from file: " + FILE); + } + assertEquals(EXPECTED_LIST, actualList); + } + + @Test + void writeReportToFile_badPath_notOk() { + assertThrows(RuntimeException.class, () -> { + writer.writeReportToFile(DATA, WRONG_FILE); + },"If path is incorrect it should throw Exception!!!"); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java b/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java new file mode 100644 index 0000000000..98362fa088 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java @@ -0,0 +1,83 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import core.basesyntax.model.Operation; +import core.basesyntax.service.DataProcessing; +import core.basesyntax.service.OperationHandler; +import core.basesyntax.service.handler.impl.BalanceOperationHandler; +import core.basesyntax.service.handler.impl.PurchaseOperationHandler; +import core.basesyntax.service.handler.impl.ReturnOperationHandler; +import core.basesyntax.service.handler.impl.SupplyOperationHandler; +import core.basesyntax.strategy.OperationStrategy; +import core.basesyntax.strategy.impl.OperationStrategyImpl; +import java.util.HashMap; +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; + +class DataProcessingImplTest { + private static final Fruit BANANA = new Fruit("banana"); + private static final Fruit APPLE = new Fruit("apple"); + private static final int EXPECTED_BANANA_AMOUNT = 125; + private static final int EXPECTED_APPLE_AMOUNT = 35; + private static final List DATA = List.of("b,banana,100", + "s,banana,40", "p,banana,35", "r,banana,20", "b,apple,40", + "r,apple,15", "s,apple,30", "p,apple,50"); + private static final List DATA_INCORRECT_OPERATION = List + .of("b,banana,100", "q,banana,40", "s,banana,35", "p,banana,20"); + private static DataProcessing dataProcessing; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + Map handlersMap = new HashMap<>(); + handlersMap.put(Operation.BALANCE, + new BalanceOperationHandler(storageDao)); + handlersMap.put(Operation.SUPPLY, + new SupplyOperationHandler(storageDao)); + handlersMap.put(Operation.PURCHASE, + new PurchaseOperationHandler(storageDao)); + handlersMap.put(Operation.RETURN, + new ReturnOperationHandler(storageDao)); + OperationStrategy operationStrategy = new OperationStrategyImpl(handlersMap); + dataProcessing = new DataProcessingImpl(operationStrategy); + } + + @Test + void processTransaction_isOk() { + dataProcessing.processTransaction(DATA); + Integer actualBananaAmount = Storage.fruits.get(BANANA); + Integer actualAppleAmount = Storage.fruits.get(APPLE); + assertEquals(EXPECTED_BANANA_AMOUNT, actualBananaAmount); + assertEquals(EXPECTED_APPLE_AMOUNT, actualAppleAmount); + } + + @Test + void processTransaction_incorrectOperation_isNotOk() { + assertThrows(RuntimeException.class, () -> { + dataProcessing.processTransaction(DATA_INCORRECT_OPERATION); + }, "If operation does not fount by letter it should throw Exception!!!"); + + } + + @Test + void processTransaction_dataIsNull_isNotOk() { + assertThrows(RuntimeException.class, () -> { + dataProcessing.processTransaction(null); + }); + + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java new file mode 100644 index 0000000000..6cfd4a0f44 --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java @@ -0,0 +1,51 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.service.DataReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class DataReaderFromCsvTest { + private static final String FILE_PATH = "src/test/resources/data.csv"; + private static final String BAD_FILE_PATH = "src/test/resources/badData.csv"; + private static final String DATA = """ + 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"""; + private static final List EXPECTED_LIST = 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"); + private static DataReader reader; + private static List expected; + + @BeforeAll + static void beforeAll() throws IOException { + reader = new DataReaderFromCsv(); + Files.write(Path.of(FILE_PATH), DATA.getBytes()); + } + + @Test + void readData_isOk() { + List actualList = reader.readData(FILE_PATH); + assertEquals(EXPECTED_LIST, actualList); + } + + @Test + public void readData_fileNotExistOrBadPath_notOk() { + assertThrows(RuntimeException.class, () -> { + reader.readData(BAD_FILE_PATH); + }, "If file does not exist or path is incorrect it should throw Exception!!!"); + } +} diff --git a/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java b/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java new file mode 100644 index 0000000000..658ad8008e --- /dev/null +++ b/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java @@ -0,0 +1,49 @@ +package core.basesyntax.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.db.Storage; +import core.basesyntax.model.Fruit; +import core.basesyntax.service.ReportCreator; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ReportCreatorImplTest { + private static final Fruit BANANA = new Fruit("banana"); + private static final Fruit GRAPE = new Fruit("grape"); + private static final Fruit APPLE = new Fruit("apple"); + private static final String EXPECTED = """ + fruit,quantity + banana,100 + apple,90 + grape,40"""; + private static ReportCreator reportCreator; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + reportCreator = new ReportCreatorImpl(storageDao); + } + + @BeforeEach + void setUp() { + Storage.fruits.put(BANANA, 100); + Storage.fruits.put(GRAPE, 40); + Storage.fruits.put(APPLE, 90); + } + + @Test + void createReport_isOk() { + String actualReport = reportCreator.createReport(); + assertEquals(EXPECTED, actualReport); + } + + @AfterEach + void tearDown() { + Storage.fruits.clear(); + } +} diff --git a/src/test/java/core/basesyntax/strategy/impl/OperationStrategyImplTest.java b/src/test/java/core/basesyntax/strategy/impl/OperationStrategyImplTest.java new file mode 100644 index 0000000000..ed528985fb --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/impl/OperationStrategyImplTest.java @@ -0,0 +1,72 @@ +package core.basesyntax.strategy.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.dao.StorageDao; +import core.basesyntax.dao.StorageDaoImpl; +import core.basesyntax.model.Operation; +import core.basesyntax.service.OperationHandler; +import core.basesyntax.service.handler.impl.BalanceOperationHandler; +import core.basesyntax.service.handler.impl.PurchaseOperationHandler; +import core.basesyntax.service.handler.impl.ReturnOperationHandler; +import core.basesyntax.service.handler.impl.SupplyOperationHandler; +import core.basesyntax.strategy.OperationStrategy; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class OperationStrategyImplTest { + private static OperationStrategy operationStrategy; + + @BeforeAll + static void beforeAll() { + StorageDao storageDao = new StorageDaoImpl(); + Map handlers = new HashMap<>(); + handlers.put(Operation.BALANCE, new BalanceOperationHandler(storageDao)); + handlers.put(Operation.SUPPLY, new SupplyOperationHandler(storageDao)); + handlers.put(Operation.PURCHASE, new PurchaseOperationHandler(storageDao)); + handlers.put(Operation.RETURN, new ReturnOperationHandler(storageDao)); + operationStrategy = new OperationStrategyImpl(handlers); + } + + @Test + void getBalanceOperationHandler_isOk() { + OperationHandler actual = operationStrategy.getOperationHandler(Operation.BALANCE); + assertEquals(BalanceOperationHandler.class, actual.getClass()); + } + + @Test + void getSupplyOperationHandler_isOk() { + OperationHandler actual = operationStrategy.getOperationHandler(Operation.SUPPLY); + assertEquals(SupplyOperationHandler.class, actual.getClass()); + } + + @Test + void getPurchaseOperationHandler_isOk() { + OperationHandler actual = operationStrategy.getOperationHandler(Operation.PURCHASE); + assertEquals(PurchaseOperationHandler.class, actual.getClass()); + } + + @Test + void getReturnOperationHandler_isOk() { + OperationHandler actual = operationStrategy.getOperationHandler(Operation.RETURN); + assertEquals(ReturnOperationHandler.class, actual.getClass()); + } + + @Test + void getNull_operationIsNull_isOk() { + OperationHandler actual = operationStrategy.getOperationHandler(null); + assertNull(actual); + } + + @Test + void getOperationHandler_mapIsNull_notOk() { + OperationStrategy strategyWithNullMap = new OperationStrategyImpl(null); + assertThrows(RuntimeException.class, () -> { + strategyWithNullMap.getOperationHandler(Operation.BALANCE); + }, "If map with handlers is Null it should throw Runtime Exception!"); + } +} diff --git a/src/test/resources/.DS_Store b/src/test/resources/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a4e79bd477b8f999af3398aa37526c09099cb0d5 GIT binary patch literal 6148 zcmeHK!Aiqm3{K`RR6Mw&$Nv%9gD9d{M?H8RdX{aVhn5b+dwme!!x!-A+j!S6f5O

4pd@#JYy5RHi_g9?uJV7?JKFWQrlMRtKK*9d3fD4gS$Q|x7X z;6E}Tdv_2D?Avj;UEjZO5pw8h$gk^mKC9aqGUadb3O)Hu0&lRozAvWpwppfJH3RE4 zZB^NN+0FCI`^V$kZY%;K;hhypgS3l4Hn?-))3^)T@#Q=wDT21gs)?0fIC%ra8 q@1P=**D0<_;G|ZJTxrFJ&?JaQae$>^r-%r|KLipDZk&NXW#9|K3Oz0W literal 0 HcmV?d00001 diff --git a/src/test/resources/data.csv b/src/test/resources/data.csv new file mode 100644 index 0000000000..c1e2881838 --- /dev/null +++ b/src/test/resources/data.csv @@ -0,0 +1,9 @@ +type,fruit,quantity +b,banana,20 +b,apple,100 +s,banana,100 +p,banana,13 +r,apple,10 +p,apple, 20 +p,banana,5 +s,banana,50 \ No newline at end of file diff --git a/src/test/resources/report.csv b/src/test/resources/report.csv new file mode 100644 index 0000000000..3a22c6ec7a --- /dev/null +++ b/src/test/resources/report.csv @@ -0,0 +1,3 @@ +fruit,quantity +apple,90 +banana,132 \ No newline at end of file From c34f14dc7c41a1c47343e676e548eb4342b060cb Mon Sep 17 00:00:00 2001 From: Dima Semeniuk Date: Thu, 26 Oct 2023 15:48:49 +0300 Subject: [PATCH 2/5] made some style and logic changes --- .DS_Store | Bin 6148 -> 8196 bytes .gitignore | 1 + src/.DS_Store | Bin 6148 -> 6148 bytes src/main/java/core/basesyntax/.DS_Store | Bin 6148 -> 6148 bytes src/test/.DS_Store | Bin 6148 -> 6148 bytes .../basesyntax/dao/StorageDaoImplTest.java | 16 +++++++++++++- .../impl/BalanceOperationHandlerTest.java | 9 +++++--- .../impl/PurchaseOperationHandlerTest.java | 20 ++++++++++++++---- .../impl/ReturnOperationHandlerTest.java | 9 +++++--- .../impl/SupplyOperationHandlerTest.java | 9 +++++--- .../service/impl/CsvWriterTest.java | 7 +++++- .../service/impl/DataProcessingImplTest.java | 11 ++++++++-- .../service/impl/DataReaderFromCsvTest.java | 7 +++++- 13 files changed, 71 insertions(+), 18 deletions(-) diff --git a/.DS_Store b/.DS_Store index 16f848fcce0423ae57598958404de587c339d2c6..7c87609586b7a9f950a7ab9f9087cf8eee1378e6 100644 GIT binary patch literal 8196 zcmeI1&2G~`5XWbmv~eg%kpL+dBuiYQ($Y{>;^Kz%z=2DR-~cGtv6WhNyiwwiN>QYo z;T?Ddt~?3v!U_KSk;vJk;s^~p($3%BopHRs^+zTlBGwv5M?`fZa?pg<_Te^|7#D5L zc685uC__HIOnEm<}h825DiT&YSY zCO;X+(Vxt8g~D{!FEdJ-MxxaarT`L{w1IId617stzh_0?7ikZ4Hw%%_$G-bo!WyJJj;V5 zi-$5HiDHDjf16|xpS1Wmi$*fnR|8Jfsdj4h>9l#=Y`9OKoz5EW^z`VY;Wm#?X0xjE z@bQ!97oDrLpYhLXs!TsF=$_hN`Y+g8&gYXq%u=59&?n+63m$YyV+su_-w;uxVnlBV z8)H@mp*u@L39g)sI1MRY<-{HtoOY3uP((2WMx_$x}m zXGA$T^(k6qMB-jNDBOz`8i0BqvDfga5iPFKVsw=*=p4sr(<|tPmJK0E-@0$#_Dtp9YVW*e#*5GADx#rf==m5-6!NuZ#Ns{N~^P zZ|(f-iCY4ez@G`Qa@%jW5V!cUY^I1>+eQ0`CQQW5mC6)2S&lAM7Ku5vY#H3b7q1w{OVDf#Ds*Pn@EQ{GWI0Tu2nt?!o8%Vf<4Bc4x boq009j3dZakg+Tvnh9bpSZs4V&m3j|eYzM1 diff --git a/.gitignore b/.gitignore index 6366460f0a..56058c5332 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* *.iml target/* +.DS_Store diff --git a/src/.DS_Store b/src/.DS_Store index 8877e45dd44061f594040ab1d37c88cbfaa61cfa..18fc9d1e042aa72db2d0fb2040bbe649537ca84f 100644 GIT binary patch delta 16 XcmZoMXffDufpzi$7SGL+Y_A0YIRge< delta 28 jcmZoMXffDuftBCL%s@xM*uj@Vyi;R0i<4#EOCuWOG8zOiwWhxfeS`(02J)nDhAhE#ZDs>Me-T` zfnVUtm+&u~;LYw9#V%ZtKr_NkA|elsvFD@vjqy6SE7o%! zUR0bzpCpA&FynM$|AGR1c6~}ICjGc*o!*U?+OjOw^vRMU9=l4sAfE|SY}oIKC7AjHu~2NHo+2v)5HN0xU&FXrfrFi8u>cFxW_AvK4xoa~j4a=oC-aLa VaxwzdB!Co7HsMj;93!%V82}k>4o3h0 diff --git a/src/test/.DS_Store b/src/test/.DS_Store index 17dcd28ee1600ffd4ef90dd6aa6ddca1d49e8eb3..05ce5c9cac77c64994640ac772389b3217e26fee 100644 GIT binary patch delta 147 zcmZoMXfc=|#>B!ku~2NHo+2aL#(>?7iyN4k7}+NCFllqLFk~?#GL$hSPOf2U;5RWg z(NQomwW!rmsJ1XPo6OB@AqrPf7F?8B)qu~2NHo+2a5#(>?7j4YFRShObxvIp6F!wdkVRu_f< diff --git a/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java b/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java index 8e984077c3..8b6e822820 100644 --- a/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java +++ b/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java @@ -1,7 +1,9 @@ package core.basesyntax.dao; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.db.Storage; import core.basesyntax.model.Fruit; @@ -24,7 +26,6 @@ class StorageDaoImplTest { static void beforeAll() { storageDao = new StorageDaoImpl(); expectedMap = new HashMap<>(); - } @BeforeEach @@ -64,6 +65,19 @@ void storageDao_getAll_isOk() { assertEquals(expectedMap, actualMap); } + @Test + void storageDao_isInStorage_isOk() { + Storage.fruits.put(fruit, AMOUNT); + boolean actual = storageDao.isInStorage(fruit.getName()); + assertTrue(actual); + } + + @Test + void storageDao_isNotInStorage_isOk() { + boolean actual = storageDao.isInStorage(FRUIT_NAME); + assertFalse(actual); + } + @AfterEach void tearDown() { Storage.fruits.clear(); diff --git a/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java index 19d843484f..d3623dcdc6 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.dao.StorageDao; import core.basesyntax.dao.StorageDaoImpl; @@ -39,7 +40,6 @@ void setUp() { transactionWithNegativeAmount.setOperation(Operation.BALANCE); transactionWithNegativeAmount.setFruitName(FRUIT_NAME); transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); - } @Test @@ -57,14 +57,17 @@ void updateStorage_fruitInStorage_isOk() { Integer expectedFruitAmount = FRUIT_AMOUNT; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); assertEquals(expectedFruitAmount, actualFruitAmount); - } @Test void updateStorage_fruitAmountIsNegative_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { operationHandler.updateStorage(transactionWithNegativeAmount); }, "If amount is negative it should throw RuntimeException!"); + + String expectedMessage = "Amount is less then zero!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @AfterEach diff --git a/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java index 3a214c5973..3dd8517671 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.dao.StorageDao; import core.basesyntax.dao.StorageDaoImpl; @@ -51,15 +52,18 @@ void setUp() { transactionWithLessAmount.setOperation(Operation.PURCHASE); transactionWithLessAmount.setFruitName(FRUIT_NAME); transactionWithLessAmount.setAmount(FRUIT_LESS_AMOUNT); - } @Test void updateStorage_emptyStorage_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { operationHandler.updateStorage(transaction); }, "If storage does not have such fruit it should throw Runtime Exception"); + String expectedMessage = "There is no such fruit!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } @Test @@ -79,16 +83,24 @@ void updateStorage_fruitInStorage_isOk() { @Test void updateStorage_fruitAmountIsGreaterThanInStorage_isNotOk() { Storage.fruits.put(new Fruit(FRUIT_NAME), FRUIT_AMOUNT); - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { operationHandler.updateStorage(transactionWithGreaterAmount); }, "If amount is greater than in Storage it should throw RuntimeException!"); + + String expectedMessage = " isn't enough!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @Test void updateStorage_fruitAmountIsNegative_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { operationHandler.updateStorage(transactionWithNegativeAmount); }, "If amount is negative it should throw RuntimeException!"); + + String expectedMessage = "Amount is less then zero!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage));; } @AfterEach diff --git a/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java index 06d1d6d068..a0acf37947 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.dao.StorageDao; import core.basesyntax.dao.StorageDaoImpl; @@ -40,7 +41,6 @@ void setUp() { transactionWithNegativeAmount.setOperation(Operation.RETURN); transactionWithNegativeAmount.setFruitName(FRUIT_NAME); transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); - } @Test @@ -59,14 +59,17 @@ void updateStorage_fruitInStorage_isOk() { Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); assertEquals(expectedFruitAmount, actualFruitAmount); - } @Test void updateStorage_fruitAmountIsNegative_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { operationHandler.updateStorage(transactionWithNegativeAmount); }, "If amount is negative it should throw RuntimeException!"); + + String expectedMessage = "Amount is less then zero!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @AfterEach diff --git a/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java index de5e8d3bbb..a80484f2af 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.dao.StorageDao; import core.basesyntax.dao.StorageDaoImpl; @@ -40,7 +41,6 @@ void setUp() { transactionWithNegativeAmount.setOperation(Operation.SUPPLY); transactionWithNegativeAmount.setFruitName(FRUIT_NAME); transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); - } @Test @@ -59,14 +59,17 @@ void updateStorage_fruitInStorage_isOk() { Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); assertEquals(expectedFruitAmount, actualFruitAmount); - } @Test void updateStorage_fruitAmountIsNegative_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { operationHandler.updateStorage(transactionWithNegativeAmount); }, "If amount is negative it should throw RuntimeException!"); + + String expectedMessage = "Amount is less then zero!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @AfterEach diff --git a/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java b/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java index 033745a0a3..89e349fcde 100644 --- a/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java +++ b/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.nio.file.Files; @@ -40,8 +41,12 @@ void writeReportToFile_isOk() { @Test void writeReportToFile_badPath_notOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { writer.writeReportToFile(DATA, WRONG_FILE); },"If path is incorrect it should throw Exception!!!"); + + String expectedMessage = "Can't write data to file: "; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } } diff --git a/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java b/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java index 98362fa088..e7bdaedd1f 100644 --- a/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.dao.StorageDao; import core.basesyntax.dao.StorageDaoImpl; @@ -62,18 +63,24 @@ void processTransaction_isOk() { @Test void processTransaction_incorrectOperation_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { dataProcessing.processTransaction(DATA_INCORRECT_OPERATION); }, "If operation does not fount by letter it should throw Exception!!!"); + String expectedMessage = "There is no such operation by letter "; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @Test void processTransaction_dataIsNull_isNotOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { dataProcessing.processTransaction(null); }); + String expectedMessage = "Data is null!!!"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @AfterEach diff --git a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java index 6cfd4a0f44..3e0f00f976 100644 --- a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java +++ b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import core.basesyntax.service.DataReader; import java.io.IOException; @@ -44,8 +45,12 @@ void readData_isOk() { @Test public void readData_fileNotExistOrBadPath_notOk() { - assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(RuntimeException.class, () -> { reader.readData(BAD_FILE_PATH); }, "If file does not exist or path is incorrect it should throw Exception!!!"); + + String expectedMessage = "Can't read data from file: "; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } } From 90b9866f686eeb1472ade4724fb157b2dd6da121 Mon Sep 17 00:00:00 2001 From: Dima Semeniuk Date: Thu, 26 Oct 2023 15:59:29 +0300 Subject: [PATCH 3/5] added DS_Store to gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 56058c5332..acf2f82e44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* *.iml target/* -.DS_Store +**/.DS_Store + From 8fa3ad96ba1b47b5bbfe047e4fec66aca51306af Mon Sep 17 00:00:00 2001 From: Dima Semeniuk Date: Thu, 26 Oct 2023 18:54:18 +0300 Subject: [PATCH 4/5] devided statements in tests by the Given-When-Then principles --- .gitignore | 2 +- .../java/core/basesyntax/dao/StorageDaoImplTest.java | 10 ++++++++++ .../handler/impl/BalanceOperationHandlerTest.java | 3 +++ .../handler/impl/PurchaseOperationHandlerTest.java | 8 ++++++++ .../handler/impl/ReturnOperationHandlerTest.java | 3 +++ .../handler/impl/SupplyOperationHandlerTest.java | 3 +++ .../core/basesyntax/service/impl/CsvWriterTest.java | 2 ++ .../service/impl/DataProcessingImplTest.java | 3 +++ .../basesyntax/service/impl/DataReaderFromCsvTest.java | 2 ++ .../basesyntax/service/impl/ReportCreatorImplTest.java | 1 + 10 files changed, 36 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index acf2f82e44..50e3bd00ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .idea/* *.iml target/* -**/.DS_Store +.DS_Store diff --git a/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java b/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java index 8b6e822820..866b3dccc4 100644 --- a/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java +++ b/src/test/java/core/basesyntax/dao/StorageDaoImplTest.java @@ -38,15 +38,18 @@ void setUp() { void storageDao_add_isOk() { storageDao.add(fruit, AMOUNT); Map actualMap = Storage.fruits; + assertEquals(expectedMap, actualMap); } @Test void storageDao_getExistingFruit_isOk() { Storage.fruits.put(fruit, AMOUNT); + Map.Entry actualEntry = storageDao.get(fruit.getName()); Fruit actualFruit = actualEntry.getKey(); Integer actualAmount = actualEntry.getValue(); + assertEquals(fruit, actualFruit); assertEquals(AMOUNT, actualAmount); } @@ -54,27 +57,34 @@ void storageDao_getExistingFruit_isOk() { @Test void storageDao_getNullIfNotExist_isOk() { Storage.fruits.put(fruit, AMOUNT); + Map.Entry actualEntry = storageDao.get(NAME_OF_NOT_EXISTING_FRUIT); + assertNull(actualEntry); } @Test void storageDao_getAll_isOk() { Storage.fruits.put(fruit, AMOUNT); + Map actualMap = storageDao.getALl(); + assertEquals(expectedMap, actualMap); } @Test void storageDao_isInStorage_isOk() { Storage.fruits.put(fruit, AMOUNT); + boolean actual = storageDao.isInStorage(fruit.getName()); + assertTrue(actual); } @Test void storageDao_isNotInStorage_isOk() { boolean actual = storageDao.isInStorage(FRUIT_NAME); + assertFalse(actual); } diff --git a/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java index d3623dcdc6..4fcda9bc81 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/BalanceOperationHandlerTest.java @@ -47,6 +47,7 @@ void updateStorage_isOk() { operationHandler.updateStorage(transaction); Integer expectedFruitAmount = FRUIT_AMOUNT; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); } @@ -56,6 +57,7 @@ void updateStorage_fruitInStorage_isOk() { operationHandler.updateStorage(transaction); Integer expectedFruitAmount = FRUIT_AMOUNT; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); } @@ -67,6 +69,7 @@ void updateStorage_fruitAmountIsNegative_isNotOk() { String expectedMessage = "Amount is less then zero!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } diff --git a/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java index 3dd8517671..69a302a370 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/PurchaseOperationHandlerTest.java @@ -62,6 +62,7 @@ void updateStorage_emptyStorage_isNotOk() { String expectedMessage = "There is no such fruit!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @@ -69,14 +70,19 @@ void updateStorage_emptyStorage_isNotOk() { @Test void updateStorage_fruitInStorage_isOk() { Storage.fruits.put(new Fruit(FRUIT_NAME), FRUIT_AMOUNT); + operationHandler.updateStorage(transaction); Integer expectedFruitAmount = FRUIT_AMOUNT - transaction.getAmount(); Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); + Storage.fruits.put(new Fruit(FRUIT_NAME), FRUIT_AMOUNT); + operationHandler.updateStorage(transactionWithLessAmount); Integer expectedFruitAmount2 = FRUIT_AMOUNT - transactionWithLessAmount.getAmount(); Integer actualFruitAmount2 = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount2, actualFruitAmount2); } @@ -89,6 +95,7 @@ void updateStorage_fruitAmountIsGreaterThanInStorage_isNotOk() { String expectedMessage = " isn't enough!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @@ -100,6 +107,7 @@ void updateStorage_fruitAmountIsNegative_isNotOk() { String expectedMessage = "Amount is less then zero!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage));; } diff --git a/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java index a0acf37947..aabe4633fd 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/ReturnOperationHandlerTest.java @@ -48,6 +48,7 @@ void updateStorage_isOk() { operationHandler.updateStorage(transaction); Integer expectedFruitAmount = FRUIT_AMOUNT; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); } @@ -58,6 +59,7 @@ void updateStorage_fruitInStorage_isOk() { } Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); } @@ -69,6 +71,7 @@ void updateStorage_fruitAmountIsNegative_isNotOk() { String expectedMessage = "Amount is less then zero!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } diff --git a/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java b/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java index a80484f2af..fab69556de 100644 --- a/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java +++ b/src/test/java/core/basesyntax/service/handler/impl/SupplyOperationHandlerTest.java @@ -48,6 +48,7 @@ void updateStorage_isOk() { operationHandler.updateStorage(transaction); Integer expectedFruitAmount = FRUIT_AMOUNT; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); } @@ -58,6 +59,7 @@ void updateStorage_fruitInStorage_isOk() { } Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); + assertEquals(expectedFruitAmount, actualFruitAmount); } @@ -69,6 +71,7 @@ void updateStorage_fruitAmountIsNegative_isNotOk() { String expectedMessage = "Amount is less then zero!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } diff --git a/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java b/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java index 89e349fcde..bc7173b836 100644 --- a/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java +++ b/src/test/java/core/basesyntax/service/impl/CsvWriterTest.java @@ -36,6 +36,7 @@ void writeReportToFile_isOk() { } catch (IOException e) { throw new RuntimeException("Cant read data from file: " + FILE); } + assertEquals(EXPECTED_LIST, actualList); } @@ -47,6 +48,7 @@ void writeReportToFile_badPath_notOk() { String expectedMessage = "Can't write data to file: "; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } } diff --git a/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java b/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java index e7bdaedd1f..ce5ee4bfbd 100644 --- a/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/DataProcessingImplTest.java @@ -57,6 +57,7 @@ void processTransaction_isOk() { dataProcessing.processTransaction(DATA); Integer actualBananaAmount = Storage.fruits.get(BANANA); Integer actualAppleAmount = Storage.fruits.get(APPLE); + assertEquals(EXPECTED_BANANA_AMOUNT, actualBananaAmount); assertEquals(EXPECTED_APPLE_AMOUNT, actualAppleAmount); } @@ -69,6 +70,7 @@ void processTransaction_incorrectOperation_isNotOk() { String expectedMessage = "There is no such operation by letter "; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @@ -80,6 +82,7 @@ void processTransaction_dataIsNull_isNotOk() { String expectedMessage = "Data is null!!!"; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } diff --git a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java index 3e0f00f976..d69e08df90 100644 --- a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java +++ b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java @@ -40,6 +40,7 @@ static void beforeAll() throws IOException { @Test void readData_isOk() { List actualList = reader.readData(FILE_PATH); + assertEquals(EXPECTED_LIST, actualList); } @@ -51,6 +52,7 @@ public void readData_fileNotExistOrBadPath_notOk() { String expectedMessage = "Can't read data from file: "; String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } } diff --git a/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java b/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java index 658ad8008e..c186bacf53 100644 --- a/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java @@ -39,6 +39,7 @@ void setUp() { @Test void createReport_isOk() { String actualReport = reportCreator.createReport(); + assertEquals(EXPECTED, actualReport); } From fe6051d9765ec71f801563db2c57ff5ff18e3c17 Mon Sep 17 00:00:00 2001 From: Dima Semeniuk Date: Fri, 27 Oct 2023 12:57:19 +0300 Subject: [PATCH 5/5] fixed previous maistakes --- src/main/java/core/basesyntax/dao/StorageDao.java | 2 +- src/main/java/core/basesyntax/dao/StorageDaoImpl.java | 4 ++-- src/main/java/core/basesyntax/model/Operation.java | 2 +- .../service/handler/impl/BalanceOperationHandler.java | 5 +++-- .../service/handler/impl/PurchaseOperationHandler.java | 6 +++--- .../service/handler/impl/ReturnOperationHandler.java | 7 ++++--- .../service/handler/impl/SupplyOperationHandler.java | 7 ++++--- .../java/core/basesyntax/service/impl/CsvWriter.java | 4 +++- .../core/basesyntax/service/impl/DataProcessingImpl.java | 3 ++- .../core/basesyntax/service/impl/DataReaderFromCsv.java | 3 ++- .../core/basesyntax/service/impl/ReportCreatorImpl.java | 5 +++-- .../basesyntax/strategy/impl/OperationStrategyImpl.java | 3 ++- .../java/core/basesyntax/dao/StorageDaoImplTest.java | 2 +- .../basesyntax/service/impl/DataReaderFromCsvTest.java | 2 +- .../basesyntax/service/impl/ReportCreatorImplTest.java | 9 ++++++--- 15 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/java/core/basesyntax/dao/StorageDao.java b/src/main/java/core/basesyntax/dao/StorageDao.java index 4ef360f96c..5961e5fbc6 100644 --- a/src/main/java/core/basesyntax/dao/StorageDao.java +++ b/src/main/java/core/basesyntax/dao/StorageDao.java @@ -10,5 +10,5 @@ public interface StorageDao { boolean isInStorage(String name); - Map getALl(); + Map getAll(); } diff --git a/src/main/java/core/basesyntax/dao/StorageDaoImpl.java b/src/main/java/core/basesyntax/dao/StorageDaoImpl.java index d4261fe032..20eac3521c 100644 --- a/src/main/java/core/basesyntax/dao/StorageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/StorageDaoImpl.java @@ -22,11 +22,11 @@ public Map.Entry get(String name) { } public boolean isInStorage(String name) { - return (get(name) != null); + return get(name) != null; } @Override - public Map getALl() { + public Map getAll() { return new HashMap<>(Storage.fruits); } } diff --git a/src/main/java/core/basesyntax/model/Operation.java b/src/main/java/core/basesyntax/model/Operation.java index bf8bd8a614..9e6002a65f 100644 --- a/src/main/java/core/basesyntax/model/Operation.java +++ b/src/main/java/core/basesyntax/model/Operation.java @@ -20,7 +20,7 @@ public String getCodeOfOperation() { public static Operation getOperationOf(String letter) { return Arrays.stream(Operation.values()) - .filter(o -> o.getCodeOfOperation().equals(letter)) + .filter(operation -> operation.getCodeOfOperation().equals(letter)) .findFirst() .orElseThrow(() -> new RuntimeException("There is no such operation by letter " + "'" + letter + "'")); diff --git a/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java index 6f370797fb..3bbf50c43b 100644 --- a/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java +++ b/src/main/java/core/basesyntax/service/handler/impl/BalanceOperationHandler.java @@ -15,8 +15,9 @@ public BalanceOperationHandler(StorageDao storageDao) { @Override public void updateStorage(FruitTransaction transaction) { validAmount(transaction); - if (!storageDao.isInStorage(transaction.getFruitName())) { - storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); + String fruitName = transaction.getFruitName(); + if (!storageDao.isInStorage(fruitName)) { + storageDao.add(new Fruit(fruitName), transaction.getAmount()); } } } diff --git a/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java index c7f22ad222..74a7510e0b 100644 --- a/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java +++ b/src/main/java/core/basesyntax/service/handler/impl/PurchaseOperationHandler.java @@ -20,15 +20,15 @@ public void updateStorage(FruitTransaction transaction) { Integer fruitAmount = fruitAndAmount.getValue(); Integer fruitAmountFromTransaction = transaction.getAmount(); fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction); - } private void validAmountOfFruit(FruitTransaction transaction) { validAmount(transaction); - if (!storageDao.isInStorage(transaction.getFruitName())) { + String fruitName = transaction.getFruitName(); + if (!storageDao.isInStorage(fruitName)) { throw new RuntimeException("There is no such fruit!!!"); } - if (storageDao.get(transaction.getFruitName()).getValue() < transaction.getAmount()) { + if (storageDao.get(fruitName).getValue() < transaction.getAmount()) { throw new RuntimeException("Amount of " + transaction.getFruitName() + " isn't enough!!!"); } diff --git a/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java index 6f8dc53fee..6aa57d9d07 100644 --- a/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java +++ b/src/main/java/core/basesyntax/service/handler/impl/ReturnOperationHandler.java @@ -16,10 +16,11 @@ public ReturnOperationHandler(StorageDao storageDao) { @Override public void updateStorage(FruitTransaction transaction) { validAmount(transaction); - if (!storageDao.isInStorage(transaction.getFruitName())) { - storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); + String fruitName = transaction.getFruitName(); + if (!storageDao.isInStorage(fruitName)) { + storageDao.add(new Fruit(fruitName), transaction.getAmount()); } else { - Map.Entry fruitAndAmount = storageDao.get(transaction.getFruitName()); + Map.Entry fruitAndAmount = storageDao.get(fruitName); Integer fruitAmount = fruitAndAmount.getValue(); Integer fruitAmountFromTransaction = transaction.getAmount(); fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); diff --git a/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java b/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java index ac3874141a..75e7f5084e 100644 --- a/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java +++ b/src/main/java/core/basesyntax/service/handler/impl/SupplyOperationHandler.java @@ -16,10 +16,11 @@ public SupplyOperationHandler(StorageDao storageDao) { @Override public void updateStorage(FruitTransaction transaction) { validAmount(transaction); - if (!storageDao.isInStorage(transaction.getFruitName())) { - storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); + String fruitName = transaction.getFruitName(); + if (!storageDao.isInStorage(fruitName)) { + storageDao.add(new Fruit(fruitName), transaction.getAmount()); } else { - Map.Entry fruitAndAmount = storageDao.get(transaction.getFruitName()); + Map.Entry fruitAndAmount = storageDao.get(fruitName); Integer fruitAmount = fruitAndAmount.getValue(); Integer fruitAmountFromTransaction = transaction.getAmount(); fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); diff --git a/src/main/java/core/basesyntax/service/impl/CsvWriter.java b/src/main/java/core/basesyntax/service/impl/CsvWriter.java index 57d0b3251d..f6d3c98dc7 100644 --- a/src/main/java/core/basesyntax/service/impl/CsvWriter.java +++ b/src/main/java/core/basesyntax/service/impl/CsvWriter.java @@ -6,12 +6,14 @@ import java.nio.file.Path; public class CsvWriter implements Writer { + private static final String EXCEPTION_MESSAGE = "Can't write data to file: "; + @Override public void writeReportToFile(String report, String toFile) { try { Files.write(Path.of(toFile), report.getBytes()); } catch (IOException e) { - throw new RuntimeException("Can't write data to file: " + toFile); + throw new RuntimeException(EXCEPTION_MESSAGE + toFile); } } } diff --git a/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java b/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java index c048dcda5d..c6d5e96103 100644 --- a/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java +++ b/src/main/java/core/basesyntax/service/impl/DataProcessingImpl.java @@ -11,6 +11,7 @@ public class DataProcessingImpl implements DataProcessing { private static final int OPERATION_POSITION = 0; private static final int FRUIT_NAME_POSITION = 1; private static final int AMOUNT_OF_FRUIT_POSITION = 2; + private static final String DATA_NULL = "Data is null!!!"; private final OperationStrategy operationStrategy; public DataProcessingImpl(OperationStrategy operationStrategy) { @@ -20,7 +21,7 @@ public DataProcessingImpl(OperationStrategy operationStrategy) { @Override public void processTransaction(List data) { if (data == null) { - throw new RuntimeException("Data is null!!!"); + throw new RuntimeException(DATA_NULL); } List fruitTransactions = getFruitTransactions(data); fruitTransactions diff --git a/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java b/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java index d41a0af177..5ad8e24f0f 100644 --- a/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java +++ b/src/main/java/core/basesyntax/service/impl/DataReaderFromCsv.java @@ -8,6 +8,7 @@ public class DataReaderFromCsv implements DataReader { private static final int ROW_TO_SKIP = 1; + private static final String EXCEPTION_MESSAGE = "Can't read data from file: "; @Override public List readData(String file) { @@ -16,7 +17,7 @@ public List readData(String file) { .skip(ROW_TO_SKIP) .toList(); } catch (IOException e) { - throw new RuntimeException("Can't read data from file: " + file); + throw new RuntimeException(EXCEPTION_MESSAGE + file); } } } diff --git a/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java b/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java index fb3a06adb9..903e5261bd 100644 --- a/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java +++ b/src/main/java/core/basesyntax/service/impl/ReportCreatorImpl.java @@ -16,8 +16,9 @@ public ReportCreatorImpl(StorageDao storageDao) { @Override public String createReport() { - return storageDao.getALl().entrySet().stream() - .map(e -> e.getKey().getName() + SEPARATOR + e.getValue()) + return storageDao.getAll().entrySet().stream() + .map(fruitAmountEntry -> fruitAmountEntry.getKey().getName() + + SEPARATOR + fruitAmountEntry.getValue()) .collect(Collectors.joining(System.lineSeparator(), REPORT_HEADER + System.lineSeparator(), JOINING_SUFFIX)); } diff --git a/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java b/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java index 5491cad412..6e92492868 100644 --- a/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java +++ b/src/main/java/core/basesyntax/strategy/impl/OperationStrategyImpl.java @@ -6,6 +6,7 @@ import java.util.Map; public class OperationStrategyImpl implements OperationStrategy { + private static final String MAP_IS_NULL = "Map with handlers is NULL!!!"; private final Map operationHandlerMap; public OperationStrategyImpl(Map actualMap = storageDao.getALl(); + Map actualMap = storageDao.getAll(); assertEquals(expectedMap, actualMap); } diff --git a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java index d69e08df90..478c9afaad 100644 --- a/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java +++ b/src/test/java/core/basesyntax/service/impl/DataReaderFromCsvTest.java @@ -45,7 +45,7 @@ void readData_isOk() { } @Test - public void readData_fileNotExistOrBadPath_notOk() { + void readData_fileNotExistOrBadPath_notOk() { Exception exception = assertThrows(RuntimeException.class, () -> { reader.readData(BAD_FILE_PATH); }, "If file does not exist or path is incorrect it should throw Exception!!!"); diff --git a/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java b/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java index c186bacf53..6106c2b05f 100644 --- a/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java +++ b/src/test/java/core/basesyntax/service/impl/ReportCreatorImplTest.java @@ -13,6 +13,9 @@ import org.junit.jupiter.api.Test; class ReportCreatorImplTest { + private static final int bananaAmount = 100; + private static final int grapeAmount = 40; + private static final int appleAmount = 90; private static final Fruit BANANA = new Fruit("banana"); private static final Fruit GRAPE = new Fruit("grape"); private static final Fruit APPLE = new Fruit("apple"); @@ -31,9 +34,9 @@ static void beforeAll() { @BeforeEach void setUp() { - Storage.fruits.put(BANANA, 100); - Storage.fruits.put(GRAPE, 40); - Storage.fruits.put(APPLE, 90); + Storage.fruits.put(BANANA, bananaAmount); + Storage.fruits.put(GRAPE, grapeAmount); + Storage.fruits.put(APPLE, appleAmount); } @Test