Skip to content

Commit

Permalink
Rewrite of the MQTT test system
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Mar 30, 2024
1 parent acf3430 commit 65a0df8
Show file tree
Hide file tree
Showing 28 changed files with 774 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package de.fraunhofer.iosb.ilt.statests;

import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.Extension;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.ControlInformation;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.ControlInformation;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import com.fasterxml.jackson.databind.JsonNode;
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.ControlInformation;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.Extension;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.IdType;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.model.IdType;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
import de.fraunhofer.iosb.ilt.statests.util.ControlInformation;
import de.fraunhofer.iosb.ilt.statests.util.EntityCounts;
import de.fraunhofer.iosb.ilt.statests.util.EntityPropertiesSampleValue;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.Expand;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.PathElement;
import de.fraunhofer.iosb.ilt.statests.util.Query;
import de.fraunhofer.iosb.ilt.statests.util.Request;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.model.Expand;
import de.fraunhofer.iosb.ilt.statests.util.model.PathElement;
import de.fraunhofer.iosb.ilt.statests.util.model.Query;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import de.fraunhofer.iosb.ilt.statests.AbstractTestClass;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.EntityHelper;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.IdType;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.model.IdType;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import de.fraunhofer.iosb.ilt.statests.AbstractTestClass;
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.ZonedDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.ControlInformation;
import de.fraunhofer.iosb.ilt.statests.util.EntityHelper;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper;
import java.io.IOException;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*/
package de.fraunhofer.iosb.ilt.statests.c08mqttsubscribe;

import static de.fraunhofer.iosb.ilt.statests.util.EntityType.DATASTREAM;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.FEATURE_OF_INTEREST;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.HISTORICAL_LOCATION;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.LOCATION;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.OBSERVATION;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.OBSERVED_PROPERTY;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.SENSOR;
import static de.fraunhofer.iosb.ilt.statests.util.EntityType.THING;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.DATASTREAM;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.FEATURE_OF_INTEREST;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.HISTORICAL_LOCATION;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.LOCATION;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.OBSERVATION;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.OBSERVED_PROPERTY;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.SENSOR;
import static de.fraunhofer.iosb.ilt.statests.util.model.EntityType.THING;
import static de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper.WAIT_AFTER_CLEANUP;
import static de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper.waitMillis;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -37,8 +37,8 @@
import de.fraunhofer.iosb.ilt.statests.AbstractTestClass;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.EntityHelper;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import de.fraunhofer.iosb.ilt.statests.util.model.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.mqtt.DeepInsertInfo;
import de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttBatchResult;
import de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.statests.c08mqttsubscribe;

import static de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper.WAIT_AFTER_CLEANUP;
import static de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper.waitMillis;

