Merge branch '86-scenepriority-tests' into 'dev'
Resolve "ScenePriority Tests" Closes #86 See merge request sa4-2020/the-sanmarinoes/backend!182
This commit is contained in:
commit
4e9aa3f726
2 changed files with 145 additions and 1 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));
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
|
@ -39,4 +39,41 @@ public class ScenePriorityTests {
|
|||
|
||||
assertEquals(20, scenePriority.getPriority());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("get and set automation")
|
||||
public void getAndSetAutomation() {
|
||||
Automation a = new Automation();
|
||||
scenePriority.setAutomation(a);
|
||||
|
||||
assertEquals(a, scenePriority.getAutomation());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("get and set scene")
|
||||
public void getAndSetScene() {
|
||||
Scene s = new Scene();
|
||||
scenePriority.setScene(s);
|
||||
|
||||
assertEquals(s, scenePriority.getScene());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("get and set scene")
|
||||
public void testRemove() {
|
||||
Scene s = new Scene();
|
||||
scenePriority.setScene(s);
|
||||
scenePriority.setSceneId(20L);
|
||||
|
||||
Automation a = new Automation();
|
||||
scenePriority.setAutomation(a);
|
||||
scenePriority.setAutomationId(20L);
|
||||
|
||||
scenePriority.preRemove();
|
||||
|
||||
assertNull(scenePriority.getAutomation());
|
||||
assertNull(scenePriority.getAutomationId());
|
||||
assertNull(scenePriority.getScene());
|
||||
assertNull(scenePriority.getSceneId());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue