WIP on SensorSocketEndpointTests

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-20 11:35:25 +02:00
parent 03bd220781
commit f534665bdb

View file

@ -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<User, Session> map =
(Multimap<User, Session>)
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() {}
}