|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | +package org.flowable.app.engine.test; |
| 14 | + |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.util.Set; |
| 17 | + |
| 18 | +import org.flowable.app.api.AppManagementService; |
| 19 | +import org.flowable.app.api.AppRepositoryService; |
| 20 | +import org.flowable.app.engine.AppEngine; |
| 21 | +import org.flowable.app.engine.AppEngineConfiguration; |
| 22 | +import org.flowable.app.engine.test.impl.AppTestHelper; |
| 23 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 24 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 25 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 26 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 27 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 28 | +import org.junit.jupiter.api.extension.ParameterResolver; |
| 29 | +import org.junit.platform.commons.support.AnnotationSupport; |
| 30 | + |
| 31 | +/** |
| 32 | + * JUnit Jupiter extension for the Flowable AppEngine and services initialization. |
| 33 | + * |
| 34 | + * <p> |
| 35 | + * Usage: |
| 36 | + * </p> |
| 37 | + * |
| 38 | + * <pre> |
| 39 | + * @ExtendWith(FlowableAppExtension.class) |
| 40 | + * class YourTest { |
| 41 | + * |
| 42 | + * @BeforeEach |
| 43 | + * void setUp(AppEngine appEngine) { |
| 44 | + * ... |
| 45 | + * } |
| 46 | + * |
| 47 | + * @Test |
| 48 | + * void myTest(AppRepositoryService appRepositoryService) { |
| 49 | + * ... |
| 50 | + * } |
| 51 | + * |
| 52 | + * ... |
| 53 | + * } |
| 54 | + * </pre> |
| 55 | + * |
| 56 | + * <p> |
| 57 | + * The AppEngine and the services will be made available to the test class through the parameter resolution (BeforeEach, AfterEach, test methods). |
| 58 | + * The AppEngine will be initialized by default with the flowable.app.cfg.xml resource on the classpath. |
| 59 | + * To specify a different configuration file, annotate your class with {@link AppConfigurationResource}. |
| 60 | + * App engines will be cached as part of the JUnit Jupiter Extension context. |
| 61 | + * Right before the first time the setUp is called for a given configuration resource, the cmmn engine will be constructed. |
| 62 | + * </p> |
| 63 | + * |
| 64 | + * <p> |
| 65 | + * You can declare a deployment with the {@link AppDeployment} annotation. This extension will make sure that this deployment gets deployed before the setUp |
| 66 | + * and {@link AppRepositoryService#deleteDeployment(String, boolean) cascade deleted} after the tearDown. |
| 67 | + * The id of the deployment can be accessed by using {@link AppDeploymentId} in a test method. |
| 68 | + * </p> |
| 69 | + * |
| 70 | + * @author Filip Hrisafov |
| 71 | + */ |
| 72 | +public class FlowableAppExtension implements ParameterResolver, BeforeEachCallback, AfterEachCallback { |
| 73 | + |
| 74 | + public static final String DEFAULT_CONFIGURATION_RESOURCE = "flowable.app.cfg.xml"; |
| 75 | + |
| 76 | + private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(FlowableAppExtension.class); |
| 77 | + |
| 78 | + private static final Set<Class<?>> SUPPORTED_PARAMETER_TYPES = Set.of( |
| 79 | + AppEngine.class, |
| 80 | + AppEngineConfiguration.class, |
| 81 | + AppRepositoryService.class, |
| 82 | + AppManagementService.class |
| 83 | + ); |
| 84 | + |
| 85 | + @Override |
| 86 | + public void beforeEach(ExtensionContext context) { |
| 87 | + FlowableAppTestHelper testHelper = getTestHelper(context); |
| 88 | + AnnotationSupport.findAnnotation(context.getTestMethod(), AppDeployment.class) |
| 89 | + .ifPresent(deployment -> { |
| 90 | + String deploymentId = AppTestHelper.annotationDeploymentSetUp(testHelper.getAppEngine(), context.getRequiredTestClass(), |
| 91 | + context.getRequiredTestMethod(), deployment); |
| 92 | + testHelper.setDeploymentIdFromDeploymentAnnotation(deploymentId); |
| 93 | + }); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void afterEach(ExtensionContext context) { |
| 99 | + FlowableAppTestHelper testHelper = getTestHelper(context); |
| 100 | + String deploymentId = testHelper.getDeploymentIdFromDeploymentAnnotation(); |
| 101 | + if (deploymentId != null) { |
| 102 | + testHelper.getAppEngine().getAppRepositoryService().deleteDeployment(deploymentId, true); |
| 103 | + testHelper.setDeploymentIdFromDeploymentAnnotation(null); |
| 104 | + } |
| 105 | + testHelper.getAppEngine().getAppEngineConfiguration().getClock().reset(); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { |
| 111 | + Class<?> parameterType = parameterContext.getParameter().getType(); |
| 112 | + return SUPPORTED_PARAMETER_TYPES.contains(parameterType) |
| 113 | + || AnnotationSupport.isAnnotated(parameterContext.getParameter(), AppDeploymentId.class); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { |
| 118 | + FlowableAppTestHelper testHelper = getTestHelper(extensionContext); |
| 119 | + if (parameterContext.isAnnotated(AppDeploymentId.class)) { |
| 120 | + return testHelper.getDeploymentIdFromDeploymentAnnotation(); |
| 121 | + } |
| 122 | + |
| 123 | + Class<?> parameterType = parameterContext.getParameter().getType(); |
| 124 | + AppEngine appEngine = testHelper.getAppEngine(); |
| 125 | + if (parameterType.isInstance(appEngine)) { |
| 126 | + return appEngine; |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + return AppEngine.class.getDeclaredMethod("get" + parameterType.getSimpleName()).invoke(appEngine); |
| 131 | + } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { |
| 132 | + throw new ParameterResolutionException("Could not find service " + parameterType, e); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected String getConfigurationResource(ExtensionContext context) { |
| 137 | + return AnnotationSupport.findAnnotation(context.getTestClass(), AppConfigurationResource.class) |
| 138 | + .map(AppConfigurationResource::value) |
| 139 | + .orElse(DEFAULT_CONFIGURATION_RESOURCE); |
| 140 | + } |
| 141 | + |
| 142 | + protected FlowableAppTestHelper getTestHelper(ExtensionContext context) { |
| 143 | + return getStore(context) |
| 144 | + .getOrComputeIfAbsent(context.getRequiredTestClass(), key -> new FlowableAppTestHelper(createAppEngine(context)), FlowableAppTestHelper.class); |
| 145 | + } |
| 146 | + |
| 147 | + protected AppEngine createAppEngine(ExtensionContext context) { |
| 148 | + return AppTestHelper.getAppEngine(getConfigurationResource(context)); |
| 149 | + } |
| 150 | + |
| 151 | + protected ExtensionContext.Store getStore(ExtensionContext context) { |
| 152 | + return context.getRoot().getStore(NAMESPACE); |
| 153 | + } |
| 154 | +} |
0 commit comments