-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding unit testing for response interceptor
- Loading branch information
1 parent
60469b8
commit 15a2627
Showing
5 changed files
with
186 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,16 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.function.UnaryOperator; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
@@ -25,6 +33,8 @@ interface HttpProcessorTest { | |
|
||
HttpProcessor getHttpProcessor(); | ||
|
||
HttpProcessor getHttpProcessor(UnaryOperator<HttpResponseData> responseInterceptor); | ||
|
||
void setMocksForString(SyncType syncType, String result) throws IOException, InterruptedException; | ||
|
||
void setMocksForBinary(SyncType syncType, InputStream result) throws IOException, InterruptedException; | ||
|
@@ -39,6 +49,8 @@ interface HttpProcessorTest { | |
|
||
void setMocksForStreamWithError(Stream<String> result) throws IOException, URISyntaxException; | ||
|
||
void setMocksForInterceptor(String result) throws IOException, InterruptedException, URISyntaxException; | ||
|
||
void testShutdown(); | ||
|
||
@BeforeAll | ||
|
@@ -336,6 +348,34 @@ default void shouldThrownExceptionWhenCallingStreamingMethodAndServerRespondsWit | |
assertTrue(nestedException.responseInfo().get().getData().contains("The resource does not exist")); | ||
} | ||
|
||
@Test | ||
default void shouldModifyAsyncResponseWhenPassingInterceptor() | ||
throws IOException, InterruptedException, URISyntaxException { | ||
var originalJsonResponse = Files.readString(Path.of("src/test/resources/users.json"), StandardCharsets.UTF_8); | ||
setMocksForInterceptor(originalJsonResponse); | ||
|
||
var service = getHttpProcessor(response -> { | ||
var body = response.getBody(); | ||
var newBody = transformUsers(body); | ||
response.setBody(newBody); | ||
return response; | ||
}).createProxy(ITest.UserService.class); | ||
var actualUserList = service.getAsyncUsers().join(); | ||
var actualUser = actualUserList.get(0); | ||
var expectedUser = ITest.User.builder() | ||
.id(1) | ||
.name("Leanne Graham") | ||
.username("Bret") | ||
.email("[email protected]") | ||
.address("Kulas Light, Apt. 556, Gwenborough") | ||
.phone("1-770-736-8031 x56442") | ||
.website("hildegard.org") | ||
.company("Romaguera-Crona") | ||
.build(); | ||
|
||
assertEquals(expectedUser, actualUser); | ||
} | ||
|
||
@Test | ||
default void shouldExecuteDefaultMethodWhenItIsCalled() { | ||
var service = getHttpProcessor().createProxy(ITest.AsyncService.class); | ||
|
@@ -350,4 +390,50 @@ default void shouldShutdownWithoutExceptions() { | |
testShutdown(); | ||
} | ||
|
||
private String transformUsers(String jsonInput) { | ||
List<String> flatUsers = new ArrayList<>(); | ||
String patternStr = "\"id\":\\s*(\\d+).*?" + // id | ||
"\"name\":\\s*\"([^\"]+)\".*?" + // name | ||
"\"username\":\\s*\"([^\"]+)\".*?" + // username | ||
"\"email\":\\s*\"([^\"]+)\".*?" + // email | ||
"\"street\":\\s*\"([^\"]+)\".*?" + // street | ||
"\"suite\":\\s*\"([^\"]+)\".*?" + // suite | ||
"\"city\":\\s*\"([^\"]+)\".*?" + // city | ||
"\"phone\":\\s*\"([^\"]+)\".*?" + // phone | ||
"\"website\":\\s*\"([^\"]+)\".*?" + // website | ||
"\"company\":\\s*\\{\\s*\"name\":\\s*\"([^\"]+)\""; // company name | ||
Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL); | ||
Matcher matcher = pattern.matcher(jsonInput); | ||
while (matcher.find()) { | ||
String flatUser = String.format( | ||
"{\n" + | ||
" \"id\": %s,\n" + | ||
" \"name\": \"%s\",\n" + | ||
" \"username\": \"%s\",\n" + | ||
" \"email\": \"%s\",\n" + | ||
" \"address\": \"%s, %s, %s\",\n" + | ||
" \"phone\": \"%s\",\n" + | ||
" \"website\": \"%s\",\n" + | ||
" \"company\": \"%s\"\n" + | ||
"}", | ||
matcher.group(1), // id | ||
matcher.group(2), // name | ||
matcher.group(3), // username | ||
matcher.group(4), // email | ||
matcher.group(5), // street | ||
matcher.group(6), // suite | ||
matcher.group(7), // city | ||
matcher.group(8), // phone | ||
matcher.group(9), // website | ||
matcher.group(10) // company name | ||
); | ||
flatUsers.add(flatUser); | ||
} | ||
if (flatUsers.isEmpty()) { | ||
System.err.println("No matches found in input: " + jsonInput); | ||
return "[]"; | ||
} | ||
return "[\n " + String.join(",\n ", flatUsers) + "\n]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"id":1,"name":"Leanne Graham","username":"Bret","email":"[email protected]","address":{"street":"Kulas Light","suite":"Apt. 556","city":"Gwenborough","zipcode":"92998-3874","geo":{"lat":"-37.3159","lng":"81.1496"}},"phone":"1-770-736-8031 x56442","website":"hildegard.org","company":{"name":"Romaguera-Crona","catchPhrase":"Multi-layered client-server neural-net","bs":"harness real-time e-markets"}},{"id":2,"name":"Ervin Howell","username":"Antonette","email":"[email protected]","address":{"street":"Victor Plains","suite":"Suite 879","city":"Wisokyburgh","zipcode":"90566-7771","geo":{"lat":"-43.9509","lng":"-34.4618"}},"phone":"010-692-6593 x09125","website":"anastasia.net","company":{"name":"Deckow-Crist","catchPhrase":"Proactive didactic contingency","bs":"synergize scalable supply-chains"}}] |