Merge branch 'tests' into 'dev'
Tests See merge request sa4-2020/the-sanmarinoes/backend!154
This commit is contained in:
commit
f802440d3a
13 changed files with 214 additions and 16 deletions
|
@ -45,7 +45,7 @@ dependencies {
|
|||
|
||||
gradle.projectsEvaluated {
|
||||
tasks.withType(JavaCompile) {
|
||||
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
|
||||
options.compilerArgs << "-Xlint:deprecation"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
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.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.models.ButtonDimmer;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
|
||||
|
@ -41,7 +41,7 @@ public class ButtonDimmerController
|
|||
|
||||
@PostMapping
|
||||
public ButtonDimmer create(
|
||||
@Valid @RequestBody final GenericDeviceSaveReguest bd, final Principal principal)
|
||||
@Valid @RequestBody final GenericDeviceSaveRequest bd, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(bd.getRoomId(), principal.getName());
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable;
|
||||
|
@ -40,7 +40,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
|
|||
|
||||
@PostMapping
|
||||
public KnobDimmer create(
|
||||
@Valid @RequestBody GenericDeviceSaveReguest kd, final Principal principal)
|
||||
@Valid @RequestBody GenericDeviceSaveRequest kd, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(kd.getRoomId(), principal.getName());
|
||||
KnobDimmer newKD = new KnobDimmer();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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.models.MotionSensor;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
||||
|
@ -23,7 +23,7 @@ public class MotionSensorController {
|
|||
|
||||
@PostMapping
|
||||
public MotionSensor create(
|
||||
@Valid @RequestBody GenericDeviceSaveReguest ms, final Principal principal)
|
||||
@Valid @RequestBody GenericDeviceSaveRequest ms, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(ms.getRoomId(), principal.getName());
|
||||
MotionSensor newMS = new MotionSensor();
|
||||
|
|
|
@ -23,7 +23,6 @@ public class SmartPlugController {
|
|||
|
||||
private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) {
|
||||
newSP.setOn(sp.isOn());
|
||||
newSP.setId(sp.getId());
|
||||
newSP.setName(sp.getName());
|
||||
newSP.setRoomId(sp.getRoomId());
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switch;
|
||||
|
@ -45,7 +45,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal)
|
||||
public Switch create(@Valid @RequestBody GenericDeviceSaveRequest s, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(s.getRoomId(), principal.getName());
|
||||
Switch newSwitch = new Switch();
|
||||
|
|
|
@ -4,7 +4,7 @@ import javax.validation.constraints.NotNull;
|
|||
import lombok.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
|
||||
* a REST call.
|
|
@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
|||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon;
|
||||
import javax.persistence.Column;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SceneSaveRequest {
|
||||
|
||||
/** Room identifier */
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SwitchableSaveRequest {
|
||||
/** The state of this switch */
|
||||
private boolean on;
|
||||
|
|
|
@ -34,7 +34,7 @@ public class SceneSaveRequestTests {
|
|||
@Test
|
||||
@DisplayName("test getId")
|
||||
public void testGetId() {
|
||||
assertEquals(0l, scene.getId());
|
||||
assertEquals(0L, scene.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -22,8 +22,8 @@ public class SwitchableSaveRequestTests {
|
|||
@Test
|
||||
@DisplayName("test setRoomId")
|
||||
public void testSetRoomId() {
|
||||
saveRequest.setRoomId(42l);
|
||||
assertEquals(42l, saveRequest.getRoomId());
|
||||
saveRequest.setRoomId(42L);
|
||||
assertEquals(42L, saveRequest.getRoomId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -56,7 +56,7 @@ public class SwitchableSaveRequestTests {
|
|||
@Test
|
||||
@DisplayName("test setId")
|
||||
public void testSetId() {
|
||||
saveRequest.setId(300771l);
|
||||
assertEquals(300771l, saveRequest.getId());
|
||||
saveRequest.setId(300771L);
|
||||
assertEquals(300771L, saveRequest.getId());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
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.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
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 SceneControllerTests {
|
||||
@InjectMocks private SceneController sceneController;
|
||||
|
||||
@Mock private UserRepository userRepository;
|
||||
|
||||
@Mock private SceneRepository sceneRepository;
|
||||
|
||||
@Mock private Principal mockPrincipal;
|
||||
|
||||
@Mock private StateRepository<State> stateStateRepository;
|
||||
|
||||
private final User u;
|
||||
|
||||
public SceneControllerTests() {
|
||||
u = new User();
|
||||
u.setName("user");
|
||||
u.setId(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAll() throws NotFoundException {
|
||||
when(mockPrincipal.getName()).thenReturn("user");
|
||||
when(sceneRepository.findByUsername("user")).thenReturn(List.of());
|
||||
assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty();
|
||||
}
|
||||
|
||||
private void equalToRequest(Scene created, SceneSaveRequest a) {
|
||||
assertThat(created.getName()).isEqualTo(a.getName());
|
||||
assertThat(created.getUserId()).isEqualTo(1L);
|
||||
assertThat(created.getIcon()).isEqualTo(a.getIcon());
|
||||
assertThat(created.isGuestAccessEnabled()).isEqualTo(a.isGuestAccessEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() {
|
||||
when(mockPrincipal.getName()).thenReturn("user");
|
||||
when(userRepository.findByUsername("user")).thenReturn(u);
|
||||
when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);
|
||||
|
||||
SceneSaveRequest s = new SceneSaveRequest(0, "New Scene", Icon.BATH, true);
|
||||
Scene created = sceneController.create(s, mockPrincipal);
|
||||
assertThat(created.getId()).isEqualTo(0L);
|
||||
equalToRequest(created, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() throws NotFoundException {
|
||||
when(mockPrincipal.getName()).thenReturn("user");
|
||||
final Scene old = new Scene();
|
||||
old.setId(42L);
|
||||
old.setUserId(1L);
|
||||
old.setName("Old Name");
|
||||
|
||||
when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);
|
||||
when(sceneRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old));
|
||||
when(sceneRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty());
|
||||
|
||||
SceneSaveRequest a = new SceneSaveRequest(42L, "New Scene", Icon.BATH, true);
|
||||
|
||||
Scene created = sceneController.update(a.getId(), a, mockPrincipal);
|
||||
assertThat(created.getId()).isEqualTo(42L);
|
||||
equalToRequest(created, a);
|
||||
|
||||
a.setId(43L);
|
||||
assertThatThrownBy(() -> sceneController.update(a.getId(), a, mockPrincipal))
|
||||
.isInstanceOf(NotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
doNothing().when(stateStateRepository).deleteAllBySceneId(42L);
|
||||
doNothing().when(sceneRepository).deleteById(42L);
|
||||
sceneController.deleteById(42L);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue