WIP on SensorSocketEndpointTests

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-20 11:10:05 +02:00
parent 4b96820d36
commit 03bd220781
2 changed files with 76 additions and 3 deletions

View file

@ -28,7 +28,7 @@ public class SensorSocketEndpoint extends Endpoint {
private final Gson gson = GsonConfig.socketGson(); private final Gson gson = GsonConfig.socketGson();
@Autowired private DevicePopulationService deviceService; private final DevicePopulationService deviceService;
private final UserRepository userRepository; private final UserRepository userRepository;
@ -43,9 +43,13 @@ public class SensorSocketEndpoint extends Endpoint {
private final Map<User, Map<Long, String>> messages = new HashMap<>(); private final Map<User, Map<Long, String>> messages = new HashMap<>();
@Autowired @Autowired
public SensorSocketEndpoint(UserRepository userRepository, JWTTokenUtils jwtTokenUtils) { public SensorSocketEndpoint(
UserRepository userRepository,
JWTTokenUtils jwtTokenUtils,
DevicePopulationService deviceService) {
this.jwtTokenUtils = jwtTokenUtils; this.jwtTokenUtils = jwtTokenUtils;
this.userRepository = userRepository; this.userRepository = userRepository;
this.deviceService = deviceService;
} }
/** /**
@ -93,7 +97,7 @@ public class SensorSocketEndpoint extends Endpoint {
* @param messages the message batch to send * @param messages the message batch to send
* @param u the user to which to send the message * @param u the user to which to send the message
*/ */
private void broadcast(User u, Collection<String> messages) { void broadcast(User u, Collection<String> messages) {
if (messages.isEmpty()) return; if (messages.isEmpty()) return;
final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u)); final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u));
for (Session s : sessions) { for (Session s : sessions) {

View file

@ -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<String> jsons =
(Collection<String>) 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();
}
}