added a couple of tests to smartPlugControllerTests

This commit is contained in:
Jacob Salvi 2020-05-24 17:42:53 +02:00
parent 7d06c5d886
commit 86ca38cd47

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);
}
} }