Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
when removing a value from a sparql-update query, remove ALL occurenc…
…es of that value (and don't let them get created in the first place.)
  • Loading branch information
cbeer committed May 13, 2013
1 parent 469684b commit c8630ee
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
Expand Up @@ -4,6 +4,7 @@
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -121,6 +122,46 @@ public void testUpdateObjectGraph() throws Exception {

}

@Test
public void testUpdateAndReplaceObjectGraph() throws Exception {
client.execute(postObjMethod("FedoraDescribeTestGraphReplace"));

final HttpPost updateObjectGraphMethod =
new HttpPost(serverAddress + "objects/FedoraDescribeTestGraphReplace");

updateObjectGraphMethod.addHeader("Content-Type", "application/sparql-update");

BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream("INSERT { <info:fedora/objects/FedoraDescribeTestGraphReplace> <info:rubydora#label> \"asdfg\" } WHERE {}".getBytes()));
updateObjectGraphMethod.setEntity(e);
client.execute(updateObjectGraphMethod);


e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(("DELETE { <info:fedora/objects/FedoraDescribeTestGraphReplace> <info:rubydora#label> ?p}\n" +
"INSERT {<info:fedora/objects/FedoraDescribeTestGraphReplace> <info:rubydora#label> \"qwerty\"} \n" +
"WHERE { <info:fedora/objects/FedoraDescribeTestGraphReplace> <info:rubydora#label> ?p}").getBytes()));
updateObjectGraphMethod.setEntity(e);

final HttpResponse response = client.execute(updateObjectGraphMethod);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusLine().getStatusCode());

final HttpGet getObjMethod =
new HttpGet(serverAddress + "objects/FedoraDescribeTestGraphReplace");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse getResponse = client.execute(getObjMethod);
assertEquals(200, getResponse.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(getResponse.getEntity());
logger.debug("Retrieved object graph:\n" + content);


assertFalse("Found a triple we thought we deleted.", compile("<info:fedora/objects/FedoraDescribeTestGraphReplace> <info:rubydora#label> \"asdfg\" \\.",
DOTALL).matcher(content).find());



}

@Test
public void testUpdateObjectGraphWithProblems() throws Exception {
client.execute(postObjMethod("FedoraDescribeTestGraphUpdateBad"));
Expand Down
Expand Up @@ -40,9 +40,11 @@ public static void appendOrReplaceNodeProperty(final Node node, final String pro
// if the property is multi-valued, go ahead and append to it.
ArrayList<Value> newValues = new ArrayList<Value>();
Collections.addAll(newValues, node.getProperty(propertyName).getValues());
newValues.add(newValue);

property.setValue((Value[]) newValues.toArray(new Value[newValues.size()]));
if (!newValues.contains(newValue)) {
newValues.add(newValue);
property.setValue((Value[]) newValues.toArray(new Value[newValues.size()]));
}
} else {
// or we'll just overwrite it
logger.debug("Overwriting {} property {} with new value {}", PropertyType.nameFromValue(property.getType()), propertyName, newValue);
Expand Down Expand Up @@ -79,8 +81,15 @@ public static void removeNodeProperty(final Node node, final String propertyName

ArrayList<Value> newValues = new ArrayList<Value>();

Collections.addAll(newValues, node.getProperty(propertyName).getValues());
final boolean remove = newValues.remove(valueToRemove);
boolean remove = false;

for ( Value v : node.getProperty(propertyName).getValues() ) {
if (v.equals(valueToRemove)) {
remove = true;
} else {
newValues.add(v);
}
}

// we only need to update the property if we did anything.
if (remove) {
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.io.InputStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@ContextConfiguration({"/spring-test/repo.xml"})
Expand Down Expand Up @@ -154,4 +155,38 @@ public void testDatastreamGraph() throws IOException, RepositoryException, Inval

session.logout();
}

@Test
public void testUpdatingObjectGraph() throws RepositoryException {

Session session = repo.login();

final FedoraResource object = objectService.createObject(session, "/testObjectGraphUpdates");

logger.warn(object.getGraphStore().toString());

object.updateGraph("INSERT { <info:fedora/testObjectGraphUpdates> <info:fcrepo/zyx> \"a\" } WHERE {} ");


logger.warn(object.getGraphStore().toString());

// jcr property
Node s = Node.createURI("info:fedora/testObjectGraphUpdates");
Node p = Node.createURI("info:fcrepo/zyx");
Node o = Node.createLiteral("a");
assertTrue(object.getGraphStore().getDefaultGraph().contains(s, p, o));



object.updateGraph("DELETE { <info:fedora/testObjectGraphUpdates> <info:fcrepo/zyx> ?o }\nINSERT { <info:fedora/testObjectGraphUpdates> <info:fcrepo/zyx> \"b\" } WHERE { <info:fedora/testObjectGraphUpdates> <info:fcrepo/zyx> ?o } ");


logger.warn(object.getGraphStore().toString());

assertFalse("found value we should have removed", object.getGraphStore().getDefaultGraph().contains(s, p, o));
o = Node.createLiteral("b");
assertTrue("could not find new value", object.getGraphStore().getDefaultGraph().contains(s, p, o));

session.logout();
}
}

0 comments on commit c8630ee

Please sign in to comment.