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.

(cherry picked from commit cfca2adbb1)
This commit is contained in:
Tim Donohue
2024-11-06 12:03:31 -06:00
committed by github-actions[bot]
parent dc0d14e4f9
commit 7ee4ba1a28
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder;
@@ -21,13 +18,11 @@ import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.core.factory.CoreServiceFactory;
import org.dspace.curate.Curator;
import org.dspace.identifier.IdentifierProvider;
import org.dspace.identifier.IdentifierServiceImpl;
import org.dspace.identifier.AbstractIdentifierProviderIT;
import org.dspace.identifier.VersionedHandleIdentifierProvider;
import org.dspace.identifier.VersionedHandleIdentifierProviderWithCanonicalHandles;
import org.dspace.kernel.ServiceManager;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.After;
import org.junit.Test;
/**
@@ -36,30 +31,19 @@ import org.junit.Test;
* @author mwood
*/
public class CreateMissingIdentifiersIT
extends AbstractIntegrationTestWithDatabase {
private ServiceManager serviceManager;
private IdentifierServiceImpl identifierService;
extends AbstractIdentifierProviderIT {
private static final String P_TASK_DEF
= "plugin.named.org.dspace.curate.CurationTask";
private static final String TASK_NAME = "test";
@Override
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<>());
}
private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
@Test
public void testPerform()
throws IOException {
// Must remove any cached named plugins before creating a new one
CoreServiceFactory.getInstance().getPluginService().clearNamedPluginClasses();
ConfigurationService configurationService = kernelImpl.getConfigurationService();
// Define a new task dynamically
configurationService.setProperty(P_TASK_DEF,
CreateMissingIdentifiers.class.getCanonicalName() + " = " + TASK_NAME);
@@ -76,7 +60,7 @@ public class CreateMissingIdentifiersIT
.build();
/*
* Curate with regular test configuration -- should succeed.
* Curate with default Handle Provider
*/
curator.curate(context, item);
int status = curator.getStatus(TASK_NAME);
@@ -92,22 +76,10 @@ public class CreateMissingIdentifiersIT
curator.getResult(TASK_NAME));
assertEquals("Curation should fail", Curator.CURATE_ERROR,
curator.getStatus(TASK_NAME));
}
@Override
@After
public void destroy() throws Exception {
super.destroy();
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));
// Unregister this non-default provider
unregisterProvider(VersionedHandleIdentifierProviderWithCanonicalHandles.class);
// Re-register the default provider (for later tests which may depend on it)
registerProvider(VersionedHandleIdentifierProvider.class);
}
}

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 java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.authorize.AuthorizeException;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
@@ -22,15 +19,10 @@ import org.dspace.builder.ItemBuilder;
import org.dspace.builder.VersionBuilder;
import org.dspace.content.Collection;
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.Test;
public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTestWithDatabase {
private ServiceManager serviceManager;
private IdentifierServiceImpl identifierService;
public class VersionedHandleIdentifierProviderIT extends AbstractIdentifierProviderIT {
private String firstHandle;
@@ -44,12 +36,6 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
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<>());
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
@@ -58,33 +44,6 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
.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 {
itemV1 = ItemBuilder.createItem(context, collection)
.withTitle("First version")
@@ -96,7 +55,6 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
@Test
public void testDefaultVersionedHandleProvider() throws Exception {
registerProvider(VersionedHandleIdentifierProvider.class);
createVersions();
// Confirm the original item only has its original handle
@@ -125,6 +83,11 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
assertEquals(firstHandle, itemV3.getHandle());
assertEquals(2, itemV3.getHandles().size());
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) {