-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Quarkus Testcontainers test resource
- Easily add Testcontainers instance to your Quarkus test using annotation based config - Enable users to access Testcontainers instance after container has been started/stopped via listeners - Enable users to supply application properties to the Quarkus application under test (e.g. set connection settings from Testcontainers instance)
- Loading branch information
1 parent
d7a99a5
commit 0be1939
Showing
26 changed files
with
1,050 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ain/java/org/citrusframework/testcontainers/aws2/quarkus/LocalStackContainerResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.citrusframework.testcontainers.aws2.quarkus; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceConfigurableLifecycleManager; | ||
import org.citrusframework.exceptions.CitrusRuntimeException; | ||
import org.citrusframework.testcontainers.aws2.LocalStackContainer; | ||
import org.citrusframework.testcontainers.aws2.StartLocalStackAction; | ||
import org.citrusframework.testcontainers.quarkus.ContainerLifecycleListener; | ||
import org.citrusframework.testcontainers.quarkus.TestcontainersResource; | ||
|
||
public class LocalStackContainerResource extends TestcontainersResource<LocalStackContainer> | ||
implements QuarkusTestResourceConfigurableLifecycleManager<LocalStackContainerSupport> { | ||
|
||
public static final String SERVICES_INIT_ARG = "aws.localstack.services"; | ||
|
||
public LocalStackContainerResource() { | ||
super(LocalStackContainer.class); | ||
} | ||
|
||
@Override | ||
public void init(LocalStackContainerSupport config) { | ||
for (Class<? extends ContainerLifecycleListener<LocalStackContainer>> lifecycleListenerType : | ||
config.containerLifecycleListener()) { | ||
try { | ||
registerContainerLifecycleListener(lifecycleListenerType.getDeclaredConstructor().newInstance()); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new CitrusRuntimeException("Failed to instantiate container lifecycle listener from type: %s" | ||
.formatted(lifecycleListenerType), e); | ||
} | ||
} | ||
|
||
container = new StartLocalStackAction.Builder() | ||
.withServices(config.services()) | ||
.build().getContainer(); | ||
} | ||
|
||
@Override | ||
protected void doInit(Map<String, String> initArgs) { | ||
String[] serviceNames = initArgs.getOrDefault(SERVICES_INIT_ARG, "").split(","); | ||
container = new StartLocalStackAction.Builder() | ||
.withServices(Arrays.stream(serviceNames) | ||
.map(String::trim) | ||
.map(LocalStackContainer.Service::fromServiceName) | ||
.toArray(LocalStackContainer.Service[]::new)) | ||
.build().getContainer(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...main/java/org/citrusframework/testcontainers/aws2/quarkus/LocalStackContainerSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.citrusframework.testcontainers.aws2.quarkus; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import org.citrusframework.testcontainers.aws2.LocalStackContainer; | ||
import org.citrusframework.testcontainers.quarkus.ContainerLifecycleListener; | ||
|
||
@QuarkusTestResource(LocalStackContainerResource.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface LocalStackContainerSupport { | ||
|
||
/** | ||
* Enabled services. | ||
* @return | ||
*/ | ||
LocalStackContainer.Service[] services() default {}; | ||
|
||
/** | ||
* Container lifecycle listeners | ||
*/ | ||
Class<? extends ContainerLifecycleListener<LocalStackContainer>>[] containerLifecycleListener() default {}; | ||
} |
56 changes: 56 additions & 0 deletions
56
...rc/main/java/org/citrusframework/testcontainers/kafka/quarkus/KafkaContainerResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.citrusframework.testcontainers.kafka.quarkus; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceConfigurableLifecycleManager; | ||
import org.citrusframework.exceptions.CitrusRuntimeException; | ||
import org.citrusframework.testcontainers.kafka.StartKafkaAction; | ||
import org.citrusframework.testcontainers.quarkus.ContainerLifecycleListener; | ||
import org.citrusframework.testcontainers.quarkus.TestcontainersResource; | ||
import org.testcontainers.containers.KafkaContainer; | ||
|
||
public class KafkaContainerResource extends TestcontainersResource<KafkaContainer> | ||
implements QuarkusTestResourceConfigurableLifecycleManager<KafkaContainerSupport> { | ||
|
||
public KafkaContainerResource() { | ||
super(KafkaContainer.class); | ||
} | ||
|
||
@Override | ||
public void init(KafkaContainerSupport config) { | ||
for (Class<? extends ContainerLifecycleListener<KafkaContainer>> lifecycleListenerType : | ||
config.containerLifecycleListener()) { | ||
try { | ||
registerContainerLifecycleListener(lifecycleListenerType.getDeclaredConstructor().newInstance()); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new CitrusRuntimeException("Failed to instantiate container lifecycle listener from type: %s" | ||
.formatted(lifecycleListenerType), e); | ||
} | ||
} | ||
|
||
doInit(Collections.emptyMap()); | ||
} | ||
|
||
@Override | ||
protected void doInit(Map<String, String> initArgs) { | ||
container = new StartKafkaAction.Builder().build().getContainer(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...src/main/java/org/citrusframework/testcontainers/kafka/quarkus/KafkaContainerSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.citrusframework.testcontainers.kafka.quarkus; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import org.citrusframework.testcontainers.quarkus.ContainerLifecycleListener; | ||
import org.testcontainers.containers.KafkaContainer; | ||
|
||
@QuarkusTestResource(KafkaContainerResource.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface KafkaContainerSupport { | ||
|
||
/** | ||
* Container lifecycle listeners | ||
*/ | ||
Class<? extends ContainerLifecycleListener<KafkaContainer>>[] containerLifecycleListener() default {}; | ||
} |
49 changes: 49 additions & 0 deletions
49
.../src/main/java/org/citrusframework/testcontainers/quarkus/ContainerLifecycleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.citrusframework.testcontainers.quarkus; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* Listener gets invoked when Testcontainers instance is started or stopped. | ||
* Allows implementations to perform actions with given container instance, | ||
* in particular configuring the application under test with the container exposed connection settings. | ||
* @param <T> the container type | ||
*/ | ||
public interface ContainerLifecycleListener<T> { | ||
|
||
String INIT_ARG = "citrus.testcontainers.lifecycle.listener"; | ||
|
||
/** | ||
* Invoked when Testcontainers instance has been started. Returned key-value | ||
* map is used to set application properties on the system under test which is the Quarkus application | ||
* started via QuarkusTest annotation. | ||
* @param container | ||
* @return | ||
*/ | ||
default Map<String, String> started(T container) { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
/** | ||
* Invoked after the Testcontainers instance has been stopped. | ||
* @param container | ||
*/ | ||
default void stopped(T container) { | ||
} | ||
} |
Oops, something went wrong.