Added basic tests to SmartPlugController

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-19 18:38:31 +02:00
parent 76e5bed119
commit 6b3886ccb1
12 changed files with 119 additions and 24 deletions

View file

@ -45,7 +45,7 @@ dependencies {
gradle.projectsEvaluated { gradle.projectsEvaluated {
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" options.compilerArgs << "-Xlint:deprecation"
} }
} }

View file

@ -1,7 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerDimRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerDimRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
@ -41,7 +41,7 @@ public class ButtonDimmerController
@PostMapping @PostMapping
public ButtonDimmer create( public ButtonDimmer create(
@Valid @RequestBody final GenericDeviceSaveReguest bd, final Principal principal) @Valid @RequestBody final GenericDeviceSaveRequest bd, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.throwIfRoomNotOwned(bd.getRoomId(), principal.getName()); deviceService.throwIfRoomNotOwned(bd.getRoomId(), principal.getName());

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerDimRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerDimRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable;
@ -40,7 +40,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
@PostMapping @PostMapping
public KnobDimmer create( public KnobDimmer create(
@Valid @RequestBody GenericDeviceSaveReguest kd, final Principal principal) @Valid @RequestBody GenericDeviceSaveRequest kd, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.throwIfRoomNotOwned(kd.getRoomId(), principal.getName()); deviceService.throwIfRoomNotOwned(kd.getRoomId(), principal.getName());
KnobDimmer newKD = new KnobDimmer(); KnobDimmer newKD = new KnobDimmer();

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
@ -23,7 +23,7 @@ public class MotionSensorController {
@PostMapping @PostMapping
public MotionSensor create( public MotionSensor create(
@Valid @RequestBody GenericDeviceSaveReguest ms, final Principal principal) @Valid @RequestBody GenericDeviceSaveRequest ms, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.throwIfRoomNotOwned(ms.getRoomId(), principal.getName()); deviceService.throwIfRoomNotOwned(ms.getRoomId(), principal.getName());
MotionSensor newMS = new MotionSensor(); MotionSensor newMS = new MotionSensor();

View file

@ -23,7 +23,6 @@ public class SmartPlugController {
private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) { private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) {
newSP.setOn(sp.isOn()); newSP.setOn(sp.isOn());
newSP.setId(sp.getId());
newSP.setName(sp.getName()); newSP.setName(sp.getName());
newSP.setRoomId(sp.getRoomId()); newSP.setRoomId(sp.getRoomId());

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchOperationRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchOperationRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switch; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switch;
@ -45,7 +45,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
} }
@PostMapping @PostMapping
public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal) public Switch create(@Valid @RequestBody GenericDeviceSaveRequest s, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.throwIfRoomNotOwned(s.getRoomId(), principal.getName()); deviceService.throwIfRoomNotOwned(s.getRoomId(), principal.getName());
Switch newSwitch = new Switch(); Switch newSwitch = new Switch();

View file

@ -4,7 +4,7 @@ import javax.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
@Data @Data
public class GenericDeviceSaveReguest { public class GenericDeviceSaveRequest {
/** /**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from * The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call. * a REST call.

View file

@ -1,9 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@NoArgsConstructor
@AllArgsConstructor
public class SwitchableSaveRequest { public class SwitchableSaveRequest {
/** The state of this switch */ /** The state of this switch */
private boolean on; private boolean on;

View file

@ -34,7 +34,7 @@ public class SceneSaveRequestTests {
@Test @Test
@DisplayName("test getId") @DisplayName("test getId")
public void testGetId() { public void testGetId() {
assertEquals(0l, scene.getId()); assertEquals(0L, scene.getId());
} }
@Test @Test

View file

@ -22,8 +22,8 @@ public class SwitchableSaveRequestTests {
@Test @Test
@DisplayName("test setRoomId") @DisplayName("test setRoomId")
public void testSetRoomId() { public void testSetRoomId() {
saveRequest.setRoomId(42l); saveRequest.setRoomId(42L);
assertEquals(42l, saveRequest.getRoomId()); assertEquals(42L, saveRequest.getRoomId());
} }
@Test @Test
@ -56,7 +56,7 @@ public class SwitchableSaveRequestTests {
@Test @Test
@DisplayName("test setId") @DisplayName("test setId")
public void testSetId() { public void testSetId() {
saveRequest.setId(300771l); saveRequest.setId(300771L);
assertEquals(300771l, saveRequest.getId()); assertEquals(300771L, saveRequest.getId());
} }
} }

View file

@ -30,6 +30,8 @@ public class SceneControllerTests {
@Mock private Principal mockPrincipal; @Mock private Principal mockPrincipal;
@Mock private StateRepository<State> stateStateRepository;
private final User u; private final User u;
public SceneControllerTests() { public SceneControllerTests() {
@ -41,7 +43,6 @@ public class SceneControllerTests {
@Test @Test
public void testGetAll() throws NotFoundException { public void testGetAll() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
when(sceneRepository.findByUsername("user")).thenReturn(List.of()); when(sceneRepository.findByUsername("user")).thenReturn(List.of());
assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty(); assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty();
} }
@ -68,16 +69,16 @@ public class SceneControllerTests {
@Test @Test
public void testUpdate() throws NotFoundException { public void testUpdate() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
final Scene old = new Scene(); final Scene old = new Scene();
old.setId(42L); old.setId(42L);
old.setUserId(1L);
old.setName("Old Name"); old.setName("Old Name");
when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]); when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);
when(sceneRepository.findById(42L)).thenReturn(Optional.of(old)); when(sceneRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old));
when(sceneRepository.findById(43L)).thenReturn(Optional.empty()); when(sceneRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty());
SceneSaveRequest a = new SceneSaveRequest(0, "New Scene", Icon.BATH, true); SceneSaveRequest a = new SceneSaveRequest(42L, "New Scene", Icon.BATH, true);
Scene created = sceneController.update(a.getId(), a, mockPrincipal); Scene created = sceneController.update(a.getId(), a, mockPrincipal);
assertThat(created.getId()).isEqualTo(42L); assertThat(created.getId()).isEqualTo(42L);
@ -90,9 +91,7 @@ public class SceneControllerTests {
@Test @Test
public void testDelete() { public void testDelete() {
final Automation old = new Automation(); doNothing().when(stateStateRepository).deleteAllBySceneId(42L);
old.setId(42L);
old.setName("Old Name");
doNothing().when(sceneRepository).deleteById(42L); doNothing().when(sceneRepository).deleteById(42L);
sceneController.deleteById(42L); sceneController.deleteById(42L);
} }

View file

@ -0,0 +1,93 @@
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.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.util.AssertionErrors.fail;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class SmartPlugControllerTests {
@InjectMocks private SmartPlugController smartPlugController;
@Mock private UserRepository userRepository;
@Mock private SmartPlugRepository smartPlugRepository;
@Mock private Principal mockPrincipal;
@Mock private DeviceService deviceService;
private final User u;
public SmartPlugControllerTests() {
u = new User();
u.setName("user");
u.setId(1L);
}
private void equalToRequest(Switchable created, SwitchableSaveRequest a) {
assertThat(created.getName()).isEqualTo(a.getName());
assertThat(created.getRoomId()).isEqualTo(30L);
assertThat(created.isOn()).isEqualTo(a.isOn());
}
@Test
public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
when(deviceService.saveAsOwner(any(), eq("user"))).thenAnswer(i -> i.getArguments()[0]);
SwitchableSaveRequest a = new SwitchableSaveRequest(true, 1L, 30L, "New SmartPlug");
try {
Switchable created = smartPlugController.create(a, mockPrincipal);
assertThat(created.getId()).isEqualTo(0L);
equalToRequest(created, a);
} catch (NotFoundException e) {
fail(e.getMessage());
}
}
@Test
public void testUpdate() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
final SmartPlug old = new SmartPlug();
old.setId(42L);
when(deviceService.saveAsOwner(any(), eq("user"))).thenAnswer(i -> i.getArguments()[0]);
when(smartPlugRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old));
when(smartPlugRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty());
SwitchableSaveRequest a = new SwitchableSaveRequest(true, 42L, 30L, "New SmartPlug");
SmartPlug created = smartPlugController.update(a, mockPrincipal);
assertThat(created.getId()).isEqualTo(42L);
equalToRequest(created, a);
a.setId(43L);
assertThatThrownBy(() -> smartPlugController.update(a, mockPrincipal))
.isInstanceOf(NotFoundException.class);
}
@Test
public void testDelete() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(42L, "user");
smartPlugController.deleteById(42L, mockPrincipal);
}
}