Skip to content

Commit

Permalink
fix some test
Browse files Browse the repository at this point in the history
  • Loading branch information
don-bigdad committed Sep 18, 2023
1 parent 26cb543 commit 59ac7b4
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import core.basesyntax.db.Storage;
import core.basesyntax.model.Fruit;
import core.basesyntax.model.FruitTransaction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;

class BalanceOperationHandlerTest {
private static Fruit apple;
private static OperationHandler balanceOperationHandler;
private final FruitTransaction fruitTransaction = new FruitTransaction();

private static FruitTransaction fruitTransaction;
@BeforeAll
static void setUp() {
apple = new Fruit("apple");
balanceOperationHandler = new BalanceOperationHandler();
fruitTransaction = new FruitTransaction();
}

@BeforeEach
@AfterEach
void clearStorage() {
Storage.storage.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
class PurchaseOperationHandlerTest {
private static Fruit apple;
private static OperationHandler purchaseOperationHandler;
private final FruitTransaction fruitTransaction = new FruitTransaction();

private static FruitTransaction fruitTransaction;
@BeforeAll
static void setUp() {
apple = new Fruit("apple");
purchaseOperationHandler = new PurchaseOperationHandler();
fruitTransaction = new FruitTransaction();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import core.basesyntax.db.Storage;
import core.basesyntax.model.Fruit;
import core.basesyntax.model.FruitTransaction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;

class ReturnOperationHandlerTest {
private static Fruit apple;
private static OperationHandler returnOperationHandler;
private final FruitTransaction fruitTransaction = new FruitTransaction();

private static FruitTransaction fruitTransaction;
@BeforeAll
static void setUp() {
apple = new Fruit("apple");
returnOperationHandler = new ReturnOperationHandler();
fruitTransaction = new FruitTransaction();
}

@BeforeEach
@AfterEach
void clearStorage() {
Storage.storage.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
import core.basesyntax.db.Storage;
import core.basesyntax.model.Fruit;
import core.basesyntax.model.FruitTransaction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;

class SupplyOperationHandlerTest {
private static Fruit apple;
private static OperationHandler supplyOperationHandler;
private final FruitTransaction fruitTransaction = new FruitTransaction();
private static FruitTransaction fruitTransaction;

@BeforeAll
static void setUp() {
apple = new Fruit("apple");
supplyOperationHandler = new SupplyOperationHandler();
Storage.storage.clear();
fruitTransaction = new FruitTransaction();
}

@BeforeEach
@AfterEach
void clearStorage() {
Storage.storage.clear();
}
Expand All @@ -34,4 +33,16 @@ void appendProductBySupplyHandler_Ok() {
Integer actual = Storage.storage.get(apple);
Assertions.assertEquals(expected, actual);
}

@Test
void appendProductToExistFruit_Ok() {
fruitTransaction.setOperation(FruitTransaction.Operation.SUPPLY);
fruitTransaction.setQuantity(33);
fruitTransaction.setFruit(apple);
Storage.storage.put(apple,22);
supplyOperationHandler.apply(fruitTransaction);
Integer expected = 55;
Integer actual = Storage.storage.get(apple);
Assertions.assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@

class OperationProcessorServiceTest {
private static Map<FruitTransaction.Operation, OperationHandler> handlerMap;
private static final Fruit banana = new Fruit("banana");
private static final Fruit apple = new Fruit("apple");
private static int initialBananaBalance;
private static int initialAppleBalance;

private static Fruit banana;
private static Fruit apple;
@BeforeAll
static void beforeAll() {
banana = new Fruit("banana");
apple = new Fruit("apple");
Storage.storage.put(banana,15);
Storage.storage.put(apple,35);
initialBananaBalance = Storage.storage.get(banana);
initialAppleBalance = Storage.storage.get(apple);
handlerMap = new HashMap<>();
handlerMap.put(FruitTransaction.Operation.BALANCE, new BalanceOperationHandler());
handlerMap.put(FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler());
Expand All @@ -41,6 +38,8 @@ static void beforeAll() {

@Test
void processValidTransactions() {
int initialBananaBalance = Storage.storage.get(banana);
int initialAppleBalance = Storage.storage.get(apple);
int quantity = 25;
FruitTransaction transaction1 = new FruitTransaction();
transaction1.setFruit(banana);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ void generateValidReport_ok() {
.toString();
Assertions.assertEquals(expected,report);
}

@Test
void generateValidReportEmptyStorage_Ok() {
String report = reportService.generateReport();
String expected = "fruit,quantity" + System.lineSeparator();
Assertions.assertEquals(expected,report);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@

import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class OutputValidatorImplTest {
private final String invalidFileForTest = "OutputValidatorinValidTest.csv";
private final String invalidQuantityFile = "resources/FileWithInvalidQuantity.csv";
private final OutputValidatorImpl outputValidator = new OutputValidatorImpl();
private static OutputValidatorImpl outputValidator;

@BeforeAll
static void setUp() {
outputValidator = new OutputValidatorImpl();
}


@Test
void fileValid_Ok() {
void validFileValidation_Ok() {
String validFileForTest = "src/test/java/resources/OutputValidatorValidTest.csv";
outputValidator.validateFile(validFileForTest);
Assertions.assertTrue(outputValidator.validateFile(validFileForTest));
}

@Test
void fileInvalid_NotOk() {
String invalidFileForTest = "src/test/java/resources/OutputValidatorInvalidTest.csv";
Assert.assertThrows(RuntimeException.class,
() -> outputValidator.validateFile(invalidFileForTest));
}

@Test
void invalidQuantity_NotOk() {
String invalidQuantityFile = "src/test/java/resources/FileWithInvalidQuantity.csv";
Assert.assertThrows(RuntimeException.class,
() -> outputValidator.validateFile(invalidQuantityFile));
}

@Test
void noDataInFile_NotOk() {
String noDataFile = "src/test/java/resources/NullFile.csv";
Assert.assertThrows(RuntimeException.class,
() -> outputValidator.validateFile(noDataFile));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import java.util.List;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class ReaderValidationImplTest {
private final ReaderValidation readerValidation = new ReaderValidationImpl();
private static ReaderValidation readerValidation;
private final List<String> emptyList = new ArrayList<>();
private final List<String> invalidFirstLine = new ArrayList<>(List.of(
"type,fruit,quantit",
Expand All @@ -33,6 +34,11 @@ class ReaderValidationImplTest {
"s,apple,35"
));

@BeforeAll
static void setUp() {
readerValidation = new ReaderValidationImpl();
}

@Test
void correctSkipInvalidQuantity_Ok() {
List<String> expected = new ArrayList<>(List.of(
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions src/test/java/resources/invalidFirstLine
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type,fruit,quantit
b,banana,20
s,apple,20

0 comments on commit 59ac7b4

Please sign in to comment.