done SensorSocketEndpointTests

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-20 11:49:54 +02:00
parent f534665bdb
commit 5c90428fca

View file

@ -16,9 +16,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DevicePopulationService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DevicePopulationService;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session; import javax.websocket.Session;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -88,7 +90,7 @@ public class SensorSocketEndpointTests {
} }
@Test @Test
public void testCheckToken() { public void testCheckToken() throws IOException {
when(userRepository.findByUsername("user")).thenReturn(u); when(userRepository.findByUsername("user")).thenReturn(u);
when(session.getRequestParameterMap()) when(session.getRequestParameterMap())
.thenReturn(Map.of("token", List.of("randomgarbage"))); .thenReturn(Map.of("token", List.of("randomgarbage")));
@ -106,8 +108,26 @@ public class SensorSocketEndpointTests {
assertThat(map.size()).isEqualTo(1); assertThat(map.size()).isEqualTo(1);
assertThat(map.entries().iterator().next().getKey()).isSameAs(u); assertThat(map.entries().iterator().next().getKey()).isSameAs(u);
assertThat(map.entries().iterator().next().getValue()).isSameAs(session); 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 boolean[] sent = new boolean[1];
public void testBroadcast() {} 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);
}
} }