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

Rewrite testcontainers to use explicit image and version #433

Merged
merged 4 commits into from
Nov 27, 2023
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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ dependencies {
compileOnly("org.projectlombok:lombok:latest.release")
annotationProcessor("org.projectlombok:lombok:latest.release")

implementation("org.testcontainers:testcontainers:latest.release")

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-groovy")

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")

testRuntimeOnly("com.tngtech.archunit:archunit:0.23.1")
testRuntimeOnly("org.testcontainers:testcontainers:latest.release")
testRuntimeOnly("org.testcontainers:nginx:latest.release")

// testImplementation("org.hamcrest:hamcrest:latest.release")
// testImplementation("org.assertj:assertj-core:latest.release")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.testcontainers;

import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.marker.Markers;

import java.util.Arrays;
import java.util.UUID;

@RequiredArgsConstructor
public class ExplicitContainerImage extends Recipe {
@Option(displayName = "Container class",
description = "The fully qualified name of the container class to use.",
example = "org.testcontainers.containers.NginxContainer")
private final String containerClass;

@Option(displayName = "Image to use",
description = "The image to use for the container.",
example = "nginx:1.9.4")
private final String image;

@Option(displayName = "Parse image",
description = "Whether to call `DockerImageName.parse(image)`.",
required = false)
private final Boolean parseImage;

@Override
public String getDisplayName() {
return "Add image argument to container constructor";
}

@Override
public String getDescription() {
return "Set the image to use for a container explicitly if unset, rather than relying on the default image for the container class.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher methodMatcher = new MethodMatcher(containerClass + " <constructor>()");
return Preconditions.check(new UsesMethod<>(methodMatcher), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
J.NewClass nc = super.visitNewClass(newClass, executionContext);
if (methodMatcher.matches(newClass)) {
return nc.withArguments(Arrays.asList(getConstructorArgument(newClass)));
}
return nc;
}

@NotNull
private Expression getConstructorArgument(J.NewClass newClass) {
if (parseImage != null && parseImage) {
maybeAddImport("org.testcontainers.utility.DockerImageName");
return JavaTemplate.builder("DockerImageName.parse(\"" + image + "\")")
.imports("org.testcontainers.utility.DockerImageName")
.javaParser(JavaParser.fromJavaVersion().classpath("testcontainers"))
.build()
.apply(getCursor(), newClass.getArguments().get(0).getCoordinates().replace())
.withPrefix(Space.EMPTY);
}
return new J.Literal(UUID.randomUUID(), Space.EMPTY, Markers.EMPTY, image, "\"" + image + "\"", null, JavaType.Primitive.String);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NonNullApi
package org.openrewrite.java.testing.testcontainers;

import org.openrewrite.internal.lang.NonNullApi;
123 changes: 123 additions & 0 deletions src/main/resources/META-INF/rewrite/testcontainers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#
# Copyright 2023 the original author or authors.
# <p>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.testcontainers.TestContainersBestPractices
displayName: Testcontainers best practices
description: Apply best practices to Testcontainers usage.
recipeList:
# Upgrade to latest version of Testcontainers
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: org.testcontainers
artifactId: "*"
newVersion: 1.19.x
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImages
- org.openrewrite.java.testing.testcontainers.GetHostMigration
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.testcontainers.GetHostMigration
displayName: Replace `ContainerState.getContainerIpAddress()` with `getHost()`
description: Replace `org.testcontainers.containers.ContainerState.getContainerIpAddress()` with `getHost()`.
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.testcontainers.containers.ContainerState getContainerIpAddress()
newMethodName: getHost
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.testcontainers.ExplicitContainerImages
displayName: Explicit container images and versions
description: Replace implicit default container images and versions with explicit versions.
recipeList:
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.CassandraContainer
image: "cassandra:3.11.2"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.ClickHouseContainer
image: "yandex/clickhouse-server:18.10.3"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.CockroachContainer
image: "cockroachdb/cockroach:v19.2.11"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.couchbase.CouchbaseContainer
image: "couchbase/server:6.5.1"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.Db2Container
image: "ibmcom/db2:11.5.0.0a"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.dynamodb.DynaliteContainer
image: "quay.io/testcontainers/dynalite:v1.2.1-1"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.elasticsearch.ElasticsearchContainer
image: "docker.elastic.co/elasticsearch/elasticsearch:7.9.2"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.InfluxDBContainer
image: "influxdb:1.4.3"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.MariaDBContainer
image: "mariadb:10.3.6"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.MongoDBContainer
image: "mongo:4.0.10"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.MSSQLServerContainer
image: "mcr.microsoft.com/mssql/server:2017-CU12"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.MySQLContainer
image: "mysql:5.7.34"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.Neo4jContainer
image: "neo4j:4.4"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.NginxContainer
image: "nginx:1.9.4"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.OracleContainer
image: "gvenzl/oracle-xe:18.4.0-slim"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.OrientDBContainer
image: "orientdb:3.0.24-tp3"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.PostgreSQLContainer
image: "postgres:9.6.12"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.RabbitMQContainer
image: "rabbitmq:3.7.25-management-alpine"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.SolrContainer
image: "solr:8.3.0"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.ToxiproxyContainer
image: "shopify/toxiproxy:2.1.0"
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.vault.VaultContainer
image: "vault:1.1.3"
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
# The following require a call to `DockerImageName.parse(image)`
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.KafkaContainer
image: "confluentinc/cp-kafka:5.4.3"
parseImage: true
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.localstack.LocalStackContainer
image: "localstack/localstack:0.11.2"
parseImage: true
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.MockServerContainer
image: "jamesdbloom/mockserver:mockserver-5.5.4"
parseImage: true
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImage:
containerClass: org.testcontainers.containers.PulsarContainer
image: "apachepulsar/pulsar:2.10.0"
parseImage: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.testcontainers;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class ExplicitContainerImageTest implements RewriteTest {
@Test
@DocumentExample
void explicitContainerImage() {
rewriteRun(
spec -> spec
.recipe(new ExplicitContainerImage("org.testcontainers.containers.NginxContainer", "nginx:1.9.4", null))
.parser(JavaParser.fromJavaVersion().classpath("nginx")),
//language=java
java(
"""
import org.testcontainers.containers.NginxContainer;
class Foo {
NginxContainer container = new NginxContainer();
}
""",
"""
import org.testcontainers.containers.NginxContainer;
class Foo {
NginxContainer container = new NginxContainer("nginx:1.9.4");
}
"""
)
);
}

@Test
void explicitContainerImageParsed() {
rewriteRun(
spec -> spec
.recipe(new ExplicitContainerImage("org.testcontainers.containers.NginxContainer", "nginx:1.9.4", true))
.parser(JavaParser.fromJavaVersion().classpath("nginx")),
//language=java
java(
"""
import org.testcontainers.containers.NginxContainer;

class Foo {
NginxContainer container = new NginxContainer();
}
""",
"""
import org.testcontainers.containers.NginxContainer;
import org.testcontainers.utility.DockerImageName;

class Foo {
NginxContainer container = new NginxContainer(DockerImageName.parse("nginx:1.9.4"));
}
"""
)
);
}

@Test
void explicitContainerImages() {
rewriteRun(
spec -> spec
.recipeFromResource("/META-INF/rewrite/testcontainers.yml", "org.openrewrite.java.testing.testcontainers.ExplicitContainerImages")
.parser(JavaParser.fromJavaVersion().classpath("nginx")),
//language=java
java(
"""
import org.testcontainers.containers.NginxContainer;
class Foo {
NginxContainer container = new NginxContainer();
}
""",
"""
import org.testcontainers.containers.NginxContainer;
class Foo {
NginxContainer container = new NginxContainer("nginx:1.9.4");
}
"""
)
);
}
}
Loading
Loading