From 26160be9c033d203798e0aad8c77d9528aa6d113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0=C5=A5astn=C3=BD?= Date: Thu, 28 Jul 2022 12:22:04 +0200 Subject: [PATCH 1/2] Throwing exceptions added to the House methods --- .../virtual_smart_home/house/House.java | 90 ++++++++++++++----- .../virtual_smart_home/house/HouseTest.java | 11 ++- 2 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java b/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java index bd6469e..aba7f0e 100644 --- a/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java +++ b/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java @@ -17,13 +17,14 @@ package io.patriot_framework.virtual_smart_home.house; import io.patriot_framework.virtual_smart_home.house.device.Device; - import java.util.Collections; import java.util.Map; +import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import javax.management.openmbean.KeyAlreadyExistsException; /** * House class is responsible for management of {@code Device} used in the Virtual Smart Home @@ -34,9 +35,12 @@ public final class House { private String houseName; private Map devices = new ConcurrentHashMap<>(); - public House(String houseName) { - this.houseName = houseName; - LOGGER.debug(String.format("Created new house with name \"%s\"",this.houseName)); + public House(String houseName) throws IllegalArgumentException { + if(houseName == null){ + throw new IllegalArgumentException("houseName can't be null"); + } + this.houseName = houseName; + LOGGER.debug(String.format("Created new house with name \"%s\"",this.houseName)); } /** @@ -44,20 +48,41 @@ public House(String houseName) { * * @param label string representing device name * @param device actual instance of device + * @throws IllegalArgumentException if one of the parameters is null + * @throws KeyAlreadyExistsException if house already contains device with given label */ - public void addDevice(String label, Device device) { - devices.put(label, device); + public void addDevice(String label, Device device) throws IllegalArgumentException, KeyAlreadyExistsException{ + if(label == null){ + throw new IllegalArgumentException("Label of the device can't be null"); + } + if(device == null){ + throw new IllegalArgumentException("Device parameter can't be null"); + } + final Device origDevice = devices.putIfAbsent(label, device); + if(origDevice != null){ + throw new KeyAlreadyExistsException(String.format("Device with label: %s is already exists", label)); + } LOGGER.debug(String.format("Device %s with label %s added to house %s", device, label, this.houseName)); - } + } /** * Method used to retrieve single device with certain label * * @param label label of desired device * @return instance of device with given label + * @throws IllegalArgumentException if parameter is null + * @throws NoSuchElementException if device with given label isn't in the House */ - public Device getDevice(String label) { - return devices.get(label); + public Device getDevice(String label) throws IllegalArgumentException, NoSuchElementException{ + if(label == null){ + throw new IllegalArgumentException("Label of the device can't be null"); + } + final Device device = devices.get(label); + if (device == null){ + throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); + }else{ + return device; + } } /** @@ -65,28 +90,39 @@ public Device getDevice(String label) { * * @param label label of device to be replaced * @param device instance of new device with given label + * @throws IllegalArgumentException if one of the parameters is null + * @throws NoSuchElementException if house doesn't contains device with given label */ - public void updateDevice(String label, Device device) { + public void updateDevice(String label, Device device) throws IllegalArgumentException, NoSuchElementException{ + if(label == null){ + throw new IllegalArgumentException("Label of the device can't be null"); + } + if(device == null){ + throw new IllegalArgumentException("Device parameter can't be null"); + } + if(!devices.containsKey(label)){ + throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); + } final Device origDevice = devices.put(label, device); - if(origDevice == null){ - LOGGER.warn(String.format("Device with label:%s does not exist in house:%s." - + " Adding new device:%s.", label, houseName, device)); - } else{ - LOGGER.debug(String.format("At house:%s device with label:%s updated. " + LOGGER.debug(String.format("At house:%s device with label:%s updated. " + "(Device:%s replaced by:%s", houseName, label, origDevice, device)); - } } /** * Method used to remove device from house object * * @param label label of device to be removed + * @throws IllegalArgumentException if label is null + * @throws NoSuchElementException if house doesn't contains device with given label */ - public void removeDevice(String label) { + public void removeDevice(String label) throws IllegalArgumentException, NoSuchElementException{ + if(label == null){ + throw new IllegalArgumentException("Label of the device can't be null"); + } final Device origDevice = devices.remove(label); if(origDevice == null){ - LOGGER.warn(String.format("Removing non-existing device with label:%s from house:%s)", label, houseName)); - } else{ + throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); + }else{ LOGGER.debug(String.format("At house:%s device:%s with label:%s removed.", houseName, origDevice, label)); } } @@ -116,10 +152,14 @@ public String getHouseName() { * Setter for house name * * @param houseName string with house name + * @throws IllegalArgumentException if houseName is null */ - public void setHouseName(String houseName) { // TODO: Remove? - LOGGER.debug(String.format("House with name:%s renamed to:%s", this.houseName, houseName)); + public void setHouseName(String houseName) throws IllegalArgumentException { // TODO: Remove? + if(houseName == null){ + throw new IllegalArgumentException("houseName can't be null"); + } this.houseName = houseName; + LOGGER.debug(String.format("House with name:%s renamed to:%s", this.houseName, houseName)); } /** @@ -135,10 +175,14 @@ public Map getDevices() { * Method used to setup or reset all the devices in house * * @param devices map of devices and their labels + * @throws IllegalArgumentException if map of devices is null */ - public void setDevices(Map devices) { // TODO: ? + public void setDevices(Map devices) throws IllegalArgumentException { // TODO: ? + if(devices == null){ + throw new IllegalArgumentException("Methd parameter devices can't be null"); + } + this.devices = devices; LOGGER.debug(String.format("At house:%s devices:%s set instead of devices:%s", houseName, devices, this.devices)); - this.devices = devices; } } diff --git a/src/test/java/io/patriot_framework/virtual_smart_home/house/HouseTest.java b/src/test/java/io/patriot_framework/virtual_smart_home/house/HouseTest.java index f338c29..af747c8 100644 --- a/src/test/java/io/patriot_framework/virtual_smart_home/house/HouseTest.java +++ b/src/test/java/io/patriot_framework/virtual_smart_home/house/HouseTest.java @@ -22,10 +22,13 @@ import java.util.HashMap; import java.util.Map; +import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentHashMap; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class HouseTest { @@ -45,7 +48,13 @@ public void removeDevice() { devices.put("fireplace", fireplace); house.setDevices(devices); house.removeDevice("fireplace"); - assertThat(house.getDevice("fireplace"), equalTo(null)); + Exception exception = assertThrows(NoSuchElementException.class, () -> { + house.getDevice("fireplace"); + }); + + String expectedMessage = "Device with label: fireplace is not present in the house"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); } @Test From 96c87ffad389d4f854cfca6fad01fbc816fddf51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0=C5=A5astn=C3=BD?= Date: Fri, 5 Aug 2022 10:45:58 +0200 Subject: [PATCH 2/2] Rules for checkstyle added and code altered acordingly --- .../InitialApplication.java | 6 +-- .../virtual_smart_home/house/House.java | 46 +++++++++---------- src/main/resources/checkstyle.xml | 5 ++ 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/patriot_framework/virtual_smart_home/InitialApplication.java b/src/main/java/io/patriot_framework/virtual_smart_home/InitialApplication.java index 67322b1..354ee72 100644 --- a/src/main/java/io/patriot_framework/virtual_smart_home/InitialApplication.java +++ b/src/main/java/io/patriot_framework/virtual_smart_home/InitialApplication.java @@ -22,7 +22,7 @@ @SpringBootApplication public class InitialApplication { - public static void main(String[] args) { - SpringApplication.run(InitialApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(InitialApplication.class, args); + } } diff --git a/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java b/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java index aba7f0e..892d0a6 100644 --- a/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java +++ b/src/main/java/io/patriot_framework/virtual_smart_home/house/House.java @@ -36,11 +36,11 @@ public final class House { private Map devices = new ConcurrentHashMap<>(); public House(String houseName) throws IllegalArgumentException { - if(houseName == null){ + if (houseName == null) { throw new IllegalArgumentException("houseName can't be null"); } - this.houseName = houseName; - LOGGER.debug(String.format("Created new house with name \"%s\"",this.houseName)); + this.houseName = houseName; + LOGGER.debug(String.format("Created new house with name \"%s\"",this.houseName)); } /** @@ -51,19 +51,19 @@ public House(String houseName) throws IllegalArgumentException { * @throws IllegalArgumentException if one of the parameters is null * @throws KeyAlreadyExistsException if house already contains device with given label */ - public void addDevice(String label, Device device) throws IllegalArgumentException, KeyAlreadyExistsException{ - if(label == null){ + public void addDevice(String label, Device device) throws IllegalArgumentException, KeyAlreadyExistsException { + if (label == null) { throw new IllegalArgumentException("Label of the device can't be null"); } - if(device == null){ + if (device == null) { throw new IllegalArgumentException("Device parameter can't be null"); } final Device origDevice = devices.putIfAbsent(label, device); - if(origDevice != null){ + if (origDevice != null) { throw new KeyAlreadyExistsException(String.format("Device with label: %s is already exists", label)); } LOGGER.debug(String.format("Device %s with label %s added to house %s", device, label, this.houseName)); - } + } /** * Method used to retrieve single device with certain label @@ -73,14 +73,14 @@ public void addDevice(String label, Device device) throws IllegalArgumentExcepti * @throws IllegalArgumentException if parameter is null * @throws NoSuchElementException if device with given label isn't in the House */ - public Device getDevice(String label) throws IllegalArgumentException, NoSuchElementException{ - if(label == null){ + public Device getDevice(String label) throws IllegalArgumentException, NoSuchElementException { + if (label == null) { throw new IllegalArgumentException("Label of the device can't be null"); } final Device device = devices.get(label); - if (device == null){ - throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); - }else{ + if (device == null) { + throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); + } else { return device; } } @@ -93,14 +93,14 @@ public Device getDevice(String label) throws IllegalArgumentException, NoSuchEle * @throws IllegalArgumentException if one of the parameters is null * @throws NoSuchElementException if house doesn't contains device with given label */ - public void updateDevice(String label, Device device) throws IllegalArgumentException, NoSuchElementException{ - if(label == null){ + public void updateDevice(String label, Device device) throws IllegalArgumentException, NoSuchElementException { + if (label == null) { throw new IllegalArgumentException("Label of the device can't be null"); } - if(device == null){ + if (device == null) { throw new IllegalArgumentException("Device parameter can't be null"); } - if(!devices.containsKey(label)){ + if (!devices.containsKey(label)) { throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); } final Device origDevice = devices.put(label, device); @@ -115,14 +115,14 @@ public void updateDevice(String label, Device device) throws IllegalArgumentExce * @throws IllegalArgumentException if label is null * @throws NoSuchElementException if house doesn't contains device with given label */ - public void removeDevice(String label) throws IllegalArgumentException, NoSuchElementException{ - if(label == null){ + public void removeDevice(String label) throws IllegalArgumentException, NoSuchElementException { + if (label == null) { throw new IllegalArgumentException("Label of the device can't be null"); } final Device origDevice = devices.remove(label); - if(origDevice == null){ + if (origDevice == null) { throw new NoSuchElementException(String.format("Device with label: %s is not present in the house", label)); - }else{ + } else { LOGGER.debug(String.format("At house:%s device:%s with label:%s removed.", houseName, origDevice, label)); } } @@ -155,7 +155,7 @@ public String getHouseName() { * @throws IllegalArgumentException if houseName is null */ public void setHouseName(String houseName) throws IllegalArgumentException { // TODO: Remove? - if(houseName == null){ + if (houseName == null) { throw new IllegalArgumentException("houseName can't be null"); } this.houseName = houseName; @@ -178,7 +178,7 @@ public Map getDevices() { * @throws IllegalArgumentException if map of devices is null */ public void setDevices(Map devices) throws IllegalArgumentException { // TODO: ? - if(devices == null){ + if (devices == null) { throw new IllegalArgumentException("Methd parameter devices can't be null"); } this.devices = devices; diff --git a/src/main/resources/checkstyle.xml b/src/main/resources/checkstyle.xml index d45c215..65c2abc 100644 --- a/src/main/resources/checkstyle.xml +++ b/src/main/resources/checkstyle.xml @@ -107,10 +107,15 @@ + + + + +