diff --git a/build.gradle b/build.gradle index a6f73d3..a933dc9 100644 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ dependencies { gradle.projectsEvaluated { tasks.withType(JavaCompile) { - options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" + options.compilerArgs << "-Xlint:deprecation" } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 8e3b313..5e3966c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -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()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index c91144e..266713d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -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 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); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java new file mode 100644 index 0000000..ddb4cc8 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java @@ -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); + } +}