From 0fdf10a3854d4015561c724c9f0877502c3c4c12 Mon Sep 17 00:00:00 2001 From: Remco Westerhoud Date: Wed, 14 Dec 2022 10:07:32 +0100 Subject: [PATCH 1/2] test(extensions): verify fields are injected into superclass The extensions should inject the ZeebeClient and the ZeebeTestEngine into the fields available with this type. This test will verify that this happens for both the embedded engine and the testcontainers engine. (cherry picked from commit db97f19bca7735734a731c6b3c2efa8212b17b08) --- .../AbstractInheritanceInjectionTest.java | 34 +++++++++++++++++++ .../injection/InheritanceInjectionTest.java | 23 +++++++++++++ .../injection/InheritanceInjectionTest.java | 23 +++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java create mode 100644 qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java create mode 100644 qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java diff --git a/qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java b/qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java new file mode 100644 index 00000000..f6453d7d --- /dev/null +++ b/qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java @@ -0,0 +1,34 @@ +/* + * Copyright © 2021 camunda services GmbH (info@camunda.com) + * + * 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 io.camunda.zeebe.process.test.qa.abstracts.injection; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.camunda.zeebe.client.ZeebeClient; +import io.camunda.zeebe.process.test.api.ZeebeTestEngine; +import org.junit.jupiter.api.Test; + +public abstract class AbstractInheritanceInjectionTest { + protected ZeebeClient client; + protected ZeebeTestEngine engine; + + @Test + void testFieldsAreInjectedSuccessfully() { + assertThat(client).isNotNull(); + assertThat(engine).isNotNull(); + } +} diff --git a/qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java b/qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java new file mode 100644 index 00000000..d329155c --- /dev/null +++ b/qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java @@ -0,0 +1,23 @@ +/* + * Copyright © 2021 camunda services GmbH (info@camunda.com) + * + * 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 io.camunda.zeebe.process.test.qa.embedded.injection; + +import io.camunda.zeebe.process.test.extension.ZeebeProcessTest; +import io.camunda.zeebe.process.test.qa.abstracts.injection.AbstractInheritanceInjectionTest; + +@ZeebeProcessTest +class InheritanceInjectionTest extends AbstractInheritanceInjectionTest {} diff --git a/qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java b/qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java new file mode 100644 index 00000000..aceef980 --- /dev/null +++ b/qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java @@ -0,0 +1,23 @@ +/* + * Copyright © 2021 camunda services GmbH (info@camunda.com) + * + * 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 io.camunda.zeebe.process.test.qa.testcontainer.injection; + +import io.camunda.zeebe.process.test.extension.testcontainer.ZeebeProcessTest; +import io.camunda.zeebe.process.test.qa.abstracts.injection.AbstractInheritanceInjectionTest; + +@ZeebeProcessTest +class InheritanceInjectionTest extends AbstractInheritanceInjectionTest {} From 55144f516e0ac54eb8e0309976dc9f7febee8ba9 Mon Sep 17 00:00:00 2001 From: Remco Westerhoud Date: Wed, 14 Dec 2022 10:12:11 +0100 Subject: [PATCH 2/2] fix(extensions): inject ZPT fields into superclasses The extensions should inject the engine, client and recordstream into the fields that are available. Before this only tried to inject them into the annotated class. This does not work when inheritance is used and the fields are defined on the superclass of the annotated class. This commit changes the extension so that it will first look on the annotated class. If it cannot find any of the annotations here, it will work it's way up to the superclass and so on until it either found a field it is able to inject, or no fields at all. (cherry picked from commit b912ba9702d81742f7a291d9e18d1db2b023be48) --- .../ZeebeProcessTestExtension.java | 13 +++++++++---- .../test/extension/ZeebeProcessTestExtension.java | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java b/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java index ba5e42bf..139f7a98 100644 --- a/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java +++ b/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java @@ -123,14 +123,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable private void injectFields(final ExtensionContext extensionContext, final Object... objects) { final Class requiredTestClass = extensionContext.getRequiredTestClass(); - final Field[] declaredFields = requiredTestClass.getDeclaredFields(); for (final Object object : objects) { - final Optional field = getField(declaredFields, object); + final Optional field = getField(requiredTestClass, object); field.ifPresent(value -> injectField(extensionContext, value, object)); } } - private Optional getField(final Field[] declaredFields, final Object object) { + private Optional getField(final Class requiredTestClass, final Object object) { + final Field[] declaredFields = requiredTestClass.getDeclaredFields(); + final List fields = Arrays.stream(declaredFields) .filter(field -> field.getType().isInstance(object)) @@ -143,8 +144,12 @@ private Optional getField(final Field[] declaredFields, final Object obje + "found %s. Please make sure at most one field of type %s has been declared in the" + " test class.", object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName())); + } else if (fields.size() == 0) { + final Class superclass = requiredTestClass.getSuperclass(); + return superclass == null ? Optional.empty() : getField(superclass, object); + } else { + return Optional.of(fields.get(0)); } - return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0)); } private void injectField( diff --git a/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java b/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java index edea3dbb..41b59fe5 100644 --- a/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java +++ b/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java @@ -99,14 +99,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable private void injectFields(final ExtensionContext extensionContext, final Object... objects) { final Class requiredTestClass = extensionContext.getRequiredTestClass(); - final Field[] declaredFields = requiredTestClass.getDeclaredFields(); for (final Object object : objects) { - final Optional field = getField(declaredFields, object); + final Optional field = getField(requiredTestClass, object); field.ifPresent(value -> injectField(extensionContext, value, object)); } } - private Optional getField(final Field[] declaredFields, final Object object) { + private Optional getField(final Class requiredTestClass, final Object object) { + final Field[] declaredFields = requiredTestClass.getDeclaredFields(); + final List fields = Arrays.stream(declaredFields) .filter(field -> field.getType().isInstance(object)) @@ -119,8 +120,12 @@ private Optional getField(final Field[] declaredFields, final Object obje + "found %s. Please make sure at most one field of type %s has been declared in the" + " test class.", object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName())); + } else if (fields.size() == 0) { + final Class superclass = requiredTestClass.getSuperclass(); + return superclass == null ? Optional.empty() : getField(superclass, object); + } else { + return Optional.of(fields.get(0)); } - return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0)); } private void injectField(