Merge branch '89-additional-tests' into 'dev'

Resolve "additional tests"

Closes #89

See merge request sa4-2020/the-sanmarinoes/backend!189
This commit is contained in:
Matteo Omenetti 2020-05-25 14:59:05 +02:00
commit 691130ada7
5 changed files with 85 additions and 6 deletions

View File

@ -38,6 +38,10 @@ public class ScenePriority {
return id;
}
public void setId(long id) {
this.id = id;
}
public Integer getPriority() {
return priority;
}

View File

@ -14,10 +14,10 @@ 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.List;
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;
import org.junit.jupiter.api.extension.ExtendWith;
@ -47,11 +47,6 @@ public class RegularLightControllerTests {
@Mock private StateRepository<State> stateRepository;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
private void checkRegularLightAgainstRequest(
final RegularLight toCheck, final SwitchableSaveRequest request) {
assertThat(toCheck).isNotNull();
@ -60,10 +55,28 @@ public class RegularLightControllerTests {
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
}
@Test
public void testGetAll() {
when(regularLightRepository.findAll()).thenReturn(List.of());
assertThat(regularLightController.findAll().isEmpty());
}
@Test
public void testGet() throws NotFoundException {
RegularLight r = new RegularLight();
when(regularLightRepository.findById(1L)).thenReturn(Optional.of(r));
when(regularLightRepository.findById(2L)).thenReturn(Optional.empty());
assertThat(regularLightController.findById(1L)).isSameAs(r);
assertThatThrownBy(() -> regularLightController.findById(2L))
.isInstanceOf(NotFoundException.class);
}
@Test
@DisplayName("when creating should return the same object")
@SneakyThrows(NotFoundException.class)
public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(RegularLight.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]);
@ -86,6 +99,7 @@ public class RegularLightControllerTests {
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
when(mockPrincipal.getName()).thenReturn("user");
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setName("rl");
toSend.setRoomId(20L);
@ -140,6 +154,7 @@ public class RegularLightControllerTests {
@SneakyThrows(NotFoundException.class)
public void testDelete() {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
@ -150,6 +165,7 @@ public class RegularLightControllerTests {
@Test
public void testSceneBinding() {
when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(regularLight));
@ -166,6 +182,7 @@ public class RegularLightControllerTests {
@Test
public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user"))

View File

@ -33,4 +33,13 @@ public class DimmableStateTests {
this.dimmableState.apply();
assertEquals(30, d.getIntensity());
}
@Test
@DisplayName("apply")
public void copy() {
this.dimmableState.setIntensity(30);
DimmableState copy = this.dimmableState.copy();
assertEquals(30, copy.getIntensity());
}
}

View File

@ -24,6 +24,14 @@ public class ScenePriorityTests {
assertEquals(20, scenePriority.getAutomationId());
}
@Test
@DisplayName("get and set id")
public void getAndSetId() {
scenePriority.setId(20L);
assertEquals(20, scenePriority.getId());
}
@Test
@DisplayName("get and set scene id")
public void getAndSetSceneId() {

View File

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@ -45,4 +46,44 @@ public class StateTests {
this.state.setSceneId(50L);
assertEquals(50, this.state.getSceneId());
}
@Test
@DisplayName("copy to scene id")
public void copyToSceneId() {
this.state.setSceneId(50L);
Scene s = new Scene();
this.state.setScene(s);
this.state.setDeviceId(30L);
DimmableLight d = new DimmableLight();
this.state.setDevice(d);
State stat = this.state.copyToSceneId(10L);
assertEquals(10, stat.getSceneId());
assertEquals(30, stat.getDeviceId());
assertEquals(s, stat.getScene());
assertEquals(d, stat.getDevice());
}
@Test
@DisplayName("preremove")
public void preRemove() {
this.state.setSceneId(50L);
Scene s = new Scene();
this.state.setScene(s);
this.state.setDeviceId(30L);
DimmableLight d = new DimmableLight();
this.state.setDevice(d);
state.removeDeviceAndScene();
assertNull(state.getSceneId());
assertNull(state.getDeviceId());
assertNull(state.getScene());
assertNull(state.getDevice());
}
}