Skip to content

Commit

Permalink
Merge pull request DSpace#9807 from kshepherd/9806_rewrite_integratio…
Browse files Browse the repository at this point in the history
…n_tests_builders

Rewrite DSpace integration tests to consistently use AbstractBuilder<T> builders
  • Loading branch information
tdonohue authored Sep 6, 2024
2 parents be1ee8b + f6cabe6 commit fc16a5f
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 895 deletions.
29 changes: 13 additions & 16 deletions dspace-api/src/test/java/org/dspace/administer/StructBuilderIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.AbstractIntegrationTest;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.authorize.AuthorizeException;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.MetadataSchemaEnum;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
Expand All @@ -38,7 +39,6 @@
import org.junit.Test;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.Comparison;
import org.xmlunit.diff.ComparisonFormatter;
Expand All @@ -52,7 +52,7 @@
* @author Mark H. Wood <[email protected]>
*/
public class StructBuilderIT
extends AbstractIntegrationTest {
extends AbstractIntegrationTestWithDatabase {
private static final Logger log = LogManager.getLogger();

private static final CommunityService communityService
Expand All @@ -79,7 +79,8 @@ public static void tearDownClass() {
* @throws IOException passed through.
*/
@Before
public void setUp() throws SQLException, AuthorizeException, IOException {
public void setUp() throws Exception {
super.setUp();
// Clear out all communities and collections.
context.turnOffAuthorisationSystem();
for (Community community : communityService.findAllTop(context)) {
Expand Down Expand Up @@ -285,19 +286,15 @@ public void testImportStructureWithHandles()
* @throws org.dspace.authorize.AuthorizeException passed through.
*/
@Test
public void testExportStructure()
throws ParserConfigurationException, SAXException, IOException,
SQLException, AuthorizeException {
public void testExportStructure() {
// Create some structure to test.
context.turnOffAuthorisationSystem();
Community community0 = communityService.create(null, context);
communityService.setMetadataSingleValue(context, community0,
MetadataSchemaEnum.DC.getName(), "title", null,
null, "Top Community 0");
Collection collection0_0 = collectionService.create(context, community0);
collectionService.setMetadataSingleValue(context, collection0_0,
MetadataSchemaEnum.DC.getName(), "title", null,
null, "Collection 0.0");
// Top level community
Community community0 = CommunityBuilder.createCommunity(context)
.withName("Top Community 0").build();
// Collection below top level community
Collection collection0_0 = CollectionBuilder.createCollection(context, community0)
.withName("Collection 0.0").build();

// Export the current structure.
System.out.println("exportStructure");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.builder.WorkspaceItemBuilder;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void packagerUUIDAlreadyExistWithoutForceTest() throws Exception {
performExportScript(article.getHandle(), tempFile);
UUID id = article.getID();
itemService.delete(context, article);
WorkspaceItem workspaceItem = workspaceItemService.create(context, col1, id, false, false);
WorkspaceItem workspaceItem = WorkspaceItemBuilder.createWorkspaceItem(context, col1, id).build();
installItemService.installItem(context, workspaceItem, "123456789/0100");
performImportNoForceScript(tempFile);
Iterator<Item> items = itemService.findByCollection(context, col1);
Expand Down
78 changes: 78 additions & 0 deletions dspace-api/src/test/java/org/dspace/builder/GroupBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.UUID;

import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.service.DSpaceObjectService;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
Expand Down Expand Up @@ -51,6 +54,33 @@ public static GroupBuilder createGroup(final Context context) {
return builder.create(context);
}

public static GroupBuilder createCollectionAdminGroup(final Context context, Collection collection) {
GroupBuilder builder = new GroupBuilder(context);
return builder.createAdminGroup(context, collection);
}

public static GroupBuilder createCollectionSubmitterGroup(final Context context, Collection collection) {
GroupBuilder builder = new GroupBuilder(context);
return builder.createSubmitterGroup(context, collection);
}

public static GroupBuilder createCollectionDefaultReadGroup(final Context context, Collection collection,
String typeOfGroupString, int defaultRead) {
GroupBuilder builder = new GroupBuilder(context);
return builder.createDefaultReadGroup(context, collection, typeOfGroupString, defaultRead);
}

public static GroupBuilder createCollectionWorkflowRoleGroup(final Context context, Collection collection,
String roleName) {
GroupBuilder builder = new GroupBuilder(context);
return builder.createWorkflowRoleGroup(context, collection, roleName);
}

public static GroupBuilder createCommunityAdminGroup(final Context context, Community community) {
GroupBuilder builder = new GroupBuilder(context);
return builder.createAdminGroup(context, community);
}

private GroupBuilder create(final Context context) {
this.context = context;
try {
Expand All @@ -61,6 +91,54 @@ private GroupBuilder create(final Context context) {
return this;
}

private GroupBuilder createAdminGroup(final Context context, DSpaceObject container) {
this.context = context;
try {
if (container instanceof Collection) {
group = collectionService.createAdministrators(context, (Collection) container);
} else if (container instanceof Community) {
group = communityService.createAdministrators(context, (Community) container);
} else {
handleException(new IllegalArgumentException("DSpaceObject must be collection or community. " +
"Type: " + container.getType()));
}
} catch (Exception e) {
return handleException(e);
}
return this;
}

private GroupBuilder createSubmitterGroup(final Context context, Collection collection) {
this.context = context;
try {
group = collectionService.createSubmitters(context, collection);
} catch (Exception e) {
return handleException(e);
}
return this;
}

private GroupBuilder createDefaultReadGroup(final Context context, Collection collection,
String typeOfGroupString, int defaultRead) {
this.context = context;
try {
group = collectionService.createDefaultReadGroup(context, collection, typeOfGroupString, defaultRead);
} catch (Exception e) {
return handleException(e);
}
return this;
}

private GroupBuilder createWorkflowRoleGroup(final Context context, Collection collection, String roleName) {
this.context = context;
try {
group = workflowService.createWorkflowRoleGroup(context, collection, roleName);
} catch (Exception e) {
return handleException(e);
}
return this;
}

@Override
protected DSpaceObjectService<Group> getService() {
return groupService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.UUID;

import org.dspace.app.ldn.NotifyPatternToTrigger;
import org.dspace.app.ldn.NotifyServiceEntity;
Expand Down Expand Up @@ -43,14 +44,31 @@ protected WorkspaceItemBuilder(Context context) {

public static WorkspaceItemBuilder createWorkspaceItem(final Context context, final Collection col) {
WorkspaceItemBuilder builder = new WorkspaceItemBuilder(context);
return builder.create(context, col);
return builder.create(context, col, null);
}

private WorkspaceItemBuilder create(final Context context, final Collection col) {
public static WorkspaceItemBuilder createWorkspaceItem(final Context context, final Collection col, UUID uuid) {
WorkspaceItemBuilder builder = new WorkspaceItemBuilder(context);
return builder.create(context, col, uuid);
}

/**
* Create with a specific UUID (e.g. restoring items with Packager import)
*
* @param context DSpace context
* @param col Parent collection
* @param uuid Item UUID
* @return WorkspaceItemBuilder
*/
private WorkspaceItemBuilder create(final Context context, final Collection col, UUID uuid) {
this.context = context;

try {
workspaceItem = workspaceItemService.create(context, col, false);
if (uuid == null) {
workspaceItem = workspaceItemService.create(context, col, false);
} else {
workspaceItem = workspaceItemService.create(context, col, uuid, false, false);
}
item = workspaceItem.getItem();
} catch (Exception e) {
return handleException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.dspace.builder.ItemBuilder;
import org.dspace.builder.RelationshipBuilder;
import org.dspace.builder.RelationshipTypeBuilder;
import org.dspace.builder.VersionBuilder;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
Expand All @@ -62,8 +63,6 @@
import org.dspace.kernel.ServiceManager;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.versioning.Version;
import org.dspace.versioning.factory.VersionServiceFactory;
import org.dspace.versioning.service.VersioningService;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -74,8 +73,6 @@ public class VersioningWithRelationshipsIT extends AbstractIntegrationTestWithDa

private final RelationshipService relationshipService =
ContentServiceFactory.getInstance().getRelationshipService();
private final VersioningService versioningService =
VersionServiceFactory.getInstance().getVersionService();
private final WorkspaceItemService workspaceItemService =
ContentServiceFactory.getInstance().getWorkspaceItemService();
private final InstallItemService installItemService =
Expand All @@ -84,7 +81,6 @@ public class VersioningWithRelationshipsIT extends AbstractIntegrationTestWithDa
ContentServiceFactory.getInstance().getItemService();
private final SolrSearchCore solrSearchCore =
DSpaceServicesFactory.getInstance().getServiceManager().getServicesByType(SolrSearchCore.class).get(0);

protected Community community;
protected Collection collection;
protected EntityType publicationEntityType;
Expand Down Expand Up @@ -291,7 +287,7 @@ public void test_createNewVersionOfItemOnLeftSideOfRelationships() throws Except
// create a new version of the publication //
/////////////////////////////////////////////

Version newVersion = versioningService.createNewVersion(context, originalPublication);
Version newVersion = VersionBuilder.createVersion(context, originalPublication, "test").build();
Item newPublication = newVersion.getItem();
assertNotSame(originalPublication, newPublication);

Expand Down Expand Up @@ -567,7 +563,7 @@ public void test_createNewVersionOfItemAndModifyRelationships() throws Exception
// create a new version of the publication //
/////////////////////////////////////////////

Version newVersion = versioningService.createNewVersion(context, originalPublication);
Version newVersion = VersionBuilder.createVersion(context, originalPublication, "test").build();
Item newPublication = newVersion.getItem();
assertNotSame(originalPublication, newPublication);

Expand Down Expand Up @@ -927,7 +923,7 @@ public void test_createNewVersionOfItemOnRightSideOfRelationships() throws Excep
// create a new version of the person //
////////////////////////////////////////

Version newVersion = versioningService.createNewVersion(context, originalPerson);
Version newVersion = VersionBuilder.createVersion(context, originalPerson, "test").build();
Item newPerson = newVersion.getItem();
assertNotSame(originalPerson, newPerson);

Expand Down Expand Up @@ -1300,7 +1296,7 @@ public void test_createNewVersionOfItemAndVerifyMetadataOrder() throws Exception
// create new version of publication //
///////////////////////////////////////

Version newVersion = versioningService.createNewVersion(context, originalPublication);
Version newVersion = VersionBuilder.createVersion(context, originalPublication, "test").build();
Item newPublication = newVersion.getItem();
assertNotSame(originalPublication, newPublication);

Expand Down Expand Up @@ -1463,7 +1459,7 @@ public void test_createNewVersionOfItemWithAddRemoveMove() throws Exception {
// create a new version of the publication //
/////////////////////////////////////////////

Version newVersion = versioningService.createNewVersion(context, originalPublication);
Version newVersion = VersionBuilder.createVersion(context, originalPublication, "test").build();
Item newPublication = newVersion.getItem();
assertNotSame(originalPublication, newPublication);

Expand Down Expand Up @@ -1782,15 +1778,15 @@ public void test_placeRecalculationAfterDelete() throws Exception {
// create new version - volume 1.2 //
/////////////////////////////////////

Item v1_2 = versioningService.createNewVersion(context, v1_1).getItem();
Item v1_2 = VersionBuilder.createVersion(context, v1_1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, v1_2));
context.commit();

////////////////////////////////////
// create new version - issue 3.2 //
////////////////////////////////////

Item i3_2 = versioningService.createNewVersion(context, i3_1).getItem();
Item i3_2 = VersionBuilder.createVersion(context, i3_1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, i3_2));
context.commit();

Expand Down Expand Up @@ -2316,15 +2312,15 @@ public void test_placeRecalculationAfterDelete_complex() throws Exception {
// create new version - person 3.2 //
/////////////////////////////////////

Item pe3_2 = versioningService.createNewVersion(context, pe3_1).getItem();
Item pe3_2 = VersionBuilder.createVersion(context, pe3_1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, pe3_2));
context.commit();

//////////////////////////////////////
// create new version - project 3.2 //
//////////////////////////////////////

Item pr3_2 = versioningService.createNewVersion(context, pr3_1).getItem();
Item pr3_2 = VersionBuilder.createVersion(context, pr3_1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, pr3_2));
context.commit();

Expand Down Expand Up @@ -3056,15 +3052,15 @@ public void test_placeRecalculationNoUseForPlace() throws Exception {
// create new version - volume 1.2 //
/////////////////////////////////////

Item v1_2 = versioningService.createNewVersion(context, v1_1).getItem();
Item v1_2 = VersionBuilder.createVersion(context, v1_1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, v1_2));
context.commit();

////////////////////////////////////
// create new version - issue 3.2 //
////////////////////////////////////

Item i3_2 = versioningService.createNewVersion(context, i3_1).getItem();
Item i3_2 = VersionBuilder.createVersion(context, i3_1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, i3_2));
context.commit();

Expand Down Expand Up @@ -3509,15 +3505,15 @@ public void test_virtualMetadataPreserved() throws Exception {
// create a new version of publication 1 and archive //
///////////////////////////////////////////////////////

Item publication1V2 = versioningService.createNewVersion(context, publication1V1).getItem();
Item publication1V2 = VersionBuilder.createVersion(context, publication1V1, "test").build().getItem();
installItemService.installItem(context, workspaceItemService.findByItem(context, publication1V2));
context.dispatchEvents();

////////////////////////////////////
// create new version of person 1 //
////////////////////////////////////

Item person1V2 = versioningService.createNewVersion(context, person1V1).getItem();
Item person1V2 = VersionBuilder.createVersion(context, person1V1, "test").build().getItem();
// update "Smith, Donald" to "Smith, D."
itemService.replaceMetadata(
context, person1V2, "person", "givenName", null, null, "D.",
Expand Down Expand Up @@ -3853,7 +3849,7 @@ public void test_virtualMetadataPreserved() throws Exception {
// create new version of person 2 //
////////////////////////////////////

Item person2V2 = versioningService.createNewVersion(context, person2V1).getItem();
Item person2V2 = VersionBuilder.createVersion(context, person2V1, "test").build().getItem();
Relationship rel1 = getRelationship(publication1V2, isAuthorOfPublication, person2V2);
assertNotNull(rel1);
rel1.setRightwardValue("Doe, Jane Jr");
Expand Down
Loading

0 comments on commit fc16a5f

Please sign in to comment.