Skip to content

Commit a29a750

Browse files
committed
Updated TestHttpClient and DataMock object to accept json file instead of json string
1 parent dea3aa1 commit a29a750

File tree

6 files changed

+75
-53
lines changed

6 files changed

+75
-53
lines changed

src/test/java/io/mailtrap/api/AttachmentsImplTest.java

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.mailtrap.config.MailtrapConfig;
66
import io.mailtrap.factory.MailtrapClientFactory;
77
import io.mailtrap.model.response.AttachmentResponse;
8+
import io.mailtrap.testutils.BaseTest;
89
import io.mailtrap.testutils.DataMock;
910
import io.mailtrap.testutils.TestHttpClient;
1011
import org.junit.jupiter.api.BeforeEach;
@@ -14,12 +15,7 @@
1415

1516
import static org.junit.jupiter.api.Assertions.*;
1617

17-
class AttachmentsImplTest {
18-
19-
private final Long accountId = 1L;
20-
private final int inboxId = 2;
21-
private final Long messageId = 3L;
22-
private final Long attachmentId = 4L;
18+
class AttachmentsImplTest extends BaseTest {
2319

2420
private Attachments api;
2521

@@ -30,44 +26,13 @@ public void init() {
3026
Constants.GENERAL_HOST + "/api/accounts/" + accountId + "/inboxes/" + inboxId + "/messages/" + messageId + "/attachments",
3127
"GET",
3228
null,
33-
"""
34-
[
35-
{
36-
"id": 4,
37-
"message_id": 3,
38-
"filename": "test.csv",
39-
"attachment_type": "inline",
40-
"content_type": "plain/text",
41-
"content_id": null,
42-
"transfer_encoding": null,
43-
"attachment_size": 1,
44-
"created_at": "2024-07-20T19:25:54.827Z",
45-
"updated_at": "2024-07-20T19:25:54.827Z",
46-
"attachment_human_size": "1 Byte",
47-
"download_path": "/api/accounts/1/inboxes/2/messages/3/attachments/4/download"
48-
}
49-
]"""
29+
"api/attachments/getAttachmentsResponse.json"
5030
),
5131
DataMock.build(
5232
Constants.GENERAL_HOST + "/api/accounts/" + accountId + "/inboxes/" + inboxId + "/messages/" + messageId + "/attachments/" + attachmentId,
5333
"GET",
5434
null,
55-
"""
56-
{
57-
"id": 4,
58-
"message_id": 3,
59-
"filename": "test.csv",
60-
"attachment_type": "inline",
61-
"content_type": "plain/text",
62-
"content_id": null,
63-
"transfer_encoding": null,
64-
"attachment_size": 1,
65-
"created_at": "2024-07-20T19:25:54.827Z",
66-
"updated_at": "2024-07-20T19:25:54.827Z",
67-
"attachment_human_size": "1 Byte",
68-
"download_path": "/api/accounts/1/inboxes/2/messages/3/attachments/4/download"
69-
}
70-
"""
35+
"api/attachments/getAttachmentResponse.json"
7136
)
7237
));
7338

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.mailtrap.testutils;
2+
3+
public class BaseTest {
4+
protected final Long accountId = 1L;
5+
protected final int inboxId = 2;
6+
protected final Long messageId = 3L;
7+
protected final Long attachmentId = 4L;
8+
9+
}

