Skip to content

Commit

Permalink
tests refactored, exception messages added
Browse files Browse the repository at this point in the history
  • Loading branch information
aliceklim committed Jun 10, 2023
1 parent 1773299 commit 4679634
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
@Getter
@Setter
@AllArgsConstructor
public class House {
public class HouseAttributes {

String motto;
String sigil;

@Override
public String toString() {
return "motto - '" + motto + "', sigil - '" + sigil + "'.";
return "motto - '" + motto + "', sigil - '" + sigil + "'";
}

}
28 changes: 15 additions & 13 deletions src/main/java/ru/faang/school/hashmap/task_1/HouseBook.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
package ru.faang.school.hashmap.task_1;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.Map;
@Getter

@RequiredArgsConstructor
public class HouseBook {

private final Map<String, House> houses = new HashMap<>();
private final Map<String, HouseAttributes> houses = new HashMap<>();

public String addNewHouse(String houseName, String houseMotto, String houseSigil){
if (houses.containsKey(houseName)){
throw new HouseBookException("The house " + houseName + " already exists in the book.");
throw new HouseBookException(String.format(Message.HOUSE_ALREADY_EXISTS, houseName));
}
houses.put(houseName, new House(houseMotto, houseSigil));
return "A house " + houseName + " added. There are " + houses.size() + " in the list so far.";
houses.put(houseName, new HouseAttributes(houseMotto, houseSigil));
return String.format(Message.HOUSE_ADDED, houseName, houses.size());
}

public String deleteHouse(String houseName){
if (houses.containsKey(houseName)){
houses.remove(houseName);
return "A house " + houseName + " deleted. There are " + houses.size() + " in the list so far.";
return String.format(Message.HOUSE_DELETED, houseName, houses.size());
}
throw new HouseBookException("The house " + houseName + " doesn't exist in the book.");
throw new HouseBookException(String.format(Message.HOUSE_DOESNT_EXIST, houseName));
}

public String getHouseSigil(String houseName){
if (houses.containsKey(houseName)){
return houses.get(houseName).sigil;
}
throw new HouseBookException("The house " + houseName + " doesn't exist in the book.");
throw new HouseBookException(String.format(Message.HOUSE_DOESNT_EXIST, houseName));
}

public void getAllHouses(){
if (houses.isEmpty()){
throw new HouseBookException("The housebook is empty for now :(");
throw new HouseBookException(Message.HOUSEBOOK_IS_EMPTY);
}
for (Map.Entry<String, House> entry : houses.entrySet()){
System.out.println("House name is " + entry.getKey() + ", " + entry.getValue());
for (Map.Entry<String, HouseAttributes> entry : houses.entrySet()){
System.out.printf((Message.HOUSE_INFO) + "%n", entry.getKey(), entry.getValue());
}
}

public int getHouseBookSize(){
return houses.size();
}
}
12 changes: 12 additions & 0 deletions src/main/java/ru/faang/school/hashmap/task_1/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.faang.school.hashmap.task_1;

public class Message {

public static final String HOUSE_DOESNT_EXIST = "The house %s doesn't exist in the book.";
public static final String HOUSE_ALREADY_EXISTS = "The house %s already exists in the book.";
public static final String HOUSE_DELETED = "A house %s deleted from the book. Houses in the book: %d.";
public static final String HOUSEBOOK_IS_EMPTY = "The housebook is empty for now :(";
public static final String HOUSE_INFO = "House name is %s, %s.";
public static final String HOUSE_ADDED = "A house %s added to the book. Houses in the book: %d.";

}
34 changes: 18 additions & 16 deletions src/test/java/ru/faang/school/hashmap/task_1/HouseBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

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

Expand All @@ -16,7 +12,7 @@ public class HouseBookTest {

@BeforeEach
void setUp(){
Map<String, House> houses = new HashMap<>();

houseBook = new HouseBook();
houseBook.addNewHouse("Lannister", "A Lannister always pays his debts",
"a golden lion on a field of crimson");
Expand All @@ -28,19 +24,19 @@ void setUp(){

@Test
void addNewHouseTest(){

houseBook.addNewHouse(
"Fire And Blood", "Targaryen", "a three-headed dragon breathing flames");

assertEquals(4, houseBook.getHouses().size());
String result = houseBook.addNewHouse(
"Targaryen", "Fire And Blood", "a three-headed dragon breathing flames");
assertEquals(4, houseBook.getHouseBookSize());
assertEquals("A house Targaryen added to the book. Houses in the book: 4.", result);

This comment has been minimized.

Copy link
@mighty-mhsl

mighty-mhsl Jun 10, 2023

Contributor

Я бы здесь тоже использовал форматирование сообщений из класса Message
Потому что иначе мы снова эти строчки дублируем) String.format поможет собрать реальную строку для проверки

}

@Test
void addNewHouseExistingInBookTest(){
HouseBookException exception = assertThrows(HouseBookException.class,
() -> houseBook.addNewHouse("Stark", "Winter is coming",
"a grey direwolf on a white background"));

assertThrows(HouseBookException.class,
() -> houseBook.addNewHouse("Stark", "Winter is coming",
"a grey direwolf on a white background"));
assertEquals("The house Stark already exists in the book.", exception.getMessage());
}

@Test
Expand All @@ -51,17 +47,23 @@ void getHouseSigilTest(){

@Test
void getNotExistingHouseSigilTest(){
assertThrows(HouseBookException.class, () -> houseBook.getHouseSigil("Martell"));
HouseBookException exception = assertThrows(HouseBookException.class,
() -> houseBook.getHouseSigil("Martell"));
assertEquals("The house Martell doesn't exist in the book.", exception.getMessage());
}

@Test
void deleteHouseTest(){
houseBook.deleteHouse("Stark");
assertEquals(2, houseBook.getHouses().size());
String result = houseBook.deleteHouse("Stark");
assertEquals(2, houseBook.getHouseBookSize());
assertEquals("A house Stark deleted from the book. Houses in the book: 2.", result);
}

@Test
void deleteNotExistingHouse(){
assertThrows(HouseBookException.class, () -> houseBook.deleteHouse("Martell"));
HouseBookException exception = assertThrows(
HouseBookException.class, () -> houseBook.deleteHouse("Martell"));
assertEquals("The house Martell doesn't exist in the book.", exception.getMessage());
}
}

0 comments on commit 4679634

Please sign in to comment.