Refactor AbstractIntegrationTestWithDatabase to use Builders to create test EPersons.

This commit is contained in:
Tim Donohue
2025-01-09 15:42:25 -06:00
parent ee1d711f7a
commit 0b8b7be22b
2 changed files with 37 additions and 27 deletions

View File

@@ -20,8 +20,8 @@ import org.dspace.app.launcher.ScriptLauncher;
import org.dspace.app.scripts.handler.impl.TestDSpaceRunnableHandler; import org.dspace.app.scripts.handler.impl.TestDSpaceRunnableHandler;
import org.dspace.authority.AuthoritySearchService; import org.dspace.authority.AuthoritySearchService;
import org.dspace.authority.MockAuthoritySolrServiceImpl; import org.dspace.authority.MockAuthoritySolrServiceImpl;
import org.dspace.authorize.AuthorizeException;
import org.dspace.builder.AbstractBuilder; import org.dspace.builder.AbstractBuilder;
import org.dspace.builder.EPersonBuilder;
import org.dspace.content.Community; import org.dspace.content.Community;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.core.I18nUtil; import org.dspace.core.I18nUtil;
@@ -127,19 +127,16 @@ public class AbstractIntegrationTestWithDatabase extends AbstractDSpaceIntegrati
EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService(); EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
eperson = ePersonService.findByEmail(context, "test@email.com"); eperson = ePersonService.findByEmail(context, "test@email.com");
if (eperson == null) { if (eperson == null) {
// This EPerson creation should only happen once (i.e. for first test run) // Create test EPerson for usage in all tests
log.info("Creating initial EPerson (email=test@email.com) for Unit Tests"); log.info("Creating Test EPerson (email=test@email.com) for Integration Tests");
eperson = ePersonService.create(context); eperson = EPersonBuilder.createEPerson(context)
eperson.setFirstName(context, "first"); .withNameInMetadata("first", "last")
eperson.setLastName(context, "last"); .withEmail("test@email.com")
eperson.setEmail("test@email.com"); .withCanLogin(true)
eperson.setCanLogIn(true); .withLanguage(I18nUtil.getDefaultLocale().getLanguage())
eperson.setLanguage(context, I18nUtil.getDefaultLocale().getLanguage()); .withPassword(password)
ePersonService.setPassword(eperson, password); .build();
// actually save the eperson to unit testing DB
ePersonService.update(context, eperson);
} }
// Set our global test EPerson as the current user in DSpace // Set our global test EPerson as the current user in DSpace
context.setCurrentUser(eperson); context.setCurrentUser(eperson);
@@ -148,26 +145,23 @@ public class AbstractIntegrationTestWithDatabase extends AbstractDSpaceIntegrati
admin = ePersonService.findByEmail(context, "admin@email.com"); admin = ePersonService.findByEmail(context, "admin@email.com");
if (admin == null) { if (admin == null) {
// This EPerson creation should only happen once (i.e. for first test run) // Create test Administrator for usage in all tests
log.info("Creating initial EPerson (email=admin@email.com) for Unit Tests"); log.info("Creating Test Admin EPerson (email=admin@email.com) for Integration Tests");
admin = ePersonService.create(context); admin = EPersonBuilder.createEPerson(context)
admin.setFirstName(context, "first (admin)"); .withNameInMetadata("first (admin)", "last (admin)")
admin.setLastName(context, "last (admin)"); .withEmail("admin@email.com")
admin.setEmail("admin@email.com"); .withCanLogin(true)
admin.setCanLogIn(true); .withLanguage(I18nUtil.getDefaultLocale().getLanguage())
admin.setLanguage(context, I18nUtil.getDefaultLocale().getLanguage()); .withPassword(password)
ePersonService.setPassword(admin, password); .build();
// actually save the eperson to unit testing DB
ePersonService.update(context, admin); // Add Test Administrator to the ADMIN group in test database
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService(); GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
Group adminGroup = groupService.findByName(context, Group.ADMIN); Group adminGroup = groupService.findByName(context, Group.ADMIN);
groupService.addMember(context, adminGroup, admin); groupService.addMember(context, adminGroup, admin);
} }
context.restoreAuthSystemState(); context.restoreAuthSystemState();
} catch (AuthorizeException ex) {
log.error("Error creating initial eperson or default groups", ex);
fail("Error creating initial eperson or default groups in AbstractUnitTest init()");
} catch (SQLException ex) { } catch (SQLException ex) {
log.error(ex.getMessage(), ex); log.error(ex.getMessage(), ex);
fail("SQL Error on AbstractUnitTest init()"); fail("SQL Error on AbstractUnitTest init()");

View File

@@ -12,6 +12,7 @@ import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.app.TestApplication; import org.dspace.app.TestApplication;
import org.dspace.app.rest.utils.DSpaceConfigurationInitializer; import org.dspace.app.rest.utils.DSpaceConfigurationInitializer;
import org.dspace.app.rest.utils.DSpaceKernelInitializer; import org.dspace.app.rest.utils.DSpaceKernelInitializer;
import org.junit.Before;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@@ -64,6 +65,21 @@ public class AbstractWebClientIntegrationTest extends AbstractIntegrationTestWit
@Autowired @Autowired
protected ApplicationContext applicationContext; protected ApplicationContext applicationContext;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
// Because AbstractWebClientIntegrationTest starts a new webserver, we need to ensure *everything* created by
// the "super.setUp()" script is committed to the test database. Otherwise, the new webserver may not see the
// created test data in the database.
// NOTE: This commit() does NOT occur in AbstractIntegrationTestDatabase because it will remove newly created
// Hibernate entities from the current Hibernate session. For most ITs we don't want that as it may result
// in "could not initialize proxy - no Session" errors when using those entities in other tests (or other tests
// would need to reload each test entity back into the Hibernate session)
context.commit();
}
/** /**
* Get client TestRestTemplate for making HTTP requests to test webserver * Get client TestRestTemplate for making HTTP requests to test webserver
* @return TestRestTemplate * @return TestRestTemplate