diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyReadTest.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyReadTest.java index 65302cfa47f0cd83788385ab0d644d2627868a9c..35fd0b77128c2e9c4735e3435dd417a48c5ae010 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyReadTest.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyReadTest.java @@ -43,13 +43,16 @@ public class DefaultOnlyReadTest extends AbstractMqttTest { private MqttHandler sender; @Override - public void closeConnections() { - if (sender != null) { - sender.close(); - } - if (model != null) { - model.ragconnectCloseConnections(); - } + protected void createModel() { + model = new A(); + integers = new NativeTypes(); + model.addNativeTypes(integers); + floats = new NativeTypes(); + model.addNativeTypes(floats); + chars = new NativeTypes(); + model.addNativeTypes(chars); + allBoxed = new BoxedTypes(); + model.addBoxedTypes(allBoxed); } @Override @@ -74,6 +77,11 @@ public class DefaultOnlyReadTest extends AbstractMqttTest { assertTrue(sender.waitUntilReady(2, TimeUnit.SECONDS)); } + @Override + protected void communicateSendInitialValue() { + // empty + } + @Override protected void communicateOnlyUpdatedValue() throws InterruptedException { final int expectedIntValue = 1; @@ -118,21 +126,13 @@ public class DefaultOnlyReadTest extends AbstractMqttTest { } @Override - protected void communicateSendInitialValue() { - // empty - } - - @Override - protected void createModel() { - model = new A(); - integers = new NativeTypes(); - model.addNativeTypes(integers); - floats = new NativeTypes(); - model.addNativeTypes(floats); - chars = new NativeTypes(); - model.addNativeTypes(chars); - allBoxed = new BoxedTypes(); - model.addBoxedTypes(allBoxed); + public void closeConnections() { + if (sender != null) { + sender.close(); + } + if (model != null) { + model.ragconnectCloseConnections(); + } } } diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyWriteTest.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyWriteTest.java index c312d4c534d624f0a6c2ee891eba36ddb63be584..05b217f705d6152adf14f6d1f45f4f394a992a76 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyWriteTest.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/DefaultOnlyWriteTest.java @@ -43,61 +43,6 @@ public class DefaultOnlyWriteTest extends AbstractMqttTest { private MqttHandler receiver; private ReceiverData data; - @Override - public void closeConnections() { - if (receiver != null) { - receiver.close(); - } - if (model != null) { - model.ragconnectCloseConnections(); - } - } - - - - - @Override - protected void communicateSendInitialValue() throws InterruptedException { - // check initial value - TestUtils.waitForMqtt(); - checkData(1, 1, 1.1, 'a', "ab"); - - // set new value - setData("2", "2.2", "cd"); - - // check new value - TestUtils.waitForMqtt(); - checkData(2, 2, 2.2, 'c', "cd"); - - // set new value - setData("3", "3.2", "ee"); - - // check new value - TestUtils.waitForMqtt(); - checkData(3, 3, 3.2, 'e', "ee"); - } - - @Override - protected void communicateOnlyUpdatedValue() throws InterruptedException { - // check initial value (will be default values) - TestUtils.waitForMqtt(); - checkData(0, null, null, null, null); - - // set new value - setData("2", "2.2", "cd"); - - // check new value - TestUtils.waitForMqtt(); - checkData(1, 2, 2.2, 'c', "cd"); - - // set new value - setData("3", "3.2", "ee"); - - // check new value - TestUtils.waitForMqtt(); - checkData(2, 3, 3.2, 'e', "ee"); - } - @Override protected void createModel() { model = new A(); @@ -211,6 +156,58 @@ public class DefaultOnlyWriteTest extends AbstractMqttTest { boxedChars.connectCharValue(mqttUri(TOPIC_BOXED_CHARACTER), writeCurrentValue); } + @Override + protected void communicateSendInitialValue() throws InterruptedException { + // check initial value + TestUtils.waitForMqtt(); + checkData(1, 1, 1.1, 'a', "ab"); + + // set new value + setData("2", "2.2", "cd"); + + // check new value + TestUtils.waitForMqtt(); + checkData(2, 2, 2.2, 'c', "cd"); + + // set new value + setData("3", "3.2", "ee"); + + // check new value + TestUtils.waitForMqtt(); + checkData(3, 3, 3.2, 'e', "ee"); + } + + @Override + protected void communicateOnlyUpdatedValue() throws InterruptedException { + // check initial value (will be default values) + TestUtils.waitForMqtt(); + checkData(0, null, null, null, null); + + // set new value + setData("2", "2.2", "cd"); + + // check new value + TestUtils.waitForMqtt(); + checkData(1, 2, 2.2, 'c', "cd"); + + // set new value + setData("3", "3.2", "ee"); + + // check new value + TestUtils.waitForMqtt(); + checkData(2, 3, 3.2, 'e', "ee"); + } + + @Override + public void closeConnections() { + if (receiver != null) { + receiver.close(); + } + if (model != null) { + model.ragconnectCloseConnections(); + } + } + private void setData(String integerDriver, String floatDriver, String stringDriver) { nativeIntegers.setDriverSyn(integerDriver); nativeFloats.setDriverSyn(floatDriver); diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Errors.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Errors.java index 17d853fba71c2ee46c6c6c7b5424958af0a5f8d2..cc0596482e7559166947febb30144118ab4dbe10 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Errors.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Errors.java @@ -19,7 +19,7 @@ import static org.jastadd.ragconnect.tests.TestUtils.exec; import static org.jastadd.ragconnect.tests.TestUtils.readFile; import static org.junit.jupiter.api.Assertions.assertTrue; -class Errors { +public class Errors { private static final Logger logger = LogManager.getLogger(Errors.class); private static final String FILENAME_PATTERN = "$FILENAME"; diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ExampleTest.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ExampleTest.java index c3df78d1b17658a1327afa0f4e2f308eb5ee159b..c3fae394f191920fdfdbe9be818a0393516faa9d 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ExampleTest.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ExampleTest.java @@ -37,16 +37,70 @@ public class ExampleTest extends AbstractMqttTest { } @Override - public void closeConnections() { - if (handler != null) { - handler.close(); - } - if (model != null) { - model.ragconnectCloseConnections(); - } + protected void createModel() { + model = new Model(); + + ZoneModel zoneModel = new ZoneModel(); + + IntPosition firstPosition = makePosition(0, 0, 0); + IntPosition secondPosition = makePosition(-1, 0, 0); + IntPosition thirdPosition = makePosition(1, 0, 0); + + Zone safetyZone = new Zone(); + safetyZone.addCoordinate(new Coordinate(firstPosition)); + safetyZone.addCoordinate(new Coordinate(secondPosition)); + safetyZone.addCoordinate(new Coordinate(thirdPosition)); + zoneModel.addSafetyZone(safetyZone); + model.setZoneModel(zoneModel); + + robotArm = new RobotArm(); + + link1 = new Link(); + link1.setName("joint1"); + link1.setCurrentPosition(firstPosition); + + link2 = new Link(); + link2.setName("joint2"); + link2.setCurrentPosition(secondPosition); + + EndEffector endEffector = new EndEffector(); + endEffector.setName("gripper"); + endEffector.setCurrentPosition(makePosition(2, 2, 3)); + + robotArm.addLink(link1); + robotArm.addLink(link2); + robotArm.setEndEffector(endEffector); + model.setRobotArm(robotArm); } + @Override + protected void setupReceiverAndConnect(boolean writeCurrentValue) throws IOException { + model.ragconnectSetupMqttWaitUntilReady(2, TimeUnit.SECONDS); + + handler = new MqttHandler().dontSendWelcomeMessage().setHost(TestUtils.getMqttHost()); + assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS)); + + // add dependencies + robotArm.addDependency1(link1); + robotArm.addDependency1(link2); + robotArm.addDependency1(robotArm.getEndEffector()); + + data = new ReceiverData(); + + handler.newConnection(TOPIC_CONFIG, bytes -> { + data.numberOfConfigs += 1; + try { + data.lastConfig = RobotConfig.parseFrom(bytes); + data.failedLastConversion = false; + } catch (InvalidProtocolBufferException e) { + data.failedLastConversion = true; + } + }); + robotArm.connectAppropriateSpeed(mqttUri(TOPIC_CONFIG), writeCurrentValue); + link1.connectCurrentPosition(mqttUri(TOPIC_JOINT1)); + link2.connectCurrentPosition(mqttUri(TOPIC_JOINT2)); + } @Override protected void communicateSendInitialValue() throws InterruptedException { @@ -182,6 +236,16 @@ public class ExampleTest extends AbstractMqttTest { assertFalse(data.failedLastConversion); } + @Override + public void closeConnections() { + if (handler != null) { + handler.close(); + } + if (model != null) { + model.ragconnectCloseConnections(); + } + } + @Test public void testFailedConversion() throws IOException { createModel(); @@ -200,72 +264,6 @@ public class ExampleTest extends AbstractMqttTest { ); } - @Override - protected void setupReceiverAndConnect(boolean writeCurrentValue) throws IOException { - model.ragconnectSetupMqttWaitUntilReady(2, TimeUnit.SECONDS); - - handler = new MqttHandler().dontSendWelcomeMessage().setHost(TestUtils.getMqttHost()); - assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS)); - - // add dependencies - robotArm.addDependency1(link1); - robotArm.addDependency1(link2); - robotArm.addDependency1(robotArm.getEndEffector()); - - data = new ReceiverData(); - - handler.newConnection(TOPIC_CONFIG, bytes -> { - data.numberOfConfigs += 1; - try { - data.lastConfig = RobotConfig.parseFrom(bytes); - data.failedLastConversion = false; - } catch (InvalidProtocolBufferException e) { - data.failedLastConversion = true; - } - }); - - robotArm.connectAppropriateSpeed(mqttUri(TOPIC_CONFIG), writeCurrentValue); - link1.connectCurrentPosition(mqttUri(TOPIC_JOINT1)); - link2.connectCurrentPosition(mqttUri(TOPIC_JOINT2)); - } - - @Override - protected void createModel() { - model = new Model(); - - ZoneModel zoneModel = new ZoneModel(); - - IntPosition firstPosition = makePosition(0, 0, 0); - IntPosition secondPosition = makePosition(-1, 0, 0); - IntPosition thirdPosition = makePosition(1, 0, 0); - - Zone safetyZone = new Zone(); - safetyZone.addCoordinate(new Coordinate(firstPosition)); - safetyZone.addCoordinate(new Coordinate(secondPosition)); - safetyZone.addCoordinate(new Coordinate(thirdPosition)); - zoneModel.addSafetyZone(safetyZone); - model.setZoneModel(zoneModel); - - robotArm = new RobotArm(); - - link1 = new Link(); - link1.setName("joint1"); - link1.setCurrentPosition(firstPosition); - - link2 = new Link(); - link2.setName("joint2"); - link2.setCurrentPosition(secondPosition); - - EndEffector endEffector = new EndEffector(); - endEffector.setName("gripper"); - endEffector.setCurrentPosition(makePosition(2, 2, 3)); - - robotArm.addLink(link1); - robotArm.addLink(link2); - robotArm.setEndEffector(endEffector); - model.setRobotArm(robotArm); - } - private static IntPosition makePosition(int x, int y, int z) { return IntPosition.of(x, y, z); } diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read1Write2Test.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read1Write2Test.java index 2940aacbf26ae1a6e63cd7c763868d6f48639217..b51b88ddc8bfccf26ab49a31e173731feea72e27 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read1Write2Test.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read1Write2Test.java @@ -37,78 +37,6 @@ public class Read1Write2Test extends AbstractMqttTest { private ReceiverData dataOther1; private ReceiverData dataOther2; - @Override - public void closeConnections() { - if (handler != null) { - handler.close(); - } - if (model != null) { - model.ragconnectCloseConnections(); - } - } - - - - @Override - protected void communicateSendInitialValue() throws InterruptedException { - // check initial value - TestUtils.waitForMqtt(); - checkData(1, Integer.parseInt(INITIAL_VALUE), prefixed(INITIAL_VALUE), 1, Integer.parseInt(INITIAL_VALUE), prefixed(INITIAL_VALUE)); - - // set new value - sendData("2", "3"); - - // check new value - TestUtils.waitForMqtt(); - checkData(2, 2, prefixed("2"), 2, 3, prefixed("3")); - - // set new value - sendData("4", "4"); - - // check new value - TestUtils.waitForMqtt(); - checkData(3, 4, prefixed("4"), 3, 4, prefixed("4")); - - // set new value only for same - setDataOnlySame("77"); - - // check new value - TestUtils.waitForMqtt(); - checkData(4, 77, prefixed("77"), 3, 4, prefixed("4")); - } - - private String prefixed(String s) { - return "prefix" + s; - } - - @Override - protected void communicateOnlyUpdatedValue() throws InterruptedException { -// check initial value - TestUtils.waitForMqtt(); - checkData(0, null, null, 0, null, null); - - // set new value - sendData("2", "3"); - - // check new value - TestUtils.waitForMqtt(); - checkData(1, 2, prefixed("2"), 1, 3, prefixed("3")); - - // set new value - sendData("4", "4"); - - // check new value - TestUtils.waitForMqtt(); - checkData(2, 4, prefixed("4"), 2, 4, prefixed("4")); - - // set new value only for same - setDataOnlySame("77"); - - // check new value - TestUtils.waitForMqtt(); - checkData(3, 77, prefixed("77"), 2, 4, prefixed("4")); - } - @Override protected void createModel() { // Setting value for Input without dependencies does not trigger any updates @@ -183,6 +111,68 @@ public class Read1Write2Test extends AbstractMqttTest { other2.connectOutString(mqttUri(TOPIC_DIFFERENT_WRITE2_STRING), writeCurrentValue); } + @Override + protected void communicateSendInitialValue() throws InterruptedException { + // check initial value + checkData(1, Integer.parseInt(INITIAL_VALUE), prefixed(INITIAL_VALUE), 1, Integer.parseInt(INITIAL_VALUE), prefixed(INITIAL_VALUE)); + + // set new value + sendData("2", "3"); + + // check new value + checkData(2, 2, prefixed("2"), 2, 3, prefixed("3")); + + // set new value + sendData("4", "4"); + + // check new value + checkData(3, 4, prefixed("4"), 3, 4, prefixed("4")); + + // set new value only for same + setDataOnlySame("77"); + + // check new value + checkData(4, 77, prefixed("77"), 3, 4, prefixed("4")); + } + + @Override + protected void communicateOnlyUpdatedValue() throws InterruptedException { +// check initial value + checkData(0, null, null, 0, null, null); + + // set new value + sendData("2", "3"); + + // check new value + checkData(1, 2, prefixed("2"), 1, 3, prefixed("3")); + + // set new value + sendData("4", "4"); + + // check new value + checkData(2, 4, prefixed("4"), 2, 4, prefixed("4")); + + // set new value only for same + setDataOnlySame("78"); + + // check new value + checkData(3, 78, prefixed("78"), 2, 4, prefixed("4")); + } + + @Override + public void closeConnections() { + if (handler != null) { + handler.close(); + } + if (model != null) { + model.ragconnectCloseConnections(); + } + } + + private String prefixed(String s) { + return "prefix" + s; + } + private void sendData(String inputSame, String inputDifferent) { handler.publish(TOPIC_SAME_READ, inputSame.getBytes()); handler.publish(TOPIC_DIFFERENT_READ, inputDifferent.getBytes()); @@ -194,32 +184,13 @@ public class Read1Write2Test extends AbstractMqttTest { private void checkData(int numberOfSameValues, Integer lastSameIntValue, String lastSameStringValue, int numberOfDifferentValues, Integer lastDifferentIntValue, - String lastDifferentStringValue) { - /* the value "-2" is never used in the test, so a test will always fail comparing to this value - especially, it is not the initial value */ - ReceiverData expectedDataSame = ReceiverData.of( - numberOfSameValues, - lastSameIntValue != null ? lastSameIntValue : -2, - lastSameStringValue); - compareData(expectedDataSame, dataSame); - ReceiverData expectedDataDifferent = ReceiverData.of( - numberOfDifferentValues, - lastDifferentIntValue != null ? lastDifferentIntValue : -2, - lastDifferentStringValue); - compareData(expectedDataDifferent, dataOther1); - compareData(expectedDataDifferent, dataOther2); - } - - private void compareData(ReceiverData expectedData, - ReceiverData actual) { - assertEquals(expectedData.numberOfIntValues, actual.numberOfIntValues); - assertEquals(expectedData.numberOfStringValues, actual.numberOfStringValues); - if (expectedData.numberOfIntValues > 0) { - assertEquals(expectedData.lastIntValue, actual.lastIntValue); - } - if (expectedData.numberOfStringValues > 0) { - assertEquals(expectedData.lastStringValue, actual.lastStringValue); - } + String lastDifferentStringValue) throws InterruptedException { + TestUtils.waitForMqtt(); + dataSame.assertEqualData(numberOfSameValues, lastSameIntValue, lastSameStringValue); + dataOther1.assertEqualData(numberOfDifferentValues, + lastDifferentIntValue, lastDifferentStringValue); + dataOther2.assertEqualData(numberOfDifferentValues, + lastDifferentIntValue, lastDifferentStringValue); } private static class ReceiverData { @@ -228,14 +199,19 @@ public class Read1Write2Test extends AbstractMqttTest { String lastStringValue; int numberOfStringValues = 0; - static ReceiverData of(int numberOfValues, int lastIntValue, String lastStringValue) { - ReceiverData result = new ReceiverData(); - result.lastIntValue = lastIntValue; - result.lastStringValue = lastStringValue; - result.numberOfIntValues = numberOfValues; - result.numberOfStringValues = numberOfValues; - return result; + void assertEqualData(int expectedNumberOfValues, + Integer expectedLastIntValue, + String expectedLastStringValue) { + /* the value "-2" is never used in the test, so a test will always fail comparing to this value + especially, it is not the initial value */ + assertEquals(expectedNumberOfValues, this.numberOfIntValues); + assertEquals(expectedNumberOfValues, this.numberOfStringValues); + if (expectedNumberOfValues > 0) { + assertEquals(expectedLastIntValue != null ? expectedLastIntValue : -2, this.lastIntValue); + } + if (expectedNumberOfValues > 0) { + assertEquals(expectedLastStringValue, this.lastStringValue); + } } } - } diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read2Write1Test.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read2Write1Test.java index 4790b173bab7cab2d6022e35c40125af7b79ed5f..b9bce7882d6552fe9da3a55c40bcbedc22429fad 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read2Write1Test.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/Read2Write1Test.java @@ -36,86 +36,6 @@ public class Read2Write1Test extends AbstractMqttTest { private ReceiverData dataOther1; private ReceiverData dataOther2; - @Override - public void closeConnections() { - if (handler != null) { - handler.close(); - } - if (model != null) { - model.ragconnectCloseConnections(); - } - } - - - - @Override - protected void communicateSendInitialValue() throws InterruptedException { - // check initial value - TestUtils.waitForMqtt(); - checkData(1, Integer.parseInt(INITIAL_VALUE + INITIAL_VALUE), - 1, Integer.parseInt(INITIAL_VALUE + INITIAL_VALUE)); - - // set new value - sendData(true, "2", true, "3"); - - // check new value. same: 2, 0. different: 3, 0. - TestUtils.waitForMqtt(); - checkData(2, 20, - 2, 30); - - // set new value - sendData(false, "4", false, "4"); - - // check new value. same: 2, 4. different: 3, 4. - TestUtils.waitForMqtt(); - checkData(3, 24, - 3, 34); - - // set new value only for same - setDataOnlySame(true, "77"); - - // check new value. same: 77, 4. different: 3, 4. - TestUtils.waitForMqtt(); - checkData(4, 774, - 3, 34); - } - - private String prefixed(String s) { - return "prefix" + s; - } - - @Override - protected void communicateOnlyUpdatedValue() throws InterruptedException { - // check initial value - TestUtils.waitForMqtt(); - checkData(0, null, - 0, null); - - // set new value - sendData(true, "2", true, "3"); - - // check new value. same: 2, 0. different: 3, 0. - TestUtils.waitForMqtt(); - checkData(1, 20, - 1, 30); - - // set new value - sendData(false, "4", false, "4"); - - // check new value. same: 2, 4. different: 3, 4. - TestUtils.waitForMqtt(); - checkData(2, 24, - 2, 34); - - // set new value only for same - setDataOnlySame(true, "77"); - - // check new value. same: 77, 4. different: 3, 4. - TestUtils.waitForMqtt(); - checkData(3, 774, - 2, 34); - } - @Override protected void createModel() { // Setting value for Input without dependencies does not trigger any updates @@ -179,6 +99,72 @@ public class Read2Write1Test extends AbstractMqttTest { other2.connectOutInteger(mqttUri(TOPIC_DIFFERENT_WRITE2_INT), writeCurrentValue); } + @Override + protected void communicateSendInitialValue() throws InterruptedException { + // check initial value + checkData(1, Integer.parseInt(INITIAL_VALUE + INITIAL_VALUE), + 1, Integer.parseInt(INITIAL_VALUE + INITIAL_VALUE)); + + // set new value + sendData(true, "2", true, "3"); + + // check new value. same: 2, 0. different: 3, 0. + checkData(2, 20, + 2, 30); + + // set new value + sendData(false, "4", false, "4"); + + // check new value. same: 2, 4. different: 3, 4. + checkData(3, 24, + 3, 34); + + // set new value only for same + setDataOnlySame("77"); + + // check new value. same: 77, 4. different: 3, 4. + checkData(4, 774, + 3, 34); + } + + @Override + protected void communicateOnlyUpdatedValue() throws InterruptedException { + // check initial value + checkData(0, null, + 0, null); + + // set new value + sendData(true, "2", true, "3"); + + // check new value. same: 2, 0. different: 3, 0. + checkData(1, 20, + 1, 30); + + // set new value + sendData(false, "4", false, "4"); + + // check new value. same: 2, 4. different: 3, 4. + checkData(2, 24, + 2, 34); + + // set new value only for same + setDataOnlySame("78"); + + // check new value. same: 78, 4. different: 3, 4. + checkData(3, 784, + 2, 34); + } + + @Override + public void closeConnections() { + if (handler != null) { + handler.close(); + } + if (model != null) { + model.ragconnectCloseConnections(); + } + } + private void sendData(boolean useSameInput1, String inputSame, boolean useDifferentInput1, String inputDifferent) { handler.publish(useSameInput1 ? TOPIC_SAME_READ1 : TOPIC_SAME_READ2, @@ -187,45 +173,32 @@ public class Read2Write1Test extends AbstractMqttTest { inputDifferent.getBytes()); } - private void setDataOnlySame(boolean useSameInput1, String inputSame) { - handler.publish(useSameInput1 ? TOPIC_SAME_READ1 : TOPIC_DIFFERENT_READ2, - inputSame.getBytes()); + private void setDataOnlySame(String inputSame) { + handler.publish(TOPIC_SAME_READ1, inputSame.getBytes()); } private void checkData(int numberOfSameValues, Integer lastSameIntValue, - int numberOfDifferentValues, Integer lastDifferentIntValue) { + int numberOfDifferentValues, Integer lastDifferentIntValue) + throws InterruptedException { + TestUtils.waitForMqtt(); /* the value "-2" is never used in the test, so a test will always fail comparing to this value especially, it is not the initial value */ - ReceiverData expectedDataSame = ReceiverData.of( - numberOfSameValues, - lastSameIntValue != null ? lastSameIntValue : -2 - ); - compareData(expectedDataSame, dataSame); - ReceiverData expectedDataDifferent = ReceiverData.of( - numberOfDifferentValues, - lastDifferentIntValue != null ? lastDifferentIntValue : -2 - ); - compareData(expectedDataDifferent, dataOther1); - compareData(expectedDataDifferent, dataOther2); - } - - private void compareData(ReceiverData expectedData, - ReceiverData actual) { - assertEquals(expectedData.numberOfIntValues, actual.numberOfIntValues); - if (expectedData.numberOfIntValues > 0) { - assertEquals(expectedData.lastIntValue, actual.lastIntValue); - } + dataSame.assertEqualData(numberOfSameValues, lastSameIntValue); + dataOther1.assertEqualData(numberOfDifferentValues, lastDifferentIntValue); + dataOther2.assertEqualData(numberOfDifferentValues, lastDifferentIntValue); } private static class ReceiverData { int lastIntValue; int numberOfIntValues = 0; - static ReceiverData of(int numberOfValues, int lastIntValue) { - ReceiverData result = new ReceiverData(); - result.lastIntValue = lastIntValue; - result.numberOfIntValues = numberOfValues; - return result; + void assertEqualData(int expectedNumberOfValues, Integer expectedLastIntValue) { + /* the value "-2" is never used in the test, so a test will always fail comparing to this value + especially, it is not the initial value */ + assertEquals(expectedNumberOfValues, this.numberOfIntValues); + if (expectedNumberOfValues > 0) { + assertEquals(expectedLastIntValue != null ? expectedLastIntValue : -2, this.lastIntValue); + } } } diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TokenValueSendTest.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TokenValueSendTest.java index bc68f5bdb8b19167deb50fa54b52e933235bceb3..a0c0a24d8e63fb1a53337e31a1f910844954385f 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TokenValueSendTest.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TokenValueSendTest.java @@ -39,13 +39,60 @@ public class TokenValueSendTest extends AbstractMqttTest { private ReceiverData dataThreeOther; @Override - public void closeConnections() { - if (handler != null) { - handler.close(); - } - if (model != null) { - model.ragconnectCloseConnections(); - } + protected void createModel() { + // Setting value for Input without dependencies does not trigger any updates + model = new A(); + + one = new OnlySend(); + one.setValue(INITIAL_VALUE); + model.setOnlySend(one); + + two = new ReceiveAndSend(); + two.setValue(INITIAL_VALUE); + model.setReceiveAndSend(two); + + three = new ReceiveSendAndDepend(); + three.setValue(INITIAL_VALUE); + model.setReceiveSendAndDepend(three); + } + + protected void setupReceiverAndConnect(boolean writeCurrentValue) throws IOException { + model.ragconnectSetupMqttWaitUntilReady(2, TimeUnit.SECONDS); + + handler = new MqttHandler().dontSendWelcomeMessage().setHost(TestUtils.getMqttHost()); + assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS)); + + three.addDependency1(three); + + dataOne = new ReceiverData(); + dataTwo = new ReceiverData(); + dataThree = new ReceiverData(); + dataThreeOther = new ReceiverData(); + + handler.newConnection(TOPIC_SEND_ONE, bytes -> { + dataOne.numberOfStringValues += 1; + dataOne.lastStringValue = new String(bytes); + }); + handler.newConnection(TOPIC_SEND_TWO, bytes -> { + dataTwo.numberOfStringValues += 1; + dataTwo.lastStringValue = new String(bytes); + }); + + handler.newConnection(TOPIC_SEND_THREE_VALUE, bytes -> { + dataThree.numberOfStringValues += 1; + dataThree.lastStringValue = new String(bytes); + }); + handler.newConnection(TOPIC_SEND_THREE_OTHER, bytes -> { + dataThreeOther.numberOfStringValues += 1; + dataThreeOther.lastStringValue = new String(bytes); + }); + + one.connectValue(mqttUri(TOPIC_SEND_ONE), writeCurrentValue); + two.connectValue(mqttUri(TOPIC_RECEIVE_TWO)); + two.connectValue(mqttUri(TOPIC_SEND_TWO), writeCurrentValue); + three.connectValue(mqttUri(TOPIC_RECEIVE_THREE_VALUE)); + three.connectValue(mqttUri(TOPIC_SEND_THREE_VALUE), writeCurrentValue); + three.connectOtherOutput(mqttUri(TOPIC_SEND_THREE_OTHER), writeCurrentValue); } @Override @@ -169,60 +216,13 @@ public class TokenValueSendTest extends AbstractMqttTest { } @Override - protected void createModel() { - // Setting value for Input without dependencies does not trigger any updates - model = new A(); - - one = new OnlySend(); - one.setValue(INITIAL_VALUE); - model.setOnlySend(one); - - two = new ReceiveAndSend(); - two.setValue(INITIAL_VALUE); - model.setReceiveAndSend(two); - - three = new ReceiveSendAndDepend(); - three.setValue(INITIAL_VALUE); - model.setReceiveSendAndDepend(three); - } - - protected void setupReceiverAndConnect(boolean writeCurrentValue) throws IOException { - model.ragconnectSetupMqttWaitUntilReady(2, TimeUnit.SECONDS); - - handler = new MqttHandler().dontSendWelcomeMessage().setHost(TestUtils.getMqttHost()); - assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS)); - - three.addDependency1(three); - - dataOne = new ReceiverData(); - dataTwo = new ReceiverData(); - dataThree = new ReceiverData(); - dataThreeOther = new ReceiverData(); - - handler.newConnection(TOPIC_SEND_ONE, bytes -> { - dataOne.numberOfStringValues += 1; - dataOne.lastStringValue = new String(bytes); - }); - handler.newConnection(TOPIC_SEND_TWO, bytes -> { - dataTwo.numberOfStringValues += 1; - dataTwo.lastStringValue = new String(bytes); - }); - - handler.newConnection(TOPIC_SEND_THREE_VALUE, bytes -> { - dataThree.numberOfStringValues += 1; - dataThree.lastStringValue = new String(bytes); - }); - handler.newConnection(TOPIC_SEND_THREE_OTHER, bytes -> { - dataThreeOther.numberOfStringValues += 1; - dataThreeOther.lastStringValue = new String(bytes); - }); - - one.connectValue(mqttUri(TOPIC_SEND_ONE), writeCurrentValue); - two.connectValue(mqttUri(TOPIC_RECEIVE_TWO)); - two.connectValue(mqttUri(TOPIC_SEND_TWO), writeCurrentValue); - three.connectValue(mqttUri(TOPIC_RECEIVE_THREE_VALUE)); - three.connectValue(mqttUri(TOPIC_SEND_THREE_VALUE), writeCurrentValue); - three.connectOtherOutput(mqttUri(TOPIC_SEND_THREE_OTHER), writeCurrentValue); + public void closeConnections() { + if (handler != null) { + handler.close(); + } + if (model != null) { + model.ragconnectCloseConnections(); + } } private void sendData(String inputTwo, String inputThree) { diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TutorialTest.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TutorialTest.java index d7f346252639630d5818af4ceeef314b7ccecd9e..dbfca5452a1f6058dfe699efce4e3d682b9e19ec 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TutorialTest.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/TutorialTest.java @@ -20,16 +20,6 @@ public class TutorialTest extends AbstractMqttTest { private B b1; private B b2; - @Override - protected void closeConnections() { - if (handler != null) { - handler.close(); - } - if (a != null) { - a.ragconnectCloseConnections(); - } - } - @Override protected void createModel() { a = new A(); @@ -65,4 +55,14 @@ public class TutorialTest extends AbstractMqttTest { protected void communicateOnlyUpdatedValue() { // empty } + + @Override + protected void closeConnections() { + if (handler != null) { + handler.close(); + } + if (a != null) { + a.ragconnectCloseConnections(); + } + } } diff --git a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ViaTest.java b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ViaTest.java index 3c173522680500703be35e96289f0a6e35117b3a..c3041f7568c08f3bb390decedd2ccfbeeb8e0cbd 100644 --- a/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ViaTest.java +++ b/ragconnect.tests/src/test/java/org/jastadd/ragconnect/tests/ViaTest.java @@ -142,7 +142,7 @@ public class ViaTest extends AbstractMqttTest { @Override protected void communicateOnlyUpdatedValue() throws InterruptedException { -// check initial value + // check initial value TestUtils.waitForMqtt(); checkData(0, null, "200-R2R-ToRest",