From d897b91be976b2bf2a3ec325cb2a9d56b56303cf Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 16 Sep 2021 15:27:13 +0200 Subject: [PATCH] integration tests for instruction service --- .../SEBClientInstructionServiceTest.java | 149 +++++++++++++++++- 1 file changed, 142 insertions(+), 7 deletions(-) diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/services/SEBClientInstructionServiceTest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/services/SEBClientInstructionServiceTest.java index fc7d820d..346f03dc 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/services/SEBClientInstructionServiceTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/services/SEBClientInstructionServiceTest.java @@ -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 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 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 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 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 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