Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: SQL data source handling #1264

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import org.citrusframework.common.Described;
import org.citrusframework.common.Named;
import org.citrusframework.context.TestContext;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;
import org.citrusframework.spi.Resource;
import org.citrusframework.spi.Resources;
import org.citrusframework.util.SqlUtils;
import org.citrusframework.util.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
Expand Down Expand Up @@ -210,16 +213,21 @@ public AbstractDatabaseConnectingTestAction setActor(TestActor actor) {
/**
* Action builder.
*/
public static abstract class Builder<T extends AbstractDatabaseConnectingTestAction, S extends Builder<T, S>> extends AbstractTestActionBuilder<T, S> {
public static abstract class Builder<T extends AbstractDatabaseConnectingTestAction, S extends Builder<T, S>> extends AbstractTestActionBuilder<T, S> implements ReferenceResolverAware {

protected JdbcTemplate jdbcTemplate;
protected DataSource dataSource;

protected String dataSourceName;

protected String sqlResourcePath;
protected final List<String> statements = new ArrayList<>();
protected PlatformTransactionManager transactionManager;
protected String transactionTimeout = String.valueOf(TransactionDefinition.TIMEOUT_DEFAULT);
protected String transactionIsolationLevel = "ISOLATION_DEFAULT";

private ReferenceResolver referenceResolver;

/**
* Sets the Spring JDBC template to use.
* @param jdbcTemplate
Expand Down Expand Up @@ -280,6 +288,16 @@ public S dataSource(DataSource dataSource) {
return self;
}

/**
* Sets the name of the SQL data source.
* @param dataSourceName
* @return
*/
public S dataSource(String dataSourceName) {
this.dataSourceName = dataSourceName;
return self;
}

/**
* List of statements to execute. Declared inline in the test case.
* @param statements
Expand Down Expand Up @@ -316,5 +334,30 @@ public S sqlResource(String filePath) {
this.sqlResourcePath = filePath;
return self;
}

@Override
public final T build() {
if (jdbcTemplate == null && dataSource == null && referenceResolver != null) {
if (StringUtils.hasText(dataSourceName)) {
dataSource = referenceResolver.resolve(dataSourceName, DataSource.class);
} else if (referenceResolver.isResolvable(DataSource.class)) {
dataSource = referenceResolver.resolve(DataSource.class);
}
}

return doBuild();
}

protected abstract T doBuild();

public S withReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
return self;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public Builder ignoreErrors(boolean ignoreErrors) {
}

@Override
public ExecutePLSQLAction build() {
public ExecutePLSQLAction doBuild() {
return new ExecutePLSQLAction(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package org.citrusframework.actions;

import java.util.List;
import javax.sql.DataSource;

import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.util.List;

/**
* Test action execute SQL statements. Use this action when executing
* database altering statements like UPDATE, INSERT, ALTER, DELETE. Statements are either
Expand Down Expand Up @@ -149,7 +149,7 @@ public Builder ignoreErrors(boolean ignoreErrors) {
}

@Override
public ExecuteSQLAction build() {
public ExecuteSQLAction doBuild() {
return new ExecuteSQLAction(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public Builder validator(SqlResultSetScriptValidator validator) {
}

@Override
public ExecuteSQLQueryAction build() {
public ExecuteSQLQueryAction doBuild() {
return new ExecuteSQLQueryAction(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package org.citrusframework.sql.xml;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;

import org.citrusframework.TestActionBuilder;
import org.citrusframework.actions.ExecutePLSQLAction;
import org.citrusframework.spi.ReferenceResolver;
Expand All @@ -37,7 +36,6 @@ public class Plsql implements TestActionBuilder<ExecutePLSQLAction>, ReferenceRe

private final ExecutePLSQLAction.Builder builder = new ExecutePLSQLAction.Builder();

private String dataSource;
private String transactionManager;

private ReferenceResolver referenceResolver;
Expand All @@ -50,7 +48,7 @@ public Plsql setDescription(String value) {

@XmlAttribute(name = "datasource", required = true)
public Plsql setDataSource(String dataSource) {
this.dataSource = dataSource;
builder.dataSource(dataSource);
builder.name(String.format("plsql:%s", dataSource));
return this;
}
Expand Down Expand Up @@ -96,7 +94,7 @@ public Plsql setIgnoreErrors(boolean value) {
@Override
public ExecutePLSQLAction build() {
if (referenceResolver != null) {
builder.dataSource(referenceResolver.resolve(dataSource, DataSource.class));
builder.withReferenceResolver(referenceResolver);

if (transactionManager != null) {
builder.transactionManager(referenceResolver.resolve(transactionManager, PlatformTransactionManager.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
Expand All @@ -43,7 +42,6 @@ public class Sql implements TestActionBuilder<AbstractDatabaseConnectingTestActi

private AbstractDatabaseConnectingTestAction.Builder<?, ?> builder = new ExecuteSQLAction.Builder();

private String dataSource;
private String transactionManager;

private ReferenceResolver referenceResolver;
Expand All @@ -60,9 +58,9 @@ public Sql setDescription(String value) {
return this;
}

@XmlAttribute(name = "datasource", required = true)
@XmlAttribute(name = "datasource")
public Sql setDataSource(String dataSource) {
this.dataSource = dataSource;
builder.dataSource(dataSource);
builder.name(String.format("sql:%s", dataSource));
return this;
}
Expand Down Expand Up @@ -120,7 +118,7 @@ public List<Extract> getExtracts() {
@Override
public AbstractDatabaseConnectingTestAction build() {
if (referenceResolver != null) {
builder.dataSource(referenceResolver.resolve(dataSource, DataSource.class));
builder.withReferenceResolver(referenceResolver);

if (transactionManager != null) {
builder.transactionManager(referenceResolver.resolve(transactionManager, PlatformTransactionManager.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.citrusframework.sql.yaml;

import java.util.List;
import javax.sql.DataSource;

import org.citrusframework.TestActionBuilder;
import org.citrusframework.actions.ExecutePLSQLAction;
Expand All @@ -29,7 +28,6 @@ public class Plsql implements TestActionBuilder<ExecutePLSQLAction>, ReferenceRe

private final ExecutePLSQLAction.Builder builder = new ExecutePLSQLAction.Builder();

private String dataSource;
private String transactionManager;

private ReferenceResolver referenceResolver;
Expand All @@ -39,7 +37,7 @@ public void setDescription(String value) {
}

public void setDataSource(String dataSource) {
this.dataSource = dataSource;
builder.dataSource(dataSource);
builder.name(String.format("plsql:%s", dataSource));
}

Expand Down Expand Up @@ -80,7 +78,7 @@ public void setIgnoreErrors(boolean value) {
@Override
public ExecutePLSQLAction build() {
if (referenceResolver != null) {
builder.dataSource(referenceResolver.resolve(dataSource, DataSource.class));
builder.withReferenceResolver(referenceResolver);

if (transactionManager != null) {
builder.transactionManager(referenceResolver.resolve(transactionManager, PlatformTransactionManager.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;

import org.citrusframework.TestActionBuilder;
import org.citrusframework.actions.AbstractDatabaseConnectingTestAction;
Expand All @@ -35,7 +34,6 @@ public class Sql implements TestActionBuilder<AbstractDatabaseConnectingTestActi

private AbstractDatabaseConnectingTestAction.Builder<?, ?> builder = new ExecuteSQLAction.Builder();

private String dataSource;
private String transactionManager;

private ReferenceResolver referenceResolver;
Expand All @@ -49,7 +47,7 @@ public void setDescription(String value) {
}

public void setDataSource(String dataSource) {
this.dataSource = dataSource;
builder.dataSource(dataSource);
builder.name(String.format("sql:%s", dataSource));
}

Expand Down Expand Up @@ -110,7 +108,7 @@ public void setExtract(List<Extract> extract) {
@Override
public AbstractDatabaseConnectingTestAction build() {
if (referenceResolver != null) {
builder.dataSource(referenceResolver.resolve(dataSource, DataSource.class));
builder.withReferenceResolver(referenceResolver);

if (transactionManager != null) {
builder.transactionManager(referenceResolver.resolve(transactionManager, PlatformTransactionManager.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,19 @@ public static void exposeConnectionSettings(PostgreSQLContainer<?> container, St
context.setVariable(getEnvVarName(containerType, "CONTAINER_ID"), dockerContainerId);
context.setVariable(getEnvVarName(containerType, "CONTAINER_NAME"), dockerContainerName);

context.setVariable(getEnvVarName(containerType, "SERVICE_PORT"), container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT));
context.setVariable(getEnvVarName(containerType, "PORT"), container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT));
context.setVariable(getEnvVarName(containerType, "LOCAL_URL"), container.getJdbcUrl());
context.setVariable(getEnvVarName(containerType, "SERVICE_LOCAL_URL"), container.getJdbcUrl());

if (!KubernetesSupport.isConnected(context) || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(getEnvVarName(containerType, "SERVICE_NAME"), serviceName);
context.setVariable(getEnvVarName(containerType, "SERVICE_URL"), container.getJdbcUrl());
context.setVariable(getEnvVarName(containerType, "URL"), container.getJdbcUrl());
} else {
context.setVariable(getEnvVarName(containerType, "SERVICE_NAME"), serviceName);
context.setVariable(getEnvVarName(containerType, "SERVICE_URL"), container.getJdbcUrl().replace("localhost", serviceName));
context.setVariable(getEnvVarName(containerType, "URL"), container.getJdbcUrl().replace("localhost", serviceName));
}

context.setVariable(getEnvVarName(containerType, "DRIVER"), container.getDriverClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="datasource" type="xs:string" use="required"/>
<xs:attribute name="datasource" type="xs:string"/>
<xs:attribute name="ignore-errors" type="xs:boolean"/>
</xs:complexType>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="datasource" type="xs:string" use="required"/>
<xs:attribute name="datasource" type="xs:string"/>
<xs:attribute name="ignore-errors" type="xs:boolean"/>
</xs:complexType>

Expand Down
Loading