-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
covered solution with tests #817
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job.
Let's add .DS_Store files to gitignore.
static void beforeAll() { | ||
storageDao = new StorageDaoImpl(); | ||
expectedMap = new HashMap<>(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map<Fruit, Integer> actualMap = storageDao.getALl(); | ||
assertEquals(expectedMap, actualMap); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have method isInStorage
, test it too.
transactionWithNegativeAmount.setOperation(Operation.BALANCE); | ||
transactionWithNegativeAmount.setFruitName(FRUIT_NAME); | ||
transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integer expectedFruitAmount = FRUIT_AMOUNT; | ||
Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); | ||
assertEquals(expectedFruitAmount, actualFruitAmount); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
@Test | ||
void updateStorage_fruitAmountIsNegative_isNotOk() { | ||
assertThrows(RuntimeException.class, () -> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the message of exception or use other exception type.
transactionWithLessAmount.setOperation(Operation.PURCHASE); | ||
transactionWithLessAmount.setFruitName(FRUIT_NAME); | ||
transactionWithLessAmount.setAmount(FRUIT_LESS_AMOUNT); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
@Test | ||
void updateStorage_emptyStorage_isNotOk() { | ||
assertThrows(RuntimeException.class, () -> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be sure that business exception was thrown.
transactionWithNegativeAmount.setOperation(Operation.RETURN); | ||
transactionWithNegativeAmount.setFruitName(FRUIT_NAME); | ||
transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integer expectedFruitAmount = FRUIT_AMOUNT * NUMBER_OF_OPERATION; | ||
Integer actualFruitAmount = Storage.fruits.get(new Fruit(FRUIT_NAME)); | ||
assertEquals(expectedFruitAmount, actualFruitAmount); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transactionWithNegativeAmount.setOperation(Operation.SUPPLY); | ||
transactionWithNegativeAmount.setFruitName(FRUIT_NAME); | ||
transactionWithNegativeAmount.setAmount(FRUIT_NEGATIVE_AMOUNT); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical points to consider
} | ||
|
||
@Test | ||
public void readData_fileNotExistOrBadPath_notOk() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you choose to put public here?
Storage.fruits.put(BANANA, 100); | ||
Storage.fruits.put(GRAPE, 40); | ||
Storage.fruits.put(APPLE, 90); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to be magic numbers
|
||
public static Operation getOperationOf(String letter) { | ||
return Arrays.stream(Operation.values()) | ||
.filter(o -> o.getCodeOfOperation().equals(letter)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please choose a proper name for parameters in the lambdas; other developers will need to spend additional time figuring out what 'o' means
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
} | ||
|
||
public boolean isInStorage(String name) { | ||
return (get(name) != null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(get(name) != null)
these parentheses are redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
|
||
boolean isInStorage(String name); | ||
|
||
Map<Fruit, Integer> getALl(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: a little problem with the name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
if (!storageDao.isInStorage(transaction.getFruitName())) { | ||
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transaction.getFruitName() can be extracted to a separate variable to avoid code duplication
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty line
@Override | ||
public String createReport() { | ||
return storageDao.getALl().entrySet().stream() | ||
.map(e -> e.getKey().getName() + SEPARATOR + e.getValue()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameter naming again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
void storageDao_add_isOk() { | ||
storageDao.add(fruit, AMOUNT); | ||
Map<Fruit, Integer> actualMap = Storage.fruits; | ||
assertEquals(expectedMap, actualMap); | ||
} | ||
|
||
@Test | ||
void storageDao_getExistingFruit_isOk() { | ||
Storage.fruits.put(fruit, AMOUNT); | ||
Map.Entry<Fruit, Integer> 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<Fruit, Integer> actualEntry = storageDao.get(NAME_OF_NOT_EXISTING_FRUIT); | ||
assertNull(actualEntry); | ||
} | ||
|
||
@Test | ||
void storageDao_getAll_isOk() { | ||
Storage.fruits.put(fruit, AMOUNT); | ||
Map<Fruit, Integer> 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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to divide statements in tests by the following principle:
https://medium.com/@gitaeklee/given-when-then-junit-test-ba49564303e7
writing comments like //when //then is optional; you can divide everything by adding empty lines between statements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other mentors' comments are still exist.
|
||
boolean isInStorage(String name); | ||
|
||
Map<Fruit, Integer> getALl(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
} | ||
|
||
public boolean isInStorage(String name) { | ||
return (get(name) != null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
|
||
public static Operation getOperationOf(String letter) { | ||
return Arrays.stream(Operation.values()) | ||
.filter(o -> o.getCodeOfOperation().equals(letter)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
if (!storageDao.isInStorage(transaction.getFruitName())) { | ||
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.skip(ROW_TO_SKIP) | ||
.toList(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't read data from file: " + file); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this message as constant.
@Override | ||
public String createReport() { | ||
return storageDao.getALl().entrySet().stream() | ||
.map(e -> e.getKey().getName() + SEPARATOR + e.getValue()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still exists.
@Override | ||
public OperationHandler getOperationHandler(Operation operation) { | ||
if (operationHandlerMap == null) { | ||
throw new RuntimeException("Map with handlers is NULL!!!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make message as constant.
try { | ||
Files.write(Path.of(toFile), report.getBytes()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't write data to file: " + toFile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this message as constant.
} | ||
|
||
@Test | ||
public void readData_fileNotExistOrBadPath_notOk() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void readData_fileNotExistOrBadPath_notOk() { | |
void readData_fileNotExistOrBadPath_notOk() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job
…/add_link_to_readme add link to readme file
No description provided.