Skip to content

Commit

Permalink
[CST-14902] Fixes VersionedHandleIdentifierProviderIT
Browse files Browse the repository at this point in the history
  • Loading branch information
vins01-4science committed Sep 17, 2024
1 parent fc3f240 commit cfcbfe0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -457,7 +458,7 @@ public final void addItem(Item i) throws Exception {
List<Collection> collections = i.getCollections();
for (Collection c : collections) {
// Only add if it is not the owning collection
if (!c.getHandle().equals(owningCollectionHandle)) {
if (!Objects.equals(c.getHandle(), owningCollectionHandle)) {
line.add("collection", c.getHandle());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dspace.AbstractIntegrationTestWithDatabase;
Expand All @@ -28,15 +29,16 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.test.util.ReflectionTestUtils;

public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTestWithDatabase {

private static final String REGISTERED_PROVIDERS_PREFIX = "VersionedHandleIdentifierProvider{0}";
private List<IdentifierProvider> originalProviders;
private List<String> registeredProviders = new ArrayList<>(1);

private ServiceManager serviceManager;
private IdentifierServiceImpl identifierService;
private List<Object> registeredBeans = new ArrayList<>();

private String firstHandle;

Expand All @@ -50,10 +52,11 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest
public void setUp() throws Exception {
super.setUp();
serviceManager = DSpaceServicesFactory.getInstance().getServiceManager();
originalProviders = serviceManager.getServicesByType(IdentifierProvider.class);
identifierService = serviceManager.getServicesByType(IdentifierServiceImpl.class).get(0);
originalProviders = (List<IdentifierProvider>) ReflectionTestUtils.getField(identifierService, "providers");
context.turnOffAuthorisationSystem();

identifierService.setProviders(List.of());
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Expand All @@ -65,42 +68,59 @@ public void setUp() throws Exception {
}

@After
public void destroy() throws Exception {
// set all providers to cleanup the context.
identifierService.setProviders(serviceManager.getServicesByType(IdentifierProvider.class));
super.destroy();
// unregister the created services
unregisterServices();
public void tearDown() throws Exception {
// restore providers
identifierService.setProviders(originalProviders);
// clean beans
unregisterBeans(registeredBeans);
}

private void unregisterServices() {
// restore the default providers
identifierService.setProviders(originalProviders);
if (registeredProviders.isEmpty()) {
return;
}
// clear the registered additional providers
for (String serviceName : registeredProviders) {
serviceManager.unregisterService(serviceName);
private void unregisterBeans(List<Object> registeredBeans) {
AutowireCapableBeanFactory factory =
DSpaceServicesFactory.getInstance()
.getServiceManager()
.getApplicationContext()
.getAutowireCapableBeanFactory();
Iterator<Object> iterator = registeredBeans.iterator();
while (iterator.hasNext()) {
Object registeredBean = iterator.next();
factory.destroyBean(registeredBean);
iterator.remove();
registeredBeans.remove(registeredBean);
}
registeredProviders.clear();
originalProviders.clear();
}

private <T> T registerBean(Class<T> type)
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
AutowireCapableBeanFactory factory =
DSpaceServicesFactory.getInstance()
.getServiceManager()
.getApplicationContext()
.getAutowireCapableBeanFactory();
// Define our special bean for testing the target class.
T bean = type.getDeclaredConstructor()
.newInstance();

registeredBeans.add(bean);

factory.autowireBean(bean);
return bean;
}

private void registerSingleProvider(IdentifierProvider provider) {
identifierService.setProviders(List.of(provider));
}

private <T> T getOrProvide(Class<T> type) {
private <T> T getOrProvide(Class<T> type)
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
List<T> servicesByType = serviceManager.getServicesByType(type);
if (servicesByType == null || servicesByType.isEmpty()) {
String serviceBeanName = MessageFormat.format(REGISTERED_PROVIDERS_PREFIX, type.getName());
servicesByType = List.of(serviceManager.registerServiceClass(serviceBeanName, type));
registeredProviders.add(serviceBeanName);
servicesByType = List.of(registerBean(type));
}
return servicesByType.get(0);
}


private void createVersions() throws SQLException, AuthorizeException {
context.turnOffAuthorisationSystem();

Expand Down

0 comments on commit cfcbfe0

Please sign in to comment.