Merge branch 'tests' into 'dev'

Fixes

See merge request sa4-2020/the-sanmarinoes/backend!183
This commit is contained in:
Claudio Maggioni 2020-05-25 11:27:50 +02:00
commit 1be860b989
5 changed files with 60 additions and 16 deletions

View file

@ -10,8 +10,8 @@ import java.security.Principal;
public abstract class GuestEnabledController<T extends Device> {
private UserRepository userRepository;
private DeviceRepository<T> deviceRepository;
private final UserRepository userRepository;
private final DeviceRepository<T> deviceRepository;
public GuestEnabledController(
final UserRepository userRepository, final DeviceRepository<T> deviceRepository) {

View file

@ -19,10 +19,10 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/regularLight")
public class RegularLightController extends GuestEnabledController<RegularLight> {
private RegularLightRepository regularLightRepository;
private SceneRepository sceneRepository;
private StateRepository<State> stateRepository;
private DeviceService deviceService;
private final RegularLightRepository regularLightRepository;
private final SceneRepository sceneRepository;
private final StateRepository<State> stateRepository;
private final DeviceService deviceService;
@Autowired
public RegularLightController(

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
@ -34,18 +35,12 @@ public class RegularLightControllerTests {
@Mock private RegularLightRepository regularLightRepository;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
@Mock private DeviceService deviceService;
@Mock private Principal mockPrincipal;
@Mock private UserRepository userRepository;
@Mock private DeviceRepository<RegularLight> deviceRepository;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
@ -86,12 +81,27 @@ public class RegularLightControllerTests {
public void testUpdate() {
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setId(50L);
toSend.setName("rl");
toSend.setRoomId(20L);
toSend.setOn(true);
final RegularLight toUpdate = new RegularLight();
toUpdate.setName("OOO");
toUpdate.setRoomId(40L);
toUpdate.setOn(false);
when(regularLightRepository.findByIdAndUsername(50L, "user"))
.thenReturn(Optional.of(toUpdate));
when(regularLightRepository.findByIdAndUsername(60L, "user")).thenReturn(Optional.empty());
when(deviceService.saveAsOwner(toUpdate, "user")).thenReturn(toUpdate);
toSend.setId(60L);
assertThatThrownBy(() -> regularLightController.update(toSend, mockPrincipal, null))
.isInstanceOf(NotFoundException.class);
toSend.setId(50L);
assertThat(regularLightController.update(toSend, mockPrincipal, null)).isEqualTo(toUpdate);
toUpdate.setName("OOO");
toUpdate.setRoomId(40L);
toSend.setId(50L);

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
@ -8,9 +9,12 @@ import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SensorService;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -35,6 +39,10 @@ public class SensorControllerTests {
@Mock private Principal mockPrincipal;
@Mock private SensorRepository sensorRepository;
@Mock private SensorService sensorService;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
@ -78,4 +86,21 @@ public class SensorControllerTests {
Assertions.assertDoesNotThrow(() -> sensorController.deleteById(42L, mockPrincipal));
}
@Test
public void testUpdateValue() 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.updateValueFromSensor(s, BigDecimal.valueOf(30)))
.thenAnswer(
i -> {
s.setValue(i.getArgument(1));
return s;
});
assertThatThrownBy(() -> sensorController.updateValue(2L, BigDecimal.ZERO, mockPrincipal))
.isInstanceOf(NotFoundException.class);
assertThat(sensorController.updateValue(1L, BigDecimal.valueOf(30), mockPrincipal))
.isSameAs(s);
}
}

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -10,7 +11,8 @@ import org.junit.jupiter.api.Test;
@DisplayName("A switch")
public class SwitchTests {
Switch aSwitch;
private Switch aSwitch;
private SmartPlug smartPlug;
@BeforeEach
public void createNewSwitch() {
@ -18,10 +20,11 @@ public class SwitchTests {
this.aSwitch = new Switch();
RegularLight regularLight = new RegularLight();
DimmableLight dimmableLight = new DimmableLight();
SmartPlug smartPlug = new SmartPlug();
smartPlug = new SmartPlug();
this.aSwitch.getSwitchables().add(regularLight);
this.aSwitch.getSwitchables().add(dimmableLight);
this.aSwitch.getSwitchables().add(smartPlug);
this.aSwitch.connect(smartPlug, true);
assertThat(this.aSwitch.getOutputs()).containsAll(this.aSwitch.getSwitchables());
}
@Test
@ -93,4 +96,10 @@ public class SwitchTests {
assertFalse(s.isOn());
}
}
@Test
public void testDisconnect() {
this.aSwitch.connect(smartPlug, false);
assertFalse(this.aSwitch.getOutputs().contains(smartPlug));
}
}