From b739602b4b9ba2c0861c1db7d6e903f845aee77a Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Wed, 9 Oct 2024 21:15:43 +0200 Subject: [PATCH] chore: Add final actions in test context - Provide the opportunity to add actions in test context that get run at the end of the test - Enables test actions to dynamically add cleanup actions for instance when auto removing created resources after a test - Used in Knative and Kubernetes test actions to auto remove created resources after the test --- .../knative/KnativeSettings.java | 2 +- .../actions/AbstractKnativeAction.java | 16 ++++++++ .../knative/actions/KnativeAction.java | 6 +++ .../actions/eventing/CreateBrokerAction.java | 9 +++++ .../actions/eventing/CreateTriggerAction.java | 12 +++++- .../messaging/CreateChannelAction.java | 12 +++++- .../messaging/CreateSubscriptionAction.java | 9 +++++ .../knative/xml/CreateBroker.java | 6 +++ .../knative/xml/CreateChannel.java | 6 +++ .../knative/xml/CreateSubscription.java | 6 +++ .../knative/xml/CreateTrigger.java | 6 +++ .../knative/xml/DeleteBroker.java | 6 +++ .../knative/xml/DeleteChannel.java | 6 +++ .../knative/xml/DeleteResource.java | 6 +++ .../knative/xml/DeleteSubscription.java | 6 +++ .../knative/xml/DeleteTrigger.java | 6 +++ .../knative/xml/ReceiveEvent.java | 6 +++ .../knative/xml/SendEvent.java | 6 +++ .../knative/xml/VerifyBroker.java | 6 +++ .../knative/yaml/CreateBroker.java | 6 +++ .../knative/yaml/CreateChannel.java | 6 +++ .../knative/yaml/CreateSubscription.java | 6 +++ .../knative/yaml/CreateTrigger.java | 6 +++ .../knative/yaml/DeleteBroker.java | 6 +++ .../knative/yaml/DeleteChannel.java | 6 +++ .../knative/yaml/DeleteResource.java | 6 +++ .../knative/yaml/DeleteSubscription.java | 6 +++ .../knative/yaml/DeleteTrigger.java | 6 +++ .../knative/yaml/ReceiveEvent.java | 6 +++ .../knative/yaml/SendEvent.java | 6 +++ .../knative/yaml/VerifyBroker.java | 6 +++ .../kubernetes/KubernetesSettings.java | 2 +- .../actions/AbstractKubernetesAction.java | 15 +++++++ .../actions/CreateConfigMapAction.java | 9 +++++ .../actions/CreateCustomResourceAction.java | 23 ++++++++++- .../actions/CreateSecretAction.java | 11 ++++- .../actions/CreateServiceAction.java | 9 +++++ .../actions/DeleteCustomResourceAction.java | 7 ++-- .../kubernetes/actions/KubernetesAction.java | 6 +++ .../kubernetes/xml/CreateAnnotations.java | 6 +++ .../kubernetes/xml/CreateConfigMap.java | 6 +++ .../kubernetes/xml/CreateCustomResource.java | 6 +++ .../kubernetes/xml/CreateLabels.java | 6 +++ .../kubernetes/xml/CreateResource.java | 6 +++ .../kubernetes/xml/CreateSecret.java | 6 +++ .../kubernetes/xml/CreateService.java | 6 +++ .../kubernetes/xml/DeleteConfigMap.java | 6 +++ .../kubernetes/xml/DeleteCustomResource.java | 6 +++ .../kubernetes/xml/DeleteResource.java | 6 +++ .../kubernetes/xml/DeleteSecret.java | 6 +++ .../kubernetes/xml/DeleteService.java | 6 +++ .../kubernetes/xml/VerifyCustomResource.java | 6 +++ .../kubernetes/xml/VerifyPod.java | 6 +++ .../kubernetes/xml/WatchPodLogs.java | 6 +++ .../kubernetes/yaml/CreateAnnotations.java | 6 +++ .../kubernetes/yaml/CreateConfigMap.java | 6 +++ .../kubernetes/yaml/CreateCustomResource.java | 6 +++ .../kubernetes/yaml/CreateLabels.java | 6 +++ .../kubernetes/yaml/CreateResource.java | 6 +++ .../kubernetes/yaml/CreateSecret.java | 6 +++ .../kubernetes/yaml/CreateService.java | 6 +++ .../kubernetes/yaml/DeleteConfigMap.java | 6 +++ .../kubernetes/yaml/DeleteCustomResource.java | 6 +++ .../kubernetes/yaml/DeleteResource.java | 6 +++ .../kubernetes/yaml/DeleteSecret.java | 6 +++ .../kubernetes/yaml/DeleteService.java | 6 +++ .../kubernetes/yaml/VerifyCustomResource.java | 6 +++ .../kubernetes/yaml/VerifyPod.java | 6 +++ .../kubernetes/yaml/WatchPodLogs.java | 6 +++ .../citrusframework/context/TestContext.java | 40 ++++++++++++++----- .../org/citrusframework/DefaultTestCase.java | 31 +++++++++----- 71 files changed, 514 insertions(+), 29 deletions(-) diff --git a/connectors/citrus-knative/src/main/java/org/citrusframework/knative/KnativeSettings.java b/connectors/citrus-knative/src/main/java/org/citrusframework/knative/KnativeSettings.java index 7f9a960a43..5d3b967187 100644 --- a/connectors/citrus-knative/src/main/java/org/citrusframework/knative/KnativeSettings.java +++ b/connectors/citrus-knative/src/main/java/org/citrusframework/knative/KnativeSettings.java @@ -68,7 +68,7 @@ public class KnativeSettings { private static final String AUTO_REMOVE_RESOURCES_PROPERTY = KNATIVE_PROPERTY_PREFIX + "auto.remove.resources"; private static final String AUTO_REMOVE_RESOURCES_ENV = KNATIVE_ENV_PREFIX + "AUTO_REMOVE_RESOURCES"; - private static final String AUTO_REMOVE_RESOURCES_DEFAULT = "true"; + private static final String AUTO_REMOVE_RESOURCES_DEFAULT = "false"; private static final String VERIFY_BROKER_RESPONSE_PROPERTY = KNATIVE_PROPERTY_PREFIX + "verify.broker.resources"; private static final String VERIFY_BROKER_RESPONSE_ENV = KNATIVE_ENV_PREFIX + "VERIFY_BROKER_RESPONSE"; diff --git a/connectors/citrus-knative/src/main/java/org/citrusframework/knative/actions/AbstractKnativeAction.java b/connectors/citrus-knative/src/main/java/org/citrusframework/knative/actions/AbstractKnativeAction.java index 32be6661d9..99d65acd48 100644 --- a/connectors/citrus-knative/src/main/java/org/citrusframework/knative/actions/AbstractKnativeAction.java +++ b/connectors/citrus-knative/src/main/java/org/citrusframework/knative/actions/AbstractKnativeAction.java @@ -21,6 +21,7 @@ import org.citrusframework.AbstractTestActionBuilder; import org.citrusframework.actions.AbstractTestAction; import org.citrusframework.context.TestContext; +import org.citrusframework.knative.KnativeSettings; import org.citrusframework.kubernetes.ClusterType; import org.citrusframework.spi.ReferenceResolver; import org.citrusframework.spi.ReferenceResolverAware; @@ -38,6 +39,8 @@ public abstract class AbstractKnativeAction extends AbstractTestAction implement private final ClusterType clusterType; private final String namespace; + private final boolean autoRemoveResources; + public AbstractKnativeAction(String name, Builder builder) { super("knative:" + name, builder); @@ -45,6 +48,7 @@ public AbstractKnativeAction(String name, Builder builder) { this.kubernetesClient = builder.kubernetesClient; this.namespace = builder.namespace; this.clusterType = builder.clusterType; + this.autoRemoveResources = builder.autoRemoveResources; } @Override @@ -66,6 +70,11 @@ public ClusterType clusterType(TestContext context) { return KnativeAction.super.clusterType(context); } + @Override + public boolean isAutoRemoveResources() { + return autoRemoveResources; + } + @Override public String getNamespace() { return namespace; @@ -84,6 +93,8 @@ public static abstract class Builder builder) { super("k8s:" + name, builder); this.kubernetesClient = builder.kubernetesClient; this.namespace = builder.namespace; + this.autoRemoveResources = builder.autoRemoveResources; this.setActor(builder.getActor()); } @@ -55,6 +59,11 @@ public String getNamespace() { return namespace; } + @Override + public boolean isAutoRemoveResources() { + return autoRemoveResources; + } + /** * Action builder. */ @@ -62,6 +71,7 @@ public static abstract class Builder> resourceType; + private final Class resourceType; private final String version; private final String kind; private final String group; @@ -71,7 +72,7 @@ public void doExecute(TestContext context) { public static class Builder extends AbstractKubernetesAction.Builder { private String resourceName; - private Class> resourceType; + private Class resourceType; private String type; private String version; private String kind; @@ -82,7 +83,7 @@ public Builder resourceName(String name) { return this; } - public Builder resourceType(Class> resourceType) { + public Builder resourceType(Class resourceType) { this.resourceType = resourceType; return this; } diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/actions/KubernetesAction.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/actions/KubernetesAction.java index e3be327bb9..a0da9a66a9 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/actions/KubernetesAction.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/actions/KubernetesAction.java @@ -39,6 +39,12 @@ public interface KubernetesAction extends TestAction { */ String getNamespace(); + /** + * Should remove Knative resources automatically after test. + * @return + */ + boolean isAutoRemoveResources(); + /** * Resolves namespace name from given test context using the stored test variable. * Fallback to the namespace given in Kubernetes environment settings when no test variable is present. diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateAnnotations.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateAnnotations.java index b7d0b937f2..9ceb8a41cd 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateAnnotations.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateAnnotations.java @@ -75,6 +75,12 @@ public CreateAnnotations inNamespace(String namespace) { return this; } + @Override + public CreateAnnotations autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateAnnotationsAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateConfigMap.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateConfigMap.java index dd6eaeab07..f116353c42 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateConfigMap.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateConfigMap.java @@ -78,6 +78,12 @@ public CreateConfigMap inNamespace(String namespace) { return this; } + @Override + public CreateConfigMap autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateConfigMapAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateCustomResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateCustomResource.java index 9f2eaee707..31be307fe4 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateCustomResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateCustomResource.java @@ -93,6 +93,12 @@ public CreateCustomResource inNamespace(String namespace) { return this; } + @Override + public CreateCustomResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateCustomResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateLabels.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateLabels.java index 978bde378c..f4b04d8658 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateLabels.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateLabels.java @@ -75,6 +75,12 @@ public CreateLabels inNamespace(String namespace) { return this; } + @Override + public CreateLabels autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateLabelsAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateResource.java index 57bf901253..41060cdd74 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateResource.java @@ -63,6 +63,12 @@ public CreateResource inNamespace(String namespace) { return this; } + @Override + public CreateResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateSecret.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateSecret.java index 0d498871af..1c513a81b5 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateSecret.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateSecret.java @@ -78,6 +78,12 @@ public CreateSecret inNamespace(String namespace) { return this; } + @Override + public CreateSecret autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateSecretAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateService.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateService.java index 282f2ed173..0b9532460d 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateService.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/CreateService.java @@ -94,6 +94,12 @@ public CreateService inNamespace(String namespace) { return this; } + @Override + public CreateService autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public void setReferenceResolver(ReferenceResolver referenceResolver) { this.delegate.setReferenceResolver(referenceResolver); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteConfigMap.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteConfigMap.java index 01d0badbfe..e20da4ec80 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteConfigMap.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteConfigMap.java @@ -57,6 +57,12 @@ public DeleteConfigMap inNamespace(String namespace) { return this; } + @Override + public DeleteConfigMap autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteConfigMapAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteCustomResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteCustomResource.java index f41b05e696..5060fda9b2 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteCustomResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteCustomResource.java @@ -87,6 +87,12 @@ public DeleteCustomResource inNamespace(String namespace) { return this; } + @Override + public DeleteCustomResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteCustomResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteResource.java index cc08d838ae..d9e0fd4609 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteResource.java @@ -63,6 +63,12 @@ public DeleteResource inNamespace(String namespace) { return this; } + @Override + public DeleteResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteSecret.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteSecret.java index 9f9e321220..48cdcd988b 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteSecret.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteSecret.java @@ -57,6 +57,12 @@ public DeleteSecret inNamespace(String namespace) { return this; } + @Override + public DeleteSecret autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteSecretAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteService.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteService.java index 3a1b048630..fd058a8c7f 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteService.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/DeleteService.java @@ -57,6 +57,12 @@ public DeleteService inNamespace(String namespace) { return this; } + @Override + public DeleteService autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteServiceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyCustomResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyCustomResource.java index e334aa16d5..3170e54f29 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyCustomResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyCustomResource.java @@ -108,6 +108,12 @@ public VerifyCustomResource inNamespace(String namespace) { return this; } + @Override + public VerifyCustomResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public VerifyCustomResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyPod.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyPod.java index d786200c13..9f844ff536 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyPod.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/VerifyPod.java @@ -101,6 +101,12 @@ public VerifyPod inNamespace(String namespace) { return this; } + @Override + public VerifyPod autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public VerifyPodAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/WatchPodLogs.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/WatchPodLogs.java index f739a8a664..35dd9e200b 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/WatchPodLogs.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/xml/WatchPodLogs.java @@ -81,6 +81,12 @@ public WatchPodLogs inNamespace(String namespace) { return this; } + @Override + public WatchPodLogs autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public WatchPodLogsAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateAnnotations.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateAnnotations.java index 20f6d583dd..bd9399934f 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateAnnotations.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateAnnotations.java @@ -64,6 +64,12 @@ public CreateAnnotations inNamespace(String namespace) { return this; } + @Override + public CreateAnnotations autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateAnnotationsAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateConfigMap.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateConfigMap.java index 552f1610bb..4862bc5827 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateConfigMap.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateConfigMap.java @@ -67,6 +67,12 @@ public CreateConfigMap inNamespace(String namespace) { return this; } + @Override + public CreateConfigMap autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateConfigMapAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateCustomResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateCustomResource.java index fc04d93deb..31b08d781f 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateCustomResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateCustomResource.java @@ -82,6 +82,12 @@ public CreateCustomResource inNamespace(String namespace) { return this; } + @Override + public CreateCustomResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateCustomResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateLabels.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateLabels.java index 3e0338951f..e422418ea2 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateLabels.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateLabels.java @@ -63,6 +63,12 @@ public CreateLabels inNamespace(String namespace) { return this; } + @Override + public CreateLabels autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateLabelsAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateResource.java index 1b451db052..57efb81e1c 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateResource.java @@ -57,6 +57,12 @@ public CreateResource inNamespace(String namespace) { return this; } + @Override + public CreateResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateSecret.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateSecret.java index 2421d56e62..5884a76b23 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateSecret.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateSecret.java @@ -67,6 +67,12 @@ public CreateSecret inNamespace(String namespace) { return this; } + @Override + public CreateSecret autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateSecretAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateService.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateService.java index a2d83474d6..7f375b5e0c 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateService.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/CreateService.java @@ -86,6 +86,12 @@ public CreateService inNamespace(String namespace) { return this; } + @Override + public CreateService autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public CreateServiceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteConfigMap.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteConfigMap.java index 5af99bc838..bdf6f3ad58 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteConfigMap.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteConfigMap.java @@ -53,6 +53,12 @@ public DeleteConfigMap inNamespace(String namespace) { return this; } + @Override + public DeleteConfigMap autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteConfigMapAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteCustomResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteCustomResource.java index 052e7f210b..d927d7d92b 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteCustomResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteCustomResource.java @@ -78,6 +78,12 @@ public DeleteCustomResource inNamespace(String namespace) { return this; } + @Override + public DeleteCustomResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteCustomResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteResource.java index ca28ca4fc8..e38b64a76d 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteResource.java @@ -57,6 +57,12 @@ public DeleteResource inNamespace(String namespace) { return this; } + @Override + public DeleteResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteSecret.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteSecret.java index 4ae03f358a..47daeddc00 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteSecret.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteSecret.java @@ -53,6 +53,12 @@ public DeleteSecret inNamespace(String namespace) { return this; } + @Override + public DeleteSecret autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteSecretAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteService.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteService.java index 252a1762dd..fb2cb94c9d 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteService.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/DeleteService.java @@ -53,6 +53,12 @@ public DeleteService inNamespace(String namespace) { return this; } + @Override + public DeleteService autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public DeleteServiceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyCustomResource.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyCustomResource.java index f956f7a2dc..9b2d684249 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyCustomResource.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyCustomResource.java @@ -95,6 +95,12 @@ public VerifyCustomResource inNamespace(String namespace) { return this; } + @Override + public VerifyCustomResource autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public VerifyCustomResourceAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyPod.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyPod.java index bcf153d1e4..3c3b302341 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyPod.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/VerifyPod.java @@ -84,6 +84,12 @@ public VerifyPod inNamespace(String namespace) { return this; } + @Override + public VerifyPod autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public VerifyPodAction doBuild() { return delegate.build(); diff --git a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/WatchPodLogs.java b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/WatchPodLogs.java index 3495241f1c..bcbdc3c794 100644 --- a/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/WatchPodLogs.java +++ b/connectors/citrus-kubernetes/src/main/java/org/citrusframework/kubernetes/yaml/WatchPodLogs.java @@ -68,6 +68,12 @@ public WatchPodLogs inNamespace(String namespace) { return this; } + @Override + public WatchPodLogs autoRemoveResources(boolean enabled) { + this.delegate.autoRemoveResources(enabled); + return this; + } + @Override public WatchPodLogsAction doBuild() { return delegate.build(); diff --git a/core/citrus-api/src/main/java/org/citrusframework/context/TestContext.java b/core/citrus-api/src/main/java/org/citrusframework/context/TestContext.java index e4c0c2ff0e..0bf62b1b7d 100644 --- a/core/citrus-api/src/main/java/org/citrusframework/context/TestContext.java +++ b/core/citrus-api/src/main/java/org/citrusframework/context/TestContext.java @@ -16,6 +16,17 @@ package org.citrusframework.context; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + import org.citrusframework.CitrusSettings; import org.citrusframework.TestAction; import org.citrusframework.TestActionBuilder; @@ -58,17 +69,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; - /** * Class holding and managing test variables. The test context also provides utility methods * for replacing dynamic content(variables and functions) in message payloads and headers. @@ -137,6 +137,12 @@ public class TestContext implements ReferenceResolverAware, TestActionListenerAw */ private List afterTest = new ArrayList<>(); + /** + * Further chain of test actions to be executed in any case (success, error) + * Usually used to clean up resources in any case of test result. + */ + private final List> finalActions = new ArrayList<>(); + /** * List of message listeners to be informed on inbound and outbound message exchange */ @@ -662,6 +668,14 @@ public void setAfterTest(List afterTest) { this.afterTest = afterTest; } + /** + * Optains the final actions. + * @return + */ + public List> getFinalActions() { + return finalActions; + } + /** * Obtains the segmentVariableExtractorRegistry * @@ -916,6 +930,10 @@ public boolean isSuccess(TestResult testResult) { .orElse(false); } + public void doFinally(TestActionBuilder action) { + this.finalActions.add(action); + } + /** * Empty test case implementation used as test result when tests fail before execution. */ diff --git a/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java b/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java index 53467d9e0f..8d2f69e8b0 100644 --- a/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java +++ b/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java @@ -16,20 +16,13 @@ package org.citrusframework; -import static java.lang.String.format; -import static java.util.Objects.isNull; -import static java.util.Objects.nonNull; -import static org.citrusframework.TestResult.failed; -import static org.citrusframework.TestResult.skipped; -import static org.citrusframework.TestResult.success; -import static org.citrusframework.util.TestUtils.waitForCompletion; - import java.time.Duration; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; + import org.apache.commons.lang3.time.StopWatch; import org.citrusframework.container.AbstractActionContainer; import org.citrusframework.container.AfterTest; @@ -41,6 +34,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static java.lang.String.format; +import static java.util.Objects.isNull; +import static java.util.Objects.nonNull; +import static org.citrusframework.TestResult.failed; +import static org.citrusframework.TestResult.skipped; +import static org.citrusframework.TestResult.success; +import static org.citrusframework.util.TestUtils.waitForCompletion; + /** * Default test case implementation holding a list of test actions to execute. Test case also holds variable definitions and * performs the test lifecycle such as start, finish, before and after test. @@ -297,6 +298,18 @@ private void executeFinalActions(TestContext context) { } } + /* test context may also have some actions to run finally */ + for (final TestActionBuilder actionBuilder : context.getFinalActions()) { + TestAction action = actionBuilder.build(); + if (!action.isDisabled(context)) { + context.getTestActionListeners().onTestActionStart(this, action); + action.execute(context); + context.getTestActionListeners().onTestActionFinish(this, action); + } else { + context.getTestActionListeners().onTestActionSkipped(this, action); + } + } + if (testResult.isSuccess() && context.hasExceptions()) { CitrusRuntimeException contextException = context.getExceptions().remove(0); testResult = getTestResultInstanceProvider(context).createFailed(this, contextException); @@ -413,7 +426,7 @@ public String toString() { stringBuilder.append("] "); - return super.toString() + stringBuilder.toString(); + return super.toString() + stringBuilder; } @Override