Merge branch '35-test-models' into 'dev'

Resolve "Test Models"

Closes #35

See merge request sa4-2020/the-sanmarinoes/backend!43
This commit is contained in:
Matteo Omenetti 2020-03-18 12:15:14 +01:00
commit 300f749144
6 changed files with 357 additions and 1 deletions

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
@ -15,7 +16,7 @@ public abstract class Dimmer extends InputDevice {
}
@OneToMany(mappedBy = "dimmer")
private Set<DimmableLight> lights;
private Set<DimmableLight> lights = new HashSet<>();
/**
* Get the lights connected to this dimmer
@ -26,4 +27,9 @@ public abstract class Dimmer extends InputDevice {
public Set<DimmableLight> getOutputs() {
return this.lights;
}
/** Add a light to be controller by this dimmer */
public void addDimmableLight(DimmableLight dimmableLight) {
lights.add(dimmableLight);
}
}

View file

@ -0,0 +1,55 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("A Button Dimmer")
public class ButtonDimmerTests {
ButtonDimmer buttonDimmer;
@BeforeEach
public void createNewButtonDimmer() {
this.buttonDimmer = new ButtonDimmer();
}
@Nested
@DisplayName(" when multiple lights are present")
class MultipleLights {
@BeforeEach
public void setLights() {
DimmableLight dl;
for (int i = 0; i < 3; i++) {
dl = new DimmableLight();
dl.setIntensity(10);
;
buttonDimmer.addDimmableLight(dl);
}
}
@Test
@DisplayName(" increase the intensity ")
public void increase() {
buttonDimmer.increaseIntensity();
for (DimmableLight dl : buttonDimmer.getOutputs()) {
assertTrue(dl.getIntensity() > 10);
}
}
@Test
@DisplayName(" decrease the intensity ")
public void decrease() {
buttonDimmer.decreaseIntensity();
for (DimmableLight dl : buttonDimmer.getOutputs()) {
assertTrue(dl.getIntensity() < 10);
}
}
}
}

View file

@ -0,0 +1,106 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("A Button Dimmer")
public class DimmableLightTests {
DimmableLight dimmableLight;
@BeforeEach
public void createNewButtonDimmer() {
this.dimmableLight = new DimmableLight();
}
@Nested
@DisplayName(" when on")
class WhenOn {
@BeforeEach
public void setLightOn() {
dimmableLight.setOn(true);
}
@Test
@DisplayName("the isOn method should return true")
public void isOn() {
assertTrue(dimmableLight.isOn());
}
@Test
@DisplayName("the intensity should be 100 when the light is turned on")
public void checkIntensityWhenTurnedOn() {
assertEquals(100, dimmableLight.getIntensity());
}
@Test
@DisplayName("setting the intensity to a number between 1 and 100")
public void checkIntensityBetweenLimits() {
dimmableLight.setIntensity(50);
assertEquals(50, dimmableLight.getIntensity());
}
@Test
@DisplayName("setting the intensity to a number > 100")
public void checkIntensityMoreThanLimits() {
dimmableLight.setIntensity(150);
assertEquals(100, dimmableLight.getIntensity());
}
@Test
@DisplayName("setting the intensity to a number < 0")
public void checkIntensityLessThanLimits() {
dimmableLight.setIntensity(-30);
assertEquals(0, dimmableLight.getIntensity());
}
@Test
@DisplayName("setting the intensity to a number <= 0 should turn the light off")
public void checkTurnOn() {
dimmableLight.setIntensity(0);
assertFalse(dimmableLight.isOn());
}
}
@Nested
@DisplayName(" when off")
class WhenOff {
@BeforeEach
public void setLightOff() {
dimmableLight.setOn(false);
}
@Test
@DisplayName("the isOn method should return false")
public void isOn() {
assertFalse(dimmableLight.isOn());
}
@Test
@DisplayName("the intensity should be 0 when the light is turned off")
public void checkIntensityWhenTurnedOff() {
assertEquals(0, dimmableLight.getIntensity());
}
@Test
@DisplayName("setting the intensity to a number between 1 and 100")
public void checkIntensityBetweenLimits() {
dimmableLight.setIntensity(50);
assertEquals(50, dimmableLight.getIntensity());
}
@Test
@DisplayName("setting the intensity to a number > 0 should turn the light on")
public void checkIntensityLessThanLimits() {
dimmableLight.setIntensity(47);
assertEquals(47, dimmableLight.getIntensity());
}
}
}

