From 3ed6102b9fd60c2ad588eefdd9b838fbd339a179 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 13 May 2020 11:57:31 +0200 Subject: [PATCH] Added example tests for SensorController --- .../smarthut/dto/SensorSaveRequest.java | 2 + .../controller/SensorControllerTests.java | 77 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java index 8566bde..bd86cd3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java @@ -5,9 +5,11 @@ import java.math.BigDecimal; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class SensorSaveRequest { /** The type of this sensor */ @NotNull diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java new file mode 100644 index 0000000..6ef3158 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java @@ -0,0 +1,77 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +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.service.DeviceService; +import java.math.BigDecimal; +import java.security.Principal; +import lombok.SneakyThrows; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@DisplayName("The sensor controller") +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class SensorControllerTests { + @InjectMocks private SensorController sensorController; + + @Mock private DeviceService deviceService; + + @Mock private Principal mockPrincipal; + + @BeforeEach + public void setup() { + when(mockPrincipal.getName()).thenReturn("user"); + } + + private void checkSensorAgainstRequest(final Sensor toCheck, final SensorSaveRequest request) { + assertThat(toCheck).isNotNull(); + assertThat(toCheck.getSensor()).isEqualTo(request.getSensor()); + assertThat(toCheck.getValue()).isEqualTo(request.getValue()); + assertThat(toCheck.getName()).isEqualTo(request.getName()); + assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); + } + + @DisplayName("when creating should return the same object") + @Test + @SneakyThrows(NotFoundException.class) + public void testCreate() { + doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); + when(deviceService.saveAsOwner(any(Sensor.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + final SensorSaveRequest toSend = + new SensorSaveRequest( + Sensor.SensorType.TEMPERATURE, BigDecimal.ZERO, 42L, "Test sensor"); + final Sensor created = sensorController.create(toSend, mockPrincipal); + + checkSensorAgainstRequest(created, toSend); + } + + @DisplayName("when deleting an existant id should succeed") + @Test + @SneakyThrows(NotFoundException.class) + public void testDelete() { + doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + sensorController.deleteById(42L, mockPrincipal); + } +}