Skip to content

Commit 64ac551

Browse files
authored
Merge pull request #197 from Sundsvallskommun/uf-6182-add-support-for-multipartbody
UF-6182: Add support for multipartbody.
2 parents d217c01 + 4399068 commit 64ac551

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

dept44-starter-test/src/main/java/se/sundsvall/dept44/test/AbstractAppTest.java

+45-18
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public abstract class AbstractAppTest {
8989
private HttpHeaders expectedResponseHeaders;
9090
private Map<String, String> headerValues;
9191
private String testDirectoryPath;
92+
private String testCaseName;
9293

9394
@Autowired
9495
protected TestRestTemplate restTemplate;
@@ -114,32 +115,39 @@ public AbstractAppTest reset() {
114115
this.expectedResponseHeaders = null;
115116
this.headerValues = null;
116117
this.testDirectoryPath = null;
118+
this.testCaseName = null;
117119

118120
return this;
119121
}
120122

121-
public AbstractAppTest setupCall() {
122-
123-
reset();
124-
initializeJsonAssert();
123+
public AbstractAppTest setupPaths() {
125124

126125
// Fetch test case name.
127-
final var testCaseName = getTestMethodName();
126+
this.testCaseName = getTestMethodName();
128127

129128
this.mappingPath = wiremock.getOptions().filesRoot().getPath();
130129
if (!this.mappingPath.endsWith("/")) {
131130
this.mappingPath += "/";
132131
}
133132

133+
this.testDirectoryPath = "classpath:" + this.mappingPath + FILES_DIR + getTestMethodName() + System.getProperty("file.separator");
134+
135+
return this;
136+
}
137+
138+
public AbstractAppTest setupCall() {
139+
140+
reset();
141+
initializeJsonAssert();
142+
setupPaths();
143+
134144
this.wiremock.loadMappingsUsing(new JsonFileMappingsSource(
135145
new ClasspathFileSource(this.mappingPath + FILES_DIR + COMMON_MAPPING_DIR + MAPPING_DIRECTORY)));
136-
if (nonNull(testCaseName)) {
146+
if (nonNull(this.testCaseName)) {
137147
this.wiremock.loadMappingsUsing(new JsonFileMappingsSource(
138-
new ClasspathFileSource(this.mappingPath + FILES_DIR + testCaseName + MAPPING_DIRECTORY)));
148+
new ClasspathFileSource(this.mappingPath + FILES_DIR + this.testCaseName + MAPPING_DIRECTORY)));
139149
}
140150

141-
this.testDirectoryPath = "classpath:" + this.mappingPath + FILES_DIR + getTestMethodName() + System.getProperty("file.separator");
142-
143151
return this;
144152
}
145153

@@ -274,6 +282,19 @@ public AbstractAppTest withRequest(final String request) {
274282
return this;
275283
}
276284

285+
/**
286+
* Method takes a file that will be added to the multipart body.
287+
*
288+
* @param fileName to be added to the request as a multipart, the method will look for the file in the
289+
* current
290+
* test-case directory.
291+
* @return
292+
* @throws FileNotFoundException
293+
*/
294+
public AbstractAppTest withRequestFile(final String parameterName, final String fileName) throws FileNotFoundException {
295+
return withRequestFile(parameterName, getFile(this.testDirectoryPath + fileName));
296+
}
297+
277298
/**
278299
* Method takes a file that will be added to the multipart body.
279300
*
@@ -285,23 +306,25 @@ public AbstractAppTest withRequestFile(final String parameterName, final File fi
285306
this.multipartBody = new LinkedMultiValueMap<>();
286307
}
287308

288-
multipartBody.add(parameterName, new FileSystemResource(file));
309+
this.multipartBody.add(parameterName, new FileSystemResource(file));
289310

290311
return this;
291312
}
292313

293314
/**
294-
* Method takes a file that will be added to the multipart body.
315+
* Method takes a MultiValueMap that will set the multipart body.
316+
* If you have added any parts to the multipartbody before a call to this method, these parts will be lost.
295317
*
296-
* @param fileName to be added to the request as a multipart, the method will look for the file in the
297-
* current
298-
* test-case directory.
318+
* @see org.springframework.http.client.MultipartBodyBuilder for instruction on how to create a
319+
* suitable MultiValueMap.
320+
*
321+
* @param multiPartBody the multipartbody (as a MultiValueMap).
299322
* @return
300-
* @throws FileNotFoundException
301323
*/
302-
public AbstractAppTest withRequestFile(final String parameterName, final String fileName) throws FileNotFoundException {
303-
final var file = getFile(this.testDirectoryPath + fileName);
304-
return withRequestFile(parameterName, file);
324+
@SuppressWarnings("unchecked")
325+
public AbstractAppTest withRequest(final MultiValueMap<?, ?> multiPartBody) {
326+
this.multipartBody = (MultiValueMap<String, Object>) multiPartBody;
327+
return this;
305328
}
306329

307330
/**
@@ -435,6 +458,10 @@ public AbstractAppTest andVerifyThat(final Callable<Boolean> conditionIsMet) {
435458
return this;
436459
}
437460

461+
public String getTestDirectoryPath() {
462+
return this.testDirectoryPath;
463+
}
464+
438465
private HttpEntity<Object> restTemplateRequest(final MediaType mediaType, Object body) {
439466
final var httpHeaders = new HttpHeaders();
440467
httpHeaders.setContentType(mediaType);

0 commit comments

Comments
 (0)