import com.fasterxml.jackson.databind.JsonNode;
import de.fraunhofer.iosb.ilt.frostclient.exception.ServiceFailureException;
import de.fraunhofer.iosb.ilt.frostclient.model.Entity;
import de.fraunhofer.iosb.ilt.frostclient.model.EntityType;
import de.fraunhofer.iosb.ilt.frostclient.models.SensorThingsSensingV11;
import de.fraunhofer.iosb.ilt.statests.AbstractTestClass;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.EntityHelper2;
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.mqtt.MqttHelper2;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Test the standard MQTT functionality.
*/
@TestMethodOrder(MethodOrderer.MethodName.class)
public class MqttCoreTests extends AbstractTestClass {

private static final Logger LOGGER = LoggerFactory.getLogger(MqttCoreTests.class);

private List<EntityType> entityTypesForCreate;

private static EntityHelper2 eh2;
private static MqttHelper2 mqttHelper;

public MqttCoreTests() {
super(ServerVersion.v_1_1);
}

@Override
protected void setUpVersion() throws ServiceFailureException, URISyntaxException {
LOGGER.info("Setting up for version {}.", version.urlPart);
eh2 = new EntityHelper2(sSrvc);
mqttHelper = new MqttHelper2(sSrvc, serverSettings.getMqttUrl(), serverSettings.getMqttTimeOutMs());
entityTypesForCreate = Arrays.asList(
sMdl.etThing,
sMdl.etLocation,
sMdl.etSensor,
sMdl.etObservedProperty,
sMdl.etFeatureOfInterest,
sMdl.etDatastream,
sMdl.etObservation,
sMdl.etHistoricalLocation);
}

@Override
protected void tearDownVersion() throws ServiceFailureException {
cleanup();
}

/**
* This method is run after all the tests of this class is run and clean the
* database.
*
* @throws ServiceFailureException if cleaning up fails,
*/
@AfterAll
public static void tearDown() throws ServiceFailureException {
LOGGER.info("Tearing down.");
cleanup();
}

public static void cleanup() throws ServiceFailureException {
EntityUtils.deleteAll(service);
eh2.clearCaches();
eh2 = null;
mqttHelper = null;

}

private void deleteCreatedEntities() throws ServiceFailureException {
EntityUtils.deleteAll(service);
eh2.clearCaches();
}

@Test
void check01SubscribeToEntitySetInsert() throws ServiceFailureException {
LOGGER.info(" checkSubscribeToEntitySetInsert");
deleteCreatedEntities();
// Give the server a second to send out the messages created by the setup.
waitMillis(WAIT_AFTER_CLEANUP);

for (var entityType : entityTypesForCreate) {
LOGGER.info(" {}", entityType);
final CompletableFuture<JsonNode> future = new CompletableFuture<>();
final Callable<Object> insertAction = getInsertAction(entityType, future);
final MqttHelper2.TestSubscription testSubscription = new MqttHelper2.TestSubscription(mqttHelper, "v1.1/" + entityType.mainContainer)
.addExpectedJson(future)
.createReceivedListener(sMdl.etObservation);
MqttHelper2.MqttAction mqttAction = new MqttHelper2.MqttAction(insertAction)
.add(testSubscription);
mqttHelper.executeRequest(mqttAction);
}
}

@Test
void check02SubscribeAutoFeatureCreation() throws ServiceFailureException {
LOGGER.info(" check02SubscribeAutoFeatureCreation");

// Now check if an Observation insert creates a new FoI and posts it over MQTT.
LOGGER.debug(" FoI creation");
EntityUtils.deleteAll(sSrvc.dao(sMdl.etFeatureOfInterest));
EntityUtils.deleteAll(sSrvc.dao(sMdl.etObservation));
eh2.observations.clear();

final CompletableFuture<JsonNode> futureObs = new CompletableFuture<>();
final CompletableFuture<JsonNode> futureFoi = new CompletableFuture<>();
final Callable<Object> insertAction = getInsertActionObs(futureObs, futureFoi);

final MqttHelper2.TestSubscription testSubscription1 = new MqttHelper2.TestSubscription(mqttHelper, "v1.1/Observations")
.addExpectedJson(futureObs)
.createReceivedListener(sMdl.etObservation);

final MqttHelper2.TestSubscription testSubscription2 = new MqttHelper2.TestSubscription(mqttHelper, "v1.1/FeaturesOfInterest")
.addExpectedJson(futureFoi)
.createReceivedListener(sMdl.etFeatureOfInterest);

MqttHelper2.MqttAction mqttAction = new MqttHelper2.MqttAction(insertAction)
.add(testSubscription1)
.add(testSubscription2);
mqttHelper.executeRequest(mqttAction);

}

private Callable<Object> getInsertAction(final EntityType et, final CompletableFuture<JsonNode> future) {
return () -> {
Entity entity = createEntity(et);
JsonNode jsonNode = eh2.getEntity(entity);
future.complete(jsonNode);
return null;
};
}

private Entity createEntity(EntityType et) throws ServiceFailureException {
switch (et.entityName) {
case SensorThingsSensingV11.NAME_THING:
return eh2.createThing();

case SensorThingsSensingV11.NAME_SENSOR:
return eh2.createSensor();

case SensorThingsSensingV11.NAME_LOCATION:
return eh2.createLocation(eh2.things.get(0));

case SensorThingsSensingV11.NAME_OBSERVEDPROPERTY:
return eh2.createObservedProperty();

case SensorThingsSensingV11.NAME_FEATUREOFINTEREST:
return eh2.createFeatureOfInterest();

case SensorThingsSensingV11.NAME_DATASTREAM:
return eh2.createDatastream(eh2.things.get(0), eh2.oProps.get(0), eh2.sensors.get(0));

case SensorThingsSensingV11.NAME_OBSERVATION:
return eh2.createObservation(eh2.datastreams.get(0));

case SensorThingsSensingV11.NAME_HISTORICALLOCATION:
return eh2.createHistoricalLocation(eh2.things.get(0), eh2.locations.get(0));

default:
throw new IllegalArgumentException("Don't know how to create a " + et);
}
}

private Callable<Object> getInsertActionObs(final CompletableFuture<JsonNode> futureObs, final CompletableFuture<JsonNode> futureFoi) {
return () -> {
Entity obs = eh2.createObservation(eh2.datastreams.get(0));
JsonNode jsonNode1 = eh2.getEntity(obs);
futureObs.complete(jsonNode1);
JsonNode jsonNode2 = eh2.getEntity(obs, sMdl.npObservationFeatureofinterest);
futureFoi.complete(jsonNode2);
return null;
};

}
}
Loading

0 comments on commit 65a0df8

Please sign in to comment.