Merge branch '93-miscellaneous-tests' into 'dev'

Resolve "miscellaneous tests"

Closes #93

See merge request sa4-2020/the-sanmarinoes/backend!194
This commit is contained in:
Matteo Omenetti 2020-05-26 18:46:14 +02:00
commit 86c87ece65
7 changed files with 89 additions and 6 deletions

View file

@ -44,8 +44,6 @@ public class SensorController {
newSensor.setName(s.getName()); newSensor.setName(s.getName());
newSensor.setRoomId(s.getRoomId()); newSensor.setRoomId(s.getRoomId());
newSensor.setValue(s.getValue()); newSensor.setValue(s.getValue());
// newSensor.setError(s.getError());
// newSensor.setTypical(s.getTypical());
return deviceService.saveAsOwner(newSensor, principal.getName()); return deviceService.saveAsOwner(newSensor, principal.getName());
} }

View file

@ -87,10 +87,6 @@ public class Sensor extends InputDevice implements RangeTriggerable {
this.typical = typical; this.typical = typical;
} }
public void setTypicalNull() {
this.typical = null;
}
public Sensor() { public Sensor() {
super("sensor"); super("sensor");
} }

View file

@ -108,4 +108,25 @@ public class SensorControllerTests {
assertThat(sensorController.updateValue(1L, BigDecimal.valueOf(30), mockPrincipal)) assertThat(sensorController.updateValue(1L, BigDecimal.valueOf(30), mockPrincipal))
.isSameAs(s); .isSameAs(s);
} }
@Test
public void testUpdateSimulation() throws NotFoundException {
final Sensor s = new Sensor();
when(sensorRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(s));
when(sensorRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty());
when(sensorService.updateSimulationFromSensor(
s, BigDecimal.valueOf(30), BigDecimal.valueOf(10)))
.thenAnswer(
i -> {
s.setError(i.getArgument(1));
s.setTypical(i.getArgument(2));
return s;
});
assertThatThrownBy(() -> sensorController.updateValue(2L, BigDecimal.ZERO, mockPrincipal))
.isInstanceOf(NotFoundException.class);
assertThat(
sensorController.updateSimulation(
1L, BigDecimal.valueOf(30), BigDecimal.valueOf(10), mockPrincipal))
.isSameAs(s);
}
} }

View file

@ -63,6 +63,13 @@ public class ThermostatControllerTests {
assertThat(toCheck.isUseExternalSensors()).isEqualTo(request.isUseExternalSensors()); assertThat(toCheck.isUseExternalSensors()).isEqualTo(request.isUseExternalSensors());
assertThat(toCheck.getName()).isEqualTo(request.getName()); assertThat(toCheck.getName()).isEqualTo(request.getName());
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
if (request.getErr() != null) {
assertThat(toCheck.getErr()).isEqualTo(request.getErr());
}
if (request.getTypical() != null) {
assertThat(toCheck.getTypical()).isEqualTo(request.getTypical());
}
} }
@Test @Test
@ -83,6 +90,8 @@ public class ThermostatControllerTests {
toSend.setTargetTemperature(new BigDecimal(40)); toSend.setTargetTemperature(new BigDecimal(40));
toSend.setName("Thermostat Test"); toSend.setName("Thermostat Test");
toSend.setUseExternalSensors(true); toSend.setUseExternalSensors(true);
toSend.setErr(BigDecimal.valueOf(10));
toSend.setTypical(BigDecimal.valueOf(15));
final Thermostat thermostat = thermostatController.create(toSend, mockPrincipal); final Thermostat thermostat = thermostatController.create(toSend, mockPrincipal);

View file

@ -26,6 +26,22 @@ public class SensorTests {
assertEquals(SensorType.LIGHT, sensor.getSensor()); assertEquals(SensorType.LIGHT, sensor.getSensor());
} }
@Test
@DisplayName("get and set err")
public void getAndSetError() {
sensor.setError(BigDecimal.valueOf(10));
assertEquals(BigDecimal.valueOf(10), sensor.getError());
}
@Test
@DisplayName("get and set typical")
public void getAndSetTypical() {
sensor.setTypical(BigDecimal.valueOf(10));
assertEquals(BigDecimal.valueOf(10), sensor.getTypical());
}
@Test @Test
@DisplayName("get and set value") @DisplayName("get and set value")
public void getAndSetValue() { public void getAndSetValue() {

View file

@ -2,6 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat.Mode;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
@ -85,12 +86,34 @@ public class ThermostatTests {
assertEquals(new BigDecimal(42), thermostat.getInternalSensorTemperature()); assertEquals(new BigDecimal(42), thermostat.getInternalSensorTemperature());
} }
@Test
@DisplayName("test err ")
public void errTest() {
thermostat.setErr(BigDecimal.valueOf(10));
assertEquals(BigDecimal.valueOf(10), thermostat.getErr());
}
@Test
@DisplayName("test typical ")
public void typicalTest() {
thermostat.setTypical(BigDecimal.valueOf(10));
assertEquals(BigDecimal.valueOf(10), thermostat.getTypical());
}
@Test @Test
@DisplayName("test triggerState") @DisplayName("test triggerState")
public void testTriggerState() { public void testTriggerState() {
assertFalse(thermostat.readTriggerState()); assertFalse(thermostat.readTriggerState());
} }
@Test
@DisplayName("test triggerState")
public void testTriggerStateTrue() {
thermostat.setMode(Mode.COOLING);
assertTrue(thermostat.readTriggerState());
}
// for obvious reasons I am not gonna check all the possible combinations of toString() // for obvious reasons I am not gonna check all the possible combinations of toString()
@Test @Test
@DisplayName("test to string") @DisplayName("test to string")

View file

@ -1,11 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service; package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -22,6 +24,8 @@ public class SceneServiceTests {
@Mock private SceneRepository sceneRepository; @Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
@Mock private DevicePropagationService deviceService; @Mock private DevicePropagationService deviceService;
@Mock private DevicePopulationService devicePopulationService; @Mock private DevicePopulationService devicePopulationService;
@ -56,4 +60,20 @@ public class SceneServiceTests {
assertThat(sceneService.applyAsGuest(s, "user", 42L)).containsExactly(d); assertThat(sceneService.applyAsGuest(s, "user", 42L)).containsExactly(d);
assertThat(d.getIntensity()).isEqualTo(40); assertThat(d.getIntensity()).isEqualTo(40);
} }
@Test
public void testCopyStates() {
final State state = new DimmableState();
final Scene sceneFrom = new Scene();
sceneFrom.getStates().add(state);
final Scene sceneTo = new Scene();
when(stateRepository.save(any(State.class))).thenReturn(state);
List<State> s = sceneService.copyStates(sceneTo, sceneFrom);
assertEquals(s.get(0), state);
}
} }