Merge branch '82-smartPlug-controller' into 'dev'

Resolve "securityCameraControllerTests"

Closes #82

See merge request sa4-2020/the-sanmarinoes/backend!177
This commit is contained in:
Claudio Maggioni 2020-05-24 22:29:22 +02:00
commit 00b4d77f76
3 changed files with 270 additions and 0 deletions

View file

@ -0,0 +1,108 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.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("RangeConditionController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class RangeConditionControllerTests {
@InjectMocks private RangeConditionController controller;
@Mock private RangeConditionRepository repository;
@Mock private Principal principal;
@Test
public void testCreate() {
RangeCondition condition = new RangeCondition();
condition.setRange(40L);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
condition.setOperator(Operator.EQUAL);
when(repository.save(condition)).thenReturn(condition);
RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest();
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOperator(Operator.EQUAL);
toSend.setRange(40L);
RangeCondition returned = controller.create(toSend);
assertThat(returned.getRange()).isEqualTo(toSend.getRange());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getOperator()).isEqualTo(toSend.getOperator());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
RangeCondition condition = new RangeCondition();
condition.setRange(40L);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
condition.setOperator(Operator.EQUAL);
when(repository.save(condition)).thenReturn(condition);
RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOperator(Operator.EQUAL);
toSend.setRange(40L);
when(repository.findById(34L)).thenReturn(Optional.of(condition));
RangeCondition returned = controller.update(toSend);
assertThat(returned.getRange()).isEqualTo(toSend.getRange());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getOperator()).isEqualTo(toSend.getOperator());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
public void testGetAll() {
RangeCondition condition1 = new RangeCondition();
condition1.setAutomationId(1L);
RangeCondition condition2 = new RangeCondition();
condition2.setAutomationId(1L);
RangeCondition condition3 = new RangeCondition();
condition3.setAutomationId(1L);
ArrayList<RangeCondition> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<RangeCondition> returned = controller.getAll(42L);
assertThat(returned).contains(condition1);
assertThat(returned).contains(condition2);
assertThat(returned).contains(condition3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}

View file

@ -9,11 +9,14 @@ import static org.mockito.Mockito.when;
import static org.springframework.test.util.AssertionErrors.fail; import static org.springframework.test.util.AssertionErrors.fail;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
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.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.math.BigDecimal;
import java.security.Principal; import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -35,6 +38,10 @@ public class SmartPlugControllerTests {
@Mock private DeviceService deviceService; @Mock private DeviceService deviceService;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
private final User u; private final User u;
public SmartPlugControllerTests() { public SmartPlugControllerTests() {
@ -91,4 +98,51 @@ public class SmartPlugControllerTests {
doNothing().when(deviceService).deleteByIdAsOwner(42L, "user"); doNothing().when(deviceService).deleteByIdAsOwner(42L, "user");
Assertions.assertDoesNotThrow(() -> smartPlugController.deleteById(42L, mockPrincipal)); Assertions.assertDoesNotThrow(() -> smartPlugController.deleteById(42L, mockPrincipal));
} }
@Test
@SneakyThrows(NotFoundException.class)
public void testResetMeter() {
when(mockPrincipal.getName()).thenReturn("user");
SmartPlug s = new SmartPlug();
s.setId(24L);
when(smartPlugRepository.findByIdAndUsername(24L, "user")).thenReturn(Optional.of(s));
s.resetTotalConsumption();
when(smartPlugRepository.save(s)).thenReturn(s);
SmartPlug toCheck = smartPlugController.resetMeter(24L, mockPrincipal);
assertThat(toCheck.getTotalConsumption()).isEqualTo(new BigDecimal(0));
}
@Test
public void testSceneBinding() {
when(mockPrincipal.getName()).thenReturn("user");
SmartPlug smartPlug = new SmartPlug();
when(smartPlugRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(smartPlug));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = smartPlug.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
Assertions.assertDoesNotThrow(
() -> smartPlugController.sceneBinding(24L, 1L, mockPrincipal));
}
@Test
public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
SmartPlug smartPlug = new SmartPlug();
when(smartPlugRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(smartPlug));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = smartPlug.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> smartPlugController.sceneBinding(24L, 1L, mockPrincipal))
.isInstanceOf(DuplicateStateException.class);
}
} }

View file

@ -0,0 +1,108 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatConditionSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.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("ThermostatConditionController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class ThermostatConditionControllerTests {
@InjectMocks private ThermostatConditionController controller;
@Mock private ThermostatConditionRepository repository;
@Mock private Principal principal;
@Test
public void testCreate() {
ThermostatCondition condition = new ThermostatCondition();
condition.setMode(Thermostat.Mode.HEATING);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
condition.setOperator(ThermostatCondition.Operator.EQUAL);
when(repository.save(condition)).thenReturn(condition);
ThermostatConditionSaveRequest toSend = new ThermostatConditionSaveRequest();
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOperator(ThermostatCondition.Operator.EQUAL);
toSend.setMode(Thermostat.Mode.HEATING);
ThermostatCondition returned = controller.create(toSend);
assertThat(returned.getMode()).isEqualTo(toSend.getMode());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getOperator()).isEqualTo(toSend.getOperator());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
ThermostatCondition condition = new ThermostatCondition();
condition.setMode(Thermostat.Mode.HEATING);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
condition.setOperator(ThermostatCondition.Operator.EQUAL);
when(repository.save(condition)).thenReturn(condition);
ThermostatConditionSaveRequest toSend = new ThermostatConditionSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOperator(ThermostatCondition.Operator.EQUAL);
toSend.setMode(Thermostat.Mode.HEATING);
when(repository.findById(34L)).thenReturn(Optional.of(condition));
ThermostatCondition returned = controller.update(toSend);
assertThat(returned.getMode()).isEqualTo(toSend.getMode());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getOperator()).isEqualTo(toSend.getOperator());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
public void testGetAll() {
ThermostatCondition condition1 = new ThermostatCondition();
condition1.setAutomationId(1L);
ThermostatCondition condition2 = new ThermostatCondition();
condition2.setAutomationId(1L);
ThermostatCondition condition3 = new ThermostatCondition();
condition3.setAutomationId(1L);
ArrayList<ThermostatCondition> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<ThermostatCondition> returned = controller.getAll(42L);
assertThat(returned).contains(condition1);
assertThat(returned).contains(condition2);
assertThat(returned).contains(condition3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}