integration tests for instruction service

This commit is contained in:
anhefti 2021-09-16 15:27:13 +02:00
parent e3ac612fb3
commit d897b91be9

View file

@ -12,6 +12,10 @@ import static org.junit.Assert.*;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
@ -39,9 +43,12 @@ public class SEBClientInstructionServiceTest extends AdministrationAPIIntegratio
@Before
public void initSEBConnection() {
this.clientConnectionDAO.createNew(new ClientConnection(
null, 1L, 2L, ConnectionStatus.ACTIVE, "testToken", "user1", "0.0.0.0", false, null, null))
.getOrThrow();
final ClientConnection cc = this.clientConnectionDAO.byConnectionToken("testToken").getOr(null);
if (cc == null) {
this.clientConnectionDAO.createNew(new ClientConnection(
null, 1L, 2L, ConnectionStatus.ACTIVE, "testToken", "user1", "0.0.0.0", false, null, null))
.getOrThrow();
}
}
@Test
@ -55,8 +62,8 @@ public class SEBClientInstructionServiceTest extends AdministrationAPIIntegratio
assertTrue(all.isEmpty());
// register instruction
this.sebClientInstructionService.registerInstruction(2L, InstructionType.SEB_QUIT, Collections.emptyMap(),
"testToken", false);
this.sebClientInstructionService.registerInstruction(
2L, InstructionType.SEB_QUIT, Collections.emptyMap(), "testToken", false);
// check on DB
all = this.clientInstructionDAO
@ -68,11 +75,139 @@ public class SEBClientInstructionServiceTest extends AdministrationAPIIntegratio
final ClientInstructionRecord instrRec = all.iterator().next();
assertEquals("testToken", instrRec.getConnectionToken());
assertEquals(InstructionType.SEB_QUIT.name(), instrRec.getType());
assertEquals("", instrRec.getAttributes());
assertEquals(null, instrRec.getAttributes());
// get instruction JSON
final String json = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals("", json);
assertEquals("{\"instruction\":\"SEB_QUIT\"}", json);
// check no instruction anymore
final String secondPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertNull(secondPing);
// check DB is empty again
all = this.clientInstructionDAO
.getAllActive()
.getOrThrow();
assertNotNull(all);
assertTrue(all.isEmpty());
}
@Test
public void testRegisterWithConfirm() {
// register instruction
this.sebClientInstructionService.registerInstruction(
2L, InstructionType.SEB_RECONFIGURE_SETTINGS, Collections.emptyMap(), "testToken", true);
// check on DB
Collection<ClientInstructionRecord> all = this.clientInstructionDAO
.getAllActive()
.getOrThrow();
// get instruction JSON
final String json = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals(
"{\"instruction\":\"SEB_RECONFIGURE_SETTINGS\",\"attributes\":{\"instruction-confirm\":\"1\"}}",
json);
// check insturction is beeing resent until confirmed
final String secondPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals(
"{\"instruction\":\"SEB_RECONFIGURE_SETTINGS\",\"attributes\":{\"instruction-confirm\":\"1\"}}",
secondPing);
// confirm instruction
this.sebClientInstructionService.confirmInstructionDone("testToken", "1");
// check no instruction anymore
final String nextPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertNull(nextPing);
// check DB is empty again
all = this.clientInstructionDAO
.getAllActive()
.getOrThrow();
assertNotNull(all);
assertTrue(all.isEmpty());
}
@Test
public void testRegisterWithConfirmAndAttributes() {
// register instruction
final Map<String, String> attributes = new LinkedHashMap<>();
attributes.put("attr1", "123");
attributes.put("attr2", "345");
this.sebClientInstructionService.registerInstruction(
2L, InstructionType.SEB_RECONFIGURE_SETTINGS, attributes, "testToken", true);
// check on DB
Collection<ClientInstructionRecord> all = this.clientInstructionDAO
.getAllActive()
.getOrThrow();
// get instruction JSON
final String json = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals(
"{\"instruction\":\"SEB_RECONFIGURE_SETTINGS\",\"attributes\":{\"attr1\":\"123\",\"attr2\":\"345\",\"instruction-confirm\":\"1\"}}",
json);
// check insturction is beeing resent until confirmed
final String secondPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals(
"{\"instruction\":\"SEB_RECONFIGURE_SETTINGS\",\"attributes\":{\"attr1\":\"123\",\"attr2\":\"345\",\"instruction-confirm\":\"1\"}}",
secondPing);
// confirm instruction
this.sebClientInstructionService.confirmInstructionDone("testToken", "1");
// check no instruction anymore
final String nextPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertNull(nextPing);
// check DB is empty again
all = this.clientInstructionDAO
.getAllActive()
.getOrThrow();
assertNotNull(all);
assertTrue(all.isEmpty());
}
@Test
public void testRegisterWithConfirmAndAttributes2() {
// register instruction
final Map<String, String> attributes = new LinkedHashMap<>();
attributes.put("attr1", "123");
attributes.put("attr2", "345");
this.sebClientInstructionService.registerInstruction(
2L, InstructionType.SEB_RECONFIGURE_SETTINGS, attributes,
Stream.of("testToken").collect(Collectors.toSet()), true);
// check on DB
Collection<ClientInstructionRecord> all = this.clientInstructionDAO
.getAllActive()
.getOrThrow();
// get instruction JSON
final String json = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals(
"{\"instruction\":\"SEB_RECONFIGURE_SETTINGS\",\"attributes\":{\"attr1\":\"123\",\"attr2\":\"345\",\"instruction-confirm\":\"1\"}}",
json);
// check insturction is beeing resent until confirmed
final String secondPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertEquals(
"{\"instruction\":\"SEB_RECONFIGURE_SETTINGS\",\"attributes\":{\"attr1\":\"123\",\"attr2\":\"345\",\"instruction-confirm\":\"1\"}}",
secondPing);
// confirm instruction
this.sebClientInstructionService.confirmInstructionDone("testToken", "1");
// check no instruction anymore
final String nextPing = this.sebClientInstructionService.getInstructionJSON("testToken");
assertNull(nextPing);
// check DB is empty again
all = this.clientInstructionDAO