Removing assertions from fields and removing duplicate AddRoleToPersonTwoStageGenerator.java
This commit is contained in:
parent
6aeb4108b2
commit
20134640bf
23 changed files with 899 additions and 2370 deletions
|
@ -4,6 +4,7 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo;
|
|||
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -16,8 +17,27 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|||
|
||||
|
||||
public class EditN3GeneratorVTwoTest {
|
||||
static EditN3GeneratorVTwo gen = new EditN3GeneratorVTwo();
|
||||
|
||||
@Test
|
||||
public void testPunctAfterVarName(){
|
||||
List<String> targets = Arrays.asList("?var.","?var;","?var]","?var,");
|
||||
|
||||
Map<String,List<String>> keyToValues = new HashMap<String,List<String>>();
|
||||
keyToValues.put("var", Arrays.asList("ABC"));
|
||||
|
||||
gen.subInMultiUris(keyToValues, targets);
|
||||
Assert.assertNotNull(targets);
|
||||
Assert.assertEquals(4,targets.size());
|
||||
|
||||
Assert.assertEquals("<ABC>.", targets.get(0));
|
||||
Assert.assertEquals("<ABC>;", targets.get(1));
|
||||
Assert.assertEquals("<ABC>]", targets.get(2));
|
||||
Assert.assertEquals("<ABC>,", targets.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubInMultiUrisNull(){
|
||||
public void testSubInMultiUrisNull(){
|
||||
String n3 = "?varXYZ" ;
|
||||
List<String> targets = new ArrayList<String>();
|
||||
targets.add(n3);
|
||||
|
@ -27,11 +47,11 @@ public class EditN3GeneratorVTwoTest {
|
|||
targetValue.add(null);
|
||||
keyToValues.put("varXYZ", targetValue);
|
||||
|
||||
List<String> result = EditN3GeneratorVTwo.subInMultiUris(keyToValues, targets);
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals(1,result.size());
|
||||
gen.subInMultiUris(keyToValues, targets);
|
||||
Assert.assertNotNull(targets);
|
||||
Assert.assertEquals(1,targets.size());
|
||||
|
||||
String resultN3 = result.get(0);
|
||||
String resultN3 = targets.get(0);
|
||||
Assert.assertNotNull(resultN3);
|
||||
Assert.assertTrue("String was empty", !resultN3.isEmpty());
|
||||
|
||||
|
@ -49,7 +69,8 @@ public class EditN3GeneratorVTwoTest {
|
|||
Map<String,String> keyToValues = new HashMap<String,String>();
|
||||
keyToValues.put("varXYZ", "xyzURI");
|
||||
|
||||
List<String> result = EditN3GeneratorVTwo.subInUris(keyToValues, targets);
|
||||
gen.subInUris(keyToValues, targets);
|
||||
List<String> result = targets;
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals(1,result.size());
|
||||
|
||||
|
@ -60,13 +81,15 @@ public class EditN3GeneratorVTwoTest {
|
|||
|
||||
keyToValues = new HashMap<String,String>();
|
||||
keyToValues.put("varXYZ", null);
|
||||
|
||||
List<String> targets2 = new ArrayList<String>();
|
||||
targets2.add(n3);
|
||||
|
||||
result = EditN3GeneratorVTwo.subInUris(keyToValues, targets);
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals(1,result.size());
|
||||
gen.subInUris(keyToValues, targets2);
|
||||
Assert.assertNotNull(targets2);
|
||||
Assert.assertEquals(1,targets2.size());
|
||||
|
||||
resultN3 = result.get(0);
|
||||
resultN3 = result.get(0);
|
||||
resultN3 = targets2.get(0);
|
||||
Assert.assertNotNull(resultN3);
|
||||
Assert.assertTrue("String was empty", !resultN3.isEmpty());
|
||||
Assert.assertEquals(" ?varXYZ ", resultN3);
|
||||
|
@ -109,19 +132,19 @@ core:educationalTrainingOf ?person ;
|
|||
keyToValues.put("subject", subject);
|
||||
keyToValues.put("predicate", predicate);
|
||||
|
||||
List<String> n3results = EditN3GeneratorVTwo.subInMultiUris(keyToValues, strs);
|
||||
gen.subInMultiUris(keyToValues, strs);
|
||||
|
||||
Assert.assertNotNull(n3results);
|
||||
Assert.assertTrue( n3results.size() == 1 );
|
||||
Assert.assertNotNull(strs);
|
||||
Assert.assertTrue( strs.size() == 1 );
|
||||
String expected ="<http://testsubject.com/1> <http://testpredicate.com/2> <http://a.com/2>, <http://b.com/ont#2>, <http://c.com/individual/n23431> .";
|
||||
Assert.assertEquals(expected, n3results.get(0));
|
||||
Assert.assertEquals(expected, strs.get(0));
|
||||
|
||||
//Replace subject and predicate with other variables
|
||||
|
||||
//make a model,
|
||||
Model expectedModel = ModelFactory.createDefaultModel();
|
||||
StringReader expectedReader = new StringReader(expected);
|
||||
StringReader resultReader = new StringReader(n3results.get(0));
|
||||
StringReader resultReader = new StringReader(strs.get(0));
|
||||
expectedModel.read(expectedReader, null, "N3");
|
||||
Model resultModel = ModelFactory.createDefaultModel();
|
||||
resultModel.read(resultReader, null, "N3");
|
||||
|
|
|
@ -0,0 +1,166 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.InsertException;
|
||||
|
||||
public class ProcessRdfFormTest extends AbstractTestClass{
|
||||
|
||||
|
||||
@Test
|
||||
public void basicNewStatementTest() throws Exception{
|
||||
|
||||
/* A very basic new statement edit. */
|
||||
EditConfigurationVTwo config = new EditConfigurationVTwo();
|
||||
config.setEditKey("mockEditKey");
|
||||
config.setN3Required(Arrays.asList("?test1 ?test2 ?test3 ." ));
|
||||
config.setUrisOnform(Arrays.asList("test1", "test2", "test3"));
|
||||
|
||||
Map<String,String[]> values = new HashMap<String, String[]>();
|
||||
values.put("test1", (new String[] {"http://test.com/uri1"}));
|
||||
values.put("test2", (new String[] {"http://test.com/uri2"}));
|
||||
values.put("test3", (new String[] {"http://test.com/uri3"}));
|
||||
values.put("editKey", (new String[] {"mockEditKey"}));
|
||||
|
||||
MultiValueEditSubmission submission = new MultiValueEditSubmission(values, config);
|
||||
|
||||
ProcessRdfForm processor = new ProcessRdfForm(config,getMockNewURIMaker());
|
||||
|
||||
/* test just the N3 substitution part */
|
||||
List<String>req = config.getN3Required();
|
||||
List<String>opt = config.getN3Optional();
|
||||
processor.subInValuesToN3( config , submission, req, opt, null , null);
|
||||
assertNotNull(req);
|
||||
assertTrue( req.size() > 0);
|
||||
assertNotNull(req.get(0));
|
||||
assertEquals("<http://test.com/uri1> <http://test.com/uri2> <http://test.com/uri3> .", req.get(0));
|
||||
/* test the N3 and parse RDF parts */
|
||||
AdditionsAndRetractions changes = processor.process( config, submission );
|
||||
|
||||
assertNotNull( changes );
|
||||
assertNotNull( changes.getAdditions() );
|
||||
assertNotNull( changes.getRetractions());
|
||||
assertTrue( changes.getAdditions().size() == 1 );
|
||||
assertTrue( changes.getRetractions().size() == 0 );
|
||||
|
||||
assertTrue( changes.getAdditions().contains(
|
||||
ResourceFactory.createResource("http://test.com/uri1"),
|
||||
ResourceFactory.createProperty("http://test.com/uri2"),
|
||||
ResourceFactory.createResource("http://test.com/uri3")));
|
||||
}
|
||||
|
||||
/* A very basic edit of an existing statement. */
|
||||
@Test
|
||||
public void basicEditStatement() throws Exception{
|
||||
String testXURI = "http://test.com/uriX";
|
||||
String testYURI = "http://test.com/uriY";
|
||||
String testZURIOrginal = "http://test.com/uriZ";
|
||||
String testZURIChanged = "http://test.com/uriZChanged";
|
||||
|
||||
/* set up model */
|
||||
Model model = ModelFactory.createDefaultModel();
|
||||
model.add(model.createResource(testXURI),
|
||||
model.createProperty(testYURI),
|
||||
model.createResource(testZURIOrginal));
|
||||
|
||||
/* set up EditConfiguration */
|
||||
EditConfigurationVTwo config = new EditConfigurationVTwo();
|
||||
config.setEditKey("mockEditKey");
|
||||
config.setUrisOnform(Arrays.asList("testX", "testY", "testZ"));
|
||||
config.setN3Required( Arrays.asList("?testX ?testY ?testZ ." ));
|
||||
|
||||
config.setVarNameForSubject("testX");
|
||||
config.setSubjectUri(testXURI);
|
||||
|
||||
config.setPredicateUri(testYURI);
|
||||
config.setVarNameForPredicate("testY");
|
||||
|
||||
config.setObject(testZURIOrginal);
|
||||
config.setVarNameForObject("testZ");
|
||||
|
||||
config.prepareForObjPropUpdate(model);
|
||||
|
||||
/* set up Submission */
|
||||
Map<String,String[]> values = new HashMap<String, String[]>();
|
||||
values.put("testZ", (new String[] {testZURIChanged}));
|
||||
values.put("editKey", (new String[] {"mockEditKey"}));
|
||||
MultiValueEditSubmission submission = new MultiValueEditSubmission(values, config);
|
||||
|
||||
ProcessRdfForm processor = new ProcessRdfForm(config,getMockNewURIMaker());
|
||||
AdditionsAndRetractions changes = processor.process( config, submission );
|
||||
|
||||
assertNotNull( changes );
|
||||
assertNotNull( changes.getAdditions() );
|
||||
assertNotNull( changes.getRetractions());
|
||||
|
||||
assertTrue( changes.getAdditions().size() == 1 );
|
||||
assertTrue( changes.getRetractions().size() == 1 );
|
||||
|
||||
assertTrue( changes.getAdditions().contains(
|
||||
ResourceFactory.createResource(testXURI),
|
||||
ResourceFactory.createProperty(testYURI),
|
||||
ResourceFactory.createResource(testZURIChanged)));
|
||||
|
||||
assertTrue( changes.getRetractions().contains(
|
||||
ResourceFactory.createResource(testXURI),
|
||||
ResourceFactory.createProperty(testYURI),
|
||||
ResourceFactory.createResource(testZURIOrginal)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void substituteInSubPredObjURIsTest(){
|
||||
String testXURI = "http://test.com/uriX";
|
||||
String testYURI = "http://test.com/uriY";
|
||||
String testZURI = "http://test.com/uriZ";
|
||||
|
||||
/* set up EditConfiguration */
|
||||
EditConfigurationVTwo config = new EditConfigurationVTwo();
|
||||
|
||||
config.setVarNameForSubject("testX");
|
||||
config.setSubjectUri(testXURI);
|
||||
|
||||
config.setPredicateUri(testYURI);
|
||||
config.setVarNameForPredicate("testY");
|
||||
|
||||
config.setObject(testZURI);
|
||||
config.setVarNameForObject("testZ");
|
||||
|
||||
List<String> a = Arrays.asList("a.0 ?testX ?testY ?testZ.", "a.1 ?testX ?testY ?testZ.");
|
||||
List<String> b = Arrays.asList("b.0 ?testX ?testY ?testZ.", "b.1 ?testX ?testY ?testZ.");
|
||||
|
||||
ProcessRdfForm processor = new ProcessRdfForm(config,getMockNewURIMaker());
|
||||
|
||||
processor.substituteInSubPredObjURIs(config, a, b);
|
||||
assertEquals("a.0 <" + testXURI + "> <" + testYURI + "> <" + testZURI + ">.", a.get(0));
|
||||
assertEquals("a.1 <" + testXURI + "> <" + testYURI + "> <" + testZURI + ">.", a.get(1));
|
||||
assertEquals("b.0 <" + testXURI + "> <" + testYURI + "> <" + testZURI + ">.", b.get(0));
|
||||
assertEquals("b.1 <" + testXURI + "> <" + testYURI + "> <" + testZURI + ">.", b.get(1));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public NewURIMaker getMockNewURIMaker(){
|
||||
return new NewURIMaker() {
|
||||
int count = 0;
|
||||
@Override
|
||||
public String getUnusedNewURI(String prefixURI) throws InsertException {
|
||||
if( prefixURI != null )
|
||||
return prefixURI + count;
|
||||
else
|
||||
return "http://newURI/n" + count;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue