Skip to content

Commit

Permalink
fixed all mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiikonov committed Dec 12, 2024
1 parent 77fc789 commit 0a65b46
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 80 deletions.
28 changes: 15 additions & 13 deletions src/test/java/core/basesyntax/ConverterTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package core.basesyntax;

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

import core.basesyntax.converter.DataConverter;
import core.basesyntax.converter.DataConverterImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -17,7 +19,7 @@ void setUp() {
}

@Test
void converter_validTransactions_ok() {
void convertToTransaction_validTransactions_ok() {
List<String> inputData = List.of(
"operation,fruit,quantity",
"b,banana,20",
Expand All @@ -30,31 +32,31 @@ void converter_validTransactions_ok() {
new FruitTransaction(FruitTransaction.Operation.SUPPLY, "apple", 50),
new FruitTransaction(FruitTransaction.Operation.PURCHASE, "banana", 10)
);
Assertions.assertEquals(expectedTransaction.size(), actualTransaction.size(),
assertEquals(expectedTransaction.size(), actualTransaction.size(),
"The number of transactions does not match");
for (int i = 0; i < expectedTransaction.size(); i++) {
FruitTransaction expected = expectedTransaction.get(i);
FruitTransaction actual = actualTransaction.get(i);

Assertions.assertEquals(expected.getOperation(), actual.getOperation(),
assertEquals(expected.getOperation(), actual.getOperation(),
"Transactions do not match");
Assertions.assertEquals(expected.getFruit(), actual.getFruit(),
assertEquals(expected.getFruit(), actual.getFruit(),
"The fruits do not match");
Assertions.assertEquals(expected.getQuantity(), actual.getQuantity(),
assertEquals(expected.getQuantity(), actual.getQuantity(),
"The quantity does not match");
}
}

@Test
void converter_testInvalidDataToConvert_notOk() {
void convertToTransaction_testInvalidDataToConvert_notOk() {
List<String> input = List.of(
"type,fruit,quantity",
"b,banana"
);
Exception exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
dataConverter.convertToTransaction(input);
});
Assertions.assertEquals("Invalid number of fields in row: b,banana",
assertEquals("Invalid number of fields in row: b,banana",
exception.getMessage());
}

Expand All @@ -65,10 +67,10 @@ void convertToTransaction_invalidQuantity_notOk() {
"p,banana,notANumber"
);

Exception exception = Assertions.assertThrows(NumberFormatException.class, () -> {
Exception exception = assertThrows(NumberFormatException.class, () -> {
dataConverter.convertToTransaction(input);
});
Assertions.assertEquals("Invalid quantity for fruit 'banana': notANumber",
assertEquals("Invalid quantity for fruit 'banana': notANumber",
exception.getMessage());
}

Expand All @@ -78,10 +80,10 @@ void convertToTransaction_extraFields_notOk() {
"type,fruit,quantity",
"p,banana,10, extraField"
);
Exception exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
dataConverter.convertToTransaction(input);
});
Assertions.assertEquals("Invalid number of fields in row: p,banana,10, extraField",
assertEquals("Invalid number of fields in row: p,banana,10, extraField",
exception.getMessage());
}
}
92 changes: 51 additions & 41 deletions src/test/java/core/basesyntax/FileReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,83 @@
package core.basesyntax;

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 static org.junit.jupiter.api.Assertions.fail;

import core.basesyntax.dao.FileReader;
import core.basesyntax.dao.FileReaderImpl;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

public class FileReaderTest {
private final FileReader fileReader = new FileReaderImpl();
private final List<Path> tempPaths = new ArrayList<>();

@AfterEach
void cleanUp() {
for (Path path : tempPaths) {
try {
Files.deleteIfExists(path);
} catch (IOException e) {
fail("Failed to delete temporary file or directory: " + path, e);
}
}
tempPaths.clear();
}

@Test
void readFromFile_validFile_ok() throws IOException {
String testFilePath = "src/main/resources/testFile.csv";
String fileContent = "type,fruit,quantity\nb,banana,20\ns,apple,50";
Files.writeString(Path.of(testFilePath), fileContent);
List<String> result = fileReader.readFromFile(testFilePath);
Assertions.assertEquals(3, result.size());
Assertions.assertEquals("type,fruit,quantity", result.get(0));
Assertions.assertEquals("b,banana,20", result.get(1));
Assertions.assertEquals("s,apple,50", result.get(2));
Files.delete(Path.of(testFilePath));
Path testFile = createTempFile("testFile.csv", """
type,fruit,quantity
b,banana,20
s,apple,50
""");

List<String> result = fileReader.readFromFile(testFile.toString());
assertEquals(3, result.size());
assertEquals("type,fruit,quantity", result.get(0));
assertEquals("b,banana,20", result.get(1));
assertEquals("s,apple,50", result.get(2));
}

@Test
void readFromFile_emptyFile_ok() throws IOException {
String emptyFilePath = "src/main/resources/emptyFile.csv";
Files.createFile(Path.of(emptyFilePath));
List<String> result = fileReader.readFromFile(emptyFilePath);
Assertions.assertTrue(result.isEmpty());
Files.delete(Path.of(emptyFilePath));
Path emptyFile = createTempFile("emptyFile.csv", "");
List<String> result = fileReader.readFromFile(emptyFile.toString());
assertTrue(result.isEmpty());
}

@Test
void readFromFile_fileDoesNotExist_notOk() {
String nonExistentFilePath = "src/main/resources/nonExistentFile.csv";

RuntimeException exception = Assertions.assertThrows(RuntimeException.class, () -> {
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
fileReader.readFromFile(nonExistentFilePath);
});

Assertions.assertTrue(exception.getMessage()
.contains("Can't read file " + nonExistentFilePath));
assertTrue(exception.getMessage().contains("Can't read file " + nonExistentFilePath));
}

@Test
void readFromFile_directoryInsteadOfFile_notOk() {
Path tempDirectory = null;
try {
tempDirectory = Files.createTempDirectory("testDirectory");
Path finalTempDirectory = tempDirectory;
RuntimeException exception = Assertions.assertThrows(RuntimeException.class, () -> {
fileReader.readFromFile(finalTempDirectory.toString());
});
void readFromFile_directoryInsteadOfFile_notOk() throws IOException {
Path tempDirectory = Files.createTempDirectory("testDirectory");
tempPaths.add(tempDirectory);

Assertions.assertTrue(exception.getMessage()
.contains("Can't read file " + tempDirectory));
} catch (IOException e) {
Assertions.fail("Failed to create temporary directory", e);
} finally {
if (tempDirectory != null) {
try {
Files.deleteIfExists(tempDirectory);
} catch (IOException e) {
Assertions.fail("Failed to delete temporary directory", e);
}
}
}
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
fileReader.readFromFile(tempDirectory.toString());
});

assertTrue(exception.getMessage().contains("Can't read file " + tempDirectory));
}

private Path createTempFile(String fileName, String content) throws IOException {
Path tempFile = Files.createTempFile(fileName, null);
Files.writeString(tempFile, content);
tempPaths.add(tempFile);
return tempFile;
}
}
10 changes: 6 additions & 4 deletions src/test/java/core/basesyntax/FileWriterTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package core.basesyntax;

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

import core.basesyntax.writer.FileWriterService;
import core.basesyntax.writer.FileWriterServiceImpl;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -25,12 +27,12 @@ void cleanUp() throws IOException {
}

@Test
void fileWriterService_createsFileWithCorrectContent_ok() throws IOException {
void writeOnFile_createsFileWithCorrectContent_ok() throws IOException {
String content = "banana,10\napple,20\n";

fileWriterService.write(content, FILE_PATH.toString());
Assertions.assertTrue(Files.exists(FILE_PATH));
assertTrue(Files.exists(FILE_PATH));
String actual = Files.readString(FILE_PATH);
Assertions.assertEquals(content, actual);
assertEquals(content, actual);
}
}
11 changes: 6 additions & 5 deletions src/test/java/core/basesyntax/OperationHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package core.basesyntax;

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

import core.basesyntax.db.Storage;
import core.basesyntax.handler.OperationHandler;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.operations.BalanceOperation;
import core.basesyntax.operations.PurchaseOperation;
import core.basesyntax.operations.ReturnOperation;
import core.basesyntax.operations.SupplyOperation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -24,7 +25,7 @@ void operationHandler_balanceOperation_ok() {
"banana", 0);
OperationHandler balanceOperation = new BalanceOperation();
balanceOperation.apply(transaction);
Assertions.assertEquals(0, Storage.fruits.get("banana"));
assertEquals(0, Storage.fruits.get("banana"));
}

@Test
Expand All @@ -33,7 +34,7 @@ void operationHandler_supplyOperation_ok() {
FruitTransaction.Operation.SUPPLY, "apple", 10);
OperationHandler supplyOperation = new SupplyOperation();
supplyOperation.apply(transaction);
Assertions.assertEquals(10, Storage.fruits.get("apple"));
assertEquals(10, Storage.fruits.get("apple"));
}

@Test
Expand All @@ -43,7 +44,7 @@ void operationHandler_purchaseOperation_ok() {
FruitTransaction.Operation.PURCHASE, "apple", 50);
OperationHandler purchaseOperation = new PurchaseOperation();
purchaseOperation.apply(transaction);
Assertions.assertEquals(50, Storage.fruits.get("apple"));
assertEquals(50, Storage.fruits.get("apple"));
}

@Test
Expand All @@ -53,6 +54,6 @@ void operationHandler_returnOperation_ok() {
FruitTransaction.Operation.RETURN, "banana", 10);
OperationHandler returnOperation = new ReturnOperation();
returnOperation.apply(transaction);
Assertions.assertEquals(20, Storage.fruits.get("banana"));
assertEquals(20, Storage.fruits.get("banana"));
}
}
42 changes: 36 additions & 6 deletions src/test/java/core/basesyntax/OperationStrategyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package core.basesyntax;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import core.basesyntax.handler.OperationHandler;
import core.basesyntax.handler.OperationStrategy;
import core.basesyntax.handler.OperationStrategyImpl;
Expand All @@ -10,7 +13,6 @@
import core.basesyntax.operations.SupplyOperation;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -28,17 +30,45 @@ void setUp() {
}

