Skip to content

Commit

Permalink
Add more integration tests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
romchellis committed Jun 2, 2023
1 parent 1c67271 commit 5df0c68
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 15 deletions.
47 changes: 47 additions & 0 deletions src/test/java/assertions/SchemaAssert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package assertions;

import static common.Common.GENERATED_SOURCES_PATH;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Path;
import org.assertj.core.api.AbstractAssert;

/**
* Generated sources schema assert, it picks generated sources root and makes assertions based on existence generated files
*
* @see common.Common Common constants
*/
public class SchemaAssert extends AbstractAssert<SchemaAssert, Path> {

private SchemaAssert(String s) {
super(GENERATED_SOURCES_PATH.resolve(s), SchemaAssert.class);
}

private SchemaAssert() {
super(GENERATED_SOURCES_PATH, SchemaAssert.class);
}

/**
* Returns assert under generated sources with provided schema name directory
*
* @param name Schema name
*/
public static SchemaAssert assertThatSchema(String name) {
return new SchemaAssert(name);
}

/**
* Returns assert under generated sources without schema name directory
*/
public static SchemaAssert assertThatDefaultSchema() {
return new SchemaAssert();
}

public SchemaAssert containsTable(String tableName) {
var table = actual.resolve("tables").resolve(tableName);
assertThat(table)
.withFailMessage("%s directory does not contain file %s", actual, tableName)
.isNotEmptyFile();
return this;
}
}
13 changes: 13 additions & 0 deletions src/test/java/common/Common.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package common;

import java.io.File;
import java.nio.file.Path;

/**
* Common constants for tests
*/
public class Common {
public static final Path GENERATED_SOURCES_ROOT = Path.of("target/generated-sources/jooq");
public static final Path GENERATED_SOURCES_PATH = GENERATED_SOURCES_ROOT.resolve("org/jooq/codegen/maven/test");
public static final File GENERATED_ROOT_DIR = GENERATED_SOURCES_ROOT.toFile();
}
72 changes: 58 additions & 14 deletions src/test/java/org/testcontainers/jooq/codegen/PluginTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,77 @@
package org.testcontainers.jooq.codegen;

import static org.assertj.core.api.Assertions.assertThat;
import static assertions.SchemaAssert.assertThatDefaultSchema;
import static assertions.SchemaAssert.assertThatSchema;
import static common.Common.GENERATED_ROOT_DIR;

import java.io.File;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;

/**
* Integration test plugin under real pom files
*/
public class PluginTest extends AbstractMojoTestCase {

private final Path generatedSourcesRoot = Path.of("target/generated-sources/jooq");
private final Path generatedSourcesPath = generatedSourcesRoot.resolve("org/jooq/codegen/maven/test");
private final Path tablesDir = generatedSourcesPath.resolve("tables");
private final File generatedRootDir = generatedSourcesRoot.toFile();
/**
* Cleanup generated sources
*/
@Override
public void tearDown() throws Exception {
super.tearDown();
FileUtils.deleteDirectory(GENERATED_ROOT_DIR);
}

public void testPostgresFlyway() throws Exception {
// given
File pom = getTestFile("src/test/resources/pom/pom.xml");
File pom = getTestPom("postgres-flyway-pom.xml");

// when
lookupMojo("generate", pom).execute();

// then
assertThatDefaultSchema().containsTable("Users.java");
assertThatDefaultSchema().containsTable("FlywaySchemaHistory.java");
}

public void testPostgresLiquibase() throws Exception {
// given
File pom = getTestPom("postgres-liquibase-pom.xml");

// when
Plugin myMojo = (Plugin) lookupMojo("generate", pom);
myMojo.execute();
lookupMojo("generate", pom).execute();

// then
assertThat(tablesDir).isNotEmptyDirectory();
assertThat(tablesDir.resolve("Users.java")).isNotEmptyFile();
assertThat(generatedSourcesPath.resolve("Public.java")).isNotEmptyFile();
assertThatSchema("custom").containsTable("Person.java");
assertThatSchema("custom").containsTable("Users.java");
assertThatSchema("public_").containsTable("Databasechangelog.java");
}

public void testMysqlFlyway() throws Exception {
// given
File pom = getTestPom("mysql-flyway-pom.xml");

// when
lookupMojo("generate", pom).execute();

// then
assertThatSchema("test").containsTable("Users.java");
assertThatSchema("test").containsTable("FlywaySchemaHistory.java");
}

public void testMariadbLiquibase() throws Exception {
// given
File pom = getTestPom("mariadb-liquibase-pom.xml");

// when
lookupMojo("generate", pom).execute();

// then
assertThatSchema("test").containsTable("Users.java");
assertThatSchema("test").containsTable("Databasechangelog.java");
}

// clean
FileUtils.deleteDirectory(generatedRootDir);
private File getTestPom(String filename) {
return getTestFile("src/test/resources/pom/%s".formatted(filename));
}
}
8 changes: 8 additions & 0 deletions src/test/resources/db/changelog/mariadb/db.changelog-root.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<includeAll path="migration" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
create table users
(
id bigint not null AUTO_INCREMENT,
email varchar(255) not null,
password varchar(255) not null,
name varchar(255) not null,
role varchar(255) not null,
verified bool not null default false,
verification_token varchar(255),
created_at timestamp,
updated_at timestamp,
PRIMARY KEY (id),
CONSTRAINT user_email_unique UNIQUE (email)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<includeAll path="migration" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet author="roman" id="1">
<sql dbms="postgresql" endDelimiter=";">
CREATE SCHEMA custom;
</sql>
</changeSet>


</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE custom.users
(
uuid uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name varchar,
age int
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="3" author="roman">
<createTable tableName="person" schemaName="custom">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="firstname" type="varchar(50)"/>
<column name="lastname" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="state" type="char(2)"/>
</createTable>
</changeSet>


</databaseChangeLog>
111 changes: 111 additions & 0 deletions src/test/resources/pom/mariadb-liquibase-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.testcontainers</groupId>
<artifactId>jooq-testcontainers-codegen-maven-plugin-test</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<java.version>17</java.version>
<testcontainers.version>1.18.1</testcontainers.version>
<jooq-testcontainers-codegen-maven-plugin.version>0.0.2</jooq-testcontainers-codegen-maven-plugin.version>
<jooq.version>3.18.3</jooq.version>
<postgresql.version>42.6.0</postgresql.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>jooq-testcontainers-codegen-maven-plugin</artifactId>
<version>${jooq-testcontainers-codegen-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.9.0</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.testcontainers</groupId>
<artifactId>jooq-testcontainers-codegen-maven-plugin</artifactId>
<version>${jooq-testcontainers-codegen-maven-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
<goals>
<goal>generate</goal>
</goals>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub">
<groupId implementation="java.lang.String">test-group-id</groupId>
<artifactId implementation="java.lang.String">test-artifact-id</artifactId>
<version implementation="java.lang.String">1.0.0-SNAPSHOT</version>
</project>
<database>
<type>MARIADB</type>
<username>test</username>
<password>test</password>
<databaseName>test</databaseName>
</database>
<liquibase>
<changeLogDirectory>
src/test/resources/db/changelog/mariadb
</changeLogDirectory>
<changeLogPath>
db.changelog-root.xml
</changeLogPath>
</liquibase>
<generator>
<database>
<includes>.*</includes>
</database>
<target>
<packageName>org.jooq.codegen.maven.test</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 5df0c68

Please sign in to comment.