Skip to content

Commit

Permalink
Issue #143 adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jyotsnaraveendran committed May 4, 2018
1 parent e8e468e commit 936dd2d
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void updateGraph(Value subjectValue, IRI property, Value objectVa
}
valueList.add(literal.getLabel());
s.property(property.toString(), valueList).property("@type",datatype);

}else{
s.property(property.toString(), literal.getLabel()).property("@type",datatype);
}
Expand All @@ -88,7 +88,7 @@ private static void updateGraph(Value subjectValue, IRI property, Value objectVa
s.addEdge(property.toString(), o);
}
}

private static Vertex getExistingVertexOrAdd(String label, Graph graph){
GraphTraversalSource t = graph.traversal();
GraphTraversal<Vertex, Vertex> traversal = t.V();
Expand Down Expand Up @@ -119,81 +119,46 @@ private static void extractModelFromVertex(ModelBuilder builder, Vertex s) {
builder.subject(s.label());
Iterator<VertexProperty<String>> propertyIter = s.properties();
while (propertyIter.hasNext()){
VertexProperty<String> property = propertyIter.next();
VertexProperty property = propertyIter.next();
logger.debug("ADDING Property "+property.label()+": "+property.value());
String literal = property.value();
Object object = property.value();
Property<Object> metaProperty = property.property("@type");
String type = null;
if(metaProperty.isPresent()){
type = metaProperty.value().toString();
}
Object object = literal;
type = metaProperty.value().toString();
}
logger.debug("TYPE is: "+type);
if(type!=null){
switch(type){
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#boolean":
logger.debug("Found boolean");
object=vf.createLiteral(XMLDatatypeUtil.parseBoolean(literal));
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#byte":
object=vf.createLiteral(XMLDatatypeUtil.parseByte(literal));
logger.debug("Found byte");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#short":
object=vf.createLiteral(XMLDatatypeUtil.parseShort(literal));
logger.debug("Found short");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#int":
object=vf.createLiteral(XMLDatatypeUtil.parseInt(literal));
logger.debug("Found int");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#long":
object=vf.createLiteral(XMLDatatypeUtil.parseLong(literal));
logger.debug("Found long");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#float":
object=vf.createLiteral(XMLDatatypeUtil.parseFloat(literal));
logger.debug("Found float");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#double":
object=vf.createLiteral(XMLDatatypeUtil.parseDouble(literal));
logger.debug("Found double");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#integer":
object=vf.createLiteral(XMLDatatypeUtil.parseInteger(literal));
logger.debug("Found integer");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#decimal":
object=vf.createLiteral(XMLDatatypeUtil.parseDecimal(literal));
logger.debug("Found decimal");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#dateTime":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#time":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#date":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gYearMonth":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gMonthDay":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gYear":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gMonth":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gDay":
object=vf.createLiteral(XMLDatatypeUtil.parseCalendar(literal));
logger.debug("Found date");
break;
// case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#duration":
// object=vf.createLiteral(XMLDatatypeUtil.parseDuration(literal));
// logger.info("Found duration");
// break;
}
}
logger.debug("OBJECT ADDED is "+object+"-"+object.getClass().getName());
builder.add(property.label(), object);
//Object object = literal;
if(object instanceof List){
for(Object ob:(List)object){
String literal = (String)ob;
Object finalObj = literal;
if(type!=null){
finalObj = matchAndAddStatements(type, literal, vf);
}
builder.add(property.label(), finalObj);

}
}else{
String literal = (String)object;
Object finalObj = literal;
if(type!=null){
finalObj = matchAndAddStatements(type, literal, vf);
}
builder.add(property.label(), finalObj);
}

logger.debug("OBJECT ADDED is "+object+"-"+object.getClass().getName());
//builder.add(property.label(), object);

}
Iterator<Edge> edgeIter = s.edges(Direction.OUT);
Edge edge;
Stack<Vertex> vStack = new Stack<Vertex>();
while(edgeIter.hasNext()){
edge = edgeIter.next();
s = edge.inVertex();
// builder.add(edge.label(), bNode);
// builder.add(edge.label(), bNode);
Resource node;
if(s.label().startsWith("_:")){
logger.debug("ADDING NODE - BLANK");
Expand All @@ -211,4 +176,63 @@ private static void extractModelFromVertex(ModelBuilder builder, Vertex s) {
extractModelFromVertex(builder,s);
}
}

private static Object matchAndAddStatements(String type, String literal, ValueFactory vf){
Object object = literal;
switch(type){
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#boolean":
logger.debug("Found boolean");

object=vf.createLiteral(XMLDatatypeUtil.parseBoolean(literal));
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#byte":
object=vf.createLiteral(XMLDatatypeUtil.parseByte(literal));
logger.debug("Found byte");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#short":
object=vf.createLiteral(XMLDatatypeUtil.parseShort(literal));
logger.debug("Found short");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#int":
object=vf.createLiteral(XMLDatatypeUtil.parseInt(literal));
logger.debug("Found int");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#long":
object=vf.createLiteral(XMLDatatypeUtil.parseLong(literal));
logger.debug("Found long");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#float":
object=vf.createLiteral(XMLDatatypeUtil.parseFloat(literal));
logger.debug("Found float");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#double":
object=vf.createLiteral(XMLDatatypeUtil.parseDouble(literal));
logger.debug("Found double");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#integer":
object=vf.createLiteral(XMLDatatypeUtil.parseInteger(literal));
logger.debug("Found integer");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#decimal":
object=vf.createLiteral(XMLDatatypeUtil.parseDecimal(literal));
logger.debug("Found decimal");
break;
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#dateTime":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#time":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#date":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gYearMonth":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gMonthDay":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gYear":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gMonth":
case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#gDay":
object=vf.createLiteral(XMLDatatypeUtil.parseCalendar(literal));
logger.debug("Found date");
break;
// case XMLConstants.W3C_XML_SCHEMA_NS_URI+"#duration":
// object=vf.createLiteral(XMLDatatypeUtil.parseDuration(literal));
// logger.info("Found duration");
// break;
}
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.IOException;
import java.io.StringReader;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -44,6 +46,7 @@ public class RegistryIntegrationSteps extends RegistryTestBase{
private static final String VALID_JSONLD= "school.jsonld";
//private static final String VALID_NEWJSONLD= "newSchool.jsonld";
private static final String VALID_NEWJSONLD= "teacher.jsonld";
private static final String ENTITY_JSONLD= "basicProficiencyLevel.jsonld";
private static final String INVALID_LABEL_JSONLD = "invalid-label.jsonld";
private static final String INVALID_NEWJSONLD= "invalid-teacher.jsonld";
private static final String ADD_ENTITY = "add";
Expand Down Expand Up @@ -73,6 +76,12 @@ public void jsonldData(){
assertNotNull(jsonld);
}

@Given("^an id for a non-existent record")
public void non_existent_record(){
id=generateRandomId();
updateId=id;
}

@Given("^a record with invalid type")
public void invalidTypeJsonldData(){
setJsonld(INVALID_LABEL_JSONLD);
Expand Down Expand Up @@ -109,6 +118,23 @@ public void setMissingAuthToken(){
public void addEntity(){
response = callRegistryCreateAPI();
}

@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");
}

@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");
}

public void jsonldData(String filename){
setJsonld(filename);
id=setJsonldWithNewRootLabel();
assertNotNull(jsonld);
}

private ResponseEntity<Response> callRegistryCreateAPI() {
headers.setContentType(MediaType.APPLICATION_JSON);
Expand All @@ -120,6 +146,20 @@ private ResponseEntity<Response> callRegistryCreateAPI() {
return response;
}

private ResponseEntity<Response> 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("prop", property);
ResponseEntity<Response> response = restTemplate.postForEntity(
baseUrl+ADD_ENTITY+"?id={id}&prop={prop}",
entity,
Response.class,
uriVariables);
return response;
}

@Then("^record issuing should be successful")
public void verifySuccessfulResponse() throws JsonParseException, JsonMappingException, IOException{
checkSuccessfulResponse();
Expand Down Expand Up @@ -210,6 +250,7 @@ public void retrieving_the_record_from_the_registry(){
response = callRegistryReadAPI();
}


private ResponseEntity<Response> callRegistryReadAPI() {
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<Response> response = restTemplate.exchange(baseUrl+"/"+id, HttpMethod.GET,entity,Response.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,26 @@ Feature: Inserting a record into the registry
And a valid auth token
When issuing the record into the registry
Then record issuing should be unsuccessful
And error message is Data validation failed!
And error message is Data validation failed!

Scenario: Adding an entity for an existing record
Given a record issued into the registry
And a valid auth token
When an entity for the record is issued into the registry
Then record issuing should be successful
And fetching the record from the registry should match the issued record

Scenario: Adding a duplicate entity for an existing record
Given a record issued into the registry
And an entity for the record is issued into the registry
And a valid auth token
When the same entity for the record is issued into the registry
Then record issuing should be unsuccessful
And error message is Cannot insert duplicate record

Scenario: Adding an entity for an non-existent record
Given an id for a non-existent record
And a valid auth token
When an entity for the record is issued into the registry
Then record issuing should be unsuccessful
And error message is Entity does not exist
35 changes: 35 additions & 0 deletions java/cukes/src/test/resources/basicProficiencyLevel.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"id": "open-saber.registry.create",
"ver": "1.0",
"ets": "11234",
"params":
{
"did": "",
"key": "",
"msgid": ""
},

"request":
{
"@context":
{
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"teacher": "http://localhost:8080/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"@vocab": "http://localhost:8080/"
},

"@id": "<@id>",
"@type": "BasicProficiencyLevel",
"proficiencySubject":
{
"@id": "teacher:SubjectCode-PHYSICS"
},

"proficiencyAcademicQualification":
{
"@id": "teacher:AcademicQualificationTypeCode-PHD"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthorizationInterceptor(authorizationFilter(), gson()))
.addPathPatterns("/**").excludePathPatterns("/health", "/error").order(1);
registry.addInterceptor(new RDFConversionInterceptor(rdfConverter(), gson()))
.addPathPatterns("/create", "/update/{id}","/add", "/update").order(2);
.addPathPatterns("/add", "/update").order(2);
registry.addInterceptor(new RDFValidationMappingInterceptor(rdfValidationMapper(), gson()))
.addPathPatterns("/create", "/update/{id}","/add", "/update").order(3);
.addPathPatterns("/add", "/update").order(3);
registry.addInterceptor(new RDFValidationInterceptor(rdfValidator(), gson()))
.addPathPatterns("/create", "/update/{id}", "/add", "/update").order(4);
.addPathPatterns("/add", "/update").order(4);
/*registry.addInterceptor(new JSONLDConversionInterceptor(jsonldConverter()))
.addPathPatterns("/read/{id}").order(2);*/
}
Expand Down
Loading

0 comments on commit 936dd2d

Please sign in to comment.