NIHVIVO-2279 add more methods to the UserAccountsDao, with tests for the Jena implementation.
This commit is contained in:
parent
9682b91f94
commit
9f91be45e1
7 changed files with 576 additions and 10 deletions
|
@ -21,6 +21,8 @@ import java.io.StringWriter;
|
|||
import java.io.Writer;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
@ -384,4 +386,8 @@ public abstract class AbstractTestClass {
|
|||
assertEquals(message, expected, actual);
|
||||
}
|
||||
|
||||
protected <T> Set<T> buildSet(T... array) {
|
||||
return new HashSet<T>(Arrays.asList(array));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
|
||||
/**
|
||||
* Another set of tests for JenaBaseDao.
|
||||
*/
|
||||
public class JenaBaseDao_2_Test extends AbstractTestClass {
|
||||
private static final String NS_MINE = "http://my.namespace.edu/";
|
||||
|
||||
private static final String EMPTY_RESOURCE_URI = NS_MINE + "emptyResource";
|
||||
private static final String FULL_RESOURCE_URI = NS_MINE + "fullResource";
|
||||
|
||||
private static final String OLD_URI_1 = NS_MINE + "oldUri1";
|
||||
private static final String OLD_URI_2 = NS_MINE + "oldUri2";
|
||||
private static final String NEW_URI_1 = NS_MINE + "newUri1";
|
||||
private static final String NEW_URI_2 = NS_MINE + "newUri2";
|
||||
private static final String BOGUS_URI = "bogusUri";
|
||||
|
||||
private OntModel ontModel;
|
||||
|
||||
private Property prop1;
|
||||
|
||||
private Resource emptyResource;
|
||||
private Resource fullResource;
|
||||
|
||||
private JenaBaseDao dao;
|
||||
|
||||
@Before
|
||||
public void initializeThings() {
|
||||
ontModel = ModelFactory.createOntologyModel();
|
||||
|
||||
prop1 = ontModel.createProperty("property1");
|
||||
|
||||
emptyResource = ontModel.createResource(EMPTY_RESOURCE_URI);
|
||||
|
||||
fullResource = ontModel.createResource(FULL_RESOURCE_URI);
|
||||
ontModel.createStatement(fullResource, prop1,
|
||||
ontModel.createResource(OLD_URI_1));
|
||||
ontModel.createStatement(fullResource, prop1,
|
||||
ontModel.createResource(OLD_URI_2));
|
||||
|
||||
WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena(ontModel);
|
||||
dao = new JenaBaseDao(wdfj);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// tests of updatePropertyResourceURIValues()
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesFromNothing() {
|
||||
updateAndConfirm(emptyResource, prop1,
|
||||
buildSet(NEW_URI_1, NEW_URI_2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesToNothing() {
|
||||
updateAndConfirm(fullResource, prop1, Collections.<String>emptySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesNoChange() {
|
||||
updateAndConfirm(fullResource, prop1,
|
||||
buildSet(OLD_URI_1, OLD_URI_2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesReplaceSome() {
|
||||
updateAndConfirm(fullResource, prop1,
|
||||
buildSet(OLD_URI_1, NEW_URI_2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesReplaceAll() {
|
||||
updateAndConfirm(fullResource, prop1, buildSet(NEW_URI_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesTryToAddEmptyURI() {
|
||||
Set<String> uris = buildSet("");
|
||||
dao.updatePropertyResourceURIValues(emptyResource, prop1, uris,
|
||||
ontModel);
|
||||
assertExpectedUriValues("update URIs", emptyResource, prop1,
|
||||
Collections.<String> emptySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePropertyResourceURIValuesTryToAddInvalidURI() {
|
||||
setLoggerLevel(JenaBaseDao.class, Level.ERROR);
|
||||
Set<String> uris = buildSet(BOGUS_URI);
|
||||
dao.updatePropertyResourceURIValues(emptyResource, prop1, uris,
|
||||
ontModel);
|
||||
assertExpectedUriValues("update URIs", emptyResource, prop1,
|
||||
Collections.<String> emptySet());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// helper methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private void updateAndConfirm(Resource res, Property prop, Set<String> uris) {
|
||||
dao.updatePropertyResourceURIValues(res, prop, uris, ontModel);
|
||||
assertExpectedUriValues("update URIs", res, prop, uris);
|
||||
}
|
||||
|
||||
private void assertExpectedUriValues(String message, Resource res,
|
||||
Property prop, Set<String> expectedUris) {
|
||||
Set<String> actualUris = new HashSet<String>();
|
||||
StmtIterator stmts = ontModel.listStatements(res, prop, (RDFNode) null);
|
||||
while (stmts.hasNext()) {
|
||||
Statement stmt = stmts.next();
|
||||
actualUris.add(stmt.getObject().asResource().getURI());
|
||||
}
|
||||
|
||||
assertEquals(message, expectedUris, actualUris);
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
|||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -22,6 +23,9 @@ import com.hp.hpl.jena.ontology.OntModel;
|
|||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.PermissionSet;
|
||||
|
@ -37,9 +41,15 @@ public class UserAccountsDaoJenaTest extends AbstractTestClass {
|
|||
*/
|
||||
private static final String N3_DATA_FILENAME = "resources/UserAccountsDaoJenaTest.n3";
|
||||
|
||||
private static final String NS_AUTH = "http://vitro.mannlib.cornell.edu/ns/vitro/authorization#";
|
||||
private static final String NS_MINE = "http://vivo.mydomain.edu/individual/";
|
||||
|
||||
private static final String URI_USER1 = NS_MINE + "user01";
|
||||
private static final String URI_NO_SUCH_USER = NS_MINE + "bogusUser";
|
||||
|
||||
private static final String URI_ROLE1 = NS_MINE + "role1";
|
||||
private static final String URI_ROLE2 = NS_MINE + "role2";
|
||||
private static final String URI_ROLE3 = NS_MINE + "role3";
|
||||
|
||||
private OntModel ontModel;
|
||||
private WebappDaoFactoryJena wadf;
|
||||
private UserAccountsDaoJena dao;
|
||||
|
@ -62,19 +72,18 @@ public class UserAccountsDaoJenaTest extends AbstractTestClass {
|
|||
|
||||
@Test
|
||||
public void getUserAccountByUriSuccess() {
|
||||
UserAccount u = dao.getUserAccountByUri(NS_MINE + "user01");
|
||||
assertEquals("uri", NS_MINE + "user01", u.getUri());
|
||||
UserAccount u = dao.getUserAccountByUri(URI_USER1);
|
||||
assertEquals("uri", URI_USER1, u.getUri());
|
||||
assertEquals("email", "email@able.edu", u.getEmailAddress());
|
||||
assertEquals("firstName", "Zack", u.getFirstName());
|
||||
assertEquals("lastName", "Roberts", u.getLastName());
|
||||
assertEquals("md5Password", "garbage", u.getMd5Password());
|
||||
assertEquals("oldPassword", "", u.getOldPassword());
|
||||
assertEquals("changeExpires", 0L, u.getPasswordLinkExpires());
|
||||
assertEquals("linkExpires", 0L, u.getPasswordLinkExpires());
|
||||
assertEquals("changeRequired", false, u.isPasswordChangeRequired());
|
||||
assertEquals("loginCount", 5, u.getLoginCount());
|
||||
assertEquals("status", Status.ACTIVE, u.getStatus());
|
||||
assertEquals("permissionSetUris",
|
||||
Collections.singleton(NS_MINE + "role1"),
|
||||
assertEquals("permissionSetUris", Collections.singleton(URI_ROLE1),
|
||||
u.getPermissionSetUris());
|
||||
}
|
||||
|
||||
|
@ -90,10 +99,126 @@ public class UserAccountsDaoJenaTest extends AbstractTestClass {
|
|||
assertNull("null result", u);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertUserAccountSuccess() {
|
||||
UserAccount in = new UserAccount();
|
||||
in.setUri("");
|
||||
in.setEmailAddress("my@email.address");
|
||||
in.setFirstName("Joe");
|
||||
in.setLastName("Bagadonuts");
|
||||
in.setMd5Password("passwordHash");
|
||||
in.setOldPassword("oldHash");
|
||||
in.setPasswordLinkExpires(999966663333L);
|
||||
in.setPasswordChangeRequired(true);
|
||||
in.setLoginCount(42);
|
||||
in.setStatus(Status.INACTIVE);
|
||||
in.setPermissionSetUris(buildSet(URI_ROLE1, URI_ROLE2));
|
||||
|
||||
String newUri = dao.insertUserAccount(in);
|
||||
|
||||
UserAccount u = dao.getUserAccountByUri(newUri);
|
||||
assertEquals("uri", newUri, u.getUri());
|
||||
assertEquals("email", "my@email.address", u.getEmailAddress());
|
||||
assertEquals("firstName", "Joe", u.getFirstName());
|
||||
assertEquals("lastName", "Bagadonuts", u.getLastName());
|
||||
assertEquals("md5Password", "passwordHash", u.getMd5Password());
|
||||
assertEquals("oldPassword", "oldHash", u.getOldPassword());
|
||||
assertEquals("linkExpires", 999966663333L, u.getPasswordLinkExpires());
|
||||
assertEquals("changeRequired", true, u.isPasswordChangeRequired());
|
||||
assertEquals("loginCount", 42, u.getLoginCount());
|
||||
assertEquals("status", Status.INACTIVE, u.getStatus());
|
||||
assertEquals("permissionSetUris", buildSet(URI_ROLE1, URI_ROLE2),
|
||||
u.getPermissionSetUris());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void insertUserAccountNullUserAccount() {
|
||||
dao.insertUserAccount(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void insertUserAccountUriIsNotEmpty() {
|
||||
UserAccount in = new UserAccount();
|
||||
in.setUri(NS_MINE + "XXXXXX");
|
||||
|
||||
dao.insertUserAccount(in);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserAccountSuccess() {
|
||||
UserAccount up = new UserAccount();
|
||||
up.setUri(URI_USER1);
|
||||
up.setEmailAddress("updatedEmail@able.edu");
|
||||
up.setFirstName("Ezekiel");
|
||||
up.setLastName("Roberts");
|
||||
up.setMd5Password("differentHash");
|
||||
up.setOldPassword("oldHash");
|
||||
up.setPasswordLinkExpires(1L);
|
||||
up.setPasswordChangeRequired(false);
|
||||
up.setLoginCount(43);
|
||||
up.setStatus(Status.ACTIVE);
|
||||
up.setPermissionSetUris(buildSet(URI_ROLE1, URI_ROLE3));
|
||||
|
||||
dao.updateUserAccount(up);
|
||||
|
||||
UserAccount u = dao.getUserAccountByUri(URI_USER1);
|
||||
assertEquals("uri", URI_USER1, u.getUri());
|
||||
assertEquals("email", "updatedEmail@able.edu", u.getEmailAddress());
|
||||
assertEquals("firstName", "Ezekiel", u.getFirstName());
|
||||
assertEquals("lastName", "Roberts", u.getLastName());
|
||||
assertEquals("md5Password", "differentHash", u.getMd5Password());
|
||||
assertEquals("oldPassword", "oldHash", u.getOldPassword());
|
||||
assertEquals("changeExpires", 1L, u.getPasswordLinkExpires());
|
||||
assertEquals("changeRequired", false, u.isPasswordChangeRequired());
|
||||
assertEquals("loginCount", 43, u.getLoginCount());
|
||||
assertEquals("status", Status.ACTIVE, u.getStatus());
|
||||
assertEquals("permissionSetUris", buildSet(URI_ROLE1, URI_ROLE3),
|
||||
u.getPermissionSetUris());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void updateUserAccountNullUserAccount() {
|
||||
dao.updateUserAccount(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void updateUserAccountDoesNotExist() {
|
||||
UserAccount up = new UserAccount();
|
||||
up.setUri(NS_MINE + "XXXXXX");
|
||||
|
||||
dao.updateUserAccount(up);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUserAccountSuccess() {
|
||||
dao.deleteUserAccount(URI_USER1);
|
||||
StmtIterator stmts = ontModel.listStatements(
|
||||
ontModel.getResource(URI_USER1), null, (RDFNode) null);
|
||||
if (stmts.hasNext()) {
|
||||
String message = "Expecting no statements to remain in the model, but found:\n";
|
||||
while (stmts.hasNext()) {
|
||||
message += " " + formatStatement(stmts.next()) + "\n";
|
||||
}
|
||||
fail(message);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUserAccountNullUri() {
|
||||
// no complaint, no action.
|
||||
dao.deleteUserAccount(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUserAccountDoesNotExist() {
|
||||
// no complaint, no action.
|
||||
dao.deleteUserAccount(URI_NO_SUCH_USER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPermissionSetByUriSuccess() {
|
||||
PermissionSet ps = dao.getPermissionSetByUri(NS_MINE + "role1");
|
||||
assertEquals("uri", NS_MINE + "role1", ps.getUri());
|
||||
PermissionSet ps = dao.getPermissionSetByUri(URI_ROLE1);
|
||||
assertEquals("uri", URI_ROLE1, ps.getUri());
|
||||
assertEquals("label", "Role 1", ps.getLabel());
|
||||
assertEquals("permissionUris",
|
||||
Collections.singleton(NS_MINE + "permissionA"),
|
||||
|
@ -119,13 +244,13 @@ public class UserAccountsDaoJenaTest extends AbstractTestClass {
|
|||
Set<PermissionSet> expected = new HashSet<PermissionSet>();
|
||||
|
||||
PermissionSet ps1 = new PermissionSet();
|
||||
ps1.setUri(NS_MINE + "role1");
|
||||
ps1.setUri(URI_ROLE1);
|
||||
ps1.setLabel("Role 1");
|
||||
ps1.setPermissionUris(Collections.singleton(NS_MINE + "permissionA"));
|
||||
expected.add(ps1);
|
||||
|
||||
PermissionSet ps2 = new PermissionSet();
|
||||
ps2.setUri(NS_MINE + "role2");
|
||||
ps2.setUri(URI_ROLE2);
|
||||
ps2.setLabel("Role 2");
|
||||
expected.add(ps2);
|
||||
|
||||
|
@ -154,4 +279,20 @@ public class UserAccountsDaoJenaTest extends AbstractTestClass {
|
|||
|
||||
assertEquals("all permission sets", expectedMaps, actualMaps);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void dumpModelStatements() {
|
||||
StmtIterator stmts = ontModel.listStatements();
|
||||
while (stmts.hasNext()) {
|
||||
Statement stmt = stmts.next();
|
||||
System.out.println(formatStatement(stmt));
|
||||
}
|
||||
}
|
||||
|
||||
private String formatStatement(Statement stmt) {
|
||||
return stmt.getSubject().getURI() + " ==> "
|
||||
+ stmt.getPredicate().getURI() + " ==> "
|
||||
+ stmt.getObject().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue