Skip to content

Commit 7d331f4

Browse files
committed
Migrate all flowable-app-engine-rest tests to JUnit Jupiter
1 parent 8c9880f commit 7d331f4

13 files changed

Lines changed: 288 additions & 289 deletions

modules/flowable-app-engine-rest/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@
151151

152152
<!-- Test dependencies -->
153153
<dependency>
154-
<groupId>junit</groupId>
155-
<artifactId>junit</artifactId>
154+
<groupId>org.junit.jupiter</groupId>
155+
<artifactId>junit-jupiter</artifactId>
156156
<scope>test</scope>
157157
</dependency>
158158
<dependency>

modules/flowable-app-engine-rest/src/test/java/org/flowable/app/rest/WebConfigurer.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import jakarta.servlet.ServletContextListener;
2323
import jakarta.servlet.ServletRegistration;
2424

25-
import org.flowable.app.rest.conf.ApplicationConfiguration;
2625
import org.slf4j.Logger;
2726
import org.slf4j.LoggerFactory;
27+
import org.springframework.context.ConfigurableApplicationContext;
2828
import org.springframework.web.context.WebApplicationContext;
2929
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3030
import org.springframework.web.context.support.WebApplicationContextUtils;
@@ -38,10 +38,10 @@ public class WebConfigurer implements ServletContextListener {
3838

3939
private static final Logger LOGGER = LoggerFactory.getLogger(WebConfigurer.class);
4040

41-
public AnnotationConfigWebApplicationContext context;
41+
protected final WebApplicationContext rootContext;
4242

43-
public void setContext(AnnotationConfigWebApplicationContext context) {
44-
this.context = context;
43+
public WebConfigurer(WebApplicationContext rootContext) {
44+
this.rootContext = rootContext;
4545
}
4646

4747
@Override
@@ -50,16 +50,6 @@ public void contextInitialized(ServletContextEvent sce) {
5050

5151
LOGGER.debug("Configuring Spring root application context");
5252

53-
AnnotationConfigWebApplicationContext rootContext = null;
54-
55-
if (context == null) {
56-
rootContext = new AnnotationConfigWebApplicationContext();
57-
rootContext.register(ApplicationConfiguration.class);
58-
rootContext.refresh();
59-
} else {
60-
rootContext = context;
61-
}
62-
6353
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);
6454

6555
EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);
@@ -73,7 +63,7 @@ public void contextInitialized(ServletContextEvent sce) {
7363
/**
7464
* Initializes Spring and Spring MVC.
7565
*/
76-
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
66+
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, WebApplicationContext rootContext) {
7767
LOGGER.debug("Configuring Spring Web application context");
7868
AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
7969
dispatcherServletConfiguration.setParent(rootContext);
@@ -103,8 +93,9 @@ private void initSpringSecurity(ServletContext servletContext, EnumSet<Dispatche
10393
public void contextDestroyed(ServletContextEvent sce) {
10494
LOGGER.info("Destroying Web application");
10595
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
106-
AnnotationConfigWebApplicationContext gwac = (AnnotationConfigWebApplicationContext) ac;
107-
gwac.close();
96+
if (ac instanceof ConfigurableApplicationContext applicationContext) {
97+
applicationContext.close();
98+
}
10899
LOGGER.debug("Web application destroyed");
109100
}
110101
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.rest.conf.common;
14+
15+
import org.apache.http.auth.AuthScope;
16+
import org.apache.http.auth.UsernamePasswordCredentials;
17+
import org.apache.http.client.CredentialsProvider;
18+
import org.apache.http.impl.client.BasicCredentialsProvider;
19+
import org.apache.http.impl.client.CloseableHttpClient;
20+
import org.apache.http.impl.client.HttpClientBuilder;
21+
import org.flowable.app.rest.util.TestServer;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.web.context.WebApplicationContext;
25+
26+
/**
27+
* @author Filip Hrisafov
28+
*/
29+
@Configuration(proxyBeanMethods = false)
30+
public class TestServerConfiguration {
31+
32+
@Bean
33+
public TestServer testServer(WebApplicationContext applicationContext) {
34+
return new TestServer(applicationContext);
35+
}
36+
37+
@Bean(destroyMethod = "close")
38+
public CloseableHttpClient httpClient() {
39+
// Create Http client for all tests
40+
CredentialsProvider provider = new BasicCredentialsProvider();
41+
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("kermit", "kermit");
42+
provider.setCredentials(AuthScope.ANY, credentials);
43+
CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
44+
return client;
45+
}
46+
47+
}

modules/flowable-app-engine-rest/src/test/java/org/flowable/app/rest/service/BaseSpringRestTestCase.java

Lines changed: 42 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.flowable.app.rest.service;
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.fail;
1617

1718
import java.io.IOException;
1819
import java.io.UncheckedIOException;
@@ -33,182 +34,82 @@
3334
import org.apache.http.HttpHeaders;
3435
import org.apache.http.HttpResponse;
3536
import org.apache.http.HttpStatus;
36-
import org.apache.http.auth.AuthScope;
37-
import org.apache.http.auth.UsernamePasswordCredentials;
38-
import org.apache.http.client.CredentialsProvider;
3937
import org.apache.http.client.methods.CloseableHttpResponse;
4038
import org.apache.http.client.methods.HttpGet;
4139
import org.apache.http.client.methods.HttpPost;
4240
import org.apache.http.client.methods.HttpUriRequest;
4341
import org.apache.http.entity.StringEntity;
44-
import org.apache.http.impl.client.BasicCredentialsProvider;
4542
import org.apache.http.impl.client.CloseableHttpClient;
46-
import org.apache.http.impl.client.HttpClientBuilder;
4743
import org.apache.http.message.BasicHeader;
48-
import org.eclipse.jetty.server.Server;
4944
import org.flowable.app.api.AppManagementService;
5045
import org.flowable.app.api.AppRepositoryService;
5146
import org.flowable.app.engine.AppEngine;
5247
import org.flowable.app.engine.AppEngineConfiguration;
53-
import org.flowable.app.engine.impl.util.CommandContextUtil;
54-
import org.flowable.app.engine.test.impl.AppTestHelper;
5548
import org.flowable.app.rest.conf.ApplicationConfiguration;
56-
import org.flowable.app.rest.util.TestServerUtil;
57-
import org.flowable.app.rest.util.TestServerUtil.TestServer;
58-
import org.flowable.common.engine.impl.db.SchemaManager;
59-
import org.flowable.common.engine.impl.interceptor.Command;
60-
import org.flowable.common.engine.impl.interceptor.CommandContext;
61-
import org.flowable.common.engine.impl.test.EnsureCleanDbUtils;
49+
import org.flowable.app.rest.util.TestServer;
50+
import org.flowable.common.engine.impl.test.EnsureCleanDb;
51+
import org.flowable.common.engine.impl.test.LoggingExtension;
6252
import org.flowable.common.rest.util.RestUrlBuilder;
6353
import org.joda.time.format.DateTimeFormatter;
6454
import org.joda.time.format.ISODateTimeFormat;
55+
import org.junit.jupiter.api.AfterEach;
56+
import org.junit.jupiter.api.BeforeEach;
57+
import org.junit.jupiter.api.extension.ExtendWith;
6558
import org.slf4j.Logger;
6659
import org.slf4j.LoggerFactory;
67-
import org.springframework.context.ApplicationContext;
60+
import org.springframework.beans.factory.annotation.Autowired;
61+
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
6862

6963
import com.fasterxml.jackson.core.JsonProcessingException;
7064
import com.fasterxml.jackson.databind.JsonNode;
7165
import com.fasterxml.jackson.databind.ObjectMapper;
7266
import com.fasterxml.jackson.databind.node.ObjectNode;
7367

74-
import junit.framework.TestCase;
75-
76-
public class BaseSpringRestTestCase extends TestCase {
68+
@SpringJUnitWebConfig(ApplicationConfiguration.class)
69+
@ExtendWith(InternalFlowableAppSpringExtension.class)
70+
@ExtendWith(LoggingExtension.class)
71+
@EnsureCleanDb(excludeTables = {
72+
"ACT_GE_PROPERTY",
73+
"ACT_ID_PROPERTY"
74+
})
75+
public class BaseSpringRestTestCase {
7776

7877
private static final Logger LOGGER = LoggerFactory.getLogger(BaseSpringRestTestCase.class);
79-
80-
protected static final String EMPTY_LINE = "\n";
81-
protected static final List<String> TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK = Arrays.asList(
82-
"ACT_GE_PROPERTY",
83-
"ACT_ID_PROPERTY",
84-
"ACT_APP_DATABASECHANGELOG",
85-
"ACT_APP_DATABASECHANGELOGLOCK",
86-
"ACT_CMMN_DATABASECHANGELOG",
87-
"ACT_CMMN_DATABASECHANGELOGLOCK",
88-
"ACT_FO_DATABASECHANGELOG",
89-
"ACT_FO_DATABASECHANGELOGLOCK",
90-
"FLW_EV_DATABASECHANGELOG",
91-
"FLW_EV_DATABASECHANGELOGLOCK"
92-
);
93-
94-
protected static String SERVER_URL_PREFIX;
95-
protected static RestUrlBuilder URL_BUILDER;
96-
97-
protected static Server server;
98-
protected static ApplicationContext appContext;
99-
protected ObjectMapper objectMapper = new ObjectMapper();
100-
101-
protected static AppEngine appEngine;
102-
103-
protected String deploymentId;
104-
protected Throwable exception;
105-
106-
protected static AppEngineConfiguration appEngineConfiguration;
107-
protected static AppRepositoryService repositoryService;
108-
protected static AppManagementService managementService;
109-
110-
protected static CloseableHttpClient client;
111-
protected static LinkedList<CloseableHttpResponse> httpResponses = new LinkedList<>();
11278

113-
protected DateFormat longDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
79+
protected String SERVER_URL_PREFIX;
80+
protected RestUrlBuilder URL_BUILDER;
11481

115-
static {
82+
@Autowired
83+
protected ObjectMapper objectMapper;
11684

117-
TestServer testServer = TestServerUtil.createAndStartServer(ApplicationConfiguration.class);
118-
server = testServer.getServer();
119-
appContext = testServer.getApplicationContext();
120-
SERVER_URL_PREFIX = testServer.getServerUrlPrefix();
121-
URL_BUILDER = RestUrlBuilder.usingBaseUrl(SERVER_URL_PREFIX);
85+
@Autowired
86+
protected AppEngine appEngine;
12287

123-
// Lookup services
124-
appEngine = appContext.getBean("appEngine", AppEngine.class);
125-
appEngineConfiguration = appContext.getBean(AppEngineConfiguration.class);
126-
repositoryService = appContext.getBean(AppRepositoryService.class);
127-
managementService = appContext.getBean(AppManagementService.class);
128-
129-
// Create http client for all tests
130-
CredentialsProvider provider = new BasicCredentialsProvider();
131-
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("kermit", "kermit");
132-
provider.setCredentials(AuthScope.ANY, credentials);
133-
client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
134-
135-
// Clean shutdown
136-
Runtime.getRuntime().addShutdownHook(new Thread() {
137-
138-
@Override
139-
public void run() {
140-
141-
if (client != null) {
142-
try {
143-
client.close();
144-
} catch (IOException e) {
145-
LOGGER.error("Could not close http client", e);
146-
}
147-
}
88+
@Autowired
89+
protected AppEngineConfiguration appEngineConfiguration;
90+
@Autowired
91+
protected AppRepositoryService repositoryService;
92+
@Autowired
93+
protected AppManagementService managementService;
14894

149-
if (server != null && server.isRunning()) {
150-
try {
151-
server.stop();
152-
} catch (Exception e) {
153-
LOGGER.error("Error stopping server", e);
154-
}
155-
}
156-
}
157-
});
158-
}
159-
160-
@Override
161-
protected void runTest() throws Throwable {
162-
if (LOGGER.isDebugEnabled()) {
163-
LOGGER.debug(EMPTY_LINE);
164-
LOGGER.debug("#### START {}.{} ###########################################################", this.getClass().getSimpleName(), getName());
165-
}
166-
167-
try {
95+
@Autowired
96+
protected CloseableHttpClient client;
97+
protected LinkedList<CloseableHttpResponse> httpResponses = new LinkedList<>();
16898

169-
super.runTest();
170-
171-
} catch (AssertionError e) {
172-
LOGGER.error(EMPTY_LINE);
173-
LOGGER.error("ASSERTION FAILED: {}", e, e);
174-
throw e;
99+
protected DateFormat longDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
175100

176-
} catch (Throwable e) {
177-
LOGGER.error(EMPTY_LINE);
178-
LOGGER.error("EXCEPTION: {}", e, e);
179-
throw e;
101+
@Autowired
102+
protected TestServer server;
180103

181-
} finally {
182-
LOGGER.debug("#### END {}.{} #############################################################", this.getClass().getSimpleName(), getName());
183-
}
104+
@BeforeEach
105+
void init() {
106+
SERVER_URL_PREFIX = server.getServerUrlPrefix();
107+
URL_BUILDER = RestUrlBuilder.usingBaseUrl(SERVER_URL_PREFIX);
184108
}
185109

186-
@Override
187-
public void runBare() throws Throwable {
188-
try {
189-
190-
deploymentId = AppTestHelper.annotationDeploymentSetUp(appEngine, getClass(), getName());
191-
192-
super.runBare();
193-
194-
} catch (AssertionError e) {
195-
LOGGER.error(EMPTY_LINE);
196-
LOGGER.error("ASSERTION FAILED: {}", e, e);
197-
exception = e;
198-
throw e;
199-
200-
} catch (Throwable e) {
201-
LOGGER.error(EMPTY_LINE);
202-
LOGGER.error("EXCEPTION: {}", e, e);
203-
exception = e;
204-
throw e;
205-
206-
} finally {
207-
AppTestHelper.annotationDeploymentTearDown(appEngine, deploymentId, getClass(), getName());
208-
assertAndEnsureCleanDb();
209-
appEngineConfiguration.getClock().reset();
210-
closeHttpConnections();
211-
}
110+
@AfterEach
111+
void cleanup() {
112+
closeHttpConnections();
212113
}
213114

214115
/**
@@ -262,21 +163,6 @@ public void closeResponse(CloseableHttpResponse response) {
262163
}
263164
}
264165

265-
/**
266-
* Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case
267-
* the DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
268-
*/
269-
protected void assertAndEnsureCleanDb() throws Throwable {
270-
EnsureCleanDbUtils.assertAndEnsureCleanDb(
271-
getName(),
272-
LOGGER,
273-
appEngineConfiguration,
274-
TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK,
275-
exception == null,
276-
appEngineConfiguration.getSchemaManagementCmd()
277-
);
278-
}
279-
280166
protected void closeHttpConnections() {
281167
for (CloseableHttpResponse response : httpResponses) {
282168
if (response != null) {

0 commit comments

Comments
 (0)