diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/common/configuration/NameResolvingEnvironmentConfigurationTest.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/common/configuration/NameResolvingEnvironmentConfigurationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b7e31d29281801390f2a97281973e68be6c92bd7 --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/common/configuration/NameResolvingEnvironmentConfigurationTest.java @@ -0,0 +1,89 @@ +package rocks.theodolite.commons.common.configuration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.apache.commons.configuration2.Configuration; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.EnvironmentVariables; + +import rocks.theodolite.commons.commons.configuration.NameResolvingEnvironmentConfiguration; + +public class NameResolvingEnvironmentConfigurationTest { + + private static final String PROPERTY_FILES_KEY = "my.env.var"; + private static final String ENV_VAR_KEY = "MY_ENV_VAR"; + private static final String STRING_VALUE = "value"; + private static final String STRING_VALUE_2 = "value2"; + private static final int INT_VALUE = 7; + + @Rule + public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); + + @Test + public void testHelperLibrary() { + this.environmentVariables.clear("name"); + this.environmentVariables.set("name", STRING_VALUE); + assertEquals("value", System.getenv("name")); + } + + @Test + public void testGetUsingEnvVarFormat() { + this.environmentVariables.clear(ENV_VAR_KEY); + this.environmentVariables.set(ENV_VAR_KEY, STRING_VALUE); + final Configuration config = new NameResolvingEnvironmentConfiguration(); + final String result = config.getString(ENV_VAR_KEY); + assertEquals(STRING_VALUE, result); + } + + @Test + public void testGetUsingPropertiesFormat() { + this.environmentVariables.clear(ENV_VAR_KEY); + this.environmentVariables.set(ENV_VAR_KEY, STRING_VALUE); + final Configuration config = new NameResolvingEnvironmentConfiguration(); + final String result = config.getString(PROPERTY_FILES_KEY); + assertEquals(STRING_VALUE, result); + } + + @Test + public void testGetOfNumber() { + this.environmentVariables.clear(ENV_VAR_KEY); + this.environmentVariables.set(ENV_VAR_KEY, String.valueOf(INT_VALUE)); + final Configuration config = new NameResolvingEnvironmentConfiguration(); + final int result = config.getInt(PROPERTY_FILES_KEY); + assertEquals(INT_VALUE, result); + } + + @Test + public void testGetOfBothExisting() { + this.environmentVariables.clear(ENV_VAR_KEY, PROPERTY_FILES_KEY); + this.environmentVariables.set(ENV_VAR_KEY, STRING_VALUE); + this.environmentVariables.set(PROPERTY_FILES_KEY, STRING_VALUE_2); + final Configuration config = new NameResolvingEnvironmentConfiguration(); + final String result = config.getString(PROPERTY_FILES_KEY); + assertEquals(STRING_VALUE_2, result); + } + + @Test + public void testGetNonExistingUsingEnvVarFormat() { + this.environmentVariables.clear(ENV_VAR_KEY); + final Configuration config = new NameResolvingEnvironmentConfiguration(); + final String result = config.getString(ENV_VAR_KEY); + assertNull(result); + } + + @Test + public void testGetNonExistingUsingPropertiesFormat() { + this.environmentVariables.clear(ENV_VAR_KEY); + final Configuration config = new NameResolvingEnvironmentConfiguration(); + final String result = config.getString(PROPERTY_FILES_KEY); + assertNull(result); + } + + @Test + public void testFormatKeyAsEnvVariable() { + assertEquals(ENV_VAR_KEY, NameResolvingEnvironmentConfiguration.formatKeyAsEnvVariable(PROPERTY_FILES_KEY)); + } + +} diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/ExampleSensors.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/ExampleSensors.java new file mode 100644 index 0000000000000000000000000000000000000000..a0705877f65d9c48dfc6c5441f0a661955e8d172 --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/ExampleSensors.java @@ -0,0 +1,36 @@ +package rocks.theodolite.commons.model.sensorregistry; + +import java.util.List; +import java.util.stream.Collectors; + +public final class ExampleSensors { + + private static final SensorRegistry REGISTRY; + + static { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + final MutableAggregatedSensor comcent = topLevel.addChildAggregatedSensor("comcent"); + final MutableAggregatedSensor server1 = comcent.addChildAggregatedSensor("comcent.server1"); + final MachineSensor server1pw1 = server1.addChildMachineSensor("comcent.server1.pw1"); + final MachineSensor server1pw2 = server1.addChildMachineSensor("comcent.server1.pw2"); + final MachineSensor server1pw3 = server1.addChildMachineSensor("comcent.server1.pw3"); + final MutableAggregatedSensor server2 = comcent.addChildAggregatedSensor("comcent.server2"); + final MachineSensor server2pw1 = server2.addChildMachineSensor("comcent.server2.pw1"); + final MachineSensor server2pw2 = server2.addChildMachineSensor("comcent.server2.pw2"); + + REGISTRY = ImmutableSensorRegistry.copyOf(sensorRegistry); + } + + private ExampleSensors() {} + + public static List<String> machineSensorNames() { + return REGISTRY.getMachineSensors().stream().map(s -> s.getIdentifier()) + .collect(Collectors.toList()); + } + + public static SensorRegistry registry() { + return REGISTRY; + } + +} diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/ImmutableSensorRegistryTest.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/ImmutableSensorRegistryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1857d1b19a0f4a33e7c9a83acd59be5dd1b21461 --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/ImmutableSensorRegistryTest.java @@ -0,0 +1,106 @@ +package rocks.theodolite.commons.model.sensorregistry; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class ImmutableSensorRegistryTest { + + @Test + public void testEquals() { + final ImmutableSensorRegistry sensorRegistry1 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + final ImmutableSensorRegistry sensorRegistry2 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.equals(sensorRegistry2)); + assertTrue(sensorRegistry2.equals(sensorRegistry1)); + } + + @Test + public void testEqualsWithDifferentNames() { + final ImmutableSensorRegistry sensorRegistry1 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + final ImmutableSensorRegistry sensorRegistry2 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistryWithDifferentNames()); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.equals(sensorRegistry2)); + assertTrue(sensorRegistry2.equals(sensorRegistry1)); + } + + @Test + public void testNotEquals() { + final ImmutableSensorRegistry sensorRegistry1 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + final ImmutableSensorRegistry sensorRegistry2 = + ImmutableSensorRegistry.copyOf(this.getOtherSensorRegistry()); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertFalse(sensorRegistry1.equals(sensorRegistry2)); + assertFalse(sensorRegistry2.equals(sensorRegistry1)); + } + + @Test + public void testEqualHashCodes() { + final ImmutableSensorRegistry sensorRegistry1 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + final ImmutableSensorRegistry sensorRegistry2 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.hashCode() == sensorRegistry2.hashCode()); + } + + @Test + public void testEqualHashCodesWithDifferentNames() { + final ImmutableSensorRegistry sensorRegistry1 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + final ImmutableSensorRegistry sensorRegistry2 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistryWithDifferentNames()); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.hashCode() == sensorRegistry2.hashCode()); + } + + @Test + public void testNotEqualHashCodes() { + final ImmutableSensorRegistry sensorRegistry1 = + ImmutableSensorRegistry.copyOf(this.getSensorRegistry()); + final ImmutableSensorRegistry sensorRegistry2 = + ImmutableSensorRegistry.copyOf(this.getOtherSensorRegistry()); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertFalse(sensorRegistry1.hashCode() == sensorRegistry2.hashCode()); + } + + private MutableSensorRegistry getSensorRegistry() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1"); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-2", "Child 2"); + aggregatedSensor.addChildMachineSensor("grandchild-1", "Grandchild 1"); + aggregatedSensor.addChildMachineSensor("grandchild-2", "Grandchild 2"); + return sensorRegistry; + } + + private MutableSensorRegistry getSensorRegistryWithDifferentNames() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1 Alternative"); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-2", "Child 2"); + aggregatedSensor.addChildMachineSensor("grandchild-1", "Grandchild 1"); + aggregatedSensor.addChildMachineSensor("grandchild-2", "Grandchild 2 Alternative"); + return sensorRegistry; + } + + private MutableSensorRegistry getOtherSensorRegistry() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1"); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-2", "Child 2"); + aggregatedSensor.addChildMachineSensor("grandchild-1", "Grandchild 1"); + aggregatedSensor.addChildMachineSensor("grandchild-2", "Grandchild 2"); + aggregatedSensor.addChildMachineSensor("grandchild-3", "Grandchild 3"); + return sensorRegistry; + } + +} diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/MaschineSensorImplExposer.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/MaschineSensorImplExposer.java new file mode 100644 index 0000000000000000000000000000000000000000..e650f40e5bfbfc20af82d9f157ebf40ed530e4cf --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/MaschineSensorImplExposer.java @@ -0,0 +1,13 @@ +package rocks.theodolite.commons.model.sensorregistry; + +/** + * Helper class to allow tests in other packages access {@link MachineSensorImpl} class objects. + */ +public final class MaschineSensorImplExposer { + + public static final Class<? extends MachineSensor> MACHINE_SENSOR_IMPL_CLASS = + MachineSensorImpl.class; + + private MaschineSensorImplExposer() {} + +} diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/MutableSensorRegistryTest.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/MutableSensorRegistryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..dbfe596962b4e7606e63ac9766bad17195ef565d --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/MutableSensorRegistryTest.java @@ -0,0 +1,102 @@ +package rocks.theodolite.commons.model.sensorregistry; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.Optional; +import org.junit.Test; + +public class MutableSensorRegistryTest { + + @Test + public void parentOfTopLevelShouldBeNotPresent() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root"); + final Optional<AggregatedSensor> parent = sensorRegistry.getTopLevelSensor().getParent(); + assertFalse(parent.isPresent()); + } + + @Test + public void testEquals() { + final MutableSensorRegistry sensorRegistry1 = this.getSensorRegistry(); + final MutableSensorRegistry sensorRegistry2 = this.getSensorRegistry(); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.equals(sensorRegistry2)); + assertTrue(sensorRegistry2.equals(sensorRegistry1)); + } + + @Test + public void testEqualsWithDifferentNames() { + final MutableSensorRegistry sensorRegistry1 = this.getSensorRegistry(); + final MutableSensorRegistry sensorRegistry2 = this.getSensorRegistryWithDifferentNames(); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.equals(sensorRegistry2)); + assertTrue(sensorRegistry2.equals(sensorRegistry1)); + } + + @Test + public void testNotEquals() { + final MutableSensorRegistry sensorRegistry1 = this.getSensorRegistry(); + final MutableSensorRegistry sensorRegistry2 = this.getOtherSensorRegistry(); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertFalse(sensorRegistry1.equals(sensorRegistry2)); + assertFalse(sensorRegistry2.equals(sensorRegistry1)); + } + + @Test + public void testEqualHashCodes() { + final MutableSensorRegistry sensorRegistry1 = this.getSensorRegistry(); + final MutableSensorRegistry sensorRegistry2 = this.getSensorRegistry(); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.hashCode() == sensorRegistry2.hashCode()); + } + + @Test + public void testEqualHashCodesWithDifferentNames() { + final MutableSensorRegistry sensorRegistry1 = this.getSensorRegistry(); + final MutableSensorRegistry sensorRegistry2 = this.getSensorRegistryWithDifferentNames(); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertTrue(sensorRegistry1.hashCode() == sensorRegistry2.hashCode()); + } + + @Test + public void testNotEqualHashCodes() { + final MutableSensorRegistry sensorRegistry1 = this.getSensorRegistry(); + final MutableSensorRegistry sensorRegistry2 = this.getOtherSensorRegistry(); + assertFalse(sensorRegistry1 == sensorRegistry2); + assertFalse(sensorRegistry1.hashCode() == sensorRegistry2.hashCode()); + } + + private MutableSensorRegistry getSensorRegistry() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1"); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-2", "Child 2"); + aggregatedSensor.addChildMachineSensor("grandchild-1", "Grandchild 1"); + aggregatedSensor.addChildMachineSensor("grandchild-2", "Grandchild 2"); + return sensorRegistry; + } + + private MutableSensorRegistry getSensorRegistryWithDifferentNames() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1 Alternative"); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-2", "Child 2"); + aggregatedSensor.addChildMachineSensor("grandchild-1", "Grandchild 1"); + aggregatedSensor.addChildMachineSensor("grandchild-2", "Grandchild 2 Alternative"); + return sensorRegistry; + } + + private MutableSensorRegistry getOtherSensorRegistry() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1"); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-2", "Child 2"); + aggregatedSensor.addChildMachineSensor("grandchild-1", "Grandchild 1"); + aggregatedSensor.addChildMachineSensor("grandchild-2", "Grandchild 2"); + aggregatedSensor.addChildMachineSensor("grandchild-3", "Grandchild 3"); + return sensorRegistry; + } + +} diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/serialization/SensorRegistryDeserializerTest.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/serialization/SensorRegistryDeserializerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..52dc7eb9689aa0eee317fcc3c759ba71fafe5ecc --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/serialization/SensorRegistryDeserializerTest.java @@ -0,0 +1,173 @@ +package rocks.theodolite.commons.model.sensorregistry.serialization; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import rocks.theodolite.commons.model.sensorregistry.AggregatedSensor; +import rocks.theodolite.commons.model.sensorregistry.MachineSensor; +import rocks.theodolite.commons.model.sensorregistry.Sensor; +import rocks.theodolite.commons.model.sensorregistry.SensorRegistry; + +public class SensorRegistryDeserializerTest { + + private Gson gson; + + @Before + public void setUp() throws Exception { + this.gson = new GsonBuilder().registerTypeAdapter(SensorRegistry.class, new SensorRegistryDeserializer()).create(); + } + + @After + public void tearDown() throws Exception { + this.gson = null; + } + + @Test + public void testEmptyRegistry() { + final String json = ""; + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + assertNull(registry); + } + + @Test + public void testRegistryOfWrongType() { + final String json = "[{\"identifier\": \"my-id\", \"name\": \"My Name\"}]"; + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + assertEquals(registry.getTopLevelSensor().getIdentifier(), ""); + assertTrue(registry.getTopLevelSensor().getChildren().isEmpty()); + } + + @Test + public void testRegistryWithMissingIdentifier() { + final String json = "{\"children\": []}"; + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + assertEquals(registry.getTopLevelSensor().getIdentifier(), ""); + assertTrue(registry.getTopLevelSensor().getChildren().isEmpty()); + } + + @Test + public void testRegistryWithMissingChildren() { + final String json = "{\"identifier\": \"my-root-id\", \"name\": \"My Name\"}"; + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + assertEquals(registry.getTopLevelSensor().getIdentifier(), "my-root-id"); + assertTrue(registry.getTopLevelSensor().getChildren().isEmpty()); + } + + @Test + public void testRegistryWithZeroChildren() { + final String json = "{\"identifier\": \"my-root-id\", \"name\": \"My Name\", \"children\": []}"; + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + assertEquals(registry.getTopLevelSensor().getIdentifier(), "my-root-id"); + assertTrue(registry.getTopLevelSensor().getChildren().isEmpty()); + } + + @Test + public void testRegistryWithOneGenerationChildren() { + final String json = "{\"identifier\": \"my-root-id\", \"name\": \"My Name\", \"children\": [{\"identifier\": \"child-id-1\", \"name\": \"Child 1\"}, {\"identifier\": \"child-id-2\", \"name\": \"Child 2\"}, {\"identifier\": \"child-id-3\", \"name\": \"Child 3\"}]}"; + final List<String> childIdentifiers = ImmutableList.of("child-id-1", "child-id-2", "child-id-3"); // List.of() in Java <= 9 + + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + final AggregatedSensor topLevelSensor = registry.getTopLevelSensor(); + assertEquals(topLevelSensor.getIdentifier(), "my-root-id"); + final List<Sensor> childSensors = Lists.newArrayList(topLevelSensor.getChildren()); + assertEquals(childSensors.size(), 3); + for (final Sensor sensor : childSensors) { + assertTrue(childIdentifiers.contains(sensor.getIdentifier())); + assertTrue(sensor instanceof MachineSensor); + } + for (final String childIdentifier : childIdentifiers) { + assertTrue(registry.getSensorForIdentifier(childIdentifier).isPresent()); + } + } + + @Test + public void testRegistryWithCorruptedChild() { + final String json = "{\"identifier\": \"my-root-id\", \"name\": \"My Name\", \"children\": [{\"identifier\": \"child-id-1\", \"name\": \"Child 1\"}, {\"no-identifier\": \"child-id-2\", \"name\": \"Child 2\"}]}"; + final List<String> childIdentifiers = ImmutableList.of("child-id-1"); // List.of() in Java <= 9 + + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + final AggregatedSensor topLevelSensor = registry.getTopLevelSensor(); + assertEquals(topLevelSensor.getIdentifier(), "my-root-id"); + final List<Sensor> childSensors = Lists.newArrayList(topLevelSensor.getChildren()); + assertEquals(childSensors.size(), 1); + for (final Sensor sensor : childSensors) { + assertTrue(childIdentifiers.contains(sensor.getIdentifier())); + assertTrue(sensor instanceof MachineSensor); + } + for (final String childIdentifier : childIdentifiers) { + assertTrue(registry.getSensorForIdentifier(childIdentifier).isPresent()); + } + } + + @Test + public void testRegistryWithArrayAsChild() { + final String json = "{\"identifier\": \"my-root-id\", \"name\": \"My Name\", \"children\": [{\"identifier\": \"child-id-1\", \"name\": \"Child 1\"}, [{\"identifier\": \"child-id-2\", \"name\": \"Child 2\"}]]}"; + final List<String> childIdentifiers = ImmutableList.of("child-id-1"); // List.of() in Java <= 9 + + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + final AggregatedSensor topLevelSensor = registry.getTopLevelSensor(); + assertEquals(topLevelSensor.getIdentifier(), "my-root-id"); + final List<Sensor> childSensors = Lists.newArrayList(topLevelSensor.getChildren()); + assertEquals(childSensors.size(), 1); + for (final Sensor sensor : childSensors) { + assertTrue(childIdentifiers.contains(sensor.getIdentifier())); + assertTrue(sensor instanceof MachineSensor); + } + for (final String childIdentifier : childIdentifiers) { + assertTrue(registry.getSensorForIdentifier(childIdentifier).isPresent()); + } + } + + @Test + public void testRegistryWithTwoGenerationChildren() { + final String json = "{\"identifier\": \"my-root-id\", \"name\": \"My Name\", \"children\": [{\"identifier\": \"child-id-1\", \"name\": \"Child 1\", \"children\": [{\"identifier\": \"child-id-1-1\", \"name\": \"Child 1a\"}, {\"identifier\": \"child-id-1-2\", \"name\": \"Child 1b\"}, {\"identifier\": \"child-id-1-3\", \"name\": \"Child 1c\"}]}, {\"identifier\": \"child-id-2\", \"name\": \"Child 2\"}]}"; + final List<String> childIdentifiers = ImmutableList.of("child-id-1", "child-id-2"); // List.of() in Java <= 9 + final List<String> grandChildIdentifiers = ImmutableList.of("child-id-1-1", "child-id-1-2", "child-id-1-3"); // List.of() in Java <= 9 + final List<String> machineSensorIdentifiers = ImmutableList.of("child-id-2", "child-id-1-1", "child-id-1-2", // List.of() in Java <= 9 + "child-id-1-3"); + + final SensorRegistry registry = this.gson.fromJson(json, SensorRegistry.class); + final AggregatedSensor topLevelSensor = registry.getTopLevelSensor(); + assertEquals(topLevelSensor.getIdentifier(), "my-root-id"); + final List<Sensor> childSensors = Lists.newArrayList(topLevelSensor.getChildren()); + assertEquals(childSensors.size(), 2); + for (final Sensor sensor : childSensors) { + assertTrue(childIdentifiers.contains(sensor.getIdentifier())); + if (sensor.getIdentifier().equals("child-id-2")) { + assertTrue(sensor instanceof MachineSensor); + } else if (sensor.getIdentifier().equals("child-id-1")) { + assertTrue(sensor instanceof AggregatedSensor); + if (sensor instanceof AggregatedSensor) { + final AggregatedSensor aggregatedSensor = (AggregatedSensor) sensor; + final List<Sensor> grandChildSensors = Lists.newArrayList(aggregatedSensor.getChildren()); + assertEquals(grandChildSensors.size(), 3); + for (final Sensor grandChildSensor : grandChildSensors) { + assertTrue(grandChildIdentifiers.contains(grandChildSensor.getIdentifier())); + assertTrue(grandChildSensor instanceof MachineSensor); + } + } else { + fail(); // Should never happen because of asserTrue check before + } + } else { + fail("Sensor is neither of type MachineSensor nor AggregatedSensor"); + } + } + for (final String identifier : machineSensorIdentifiers) { + assertTrue(registry.getSensorForIdentifier(identifier).isPresent()); + } + } + +} diff --git a/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/serialization/SensorRegistrySerializerTest.java b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/serialization/SensorRegistrySerializerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2662abb7e9ba1e295491cffeea7ab740ad178a8b --- /dev/null +++ b/theodolite-benchmarks/commons/src/test/java/rocks/theodolite/commons/model/sensorregistry/serialization/SensorRegistrySerializerTest.java @@ -0,0 +1,70 @@ +package rocks.theodolite.commons.model.sensorregistry.serialization; + +import static org.junit.Assert.assertEquals; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import rocks.theodolite.commons.model.sensorregistry.MaschineSensorImplExposer; +import rocks.theodolite.commons.model.sensorregistry.MutableAggregatedSensor; +import rocks.theodolite.commons.model.sensorregistry.MutableSensorRegistry; + +public class SensorRegistrySerializerTest { + + private Gson gson; + + @Before + public void setUp() throws Exception { + this.gson = new GsonBuilder() + .registerTypeAdapter(MutableSensorRegistry.class, new SensorRegistrySerializer()) + .registerTypeAdapter(MutableAggregatedSensor.class, new AggregatedSensorSerializer()) + .registerTypeAdapter(MaschineSensorImplExposer.MACHINE_SENSOR_IMPL_CLASS, + new MachineSensorSerializer()) + .create(); + } + + @After + public void tearDown() throws Exception { + this.gson = null; + } + + @Test + public void testEmptySensorRegistry() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + + final String json = this.gson.toJson(sensorRegistry); + System.out.println(json); + assertEquals(json, "{\"identifier\":\"root\",\"name\":\"Root\",\"children\":[]}"); + } + + @Test + public void testEmptySensorRegistryWithChildren() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + topLevel.addChildMachineSensor("child-1", "Child 1"); + topLevel.addChildMachineSensor("child-2"); + + final String json = this.gson.toJson(sensorRegistry); + System.out.println(json); + assertEquals(json, + "{\"identifier\":\"root\",\"name\":\"Root\",\"children\":[{\"identifier\":\"child-1\",\"name\":\"Child 1\"},{\"identifier\":\"child-2\",\"name\":\"\"}]}"); + } + + @Test + public void testEmptySensorRegistryWithGrandChildren() { + final MutableSensorRegistry sensorRegistry = new MutableSensorRegistry("root", "Root"); + final MutableAggregatedSensor topLevel = sensorRegistry.getTopLevelSensor(); + final MutableAggregatedSensor aggregatedSensor = + topLevel.addChildAggregatedSensor("child-1", "Child 1"); + aggregatedSensor.addChildMachineSensor("child-1-1", "Child 1a"); + aggregatedSensor.addChildMachineSensor("child-1-2", "Child 1b"); + topLevel.addChildMachineSensor("child-2", "Child 2"); + + final String json = this.gson.toJson(sensorRegistry); + System.out.println(json); + assertEquals(json, + "{\"identifier\":\"root\",\"name\":\"Root\",\"children\":[{\"identifier\":\"child-1\",\"name\":\"Child 1\",\"children\":[{\"identifier\":\"child-1-1\",\"name\":\"Child 1a\"},{\"identifier\":\"child-1-2\",\"name\":\"Child 1b\"}]},{\"identifier\":\"child-2\",\"name\":\"Child 2\"}]}"); + } + +}