Skip to content

Commit

Permalink
Issue #131, #134 removing unused files, adding middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jyotsnaraveendran committed Apr 3, 2018
1 parent e828380 commit b7bf354
Show file tree
Hide file tree
Showing 20 changed files with 414 additions and 351 deletions.
43 changes: 0 additions & 43 deletions .circleci/config.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Map;

Expand Down Expand Up @@ -38,6 +37,7 @@
import io.opensaber.pojos.Response;
import io.opensaber.pojos.Response.Status;


public class RegistryIntegrationSteps extends RegistryTestBase{


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Constants {

public static final String REQUEST_ATTRIBUTE_NAME = "dataObject";
public static final String RESPONSE_ATTRIBUTE_NAME = "dataObject";
public static final String RESPONSE_ATTRIBUTE = "responseModel";
public static final String ATTRIBUTE_NAME = "dataObject";
public static final String REQUEST_ATTRIBUTE= "requestModel";
public static final String RDF_OBJECT = "rdf";
Expand Down
67 changes: 52 additions & 15 deletions java/middleware/registry-middleware/jsonld-conversion/pom.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.opensaber</groupId>
<artifactId>registry-middleware</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<groupId>io.opensaber.middleware</groupId>
<artifactId>jsonld-conversion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JSON-LD Conversion</name>
<description>Conversion from rdf to json-ld</description>

<dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.opensaber</groupId>
<artifactId>registry-middleware</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<groupId>io.opensaber.middleware</groupId>
<artifactId>jsonld-conversion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JSON-LD Conversion</name>
<description>Conversion from rdf to json-ld</description>

<properties>
<junit.version>4.12</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>io.opensaber</groupId>
<artifactId>middleware-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>3.6.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.open-saber</groupId>
<artifactId>jenardf4j</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-model</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>io.open-saber</groupId>
<artifactId>shaclex</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.opensaber.registry.middleware.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.jena.ext.com.google.common.io.ByteStreams;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.JsonLDWriteContext;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.WriterDatasetRIOT;
import org.apache.jena.riot.system.PrefixMap;
import org.apache.jena.riot.system.RiotLib;
import org.apache.jena.sparql.core.DatasetGraph;

import io.opensaber.converters.JenaRDF4J;
import io.opensaber.registry.middleware.BaseMiddleware;
import io.opensaber.registry.middleware.MiddlewareHaltException;
import io.opensaber.registry.middleware.util.Constants;

/**
*
* @author jyotsna
*
*/
public class JSONLDConverter implements BaseMiddleware{

private static final String INVALID_RDF_DATA = "RDF data is invalid!";

public Map<String,Object> execute(Map<String,Object> mapData) throws IOException, MiddlewareHaltException {
Object responseData = mapData.get(Constants.RESPONSE_ATTRIBUTE);
if(responseData != null){
if(responseData instanceof org.eclipse.rdf4j.model.Model){
Model jenaEntityModel = JenaRDF4J.asJenaModel((org.eclipse.rdf4j.model.Model)responseData);
DatasetGraph g = DatasetFactory.create(jenaEntityModel).asDatasetGraph();
JsonLDWriteContext ctx = new JsonLDWriteContext();
InputStream is = this.getClass().getClassLoader().getResourceAsStream("frame.json");
String fileString = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
ctx.setFrame(fileString);
WriterDatasetRIOT w = RDFDataMgr.createDatasetWriter(org.apache.jena.riot.RDFFormat.JSONLD_FRAME_FLAT) ;
PrefixMap pm = RiotLib.prefixMap(g);
String base = null;
StringWriter sWriterJena = new StringWriter();
w.write(sWriterJena, g, pm, base, ctx) ;
String jenaJSON = sWriterJena.toString();
mapData.put(Constants.RESPONSE_ATTRIBUTE, jenaJSON);
} else{
throw new MiddlewareHaltException(INVALID_RDF_DATA);
}
}

return mapData;
}

public Map<String,Object> next(Map<String,Object> mapData) throws IOException {
return new HashMap<String,Object>();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.opensaber.registry.middleware.impl;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.TreeModel;
import org.eclipse.rdf4j.model.util.ModelBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import io.opensaber.converters.JenaRDF4J;
import io.opensaber.registry.middleware.BaseMiddleware;
import io.opensaber.registry.middleware.MiddlewareHaltException;
import io.opensaber.registry.middleware.util.Constants;
import io.opensaber.validators.shex.shaclex.ShaclexValidator;

public class JSONLDConversionTest {

private static final String TEACHER_JSONLD = "teacher.jsonld";
private static final String FORMAT = "JSON-LD";

@Rule
public ExpectedException expectedEx = ExpectedException.none();

Map<String, Object> mapData;
private BaseMiddleware m;
private Model rdfModel;

private void setup() throws IOException, URISyntaxException{
m = new JSONLDConverter();
String jsonLDData = Paths.get(getPath(TEACHER_JSONLD)).toString();
Path filePath = Paths.get(jsonLDData);
String jsonld = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
org.apache.jena.rdf.model.Model model = ShaclexValidator.parse(jsonld, FORMAT);
rdfModel = JenaRDF4J.asRDF4JModel(model);
}

private URI getPath(String file) throws URISyntaxException {
return this.getClass().getClassLoader().getResource(file).toURI();
}

@Test
public void testHaltIfInvalidRdf() throws MiddlewareHaltException, IOException, URISyntaxException{
expectedEx.expect(MiddlewareHaltException.class);
expectedEx.expectMessage("RDF data is invalid!");
setup();
mapData = new HashMap<String,Object>();
mapData.put(Constants.RESPONSE_ATTRIBUTE, "{}");
m.execute(mapData);
}

@Test
public void testValidRdf() throws URISyntaxException, IOException, MiddlewareHaltException{
setup();
mapData = new HashMap<String,Object>();
mapData.put(Constants.RESPONSE_ATTRIBUTE, rdfModel);
m.execute(mapData);
}

}
Loading

0 comments on commit b7bf354

Please sign in to comment.