Refactor identifier ITs to ensure they unregister all utilized IdentifierProviders which are non-default. Cannot use "getApplicationContext().refresh()" as that seems to result in empty test database in Hibernate 6.6.

This commit is contained in:
Tim Donohue
2024-11-06 12:03:31 -06:00
parent 37baff60a2
commit cfca2adbb1
3 changed files with 84 additions and 81 deletions

View File

@@ -10,10 +10,7 @@ package org.dspace.ctask.general;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder; import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder; import org.dspace.builder.ItemBuilder;
@@ -21,13 +18,11 @@ import org.dspace.content.Collection;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.core.factory.CoreServiceFactory; import org.dspace.core.factory.CoreServiceFactory;
import org.dspace.curate.Curator; import org.dspace.curate.Curator;
import org.dspace.identifier.IdentifierProvider; import org.dspace.identifier.AbstractIdentifierProviderIT;
import org.dspace.identifier.IdentifierServiceImpl; import org.dspace.identifier.VersionedHandleIdentifierProvider;
import org.dspace.identifier.VersionedHandleIdentifierProviderWithCanonicalHandles; import org.dspace.identifier.VersionedHandleIdentifierProviderWithCanonicalHandles;
import org.dspace.kernel.ServiceManager;
import org.dspace.services.ConfigurationService; import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory; import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
/** /**
@@ -36,30 +31,19 @@ import org.junit.Test;
* @author mwood * @author mwood
*/ */
public class CreateMissingIdentifiersIT public class CreateMissingIdentifiersIT
extends AbstractIntegrationTestWithDatabase { extends AbstractIdentifierProviderIT {
private ServiceManager serviceManager;
private IdentifierServiceImpl identifierService;
private static final String P_TASK_DEF private static final String P_TASK_DEF
= "plugin.named.org.dspace.curate.CurationTask"; = "plugin.named.org.dspace.curate.CurationTask";
private static final String TASK_NAME = "test"; private static final String TASK_NAME = "test";
@Override private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
public void setUp() throws Exception {
super.setUp();
context.turnOffAuthorisationSystem();
serviceManager = DSpaceServicesFactory.getInstance().getServiceManager();
identifierService = serviceManager.getServicesByType(IdentifierServiceImpl.class).get(0);
// Clean out providers to avoid any being used for creation of community and collection
identifierService.setProviders(new ArrayList<>());
}
@Test @Test
public void testPerform() public void testPerform()
throws IOException { throws IOException {
// Must remove any cached named plugins before creating a new one // Must remove any cached named plugins before creating a new one
CoreServiceFactory.getInstance().getPluginService().clearNamedPluginClasses(); CoreServiceFactory.getInstance().getPluginService().clearNamedPluginClasses();
ConfigurationService configurationService = kernelImpl.getConfigurationService();
// Define a new task dynamically // Define a new task dynamically
configurationService.setProperty(P_TASK_DEF, configurationService.setProperty(P_TASK_DEF,
CreateMissingIdentifiers.class.getCanonicalName() + " = " + TASK_NAME); CreateMissingIdentifiers.class.getCanonicalName() + " = " + TASK_NAME);
@@ -76,7 +60,7 @@ public class CreateMissingIdentifiersIT
.build(); .build();
/* /*
* Curate with regular test configuration -- should succeed. * Curate with default Handle Provider
*/ */
curator.curate(context, item); curator.curate(context, item);
int status = curator.getStatus(TASK_NAME); int status = curator.getStatus(TASK_NAME);
@@ -92,22 +76,10 @@ public class CreateMissingIdentifiersIT
curator.getResult(TASK_NAME)); curator.getResult(TASK_NAME));
assertEquals("Curation should fail", Curator.CURATE_ERROR, assertEquals("Curation should fail", Curator.CURATE_ERROR,
curator.getStatus(TASK_NAME)); curator.getStatus(TASK_NAME));
}
@Override // Unregister this non-default provider
@After unregisterProvider(VersionedHandleIdentifierProviderWithCanonicalHandles.class);
public void destroy() throws Exception { // Re-register the default provider (for later tests which may depend on it)
super.destroy(); registerProvider(VersionedHandleIdentifierProvider.class);
DSpaceServicesFactory.getInstance().getServiceManager().getApplicationContext().refresh();
}
private void registerProvider(Class type) {
// Register our new provider
serviceManager.registerServiceClass(type.getName(), type);
IdentifierProvider identifierProvider =
(IdentifierProvider) serviceManager.getServiceByName(type.getName(), type);
// Overwrite the identifier-service's providers with the new one to ensure only this provider is used
identifierService.setProviders(List.of(identifierProvider));
} }
} }

View File

