Skip to content

Commit

Permalink
support for new repo in integration tests, first test passes
Browse files Browse the repository at this point in the history
Similar to old repo, there is TestSqaleRepositoryBeanConfig, which also
takes care of DB cleanup that is required for integration tests.
Config XML file for StartupConfiguration in Spring test XML config can
be overridden like: -Dtest.config.file=test-config-new-repo.xml
  • Loading branch information
virgo47 committed Jul 12, 2021
1 parent be5d1d5 commit b9a5df8
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 1 deletion.
5 changes: 5 additions & 0 deletions repo/repo-sql-impl-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>repo-sql-impl</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.evolveum.midpoint.repo</groupId>
<artifactId>repo-sqale</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.repo.sql.testing;

import javax.sql.DataSource;

import net.ttddyy.dsproxy.listener.ChainListener;
import net.ttddyy.dsproxy.support.ProxyConfigSpringXmlSupport;
import net.ttddyy.dsproxy.support.ProxyDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.*;

import com.evolveum.midpoint.repo.api.SqlPerformanceMonitorsCollection;
import com.evolveum.midpoint.repo.sqale.SqaleRepoContext;
import com.evolveum.midpoint.repo.sqale.SqaleRepositoryBeanConfig;
import com.evolveum.midpoint.repo.sqale.SqaleRepositoryService;
import com.evolveum.midpoint.repo.sqlbase.JdbcSession;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
* Test configuration for new repository, adding test query listener and database cleanup.
*/
@Configuration
@ConditionalOnExpression("#{midpointConfiguration.keyMatches("
+ "'midpoint.repository.repositoryServiceFactoryClass',"
+ " '(?i)com\\.evolveum\\.midpoint\\.repo\\.sqale\\..*', '(?i)s[qc]ale')"
+ "|| midpointConfiguration.keyMatches("
+ "'midpoint.repository.type',"
+ " '(?i)com\\.evolveum\\.midpoint\\.repo\\.sqale\\..*', '(?i)s[qc]ale')"
+ "}")
@ComponentScan
@Import(SqaleRepositoryBeanConfig.class)
public class TestSqaleRepositoryBeanConfig {

private static final Trace LOGGER = TraceManager.getTrace(TestSqaleRepositoryBeanConfig.class);

@Bean
public TestQueryListener testQueryListener() {
return new TestQueryListener();
}

/** Proxied data source used to collect SQL statements. */
@Primary
@Bean
public DataSource proxiedTestDataSource(
DataSource dataSource,
TestQueryListener testQueryListener) {
ChainListener chainListener = new ChainListener();
chainListener.addListener(testQueryListener);

ProxyConfigSpringXmlSupport proxyConfigSupport = new ProxyConfigSpringXmlSupport();
proxyConfigSupport.setDataSourceName("my-ds");
proxyConfigSupport.setQueryListener(chainListener);

ProxyDataSource bean = new ProxyDataSource();
bean.setDataSource(dataSource);
bean.setProxyConfig(proxyConfigSupport.create());
return bean;
}

@Primary
@Bean
public SqaleRepositoryService repositoryService(
SqaleRepoContext sqlRepoContext,
SqlPerformanceMonitorsCollection sqlPerformanceMonitorsCollection) {
clearDatabase(sqlRepoContext);

return new SqaleRepositoryService(
sqlRepoContext,
sqlPerformanceMonitorsCollection);
}

/**
* Taken from `SqaleRepoBaseTest#clearDatabase()` - this is the new repo version of
* TestSqlRepositoryBeanPostProcessor.
*/
public void clearDatabase(SqaleRepoContext sqlRepoContext) {
LOGGER.info("Postprocessing session factory - removing everything from database if necessary.");
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
// object delete cascades to sub-rows of the "object aggregate"

jdbcSession.executeStatement("TRUNCATE m_object CASCADE;");
// truncate does not run ON DELETE trigger, many refs/container tables are not cleaned
jdbcSession.executeStatement("TRUNCATE m_object_oid CASCADE;");
// but after truncating m_object_oid it cleans all the tables

/*
Truncates are much faster than this delete probably because it works row by row:
long count = jdbcSession.newDelete(QObjectMapping.INSTANCE.defaultAlias()).execute();
display("Deleted " + count + " objects from DB");
*/
jdbcSession.commit();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
<bean id="sqlRepositoryBeanConfig"
class="com.evolveum.midpoint.repo.sql.testing.TestSqlRepositoryBeanConfig"/>

<!--
This is not necessary for tests in repo-sql-impl-test (those are only for old repo),
but it is used by integration tests that use this config.
-->
<bean id="sqaleRepositoryBeanConfig"
class="com.evolveum.midpoint.repo.sql.testing.TestSqaleRepositoryBeanConfig"/>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class="com.evolveum.midpoint.init.StartupConfiguration"
init-method="init">
<constructor-arg value="./target/midpoint-home"/>
<constructor-arg value="test-config.xml"/>
<constructor-arg value="#{systemProperties['test.config.file'] ?: 'test-config.xml'}"/>
</bean>

<bean id="dummyAuditServiceFactory" class="com.evolveum.midpoint.test.DummyAuditServiceFactory"/>
Expand Down
82 changes: 82 additions & 0 deletions repo/repo-test-util/src/main/resources/test-config-new-repo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0"?>
<!--
~ Copyright (C) 2010-2021 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->
<configuration>
<midpoint>
<global>
<extensionDir>./src/test/resources/schema</extensionDir>
</global>
<repository>
<type>sqale</type>

<!--
Use this Vagrantbox to run this:
https://github.com/virgo47/midpoint-vagrantboxes/tree/master/vagrant-midpoint-db-pg-new-repo
Then connect to it with the info below and execute pgnew-repo.sql file to initialize the DB.
-->
<database>postgresql</database>
<jdbcUrl>jdbc:postgresql://192.168.56.33:5432/midtest</jdbcUrl>
<jdbcUsername>midtest</jdbcUsername>
<jdbcPassword>password</jdbcPassword>

<performanceStatisticsLevel>10</performanceStatisticsLevel>
<performanceStatisticsFile>./target/performance.log</performanceStatisticsFile>
</repository>
<workflow>
<enabled>true</enabled>
</workflow>
<audit>
<auditService>
<auditServiceFactoryClass>com.evolveum.midpoint.audit.impl.LoggerAuditServiceFactory</auditServiceFactoryClass>
</auditService>
<!-- TODO audit not supported for new repo yet
<auditService>
<auditServiceFactoryClass>com.evolveum.midpoint.repo.sql.SqlAuditServiceFactory</auditServiceFactoryClass>
&lt;!&ndash; Needed for model-intest where these properties are used. &ndash;&gt;
<customColumn>
<columnName>foo</columnName>
<eventRecordPropertyName>foo</eventRecordPropertyName>
</customColumn>
<customColumn>
<columnName>ship</columnName>
<eventRecordPropertyName>ship</eventRecordPropertyName>
</customColumn>
</auditService>
-->
<auditService>
<auditServiceFactoryClass>com.evolveum.midpoint.test.DummyAuditServiceFactory</auditServiceFactoryClass>
</auditService>
</audit>
<taskManager>
<jdbcJobStore>false</jdbcJobStore>
<waitingTasksCheckInterval>5</waitingTasksCheckInterval>
<testMode>true</testMode>
<useThreadInterrupt>whenNecessary</useThreadInterrupt> <!-- needed for task manager tests -->
<threads>50</threads> <!-- some performance tests use this -->
</taskManager>
<icf>
<scanClasspath>true</scanClasspath>
<scanDirectory>${midpoint.home}/icf-connectors</scanDirectory>
</icf>
<keystore>
<keyStorePath>${midpoint.home}/keystore.jceks</keyStorePath>
<keyStorePassword>changeit</keyStorePassword>
<encryptionKeyAlias>default</encryptionKeyAlias>
</keystore>
<constants>
<useless>xUSEless</useless>
<drink>rum</drink>
<blabla>Bla bla bla</blabla>
<baseDn>dc=example,dc=com</baseDn>
<passwordClear>secret</passwordClear>
<adBaseDn>DC=ad,DC=evolveum,DC=com</adBaseDn>
<adUsersDn>CN=Users,DC=ad,DC=evolveum,DC=com</adUsersDn>
<ad2012BaseDn>DC=ad2012,DC=lab,DC=evolveum,DC=com</ad2012BaseDn>
<ad2012UsersDn>CN=Users,DC=ad2012,DC=lab,DC=evolveum,DC=com</ad2012UsersDn>
</constants>
</midpoint>
</configuration>

0 comments on commit b9a5df8

Please sign in to comment.