Refactor AbstractIntegrationTestWithDatabase to use Builders to create test EPersons.

(cherry picked from commit 0b8b7be22b)
This commit is contained in:
Tim Donohue
2025-01-09 15:42:25 -06:00
committed by github-actions[bot]
parent cc49fd3999
commit 391ba5d7a7
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.authority.AuthoritySearchService;
import org.dspace.authority.MockAuthoritySolrServiceImpl;
import org.dspace.authorize.AuthorizeException;
import org.dspace.builder.AbstractBuilder;
import org.dspace.builder.EPersonBuilder;
import org.dspace.content.Community;
import org.dspace.core.Context;
import org.dspace.core.I18nUtil;
@@ -127,19 +127,16 @@ public class AbstractIntegrationTestWithDatabase extends AbstractDSpaceIntegrati
EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
eperson = ePersonService.findByEmail(context, "test@email.com");
if (eperson == null) {
// This EPerson creation should only happen once (i.e. for first test run)
log.info("Creating initial EPerson (email=test@email.com) for Unit Tests");
eperson = ePersonService.create(context);
eperson.setFirstName(context, "first");
eperson.setLastName(context, "last");
eperson.setEmail("test@email.com");
eperson.setCanLogIn(true);
eperson.setLanguage(context, I18nUtil.getDefaultLocale().getLanguage());
ePersonService.setPassword(eperson, password);
// actually save the eperson to unit testing DB
ePersonService.update(context, eperson);
// Create test EPerson for usage in all tests
log.info("Creating Test EPerson (email=test@email.com) for Integration Tests");
eperson = EPersonBuilder.createEPerson(context)
.withNameInMetadata("first", "last")
.withEmail("test@email.com")
.withCanLogin(true)
.withLanguage(I18nUtil.getDefaultLocale().getLanguage())
.withPassword(password)
.build();
}
// Set our global test EPerson as the current user in DSpace
context.setCurrentUser(eperson);
@@ -148,26 +145,23 @@ public class AbstractIntegrationTestWithDatabase extends AbstractDSpaceIntegrati
admin = ePersonService.findByEmail(context, "admin@email.com");
if (admin == null) {
// This EPerson creation should only happen once (i.e. for first test run)
log.info("Creating initial EPerson (email=admin@email.com) for Unit Tests");
admin = ePersonService.create(context);
admin.setFirstName(context, "first (admin)");
admin.setLastName(context, "last (admin)");
admin.setEmail("admin@email.com");
admin.setCanLogIn(true);
admin.setLanguage(context, I18nUtil.getDefaultLocale().getLanguage());
ePersonService.setPassword(admin, password);
// actually save the eperson to unit testing DB
ePersonService.update(context, admin);
// Create test Administrator for usage in all tests
log.info("Creating Test Admin EPerson (email=admin@email.com) for Integration Tests");
admin = EPersonBuilder.createEPerson(context)
.withNameInMetadata("first (admin)", "last (admin)")
.withEmail("admin@email.com")
.withCanLogin(true)
.withLanguage(I18nUtil.getDefaultLocale().getLanguage())
.withPassword(password)
.build();
// Add Test Administrator to the ADMIN group in test database
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
Group adminGroup = groupService.findByName(context, Group.ADMIN);
groupService.addMember(context, adminGroup, admin);
}
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) {
log.error(ex.getMessage(), ex);
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.rest.utils.DSpaceConfigurationInitializer;
import org.dspace.app.rest.utils.DSpaceKernelInitializer;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -64,6 +65,21 @@ public class AbstractWebClientIntegrationTest extends AbstractIntegrationTestWit
@Autowired
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
* @return TestRestTemplate