src/test/java/io/mailtrap/testutils/DataMock.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ public class DataMock {
1515
@NonNull
1616
private final String methodName;
1717

18-
private final String requestJson;
19-
private final String responseJson;
18+
private final String requestFile;
19+
private final String responseFile;
2020

2121
@NonNull
2222
private final Map<String, ?> queryParams;
2323

24-
public static DataMock build(String url, String methodName, String requestJson, String responseJson) {
24+
public static DataMock build(String url, String methodName, String requestFile, String responseFile) {
2525
return new DataMock(
2626
url,
2727
methodName,
28-
requestJson,
29-
responseJson,
28+
requestFile,
29+
responseFile,
3030
Collections.emptyMap()
3131
);
3232
}
3333

34-
public static DataMock build(String url, String methodName, String requestJson, String responseJson, Map<String, ?> queryParams) {
34+
public static DataMock build(String url, String methodName, String requestFile, String responseFile, Map<String, ?> queryParams) {
3535
return new DataMock(
3636
url,
3737
methodName,
38-
requestJson,
39-
responseJson,
38+
requestFile,
39+
responseFile,
4040
queryParams
4141
);
4242
}
43-
}
43+
}

src/test/java/io/mailtrap/testutils/TestHttpClient.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import io.mailtrap.model.AbstractModel;
1010
import org.apache.commons.lang3.StringUtils;
1111

12+
import java.io.BufferedReader;
13+
import java.io.InputStream;
14+
import java.io.InputStreamReader;
1215
import java.util.List;
1316
import java.util.Map;
1417
import java.util.stream.Collectors;
@@ -91,11 +94,19 @@ private <T, V extends AbstractModel> T requestInternal(String url, String method
9194

9295
// request
9396
if (requestBody != null) {
94-
if (StringUtils.isEmpty(dataMock.getRequestJson())) {
97+
if (StringUtils.isEmpty(dataMock.getRequestFile())) {
9598
throw new AssertionError("No mock request body provided: " + requestIdentifier);
9699
}
97100

98-
boolean sameRequests = dataMock.getRequestJson().equals(requestBody.toJson());
101+
InputStream requestInputStream = this.getClass().getClassLoader().getResourceAsStream(dataMock.getRequestFile());
102+
103+
if (requestInputStream == null) {
104+
throw new AssertionError(String.format("Failed to load request mock payload %s for request %s", dataMock.getRequestFile(), requestIdentifier));
105+
}
106+
107+
String requestPayloadMock = new BufferedReader(new InputStreamReader(requestInputStream)).lines().collect(Collectors.joining("\n"));
108+
109+
boolean sameRequests = Mapper.get().readTree(requestPayloadMock).equals(Mapper.get().readTree(requestBody.toJson()));
99110

100111
if (!sameRequests && i == dataMocks.size() - 1) {
101112
throw new AssertionError("No match for request payload " + requestIdentifier);
@@ -113,14 +124,21 @@ private <T, V extends AbstractModel> T requestInternal(String url, String method
113124
if (Void.class.equals(responseClassType)) {
114125
return null;
115126
}
116-
if (StringUtils.isEmpty(dataMock.getResponseJson())) {
127+
if (StringUtils.isEmpty(dataMock.getResponseFile())) {
117128
throw new AssertionError("No mock response body provided: " + requestIdentifier);
118129
}
119130

131+
InputStream responseInputStream = this.getClass().getClassLoader().getResourceAsStream(dataMock.getResponseFile());
132+
if (responseInputStream == null) {
133+
throw new AssertionError("Failed to load response mock payload " + dataMock.getResponseFile() + " for request" + requestIdentifier);
134+
}
135+
String responsePayloadMock = new BufferedReader(new InputStreamReader(responseInputStream)).lines().collect(Collectors.joining("\n"));
136+
137+
120138
if (responseClassType != null) {
121-
return Mapper.get().readValue(dataMock.getResponseJson(), responseClassType);
139+
return Mapper.get().readValue(responsePayloadMock, responseClassType);
122140
} else if (responseJavaType != null) {
123-
return Mapper.get().readValue(dataMock.getResponseJson(), responseJavaType);
141+
return Mapper.get().readValue(responsePayloadMock, responseJavaType);
124142
} else {
125143
throw new IllegalArgumentException("Both responseType and typeReference are null");
126144
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": 4,
3+
"message_id": 3,
4+
"filename": "test.csv",
5+
"attachment_type": "inline",
6+
"content_type": "plain/text",
7+
"content_id": null,
8+
"transfer_encoding": null,
9+
"attachment_size": 1,
10+
"created_at": "2024-07-20T19:25:54.827Z",
11+
"updated_at": "2024-07-20T19:25:54.827Z",
12+
"attachment_human_size": "1 Byte",
13+
"download_path": "/api/accounts/1/inboxes/2/messages/3/attachments/4/download"
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"id": 4,
4+
"message_id": 3,
5+
"filename": "test.csv",
6+
"attachment_type": "inline",
7+
"content_type": "plain/text",
8+
"content_id": null,
9+
"transfer_encoding": null,
10+
"attachment_size": 1,
11+
"created_at": "2024-07-20T19:25:54.827Z",
12+
"updated_at": "2024-07-20T19:25:54.827Z",
13+
"attachment_human_size": "1 Byte",
14+
"download_path": "/api/accounts/1/inboxes/2/messages/3/attachments/4/download"
15+
}
16+
]

0 commit comments

Comments
 (0)