Skip to content
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

Throwing exceptions added to the House methods #38

Merged
merged 2 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +35,10 @@ public final class House {
private String houseName;
private Map<String, Device> devices = new ConcurrentHashMap<>();

public House(String 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));
}
Expand All @@ -44,9 +48,20 @@ 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));
}

Expand All @@ -55,38 +70,59 @@ public void addDevice(String label, Device device) {
*
* @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;
}
}

/**
* Method used to replace device with certain 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{
if (origDevice == null) {
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));
}
}
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -135,10 +175,14 @@ public Map<String, Device> 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<String, Device> devices) { // TODO: ?
public void setDevices(Map<String, Device> 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;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@
</module>
<module name="RightCurly"/>

<!-- Checks for whitespaces. -->
<!-- See https://checkstyle.sourceforge.io/config_whitespace.html -->
<module name="WhitespaceAround"/>

<!-- Checks for miscellaneous code style criteria. -->
<!-- See https://checkstyle.sourceforge.io/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="CommentsIndentation"/>
<module name="Indentation"/>
</module>

<!-- Checks whether files end with a new line. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down