-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTokensWireTest.java
More file actions
111 lines (103 loc) · 5.42 KB
/
TokensWireTest.java
File metadata and controls
111 lines (103 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.pipedream.api;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pipedream.api.core.ObjectMappers;
import com.pipedream.api.resources.tokens.requests.TokensValidateRequest;
import com.pipedream.api.types.ValidateTokenResponse;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TokensWireTest {
private MockWebServer server;
private BaseClient client;
private ObjectMapper objectMapper = ObjectMappers.JSON_MAPPER;
@BeforeEach
public void setup() throws Exception {
server = new MockWebServer();
server.start();
client = BaseClient.builder()
.url(server.url("/").toString())
.token("oauth-test-token")
.build();
}
@AfterEach
public void teardown() throws Exception {
server.shutdown();
}
@Test
public void testValidate() throws Exception {
server.enqueue(
new MockResponse()
.setResponseCode(200)
.setBody(
"{\"app\":{\"id\":\"id\",\"name_slug\":\"name_slug\",\"name\":\"name\",\"auth_type\":\"keys\",\"description\":\"description\",\"img_src\":\"img_src\",\"custom_fields_json\":\"custom_fields_json\",\"categories\":[\"categories\"],\"featured_weight\":1.1},\"error\":\"error\",\"error_redirect_uri\":\"error_redirect_uri\",\"oauth_app_id\":\"oauth_app_id\",\"project_app_name\":\"project_app_name\",\"project_environment\":\"project_environment\",\"project_id\":\"project_id\",\"project_support_email\":\"project_support_email\",\"success\":true,\"success_redirect_uri\":\"success_redirect_uri\"}"));
ValidateTokenResponse response = client.tokens()
.validate(
"ctok",
TokensValidateRequest.builder()
.appId("app_id")
.oauthAppId("oauth_app_id")
.build());
RecordedRequest request = server.takeRequest();
Assertions.assertNotNull(request);
Assertions.assertEquals("GET", request.getMethod());
// Validate response body
Assertions.assertNotNull(response, "Response should not be null");
String actualResponseJson = objectMapper.writeValueAsString(response);
String expectedResponseBody = ""
+ "{\n"
+ " \"app\": {\n"
+ " \"id\": \"id\",\n"
+ " \"name_slug\": \"name_slug\",\n"
+ " \"name\": \"name\",\n"
+ " \"auth_type\": \"keys\",\n"
+ " \"description\": \"description\",\n"
+ " \"img_src\": \"img_src\",\n"
+ " \"custom_fields_json\": \"custom_fields_json\",\n"
+ " \"categories\": [\n"
+ " \"categories\"\n"
+ " ],\n"
+ " \"featured_weight\": 1.1\n"
+ " },\n"
+ " \"error\": \"error\",\n"
+ " \"error_redirect_uri\": \"error_redirect_uri\",\n"
+ " \"oauth_app_id\": \"oauth_app_id\",\n"
+ " \"project_app_name\": \"project_app_name\",\n"
+ " \"project_environment\": \"project_environment\",\n"
+ " \"project_id\": \"project_id\",\n"
+ " \"project_support_email\": \"project_support_email\",\n"
+ " \"success\": true,\n"
+ " \"success_redirect_uri\": \"success_redirect_uri\"\n"
+ "}";
JsonNode actualResponseNode = objectMapper.readTree(actualResponseJson);
JsonNode expectedResponseNode = objectMapper.readTree(expectedResponseBody);
Assertions.assertEquals(
expectedResponseNode, actualResponseNode, "Response body structure does not match expected");
if (actualResponseNode.has("type") || actualResponseNode.has("_type") || actualResponseNode.has("kind")) {
String discriminator = null;
if (actualResponseNode.has("type"))
discriminator = actualResponseNode.get("type").asText();
else if (actualResponseNode.has("_type"))
discriminator = actualResponseNode.get("_type").asText();
else if (actualResponseNode.has("kind"))
discriminator = actualResponseNode.get("kind").asText();
Assertions.assertNotNull(discriminator, "Union type should have a discriminator field");
Assertions.assertFalse(discriminator.isEmpty(), "Union discriminator should not be empty");
}
if (!actualResponseNode.isNull()) {
Assertions.assertTrue(
actualResponseNode.isObject() || actualResponseNode.isArray() || actualResponseNode.isValueNode(),
"response should be a valid JSON value");
}
if (actualResponseNode.isArray()) {
Assertions.assertTrue(actualResponseNode.size() >= 0, "Array should have valid size");
}
if (actualResponseNode.isObject()) {
Assertions.assertTrue(actualResponseNode.size() >= 0, "Object should have valid field count");
}
}
}