@@ -0,0 +1,68 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.identifier;
import java.util.ArrayList;
import java.util.List;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.kernel.ServiceManager;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
* AbstractIdentifierProviderIT which contains a few useful utility methods for IdentifierProvider Integration Tests
*/
public class AbstractIdentifierProviderIT extends AbstractIntegrationTestWithDatabase {
protected final ServiceManager serviceManager = DSpaceServicesFactory.getInstance().getServiceManager();
protected final IdentifierServiceImpl identifierService =
serviceManager.getServicesByType(IdentifierServiceImpl.class).get(0);
/**
* Register a specific IdentifierProvider into the current IdentifierService (replacing any existing providers).
* This method will also ensure the IdentifierProvider service is registered in the DSpace Service Manager.
* @param type IdentifierProvider Class
*/
protected void registerProvider(Class type) {
// Register our new provider
IdentifierProvider identifierProvider =
(IdentifierProvider) DSpaceServicesFactory.getInstance().getServiceManager()
.getServiceByName(type.getName(), type);
if (identifierProvider == null) {
DSpaceServicesFactory.getInstance().getServiceManager().registerServiceClass(type.getName(), type);
identifierProvider = (IdentifierProvider) DSpaceServicesFactory.getInstance().getServiceManager()
.getServiceByName(type.getName(), type);
}
identifierService.setProviders(List.of(identifierProvider));
}
/**
* Unregister a specific IdentifierProvider from the current IdentifierService (removing all existing providers).
* This method will also ensure the IdentifierProvider service is unregistered in the DSpace Service Manager,
* which ensures it does not conflict with other IdentifierProvider services.
* @param type IdentifierProvider Class
*/
protected void unregisterProvider(Class type) {
// Find the provider service
IdentifierProvider identifierProvider =
(IdentifierProvider) DSpaceServicesFactory.getInstance().getServiceManager()
.getServiceByName(type.getName(), type);
// If found, unregister it
if (identifierProvider == null) {
DSpaceServicesFactory.getInstance().getServiceManager().unregisterService(type.getName());
}
// Overwrite the identifier-service's providers with an empty list
identifierService.setProviders(new ArrayList<>());
}
}

View File

@@ -11,10 +11,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder; import org.dspace.builder.CommunityBuilder;
@@ -22,15 +19,10 @@ import org.dspace.builder.ItemBuilder;
import org.dspace.builder.VersionBuilder; import org.dspace.builder.VersionBuilder;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.kernel.ServiceManager;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTestWithDatabase { public class VersionedHandleIdentifierProviderIT extends AbstractIdentifierProviderIT {
private ServiceManager serviceManager;
private IdentifierServiceImpl identifierService;
private String firstHandle; private String firstHandle;
@@ -44,12 +36,6 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
serviceManager = DSpaceServicesFactory.getInstance().getServiceManager();
identifierService = serviceManager.getServicesByType(IdentifierServiceImpl.class).get(0);
// Clean out providers to avoid any being used for creation of community and collection
identifierService.setProviders(new ArrayList<>());
parentCommunity = CommunityBuilder.createCommunity(context) parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community") .withName("Parent Community")
.build(); .build();
@@ -58,33 +44,6 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
.build(); .build();
} }
@After
@Override
public void destroy() throws Exception {
super.destroy();
// After this test has finished running, refresh application context and
// set the expected 'default' versioned handle provider back to ensure other tests don't fail
DSpaceServicesFactory.getInstance().getServiceManager().getApplicationContext().refresh();
}
private void registerProvider(Class type) {
// Register our new provider
IdentifierProvider identifierProvider =
(IdentifierProvider) DSpaceServicesFactory.getInstance().getServiceManager()
.getServiceByName(type.getName(), type);
if (identifierProvider == null) {
DSpaceServicesFactory.getInstance().getServiceManager().registerServiceClass(type.getName(), type);
identifierProvider = (IdentifierProvider) DSpaceServicesFactory.getInstance().getServiceManager()
.getServiceByName(type.getName(), type);
}
// Overwrite the identifier-service's providers with the new one to ensure only this provider is used
identifierService = DSpaceServicesFactory.getInstance().getServiceManager()
.getServicesByType(IdentifierServiceImpl.class).get(0);
identifierService.setProviders(new ArrayList<>());
identifierService.setProviders(List.of(identifierProvider));
}
private void createVersions() throws SQLException, AuthorizeException { private void createVersions() throws SQLException, AuthorizeException {
itemV1 = ItemBuilder.createItem(context, collection) itemV1 = ItemBuilder.createItem(context, collection)
.withTitle("First version") .withTitle("First version")
@@ -96,7 +55,6 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
@Test @Test
public void testDefaultVersionedHandleProvider() throws Exception { public void testDefaultVersionedHandleProvider() throws Exception {
registerProvider(VersionedHandleIdentifierProvider.class);
createVersions(); createVersions();
// Confirm the original item only has its original handle // Confirm the original item only has its original handle
@@ -125,6 +83,11 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
assertEquals(firstHandle, itemV3.getHandle()); assertEquals(firstHandle, itemV3.getHandle());
assertEquals(2, itemV3.getHandles().size()); assertEquals(2, itemV3.getHandles().size());
containsHandle(itemV3, firstHandle + ".3"); containsHandle(itemV3, firstHandle + ".3");
// Unregister this non-default provider
unregisterProvider(VersionedHandleIdentifierProviderWithCanonicalHandles.class);
// Re-register the default provider (for later tests)
registerProvider(VersionedHandleIdentifierProvider.class);
} }
private void containsHandle(Item item, String handle) { private void containsHandle(Item item, String handle) {