Skip to content

Commit

Permalink
Issue #157 feat: saving ID in a new internal property
Browse files Browse the repository at this point in the history
  • Loading branch information
steotia committed Jun 22, 2018
1 parent 6505961 commit 3450e96
Show file tree
Hide file tree
Showing 17 changed files with 1,459 additions and 587 deletions.
5 changes: 5 additions & 0 deletions java/converters/rdf2Graph/pom.xml
Expand Up @@ -94,6 +94,11 @@
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Stack;

import com.google.gson.Gson;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
Expand Down Expand Up @@ -120,8 +121,15 @@ private static void extractModelFromVertex(ModelBuilder builder, Vertex s) {
Iterator<VertexProperty<String>> propertyIter = s.properties();
while (propertyIter.hasNext()) {
VertexProperty property = propertyIter.next();
logger.debug("ADDING Property " + property.label() + ": " + property.value());
Object object = property.value();
logger.debug("ADDING Property "+property.label()+": "+property.value());
Object propValue = property.value();
try{
List list = (new Gson()).fromJson(String.valueOf(propValue), List.class);
propValue = list;
} catch(com.google.gson.JsonSyntaxException ex) {

}
Object object = propValue;
Property<Object> metaProperty = property.property("@type");
String type = null;
if (metaProperty.isPresent()) {
Expand Down
Expand Up @@ -6,11 +6,14 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.util.ResourceUtils;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -53,6 +56,7 @@ public class RegistryIntegrationSteps extends RegistryTestBase {
private static String duplicateLabel;
private HttpHeaders headers;
private String updateId;
private Map<String,String> IDMap = new HashMap<>();

Type type = new TypeToken<Map<String, String>>() {
}.getType();
Expand Down Expand Up @@ -110,18 +114,28 @@ public void setMissingAuthToken(){

@When("^issuing the record into the registry")
public void addEntity(){
response = callRegistryCreateAPI();
callRegistryCreateAPI();
}

@When("^an entity for the record is issued into the registry$")

private void extractAndMapIDfromResponse() {
String newid = extractID(String.valueOf(response.getBody().getResult().get("entity")));
mapID(id,newid);
}

private void mapID(String id, String newid) {
IDMap.put(baseUrl+id,baseUrl+newid);
IDMap.put(baseUrl+newid,baseUrl+id);
}

@When("^an entity for the record is issued into the registry$")
public void add_entity_to_existing_record_in_registry(){
jsonldData(ENTITY_JSONLD);
response = callRegistryCreateAPI(baseUrl+updateId,baseUrl+"basicProficiencyLevel");
callRegistryCreateAPI(baseUrl+updateId,baseUrl+"basicProficiencyLevel");
}

@When("^the same entity for the record is issued into the registry$")
public void add_existing_entity_to_existing_record_in_registry(){
response = callRegistryCreateAPI(baseUrl+updateId,baseUrl+"basicProficiencyLevel");
callRegistryCreateAPI(baseUrl+updateId,baseUrl+"basicProficiencyLevel");
}

public void jsonldData(String filename){
Expand All @@ -130,28 +144,28 @@ public void jsonldData(String filename){
assertNotNull(jsonld);
}

private ResponseEntity<Response> callRegistryCreateAPI() {
private void callRegistryCreateAPI() {
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(jsonld,headers);
ResponseEntity<Response> response = restTemplate.postForEntity(
response = restTemplate.postForEntity(
baseUrl+ADD_ENTITY,
entity,
Response.class);
return response;
Response.class);
extractAndMapIDfromResponse();
}

private ResponseEntity<Response> callRegistryCreateAPI(String entityLabel, String property) {
private void callRegistryCreateAPI(String entityLabel, String property) {
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(jsonld,headers);
Map<String,String> uriVariables = new HashMap<String,String>();
uriVariables.put("id", entityLabel);
uriVariables.put("id", getMappedID(entityLabel));
uriVariables.put("prop", property);
ResponseEntity<Response> response = restTemplate.postForEntity(
response = restTemplate.postForEntity(
baseUrl+ADD_ENTITY+"?id={id}&prop={prop}",
entity,
Response.class,
uriVariables);
return response;
uriVariables);
extractAndMapIDfromResponse();
}

@Then("^record issuing should be successful")
Expand Down Expand Up @@ -192,6 +206,9 @@ private void checkForIsomorphicModel() throws IOException{
Model actualModel = ModelFactory.createDefaultModel();
String newJsonld = new JSONObject(result).toString(2);
RDFDataMgr.read(actualModel, new StringReader(newJsonld), null, org.apache.jena.riot.RDFLanguages.JSONLD);
remapURIs(expectedModel);
// printModel(expectedModel);
// printModel(actualModel);
assertTrue(expectedModel.isIsomorphicWith(actualModel));
}

Expand Down Expand Up @@ -247,12 +264,20 @@ public void retrieving_the_record_from_the_registry(){

private ResponseEntity<Response> callRegistryReadAPI() {
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<Response> response = restTemplate.exchange(baseUrl+"/"+id, HttpMethod.GET,entity,Response.class);
// String fetchID;
// if(updateId!=null)
// fetchID = updateId;
// else
// fetchID = id;
ResponseEntity<Response> response = restTemplate.exchange(getMappedID(baseUrl+id), HttpMethod.GET,entity,Response.class);
return response;

}

@Then("^record retrieval should be unsuccessful$")
private String getMappedID(String id) {
return IDMap.get(id);
}

@Then("^record retrieval should be unsuccessful$")
public void record_retrieval_should_be_unsuccessful() throws Exception {
checkUnsuccessfulResponse();
}
Expand Down Expand Up @@ -282,7 +307,7 @@ public void validResponseFormat(){
setJsonld(VALID_NEWJSONLD);
id=setJsonldWithNewRootLabel();
setValidAuthHeader();
response = callRegistryCreateAPI();
callRegistryCreateAPI();
} catch (Exception e) {
response = null;
}
Expand Down Expand Up @@ -333,4 +358,16 @@ public void test_audit_record_unexpected_in_read() throws Exception {
checkSuccessfulResponse();
}
}

private void remapURIs(Model expectedModel) {
Iterator<Resource> iter = expectedModel.listSubjects();
while(iter.hasNext()){
Resource subject = iter.next();
String oldURI = subject.getURI();
String newURI = IDMap.get(oldURI);
if(oldURI!=null&&newURI!=null){
ResourceUtils.renameResource(subject,newURI);
}
}
}
}
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.opensaber.pojos.Response;
import org.apache.jena.rdf.model.Model;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
Expand All @@ -17,6 +18,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -162,4 +164,23 @@ private static String generateAuthToken() {
return myMap.getOrDefault("access_token", "");
}

public void printModel(Model rdfModel) {
System.out.println("===================");
Iterator iter = rdfModel.listStatements();
while(iter.hasNext()){
System.out.println(iter.next());
}
System.out.println("===================");
}

public String extractID(String URI) {
String longLabel = "";
Pattern r = Pattern.compile(".*\\/(\\d+)");
Matcher m = r.matcher(URI);
if (m.find( )) {
longLabel = m.group(1);
}
return longLabel;
}

}
@@ -1,19 +1,19 @@
@controller @create
Feature: Inserting a record into the registry

@issue
Scenario: Issuing a valid record
Given a valid record
And a valid auth token
When issuing the record into the registry
Then record issuing should be successful
And fetching the record from the registry should match the issued record

Scenario: Issuing a duplicate record
Scenario: Issuing a duplicate record, should not be ok
Given a record issued into the registry
And a valid auth token
When issuing the record into the registry again
Then record issuing should be unsuccessful
And error message is Cannot insert duplicate record

Scenario: Inserting second valid record into the registry
Given a record issued into the registry
Expand Down

0 comments on commit 3450e96

Please sign in to comment.