Skip to content

Commit

Permalink
Added changes after verification
Browse files Browse the repository at this point in the history
  • Loading branch information
slaybrute committed Oct 27, 2023
1 parent 5cf2ea1 commit b4141ae
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 118 deletions.
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/core/basesyntax/dao/FruitDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface FruitDao {
void set(String name, int quantity);

void remove(String name, int quantity);

void remove(String name);
}
21 changes: 13 additions & 8 deletions src/main/java/core/basesyntax/dao/FruitDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ public void add(String name, int quantity) {
fruits.put(name, currentAmount + quantity);
}

@Override
public void remove(String name, int quantity) {
if (fruits.get(name) < quantity) {
throw new RuntimeException("Not enough " + name + " to purchase");
}
fruits.put(name, fruits.get(name) - quantity);
}

@Override
public void set(String name, int quantity) {
if (quantity < 0) {
throw new RuntimeException("Cannot put such quantity: " + quantity);
}
fruits.put(name, quantity);
}

@Override
public void remove(String name) {
fruits.remove(name);
}

@Override
public void remove(String name, int quantity) {
if (fruits.get(name) < quantity) {
throw new RuntimeException("Not enough " + name + " to purchase");
}
fruits.put(name, fruits.get(name) - quantity);
}
}
7 changes: 0 additions & 7 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/java/core/basesyntax/dao/FruitDaoImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void setValidAmount_Ok() {
}

