Some tests on SceneServiceTests

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-23 19:08:40 +02:00
parent 2f9bdd4a59
commit 5308ddec6f

View file

@ -0,0 +1,59 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
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 SceneServiceTests {
@InjectMocks private SceneService sceneService;
@Mock private SceneRepository sceneRepository;
@Mock private DevicePropagationService deviceService;
@Mock private DevicePopulationService devicePopulationService;
@Test
public void testFindByValidatedId() {
final Scene s = new Scene();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(s));
assertThat(sceneService.findByValidatedId(1L)).isSameAs(s);
}
@Test
public void testApply() {
when(deviceService.saveAllAsOwner(any(), eq("user"), eq(true), anyBoolean()))
.thenAnswer(i -> i.getArgument(0));
doNothing().when(deviceService).saveAllAsGuestSceneApplication(any(), eq("user"), eq(42L));
doNothing().when(devicePopulationService).populateComputedFields(any());
final Scene s = new Scene();
final DimmableState st = new DimmableState();
final DimmableLight d = new DimmableLight();
d.setIntensity(20);
st.setDevice(d);
st.setIntensity(40);
s.getStates().add(st);
assertThat(sceneService.apply(s, "user", false)).containsExactly(d);
assertThat(d.getIntensity()).isEqualTo(40);
d.setIntensity(20);
assertThat(sceneService.applyAsGuest(s, "user", 42L)).containsExactly(d);
assertThat(d.getIntensity()).isEqualTo(40);
}
}