From 5df5b88dd1c3f4d1b73b021f2e462e3c15c9e08f Mon Sep 17 00:00:00 2001 From: Fabrizio Cannizzo Date: Mon, 3 Jun 2024 14:30:27 +0100 Subject: [PATCH] added assertions --- README.md | 17 +++++++++++++ .../nifi/processors/IoticsDescriberIT.java | 16 +++++++++++++ .../nifi/processors/tools/IoticsIT.java | 5 ++-- .../processors/objects/JsonLdTwinTest.java | 24 +++++++++++++------ 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c3f6454..fa62dc8 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,20 @@ Processors and service to integrate NiFi and IOTICSpace ## Build and test `mvn clean package` + +A successful build creates a `nar` file in `nifi-iotics-nar\target` with the services and processors. + +To run the integration tests, you need to run the `verify` target as following + +`mvn -DskipITs=false verify` + +Integration tests need the file `nifi-iotics-processor/.env` with the following structure: + +```properties +hostDNS= +agentKey= +userKey= +seed= +tokenDuration= +``` + diff --git a/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/IoticsDescriberIT.java b/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/IoticsDescriberIT.java index 4e14643..8fb7a32 100644 --- a/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/IoticsDescriberIT.java +++ b/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/IoticsDescriberIT.java @@ -1,6 +1,7 @@ package smartrics.iotics.nifi.processors; import com.google.common.util.concurrent.ListenableFuture; +import com.google.gson.Gson; import com.iotics.api.TwinID; import com.iotics.api.UpsertTwinResponse; import org.apache.nifi.util.MockFlowFile; @@ -16,9 +17,12 @@ import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; import static smartrics.iotics.nifi.processors.IoticsControllerServiceFactory.injectIoticsHostService; @@ -42,6 +46,7 @@ public void init() throws Exception { @Test public void testProcessor() throws Exception { + Gson g = new Gson(); String content = new MyTwinModel(twinID).toJson(); testRunner.enqueue(content); @@ -54,9 +59,20 @@ public void testProcessor() throws Exception { String outputFlowfileContent = new String(testRunner.getContentAsByteArray(results.getFirst())); System.out.println(outputFlowfileContent); + Map map = g.fromJson(outputFlowfileContent, Map.class); + assertThat(map.get("id").toString(), is(twinID.getId())); + assertThat(map.get("hostId").toString(), is(twinID.getHostId())); + List properties = (List)map.get("properties"); + assertThat(properties.size(), is(greaterThan(0))); + results = testRunner.getFlowFilesForRelationship(Constants.ORIGINAL); assertThat(results.size(), is(1)); outputFlowfileContent = new String(testRunner.getContentAsByteArray(results.getFirst())); + map = g.fromJson(outputFlowfileContent, Map.class); + properties = (List)map.get("properties"); + assertThat(properties.size(), is(0)); + assertThat(map.get("id").toString(), is(twinID.getId())); + assertThat(map.get("hostId").toString(), is(twinID.getHostId())); System.out.println(outputFlowfileContent); Thread.sleep(1000); diff --git a/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/tools/IoticsIT.java b/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/tools/IoticsIT.java index 95ee309..920146a 100644 --- a/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/tools/IoticsIT.java +++ b/nifi-iotics-processors/src/integration/java/smartrics/iotics/nifi/processors/tools/IoticsIT.java @@ -41,11 +41,10 @@ private static Configuration newConfiguration(int tokenDuration) throws IOExcept @Test void validToken() throws Exception { Iotics iotics = Iotics.Builder.newBuilder() - .withConfiguration(newConfiguration(3)) + .withConfiguration(newConfiguration(2)) .build(); for(int i = 0; i < 10; i++) { - Thread.sleep(1000); - System.out.println("Iteration " + i); + Thread.sleep(500); ListenableFuture res = iotics.api().twinAPIFuture().listAllTwins(ListAllTwinsRequest.newBuilder() .setHeaders(Builders.newHeadersBuilder(iotics.sim().agentIdentity())) .build()); diff --git a/nifi-iotics-processors/src/test/java/smartrics/iotics/nifi/processors/objects/JsonLdTwinTest.java b/nifi-iotics-processors/src/test/java/smartrics/iotics/nifi/processors/objects/JsonLdTwinTest.java index ea8ca6a..cf27481 100644 --- a/nifi-iotics-processors/src/test/java/smartrics/iotics/nifi/processors/objects/JsonLdTwinTest.java +++ b/nifi-iotics-processors/src/test/java/smartrics/iotics/nifi/processors/objects/JsonLdTwinTest.java @@ -135,14 +135,24 @@ void readsFullTwin() throws IOException { // Convert JSON-LD to RDF triples RDFDataset dataset = (RDFDataset) JsonLdProcessor.toRDF(jsonObject, options); + String graphName = dataset.keySet().iterator().next(); + String type = dataset.getQuads(graphName) + .stream().filter(p -> p.getPredicate().getValue().equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) + .findFirst() + .orElseThrow().getObject().getValue(); + String label = dataset.getQuads(graphName) + .stream().filter(p -> p.getPredicate().getValue().equals("http://www.w3.org/2000/01/rdf-schema#label")) + .findFirst() + .orElseThrow().getObject().getValue(); + assertThat(label, is("Toyota Camry 1")); + assertThat(type, is("http://schema.org/Car")); + // Output RDF triples - for (String graphName : dataset.keySet()) { - for (RDFDataset.Quad quad : dataset.getQuads(graphName)) { - System.out.println("Graph: " + graphName - + " Subject: " + quad.getSubject().getValue() - + ", Predicate: " + quad.getPredicate().getValue() - + ", Object: " + quad.getObject().getValue()); - } + for (RDFDataset.Quad quad : dataset.getQuads(graphName)) { + System.out.println("Graph: " + graphName + + " Subject: " + quad.getSubject().getValue() + + ", Predicate: " + quad.getPredicate().getValue() + + ", Object: " + quad.getObject().getValue()); } }