Skip to content

Commit

Permalink
added assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
smartrics committed Jun 3, 2024
1 parent c2fa968 commit 5df5b88
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your IOTICSpace dns (e.g. myspace.iotics.com) >
agentKey=<your agent key name>
userKey=<your user key name>
seed=<a seed>
tokenDuration=<token duration in seconds>
```

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListAllTwinsResponse> res = iotics.api().twinAPIFuture().listAllTwins(ListAllTwinsRequest.newBuilder()
.setHeaders(Builders.newHeadersBuilder(iotics.sim().agentIdentity()))
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down

0 comments on commit 5df5b88

Please sign in to comment.