From 03bd2207811f6cbb51aadc0f1c05681cb446ec9c Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 20 May 2020 11:10:05 +0200 Subject: [PATCH 1/3] WIP on SensorSocketEndpointTests --- .../smarthut/socket/SensorSocketEndpoint.java | 10 ++- .../socket/SensorSocketEndpointTests.java | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java index c924db4..2b54e62 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java @@ -28,7 +28,7 @@ public class SensorSocketEndpoint extends Endpoint { private final Gson gson = GsonConfig.socketGson(); - @Autowired private DevicePopulationService deviceService; + private final DevicePopulationService deviceService; private final UserRepository userRepository; @@ -43,9 +43,13 @@ public class SensorSocketEndpoint extends Endpoint { private final Map> messages = new HashMap<>(); @Autowired - public SensorSocketEndpoint(UserRepository userRepository, JWTTokenUtils jwtTokenUtils) { + public SensorSocketEndpoint( + UserRepository userRepository, + JWTTokenUtils jwtTokenUtils, + DevicePopulationService deviceService) { this.jwtTokenUtils = jwtTokenUtils; this.userRepository = userRepository; + this.deviceService = deviceService; } /** @@ -93,7 +97,7 @@ public class SensorSocketEndpoint extends Endpoint { * @param messages the message batch to send * @param u the user to which to send the message */ - private void broadcast(User u, Collection messages) { + void broadcast(User u, Collection messages) { if (messages.isEmpty()) return; final HashSet sessions = new HashSet<>(authorizedClients.get(u)); for (Session s : sessions) { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java new file mode 100644 index 0000000..8c61124 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java @@ -0,0 +1,69 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doNothing; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DevicePopulationService; +import com.google.gson.Gson; +import java.util.Collection; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class SensorSocketEndpointTests { + + @InjectMocks private SensorSocketEndpoint sensorSocketEndpoint; + + private final Gson gson = GsonConfig.socketGson(); + + @Mock private DevicePopulationService deviceService; + + @Test + public void testQueueDeviceUpdate() { + doNothing().when(deviceService).populateComputedFields(any()); + Device d = new ButtonDimmer(); + + User u = new User(); + u.setId(1L); + + sensorSocketEndpoint.queueDeviceUpdate(d, u, true, 42L, true); + assertThat(d.isFromGuest()).isTrue(); + assertThat(d.getFromHostId()).isEqualTo(42L); + assertThat(d.isDeleted()).isTrue(); + + final boolean[] done = new boolean[1]; + + final SensorSocketEndpoint endpoint = Mockito.spy(sensorSocketEndpoint); + + doAnswer( + i -> { + if (done[0]) fail("Broadcast called more than once"); + final User us = (User) i.getArguments()[0]; + @SuppressWarnings("unchecked") + final Collection jsons = + (Collection) i.getArguments()[1]; + assertThat(us).isSameAs(u); + assertThat(jsons).containsExactly(gson.toJson(d)); + done[0] = true; + return null; + }) + .when(endpoint) + .broadcast(eq(u), any()); + + endpoint.flushDeviceUpdates(); + + assertThat(done[0]).isTrue(); + } +} From f534665bdb7775325499310b215eeb22b1daefde Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 20 May 2020 11:35:25 +0200 Subject: [PATCH 2/3] WIP on SensorSocketEndpointTests --- .../socket/SensorSocketEndpointTests.java | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java index 8c61124..eeee07d 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java @@ -4,16 +4,22 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.*; +import static org.springframework.test.util.ReflectionTestUtils.getField; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DevicePopulationService; +import com.google.common.collect.Multimap; import com.google.gson.Gson; import java.util.Collection; +import java.util.List; +import java.util.Map; +import javax.websocket.Session; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -30,6 +36,20 @@ public class SensorSocketEndpointTests { @Mock private DevicePopulationService deviceService; + @Mock private Session session; + + @Mock private JWTTokenUtils jwtTokenUtils; + + @Mock private UserRepository userRepository; + + private final User u; + + public SensorSocketEndpointTests() { + u = new User(); + u.setName("user"); + u.setId(1L); + } + @Test public void testQueueDeviceUpdate() { doNothing().when(deviceService).populateComputedFields(any()); @@ -66,4 +86,28 @@ public class SensorSocketEndpointTests { assertThat(done[0]).isTrue(); } + + @Test + public void testCheckToken() { + when(userRepository.findByUsername("user")).thenReturn(u); + when(session.getRequestParameterMap()) + .thenReturn(Map.of("token", List.of("randomgarbage"))); + when(jwtTokenUtils.getUsernameFromToken("randomgarbage")).thenReturn("user"); + when(jwtTokenUtils.isTokenExpired("randomgarbage")).thenReturn(false); + sensorSocketEndpoint.onOpen(session, null); + @SuppressWarnings("unchecked") + Multimap map = + (Multimap) + getField( + sensorSocketEndpoint, + SensorSocketEndpoint.class, + "authorizedClients"); + assertThat(map).isNotNull(); + assertThat(map.size()).isEqualTo(1); + assertThat(map.entries().iterator().next().getKey()).isSameAs(u); + assertThat(map.entries().iterator().next().getValue()).isSameAs(session); + } + + @Test + public void testBroadcast() {} } From 5c90428fcad5d41f2c777c70d90fc3df85833468 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 20 May 2020 11:49:54 +0200 Subject: [PATCH 3/3] done SensorSocketEndpointTests --- .../socket/SensorSocketEndpointTests.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java index eeee07d..9104034 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpointTests.java @@ -16,9 +16,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DevicePopulationService; import com.google.common.collect.Multimap; import com.google.gson.Gson; +import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Map; +import javax.websocket.RemoteEndpoint; import javax.websocket.Session; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -88,7 +90,7 @@ public class SensorSocketEndpointTests { } @Test - public void testCheckToken() { + public void testCheckToken() throws IOException { when(userRepository.findByUsername("user")).thenReturn(u); when(session.getRequestParameterMap()) .thenReturn(Map.of("token", List.of("randomgarbage"))); @@ -106,8 +108,26 @@ public class SensorSocketEndpointTests { assertThat(map.size()).isEqualTo(1); assertThat(map.entries().iterator().next().getKey()).isSameAs(u); assertThat(map.entries().iterator().next().getValue()).isSameAs(session); - } + final Session closedSession = Mockito.mock(Session.class); + when(closedSession.isOpen()).thenReturn(false); + when(session.isOpen()).thenReturn(true); + map.put(u, closedSession); - @Test - public void testBroadcast() {} + boolean[] sent = new boolean[1]; + final RemoteEndpoint.Basic b = Mockito.mock(RemoteEndpoint.Basic.class); + when(session.getBasicRemote()).thenReturn(b); + + doAnswer( + i -> { + sent[0] = true; + return null; + }) + .when(b) + .sendText("[\"Malusa\",\"Luciano\"]"); + + sensorSocketEndpoint.broadcast(null, List.of()); + sensorSocketEndpoint.broadcast(u, List.of("\"Malusa\"", "\"Luciano\"")); + assertThat(sent[0]).isTrue(); + assertThat(map.get(u)).containsExactly(session); + } }