From 63c4981da7464b24e454c7dad3c04ec2bbdd5d66 Mon Sep 17 00:00:00 2001 From: omenem Date: Mon, 25 May 2020 11:20:52 +0200 Subject: [PATCH] added scenePriorityControllerTests --- .../ScenePriorityControllerTests.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityControllerTests.java new file mode 100644 index 0000000..bf854f8 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityControllerTests.java @@ -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)); + } +}