Skip to content

Commit

Permalink
fixed test problems
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiikonov committed Dec 11, 2024
1 parent fe131ea commit 77fc789
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
11 changes: 5 additions & 6 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import core.basesyntax.service.ShopServiceImpl;
import core.basesyntax.writer.FileWriterService;
import core.basesyntax.writer.FileWriterServiceImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -33,11 +32,11 @@ public static void main(String[] args) {
final List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport);

// 3. Create and feel the map with all OperationHandler implementations
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>();
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation());
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation());
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation());
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation());
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = Map.of(
FruitTransaction.Operation.BALANCE, new BalanceOperation(),
FruitTransaction.Operation.PURCHASE, new PurchaseOperation(),
FruitTransaction.Operation.RETURN, new ReturnOperation(),
FruitTransaction.Operation.SUPPLY, new SupplyOperation());
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers);

// 4. Process the incoming transactions with applicable OperationHandler implementations
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/core/basesyntax/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void setUp() {
}

@Test
void converter_testConverterToTransactions_ok() {
void converter_validTransactions_ok() {
List<String> inputData = List.of(
"operation,fruit,quantity",
"b,banana,20",
Expand All @@ -31,17 +31,17 @@ void converter_testConverterToTransactions_ok() {
new FruitTransaction(FruitTransaction.Operation.PURCHASE, "banana", 10)
);
Assertions.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(),
"Операції не збігаються");
"Transactions do not match");
Assertions.assertEquals(expected.getFruit(), actual.getFruit(),
"Фрукти не збігаються");
"The fruits do not match");
Assertions.assertEquals(expected.getQuantity(), actual.getQuantity(),
"Кількість не збігається");
"The quantity does not match");
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/test/java/core/basesyntax/FileReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ void readFromFile_fileDoesNotExist_notOk() {

@Test
void readFromFile_directoryInsteadOfFile_notOk() {
String directoryPath = "src/main/resources/testDirectory";
Path directory = Path.of(directoryPath);
Path tempDirectory = null;
try {
Files.createDirectory(directory);

tempDirectory = Files.createTempDirectory("testDirectory");
Path finalTempDirectory = tempDirectory;
RuntimeException exception = Assertions.assertThrows(RuntimeException.class, () -> {
fileReader.readFromFile(directoryPath);
fileReader.readFromFile(finalTempDirectory.toString());
});

Assertions.assertTrue(exception.getMessage()
.contains("Can't read file " + directoryPath));
.contains("Can't read file " + tempDirectory));
} catch (IOException e) {
Assertions.fail("Failed to create test directory");
Assertions.fail("Failed to create temporary directory", e);
} finally {
try {
Files.deleteIfExists(directory);
} catch (IOException e) {
Assertions.fail("Failed to delete test directory");
if (tempDirectory != null) {
try {
Files.deleteIfExists(tempDirectory);
} catch (IOException e) {
Assertions.fail("Failed to delete temporary directory", e);
}
}
}
}
Expand Down
43 changes: 22 additions & 21 deletions src/test/java/core/basesyntax/ShopServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import core.basesyntax.operations.SupplyOperation;
import core.basesyntax.service.ShopService;
import core.basesyntax.service.ShopServiceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
Expand All @@ -26,38 +24,41 @@ public class ShopServiceTest {
void setUp() {
Storage.fruits.clear();

Map<FruitTransaction.Operation, OperationHandler> handlers = new HashMap<>();
handlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation());
handlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation());
handlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation());
handlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation());
Map<FruitTransaction.Operation, OperationHandler> handlers = Map.of(
FruitTransaction.Operation.BALANCE, new BalanceOperation(),
FruitTransaction.Operation.PURCHASE, new PurchaseOperation(),
FruitTransaction.Operation.SUPPLY, new SupplyOperation(),
FruitTransaction.Operation.RETURN, new ReturnOperation()
);
OperationStrategy operationStrategy = new OperationStrategyImpl(handlers);
shopService = new ShopServiceImpl(operationStrategy);
}

@Test
void shopServiceTest_validTransaction_ok() {
List<FruitTransaction> transactions = new ArrayList<>();
transactions.add(new FruitTransaction(
FruitTransaction.Operation.BALANCE, "banana", 0));
transactions.add(new FruitTransaction(
FruitTransaction.Operation.SUPPLY, "banana", 10));
transactions.add(new FruitTransaction(
FruitTransaction.Operation.PURCHASE, "banana", 5));
transactions.add(new FruitTransaction(
FruitTransaction.Operation.RETURN, "banana", 5));
List<FruitTransaction> transactions = List.of(
new FruitTransaction(
FruitTransaction.Operation.BALANCE, "banana", 0),
new FruitTransaction(
FruitTransaction.Operation.SUPPLY, "banana", 10),
new FruitTransaction(
FruitTransaction.Operation.PURCHASE, "banana", 5),
new FruitTransaction(
FruitTransaction.Operation.RETURN, "banana", 5)
);
shopService.process(transactions);
Integer banana = Storage.fruits.getOrDefault("banana", 0);
Assertions.assertEquals(10, banana);
}

@Test
void shopServiceTest_invalidTransaction_notOk() {
List<FruitTransaction> transactions = new ArrayList<>();
transactions.add(new FruitTransaction(
FruitTransaction.Operation.BALANCE, "banana", -50));
transactions.add(new FruitTransaction(
FruitTransaction.Operation.PURCHASE, "banana", 20));
List<FruitTransaction> transactions = List.of(
new FruitTransaction(
FruitTransaction.Operation.BALANCE, "banana", -50),
new FruitTransaction(
FruitTransaction.Operation.PURCHASE, "banana", 20)
);
Assertions.assertThrows(IllegalArgumentException.class,
() -> shopService.process(transactions));
}
Expand Down

0 comments on commit 77fc789

Please sign in to comment.