@Test
void operationStrategy_returnsCorrectHandler_ok() {
OperationHandler handler = operationStrategy.getHandler(FruitTransaction.Operation.SUPPLY);
void operationStrategy_returnsCorrectHandlerForSupply_ok() {
OperationHandler handler = operationStrategy.getHandler(
FruitTransaction.Operation.SUPPLY);

Assertions.assertTrue(handler instanceof SupplyOperation,
assertTrue(handler instanceof SupplyOperation,
"Handler for SUPPLY should be of type SupplyOperation.");
}

@Test
void operationStrategy_returnsCorrectHandlerForPurchase_ok() {
OperationHandler handler = operationStrategy.getHandler(
FruitTransaction.Operation.PURCHASE);

assertTrue(handler instanceof PurchaseOperation,
"Handler for Purchase should be of type PurchaseOperation.");
}

@Test
void operationStrategy_returnsCorrectHandlerForBalance_ok() {
OperationHandler handler = operationStrategy.getHandler(
FruitTransaction.Operation.BALANCE);

assertTrue(handler instanceof BalanceOperation,
"Handler for Balance should be of type BalanceOperation.");
}

@Test
void operationStrategy_returnsCorrectHandlerForReturn_ok() {
OperationHandler handler = operationStrategy.getHandler(
FruitTransaction.Operation.RETURN);

assertTrue(handler instanceof ReturnOperation,
"Handler for Return should be of type ReturnOperation.");
}

@Test
void operationStrategy_empty_notOk() {
Assertions.assertThrows(IllegalArgumentException.class,
assertThrows(IllegalArgumentException.class,
() -> operationStrategy.getHandler(null),
"IllegalArgumentException.");
"Expected IllegalArgumentException when operation is null.");
}
}
13 changes: 8 additions & 5 deletions src/test/java/core/basesyntax/ReportGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package core.basesyntax;

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

import core.basesyntax.db.Storage;
import core.basesyntax.generator.ReportGenerator;
import core.basesyntax.generator.ReportGeneratorImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -18,15 +20,16 @@ void setUp() {
}

@Test
void reportGenerator_validReport_ok() {
void generateReport_validReport_ok() {
Storage.fruits.put("banana", 10);
Storage.fruits.put("apple", 20);
String expected = "banana,10\napple,20\n";
Assertions.assertEquals(expected, reportGenerator.getReport());
assertEquals(expected, reportGenerator.getReport());
}

@Test
void reportGenerator_EmptyStorage_notOk() {
Assertions.assertThrows(RuntimeException.class, () -> reportGenerator.getReport());
void generateReport_emptyStorage_notOk() {
assertThrows(RuntimeException.class, () -> reportGenerator.getReport(),
"Generating a report with an empty storage should throw a RuntimeException.");
}
}
Loading

0 comments on commit 0a65b46

Please sign in to comment.