|
13 | 13 | package org.flowable.app.rest.service; |
14 | 14 |
|
15 | 15 | import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.Assertions.fail; |
16 | 17 |
|
17 | 18 | import java.io.IOException; |
18 | 19 | import java.io.UncheckedIOException; |
|
33 | 34 | import org.apache.http.HttpHeaders; |
34 | 35 | import org.apache.http.HttpResponse; |
35 | 36 | 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; |
39 | 37 | import org.apache.http.client.methods.CloseableHttpResponse; |
40 | 38 | import org.apache.http.client.methods.HttpGet; |
41 | 39 | import org.apache.http.client.methods.HttpPost; |
42 | 40 | import org.apache.http.client.methods.HttpUriRequest; |
43 | 41 | import org.apache.http.entity.StringEntity; |
44 | | -import org.apache.http.impl.client.BasicCredentialsProvider; |
45 | 42 | import org.apache.http.impl.client.CloseableHttpClient; |
46 | | -import org.apache.http.impl.client.HttpClientBuilder; |
47 | 43 | import org.apache.http.message.BasicHeader; |
48 | | -import org.eclipse.jetty.server.Server; |
49 | 44 | import org.flowable.app.api.AppManagementService; |
50 | 45 | import org.flowable.app.api.AppRepositoryService; |
51 | 46 | import org.flowable.app.engine.AppEngine; |
52 | 47 | import org.flowable.app.engine.AppEngineConfiguration; |
53 | | -import org.flowable.app.engine.impl.util.CommandContextUtil; |
54 | | -import org.flowable.app.engine.test.impl.AppTestHelper; |
55 | 48 | 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; |
62 | 52 | import org.flowable.common.rest.util.RestUrlBuilder; |
63 | 53 | import org.joda.time.format.DateTimeFormatter; |
64 | 54 | 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; |
65 | 58 | import org.slf4j.Logger; |
66 | 59 | 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; |
68 | 62 |
|
69 | 63 | import com.fasterxml.jackson.core.JsonProcessingException; |
70 | 64 | import com.fasterxml.jackson.databind.JsonNode; |
71 | 65 | import com.fasterxml.jackson.databind.ObjectMapper; |
72 | 66 | import com.fasterxml.jackson.databind.node.ObjectNode; |
73 | 67 |
|
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 { |
77 | 76 |
|
78 | 77 | 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<>(); |
112 | 78 |
|
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; |
114 | 81 |
|
115 | | - static { |
| 82 | + @Autowired |
| 83 | + protected ObjectMapper objectMapper; |
116 | 84 |
|
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; |
122 | 87 |
|
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; |
148 | 94 |
|
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<>(); |
168 | 98 |
|
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'"); |
175 | 100 |
|
176 | | - } catch (Throwable e) { |
177 | | - LOGGER.error(EMPTY_LINE); |
178 | | - LOGGER.error("EXCEPTION: {}", e, e); |
179 | | - throw e; |
| 101 | + @Autowired |
| 102 | + protected TestServer server; |
180 | 103 |
|
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); |
184 | 108 | } |
185 | 109 |
|
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(); |
212 | 113 | } |
213 | 114 |
|
214 | 115 | /** |
@@ -262,21 +163,6 @@ public void closeResponse(CloseableHttpResponse response) { |
262 | 163 | } |
263 | 164 | } |
264 | 165 |
|
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 | | - |
280 | 166 | protected void closeHttpConnections() { |
281 | 167 | for (CloseableHttpResponse response : httpResponses) { |
282 | 168 | if (response != null) { |
|
0 commit comments