From 5308ddec6f71560332052ae71a32649809248ca0 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 19:08:40 +0200 Subject: [PATCH] Some tests on SceneServiceTests --- .../smarthut/service/SceneServiceTests.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java new file mode 100644 index 0000000..b42f263 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java @@ -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); + } +}