Merge branch '87-missing-test-in-regularlightcontroller' into 'dev'

added missing tests

Closes #87

See merge request sa4-2020/the-sanmarinoes/backend!184
This commit is contained in:
Matteo Omenetti 2020-05-25 11:37:13 +02:00
commit 2e626b1f4d

View file

@ -9,12 +9,14 @@ import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
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.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -41,6 +43,10 @@ public class RegularLightControllerTests {
@Mock private UserRepository userRepository;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
@ -128,4 +134,50 @@ public class RegularLightControllerTests {
checkRegularLightAgainstRequest(regularLight, toSend);
}
@Test
@DisplayName("an existing id should succeed")
@SneakyThrows(NotFoundException.class)
public void testDelete() {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> regularLightController.delete(42L, mockPrincipal));
}
@Test
public void testSceneBinding() {
RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(regularLight));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = regularLight.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
Assertions.assertDoesNotThrow(
() -> regularLightController.sceneBinding(24L, 1L, mockPrincipal));
}
@Test
public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(regularLight));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = regularLight.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> regularLightController.sceneBinding(24L, 1L, mockPrincipal))
.isInstanceOf(DuplicateStateException.class);
}
}