View file

@ -0,0 +1,46 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("A Knob Dimmer")
public class KnobDimmerTests {
KnobDimmer knobDimmer;
@BeforeEach
public void createNewKnobDimmer() {
this.knobDimmer = new KnobDimmer();
}
@Nested
@DisplayName(" when multiple lights are present")
class MultipleLights {
@BeforeEach
public void setLights() {
DimmableLight dl;
for (int i = 0; i < 3; i++) {
dl = new DimmableLight();
dl.setIntensity(10);
;
knobDimmer.addDimmableLight(dl);
}
}
@Test
@DisplayName(" set the intensity ")
public void increase() {
knobDimmer.setLightIntensity(30);
for (DimmableLight dl : knobDimmer.getOutputs()) {
assertEquals(30, dl.getIntensity());
}
}
}
}

View file

@ -0,0 +1,47 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("A RegularLight")
public class RegularLightTests {
RegularLight regularLight;
@BeforeEach
public void createRegularLight() {
this.regularLight = new RegularLight();
}
@Test
@DisplayName("State when just created")
public void beginningState() {
assertFalse(regularLight.isOn());
}
@Test
@DisplayName("Changing state to on after creating the light")
public void createAndSetOn() {
regularLight.setOn(true);
assertTrue(regularLight.isOn());
}
@Test
@DisplayName("Change state of the light to off after creating it")
public void createAndSetOff() {
regularLight.setOn(false);
assertFalse(regularLight.isOn());
}
@Test
@DisplayName("Checks whether a turned on light getting turned on is still in the on State")
public void setOn() {
regularLight.setOn(true);
regularLight.setOn(true);
assertTrue(regularLight.isOn());
}
}

View file

@ -0,0 +1,96 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("A switch")
public class SwitchTests {
Switch aSwitch;
@BeforeEach
public void createNewSwitch() {
this.aSwitch = new Switch();
RegularLight regularLight = new RegularLight();
DimmableLight dimmableLight = new DimmableLight();
SmartPlug smartPlug = new SmartPlug();
this.aSwitch.getOutputs().add(regularLight);
this.aSwitch.getOutputs().add(dimmableLight);
this.aSwitch.getOutputs().add(smartPlug);
}
@Test
@DisplayName("check state when switch created")
public void createdSwitch() {
assertFalse(aSwitch.isOn());
}
@Test
@DisplayName("Check toggle on a switch in its off state")
public void offToggle() {
aSwitch.toggle();
assertTrue(aSwitch.isOn());
}
@Test
@DisplayName("Checks whether setting on a off switch works as intended")
public void offSetOn() {
aSwitch.setOn(true);
assertTrue(aSwitch.isOn());
}
@Test
@DisplayName("Checks whether setting off a on switch works as intended")
public void onSetOff() {
aSwitch.toggle();
aSwitch.setOn(false);
assertFalse(aSwitch.isOn());
}
@Test
@DisplayName("Checks that setting off an off switch results in a off state")
public void offSetOff() {
aSwitch.setOn(false);
assertFalse(aSwitch.isOn());
}
@Test
@DisplayName("Checks that setting on an on switch results in a on state")
public void onSetOn() {
aSwitch.toggle();
aSwitch.setOn(true);
assertTrue(aSwitch.isOn());
}
@Test
@DisplayName("Checks wheter toggling a on switch set its state to off")
public void onToggle() {
aSwitch.setOn(true);
aSwitch.toggle();
assertFalse(aSwitch.isOn());
}
@Test
@DisplayName("Checks that toggling on sets all elements of the Set on as well")
public void toggleEffctOnSet() {
aSwitch.toggle();
for (final Switchable s : aSwitch.getOutputs()) {
assertTrue(s.isOn());
}
}
@Test
@DisplayName("Checks that toggling the switch off also sets all elements of its set off")
public void toggleOffEffectOnElementes() {
aSwitch.setOn(true);
aSwitch.toggle();
for (final Switchable s : aSwitch.getOutputs()) {
assertFalse(s.isOn());
}
}
}