@Test
void addInvalidAmount_Not_Ok() {
void addInvalidAmount_notOk() {
assertThrows(RuntimeException.class, () -> fruitDao.add("apple", INVALID_AMOUNT));
}

Expand All @@ -47,7 +47,7 @@ void addValidAmount_Ok() {
}

@Test
void removeInvalidAmount_Not_Ok() {
void removeInvalidAmount_notOk() {
assertThrows(RuntimeException.class, () -> fruitDao.remove("apple", Integer.MAX_VALUE));
}

Expand Down
18 changes: 8 additions & 10 deletions src/test/java/core/basesyntax/service/FileReaderImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.junit.jupiter.api.Test;

public class FileReaderImplTest {
private static FileReader fileReader;
private static final String INPUT_1_FILE_NAME = "input1.csv";
private static final String INPUT_2_FILE_NAME = "input2.csv";
private static final String INPUT_3_FILE_NAME = "input3.csv";
private static final String INVALID_FILE_NAME = "input4.csv";
private static FileReader fileReader;
private static ClassLoader classLoader;

@BeforeAll
Expand All @@ -25,15 +25,14 @@ static void beforeAll() {
}

@Test
void readFromFile_Not_Ok() {
void readFromFile_notOk() {
assertThrows(RuntimeException.class, () ->
fileReader.readFromFile(new File(INVALID_FILE_NAME)));
}

@Test
void readFromInput1File_Ok() {
URL resources = classLoader.getResource(INPUT_1_FILE_NAME);
List<String> actual = fileReader.readFromFile(new File(resources.getFile()));
List<String> actual = readFromFile(INPUT_1_FILE_NAME);
List<String> expected = new ArrayList<>(List.of("type,fruit,quantity",
"b,banana,20", "b,apple,100", "s,banana,100",
"p,banana,13", "r,apple,10", "p,apple,20",
Expand All @@ -43,8 +42,7 @@ void readFromInput1File_Ok() {

@Test
void readFromInput2File_Ok() {
URL resources = classLoader.getResource(INPUT_2_FILE_NAME);
List<String> actual = fileReader.readFromFile(getFilefromUrl(resources));
List<String> actual = readFromFile(INPUT_2_FILE_NAME);
List<String> expected = new ArrayList<>(List.of("type,fruit,quantity",
"b,banana,200", "b,apple,150", "p,banana,100", "s,banana,25",
"r,apple,10", "r,banana,20", "p,banana,5", "s,banana,15"));
Expand All @@ -53,16 +51,16 @@ void readFromInput2File_Ok() {

@Test
void readFromInput3File_Ok() {
URL resources = classLoader.getResource(INPUT_3_FILE_NAME);
List<String> actual = fileReader.readFromFile(getFilefromUrl(resources));
List<String> actual = readFromFile(INPUT_3_FILE_NAME);
List<String> expected = new ArrayList<>(List.of("type,fruit,quantity",
"b,pear,200", "b,apple,150", "b,banana,100", "p,pear,80",
"s,banana,25", "r,apple,10", "r,pear,20", "p,banana,5",
"s,banana,15", "p,banana,20"));
assertEquals(expected, actual);
}

private static File getFilefromUrl(URL resources) {
return new File(resources.getFile());
private List<String> readFromFile(String fileName) {
URL resources = classLoader.getResource(fileName);
return fileReader.readFromFile(new File(resources.getFile()));
}
}
5 changes: 3 additions & 2 deletions src/test/java/core/basesyntax/service/FileWriterImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
public class FileWriterImplTest {
private static final String SEPARATOR = ",";
private static final String HEADER = "fruit,quantity";
private static final String FILE_NAME = "report.csv";
private static FileWriter fileWriter;
private static FileReader fileReader;
private static final String FILE_NAME = "report.csv";

private static final String REPORT = HEADER + System.lineSeparator()
+ "apple" + SEPARATOR + 160 + System.lineSeparator()
+ "banana" + SEPARATOR + 115 + System.lineSeparator()
Expand All @@ -39,7 +40,7 @@ void writeToFile_Ok() {
}

@Test
void writeToReadOnlyFile_Not_Ok() {
void writeToReadOnlyFile_notOk() {
assertThrows(RuntimeException.class, () ->
fileWriter.writeToFile(null, REPORT));
}
Expand Down
40 changes: 17 additions & 23 deletions src/test/java/core/basesyntax/service/OperationHandlerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import core.basesyntax.dao.FruitDao;
import core.basesyntax.dao.FruitDaoImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationStrategy;
Expand All @@ -14,19 +15,21 @@
import org.junit.jupiter.api.Test;

public class OperationHandlerImplTest {
private static FileReader fileReader;
private static ParseService parseService;
private static OperationStrategy operationStrategy;
private static final String INPUT_1_FILE_NAME = "input1.csv";
private static final String INPUT_2_FILE_NAME = "input2.csv";
private static final String INPUT_3_FILE_NAME = "input3.csv";
private static FileReader fileReader;
private static ParseService parseService;
private static OperationStrategy operationStrategy;
private static FruitDao fruitDao;
private static ClassLoader classLoader;

@BeforeAll
static void beforeAll() {
classLoader = FileReaderImplTest.class.getClassLoader();
fileReader = new FileReaderImpl();
parseService = new ParseServiceImpl();
fruitDao = new FruitDaoImpl();
operationStrategy = new OperationStrategyImpl(new HashMap<>() {{
put(FruitTransaction.Operation.BALANCE, new RemnantOperationHandler());
put(FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler());
Expand All @@ -38,44 +41,35 @@ static void beforeAll() {

@Test
void handleInput1File_Ok() {
URL resources = classLoader.getResource(INPUT_1_FILE_NAME);
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> fruitTransactions = parseService.parse(readFromFile);
for (FruitTransaction fruitTransaction : fruitTransactions) {
operationStrategy.get(fruitTransaction.getOperation())
.handle(fruitTransaction.getFruit(),
fruitTransaction.getQuantity());
}
handle(INPUT_1_FILE_NAME);
assertEquals(FruitDaoImpl.getFruits().get("banana"), 152);
assertEquals(FruitDaoImpl.getFruits().get("apple"), 90);
}

@Test
void handleInput2File_Ok() {
URL resources = classLoader.getResource(INPUT_2_FILE_NAME);
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> fruitTransactions = parseService.parse(readFromFile);
for (FruitTransaction fruitTransaction : fruitTransactions) {
operationStrategy.get(fruitTransaction.getOperation())
.handle(fruitTransaction.getFruit(),
fruitTransaction.getQuantity());
}
handle(INPUT_2_FILE_NAME);
assertEquals(FruitDaoImpl.getFruits().get("banana"), 155);
assertEquals(FruitDaoImpl.getFruits().get("apple"), 160);
}

@Test
void handleInput3File_Ok() {
URL resources = classLoader.getResource(INPUT_3_FILE_NAME);
handle(INPUT_3_FILE_NAME);
assertEquals(FruitDaoImpl.getFruits().get("banana"), 115);
assertEquals(FruitDaoImpl.getFruits().get("apple"), 160);
assertEquals(FruitDaoImpl.getFruits().get("pear"), 140);
fruitDao.remove("pear");
}

private void handle(String fileName) {
URL resources = classLoader.getResource(fileName);
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> fruitTransactions = parseService.parse(readFromFile);
for (FruitTransaction fruitTransaction : fruitTransactions) {
operationStrategy.get(fruitTransaction.getOperation())
.handle(fruitTransaction.getFruit(),
fruitTransaction.getQuantity());
}
assertEquals(FruitDaoImpl.getFruits().get("banana"), 115);
assertEquals(FruitDaoImpl.getFruits().get("apple"), 160);
assertEquals(FruitDaoImpl.getFruits().get("pear"), 140);
}
}
34 changes: 15 additions & 19 deletions src/test/java/core/basesyntax/service/ParseServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import org.junit.jupiter.api.Test;

public class ParseServiceImplTest {
private static FileReader fileReader;
private static ParseService parseService;
private static final String INPUT_1_FILE_NAME = "input1.csv";
private static final String INPUT_2_FILE_NAME = "input2.csv";
private static final String INPUT_3_FILE_NAME = "input3.csv";
private static final String INVALID_OPERATION_FILE_NAME = "invalid1.csv";
private static final String INVALID_QUANTITY_FILE_NAME = "invalid2.csv";
private static FileReader fileReader;
private static ParseService parseService;
private static ClassLoader classLoader;

@BeforeAll
Expand All @@ -30,8 +30,6 @@ static void beforeAll() {

@Test
void parseInput1File_Ok() {
URL resources = classLoader.getResource(INPUT_1_FILE_NAME);
List<String> result = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> expected = new ArrayList<>(List.of(
new FruitTransaction("banana", 20, FruitTransaction.Operation.BALANCE),
new FruitTransaction("apple", 100, FruitTransaction.Operation.BALANCE),
Expand All @@ -41,14 +39,12 @@ void parseInput1File_Ok() {
new FruitTransaction("apple", 20, FruitTransaction.Operation.PURCHASE),
new FruitTransaction("banana", 5, FruitTransaction.Operation.PURCHASE),
new FruitTransaction("banana", 50, FruitTransaction.Operation.SUPPLY)));
List<FruitTransaction> actual = parseService.parse(result);
List<FruitTransaction> actual = parse(INPUT_1_FILE_NAME);
assertEquals(expected, actual);
}

@Test
void parseInput2File_Ok() {
URL resources = classLoader.getResource(INPUT_2_FILE_NAME);
List<String> result = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> expected = new ArrayList<>(List.of(
new FruitTransaction("banana", 200, FruitTransaction.Operation.BALANCE),
new FruitTransaction("apple", 150, FruitTransaction.Operation.BALANCE),
Expand All @@ -58,14 +54,12 @@ void parseInput2File_Ok() {
new FruitTransaction("banana", 20, FruitTransaction.Operation.RETURN),
new FruitTransaction("banana", 5, FruitTransaction.Operation.PURCHASE),
new FruitTransaction("banana", 15, FruitTransaction.Operation.SUPPLY)));
List<FruitTransaction> actual = parseService.parse(result);
List<FruitTransaction> actual = parse(INPUT_2_FILE_NAME);
assertEquals(expected, actual);
}

@Test
void parseInput3File_Ok() {
URL resources = classLoader.getResource(INPUT_3_FILE_NAME);
List<String> result = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> expected = new ArrayList<>(List.of(
new FruitTransaction("pear", 200, FruitTransaction.Operation.BALANCE),
new FruitTransaction("apple", 150, FruitTransaction.Operation.BALANCE),
Expand All @@ -77,21 +71,23 @@ void parseInput3File_Ok() {
new FruitTransaction("banana", 5, FruitTransaction.Operation.PURCHASE),
new FruitTransaction("banana", 15, FruitTransaction.Operation.SUPPLY),
new FruitTransaction("banana", 20, FruitTransaction.Operation.PURCHASE)));
List<FruitTransaction> actual = parseService.parse(result);
List<FruitTransaction> actual = parse(INPUT_3_FILE_NAME);
assertEquals(expected, actual);
}

@Test
void parseInvalidOperationFile_Not_Ok() {
URL resources = classLoader.getResource(INVALID_OPERATION_FILE_NAME);
List<String> result = fileReader.readFromFile(new File(resources.getFile()));
assertThrows(RuntimeException.class, () -> parseService.parse(result));
void parseInvalidOperationFile_notOk() {
assertThrows(RuntimeException.class, () -> parse(INVALID_OPERATION_FILE_NAME));
}

@Test
void parseInvalidQuantityFile_Not_Ok() {
URL resources = classLoader.getResource(INVALID_QUANTITY_FILE_NAME);
List<String> result = fileReader.readFromFile(new File(resources.getFile()));
assertThrows(RuntimeException.class, () -> parseService.parse(result));
void parseInvalidQuantityFile_notOk() {
assertThrows(RuntimeException.class, () -> parse(INVALID_QUANTITY_FILE_NAME));
}

private List<FruitTransaction> parse(String fileName) {
URL resources = classLoader.getResource(fileName);
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile()));
return parseService.parse(readFromFile);
}
}
Loading

0 comments on commit b4141ae

Please sign in to comment.