From 4679634a89475dff4524d9ec8ac2a1e247c115a2 Mon Sep 17 00:00:00 2001 From: *Yuliya Date: Sat, 10 Jun 2023 10:31:51 +0200 Subject: [PATCH] tests refactored, exception messages added --- .../{House.java => HouseAttributes.java} | 4 +-- .../school/hashmap/task_1/HouseBook.java | 28 ++++++++------- .../faang/school/hashmap/task_1/Message.java | 12 +++++++ .../school/hashmap/task_1/HouseBookTest.java | 34 ++++++++++--------- 4 files changed, 47 insertions(+), 31 deletions(-) rename src/main/java/ru/faang/school/hashmap/task_1/{House.java => HouseAttributes.java} (90%) create mode 100644 src/main/java/ru/faang/school/hashmap/task_1/Message.java diff --git a/src/main/java/ru/faang/school/hashmap/task_1/House.java b/src/main/java/ru/faang/school/hashmap/task_1/HouseAttributes.java similarity index 90% rename from src/main/java/ru/faang/school/hashmap/task_1/House.java rename to src/main/java/ru/faang/school/hashmap/task_1/HouseAttributes.java index 35734a39..d93fcc5e 100644 --- a/src/main/java/ru/faang/school/hashmap/task_1/House.java +++ b/src/main/java/ru/faang/school/hashmap/task_1/HouseAttributes.java @@ -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 + "'"; } } diff --git a/src/main/java/ru/faang/school/hashmap/task_1/HouseBook.java b/src/main/java/ru/faang/school/hashmap/task_1/HouseBook.java index 68b04976..2e590047 100644 --- a/src/main/java/ru/faang/school/hashmap/task_1/HouseBook.java +++ b/src/main/java/ru/faang/school/hashmap/task_1/HouseBook.java @@ -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 houses = new HashMap<>(); + private final Map 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 entry : houses.entrySet()){ - System.out.println("House name is " + entry.getKey() + ", " + entry.getValue()); + for (Map.Entry entry : houses.entrySet()){ + System.out.printf((Message.HOUSE_INFO) + "%n", entry.getKey(), entry.getValue()); } } + + public int getHouseBookSize(){ + return houses.size(); + } } diff --git a/src/main/java/ru/faang/school/hashmap/task_1/Message.java b/src/main/java/ru/faang/school/hashmap/task_1/Message.java new file mode 100644 index 00000000..40b78c97 --- /dev/null +++ b/src/main/java/ru/faang/school/hashmap/task_1/Message.java @@ -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."; + +} diff --git a/src/test/java/ru/faang/school/hashmap/task_1/HouseBookTest.java b/src/test/java/ru/faang/school/hashmap/task_1/HouseBookTest.java index 43faf3a3..61750783 100644 --- a/src/test/java/ru/faang/school/hashmap/task_1/HouseBookTest.java +++ b/src/test/java/ru/faang/school/hashmap/task_1/HouseBookTest.java @@ -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; @@ -16,7 +12,7 @@ public class HouseBookTest { @BeforeEach void setUp(){ - Map houses = new HashMap<>(); + houseBook = new HouseBook(); houseBook.addNewHouse("Lannister", "A Lannister always pays his debts", "a golden lion on a field of crimson"); @@ -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); } @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 @@ -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()); } }