Added basic tests to SceneController

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-19 18:06:48 +02:00
parent fc6c1e7492
commit 76e5bed119
2 changed files with 103 additions and 0 deletions

View file

@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon;
import javax.persistence.Column; import javax.persistence.Column;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@NoArgsConstructor
@AllArgsConstructor
public class SceneSaveRequest { public class SceneSaveRequest {
/** Room identifier */ /** Room identifier */

View file

@ -0,0 +1,99 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
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.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class SceneControllerTests {
@InjectMocks private SceneController sceneController;
@Mock private UserRepository userRepository;
@Mock private SceneRepository sceneRepository;
@Mock private Principal mockPrincipal;
private final User u;
public SceneControllerTests() {
u = new User();
u.setName("user");
u.setId(1L);
}
@Test
public void testGetAll() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
when(sceneRepository.findByUsername("user")).thenReturn(List.of());
assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty();
}
private void equalToRequest(Scene created, SceneSaveRequest a) {
assertThat(created.getName()).isEqualTo(a.getName());
assertThat(created.getUserId()).isEqualTo(1L);
assertThat(created.getIcon()).isEqualTo(a.getIcon());
assertThat(created.isGuestAccessEnabled()).isEqualTo(a.isGuestAccessEnabled());
}
@Test
public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);
SceneSaveRequest s = new SceneSaveRequest(0, "New Scene", Icon.BATH, true);
Scene created = sceneController.create(s, mockPrincipal);
assertThat(created.getId()).isEqualTo(0L);
equalToRequest(created, s);
}
@Test
public void testUpdate() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
final Scene old = new Scene();
old.setId(42L);
old.setName("Old Name");
when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);
when(sceneRepository.findById(42L)).thenReturn(Optional.of(old));
when(sceneRepository.findById(43L)).thenReturn(Optional.empty());
SceneSaveRequest a = new SceneSaveRequest(0, "New Scene", Icon.BATH, true);
Scene created = sceneController.update(a.getId(), a, mockPrincipal);
assertThat(created.getId()).isEqualTo(42L);
equalToRequest(created, a);
a.setId(43L);
assertThatThrownBy(() -> sceneController.update(a.getId(), a, mockPrincipal))
.isInstanceOf(NotFoundException.class);
}
@Test
public void testDelete() {
final Automation old = new Automation();
old.setId(42L);
old.setName("Old Name");
doNothing().when(sceneRepository).deleteById(42L);
sceneController.deleteById(42L);
}
}