Added example tests for SensorController
This commit is contained in:
parent
ba9001a9c8
commit
3ed6102b9f
2 changed files with 79 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue