Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed May 12, 2022
2 parents 2632227 + d682041 commit a5f5aa6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.testng.AssertJUnit;

import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.repo.sqale.SqaleRepoContext;
import com.evolveum.midpoint.repo.sqale.SqaleRepositoryService;
import com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration;
import com.evolveum.midpoint.repo.sql.helpers.BaseHelper;
import com.evolveum.midpoint.repo.sqlbase.JdbcSession;
import com.evolveum.midpoint.repo.sqlbase.SqlRepoContext;
import com.evolveum.midpoint.schema.SchemaService;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;

/**
Expand Down Expand Up @@ -74,12 +80,12 @@ protected void executeTest(ExecutionValidator preExecutionValidator,
}

/**
TODO: Messing with stdout/err is not ideal, Maven also complains:
[WARNING] Corrupted STDOUT by directly writing to native stream in forked JVM 1. See FAQ web page and the dump file ...
It would be better to use PrintStream variables in Ninja directly, by default these would be System.out/err,
but the can be provided from the outside too (for tests).
Also, the test stream could do double duty - add the output to the list for asserts (or even work as asserter!),
and still print it to the original stream as well for better log output from the maven (test.log).
* TODO: Messing with stdout/err is not ideal, Maven also complains:
* [WARNING] Corrupted STDOUT by directly writing to native stream in forked JVM 1. See FAQ web page and the dump file ...
* It would be better to use PrintStream variables in Ninja directly, by default these would be System.out/err,
* but the can be provided from the outside too (for tests).
* Also, the test stream could do double duty - add the output to the list for asserts (or even work as asserter!),
* and still print it to the original stream as well for better log output from the maven (test.log).
*/
protected void executeTest(ExecutionValidator preInit,
ExecutionValidator preExecution,
Expand Down Expand Up @@ -166,16 +172,37 @@ private void logTestExecutionFail(String message, Exception ex) {
AssertJUnit.fail(message + ": " + ex.getMessage());
}

protected void clearDbIfNative(RepositoryService repo) {
if (repo instanceof SqaleRepositoryService) {
SqaleRepoContext repoCtx = ((SqaleRepositoryService) repo).sqlRepoContext();
protected void clearDbIfNative(NinjaContext ninjaContext) {
RepositoryService repository = ninjaContext.getRepository();
if (repository instanceof SqaleRepositoryService) {
SqaleRepoContext repoCtx = ((SqaleRepositoryService) repository).sqlRepoContext();
// Just like in model-intest TestSqaleRepositoryBeanConfig.clearDatabase()
try (JdbcSession jdbcSession = repoCtx.newJdbcSession().startTransaction()) {
jdbcSession.executeStatement("TRUNCATE m_object CASCADE;");
jdbcSession.executeStatement("TRUNCATE m_object_oid CASCADE;");
jdbcSession.executeStatement("TRUNCATE ma_audit_event CASCADE;");
jdbcSession.commit();
}
} else {
// Logic adapted from TestSqlRepositoryBeanPostProcessor, we just need to get all those beans
ApplicationContext appContext = ninjaContext.getApplicationContext();
BaseHelper baseHelper = appContext.getBean(BaseHelper.class);
SchemaService schemaService = appContext.getBean(SchemaService.class);

SqlRepoContext fakeRepoContext = new SqlRepoContext(
baseHelper.getConfiguration(), baseHelper.dataSource(), schemaService, null);
try (JdbcSession jdbcSession = fakeRepoContext.newJdbcSession().startTransaction()) {
jdbcSession.executeStatement(useProcedure(baseHelper.getConfiguration())
? "{ call cleanupTestDatabaseProc() }"
: "select cleanupTestDatabase();");
jdbcSession.commit();
}
}
}

// Only for generic repo, eventually goes away...
private boolean useProcedure(SqlRepositoryConfiguration config) {
return StringUtils.containsIgnoreCase(config.getHibernateDialect(), "oracle")
|| StringUtils.containsIgnoreCase(config.getHibernateDialect(), "SQLServer");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void test100ImportByOid() {

executeTest(null,
context -> {
RepositoryService repo = context.getRepository();
clearDbIfNative(repo);
clearDbIfNative(context);

OperationResult result = new OperationResult("count objects");
RepositoryService repo = context.getRepository();
int count = repo.countObjects(ObjectType.class, null, null, result);

AssertJUnit.assertEquals(0, count);
Expand Down Expand Up @@ -74,10 +74,10 @@ public void test110ImportByFilterAsOption() {

executeTest(null,
context -> {
RepositoryService repo = context.getRepository();
clearDbIfNative(repo);
clearDbIfNative(context);

OperationResult result = new OperationResult("count objects");
RepositoryService repo = context.getRepository();
int count = repo.countObjects(ObjectType.class, null, null, result);

AssertJUnit.assertEquals(0, count);
Expand All @@ -104,10 +104,10 @@ public void test120ImportByFilterAsFile() {

executeTest(null,
context -> {
RepositoryService repo = context.getRepository();
clearDbIfNative(repo);
clearDbIfNative(context);

OperationResult result = new OperationResult("count objects");
RepositoryService repo = context.getRepository();
int count = repo.countObjects(ObjectType.class, null, null, result);

AssertJUnit.assertEquals(0, count);
Expand Down Expand Up @@ -136,7 +136,7 @@ public void testMid7668Failure() {
final String ROLE_2_OID = "3ed7c747-ff1b-4b45-90c6-b158bc844e2b";

executeTest(null,
context -> clearDbIfNative(context.getRepository()),
context -> clearDbIfNative(context),
context -> {
RepositoryService repo = context.getRepository();

Expand All @@ -163,7 +163,7 @@ public void testMid7668CustomPolyStringNormalizer() {
final String ROLE_2_OID = "3ed7c747-ff1b-4b45-90c6-b158bc844e2b";

executeTest(null,
context -> clearDbIfNative(context.getRepository()),
context -> clearDbIfNative(context),
context -> {
RepositoryService repo = context.getRepository();

Expand Down

0 comments on commit a5f5aa6

Please sign in to comment.