added scenePriorityControllerTests
This commit is contained in:
parent
ea0361c384
commit
63c4981da7
1 changed files with 107 additions and 0 deletions
|
@ -0,0 +1,107 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
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.ScenePrioritySaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
|
||||||
|
import java.util.List;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@WithMockUser(username = "user")
|
||||||
|
public class ScenePriorityControllerTests {
|
||||||
|
|
||||||
|
@InjectMocks private ScenePriorityController scenePriorityController;
|
||||||
|
|
||||||
|
@Mock private ScenePriorityRepository scenePriorityRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("test for get all should return empty")
|
||||||
|
public void testGetAll() throws NotFoundException {
|
||||||
|
when(scenePriorityRepository.findAllByAutomationId(anyLong())).thenReturn(List.of());
|
||||||
|
assertThat(scenePriorityController.getByAutomationId(20)).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkScenePriorityAgainstRequest(
|
||||||
|
final ScenePriority toCheck, final ScenePrioritySaveRequest request) {
|
||||||
|
assertThat(toCheck).isNotNull();
|
||||||
|
assertThat(toCheck.getAutomationId()).isEqualTo(request.getAutomationId());
|
||||||
|
assertThat(toCheck.getPriority()).isEqualTo(request.getPriority());
|
||||||
|
assertThat(toCheck.getSceneId()).isEqualTo(request.getSceneId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test for create")
|
||||||
|
public void create() {
|
||||||
|
|
||||||
|
ScenePrioritySaveRequest saveRequest = new ScenePrioritySaveRequest();
|
||||||
|
saveRequest.setAutomationId(20L);
|
||||||
|
saveRequest.setPriority(40);
|
||||||
|
saveRequest.setSceneId(10L);
|
||||||
|
|
||||||
|
when(scenePriorityRepository.save(any(ScenePriority.class)))
|
||||||
|
.thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
ScenePriority scenePriority = scenePriorityController.create(saveRequest);
|
||||||
|
|
||||||
|
checkScenePriorityAgainstRequest(scenePriority, saveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test for update")
|
||||||
|
@SneakyThrows(NotFoundException.class)
|
||||||
|
public void update() {
|
||||||
|
|
||||||
|
ScenePrioritySaveRequest saveRequest = new ScenePrioritySaveRequest();
|
||||||
|
saveRequest.setAutomationId(20L);
|
||||||
|
saveRequest.setPriority(40);
|
||||||
|
saveRequest.setSceneId(10L);
|
||||||
|
|
||||||
|
ScenePriority toUpdate = new ScenePriority();
|
||||||
|
|
||||||
|
when(scenePriorityRepository.findById(anyLong()))
|
||||||
|
.thenReturn(java.util.Optional.of(toUpdate));
|
||||||
|
when(scenePriorityRepository.save(any(ScenePriority.class)))
|
||||||
|
.thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
ScenePriority scenePriority = scenePriorityController.update(saveRequest);
|
||||||
|
|
||||||
|
checkScenePriorityAgainstRequest(scenePriority, saveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("an existing id should succeed")
|
||||||
|
public void testDelete() {
|
||||||
|
|
||||||
|
doNothing().when(scenePriorityRepository).deleteBySceneId(eq(42L));
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
Assertions.assertDoesNotThrow(() -> scenePriorityController.delete(42L));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue