|
| 1 | +package com.pipedream.api; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.pipedream.api.core.ObjectMappers; |
| 6 | +import com.pipedream.api.resources.oauthtokens.requests.CreateOAuthTokenOpts; |
| 7 | +import com.pipedream.api.types.CreateOAuthTokenResponse; |
| 8 | +import okhttp3.mockwebserver.MockResponse; |
| 9 | +import okhttp3.mockwebserver.MockWebServer; |
| 10 | +import okhttp3.mockwebserver.RecordedRequest; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +public class OauthTokensWireTest { |
| 17 | + private MockWebServer server; |
| 18 | + private BaseClient client; |
| 19 | + private ObjectMapper objectMapper = ObjectMappers.JSON_MAPPER; |
| 20 | + |
| 21 | + @BeforeEach |
| 22 | + public void setup() throws Exception { |
| 23 | + server = new MockWebServer(); |
| 24 | + server.start(); |
| 25 | + client = BaseClient.builder().url(server.url("/").toString()).build(); |
| 26 | + } |
| 27 | + |
| 28 | + @AfterEach |
| 29 | + public void teardown() throws Exception { |
| 30 | + server.shutdown(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCreate() throws Exception { |
| 35 | + server.enqueue(new MockResponse() |
| 36 | + .setResponseCode(200) |
| 37 | + .setBody("{\"access_token\":\"access_token\",\"token_type\":\"token_type\",\"expires_in\":1}")); |
| 38 | + CreateOAuthTokenResponse response = client.oauthTokens() |
| 39 | + .create(CreateOAuthTokenOpts.builder() |
| 40 | + .clientId("client_id") |
| 41 | + .clientSecret("client_secret") |
| 42 | + .build()); |
| 43 | + RecordedRequest request = server.takeRequest(); |
| 44 | + Assertions.assertNotNull(request); |
| 45 | + Assertions.assertEquals("POST", request.getMethod()); |
| 46 | + // Validate request body |
| 47 | + String actualRequestBody = request.getBody().readUtf8(); |
| 48 | + String expectedRequestBody = "" |
| 49 | + + "{\n" |
| 50 | + + " \"grant_type\": \"client_credentials\",\n" |
| 51 | + + " \"client_id\": \"client_id\",\n" |
| 52 | + + " \"client_secret\": \"client_secret\"\n" |
| 53 | + + "}"; |
| 54 | + JsonNode actualJson = objectMapper.readTree(actualRequestBody); |
| 55 | + JsonNode expectedJson = objectMapper.readTree(expectedRequestBody); |
| 56 | + Assertions.assertEquals(expectedJson, actualJson, "Request body structure does not match expected"); |
| 57 | + if (actualJson.has("type") || actualJson.has("_type") || actualJson.has("kind")) { |
| 58 | + String discriminator = null; |
| 59 | + if (actualJson.has("type")) discriminator = actualJson.get("type").asText(); |
| 60 | + else if (actualJson.has("_type")) |
| 61 | + discriminator = actualJson.get("_type").asText(); |
| 62 | + else if (actualJson.has("kind")) |
| 63 | + discriminator = actualJson.get("kind").asText(); |
| 64 | + Assertions.assertNotNull(discriminator, "Union type should have a discriminator field"); |
| 65 | + Assertions.assertFalse(discriminator.isEmpty(), "Union discriminator should not be empty"); |
| 66 | + } |
| 67 | + |
| 68 | + if (!actualJson.isNull()) { |
| 69 | + Assertions.assertTrue( |
| 70 | + actualJson.isObject() || actualJson.isArray() || actualJson.isValueNode(), |
| 71 | + "request should be a valid JSON value"); |
| 72 | + } |
| 73 | + |
| 74 | + if (actualJson.isArray()) { |
| 75 | + Assertions.assertTrue(actualJson.size() >= 0, "Array should have valid size"); |
| 76 | + } |
| 77 | + if (actualJson.isObject()) { |
| 78 | + Assertions.assertTrue(actualJson.size() >= 0, "Object should have valid field count"); |
| 79 | + } |
| 80 | + |
| 81 | + // Validate response body |
| 82 | + Assertions.assertNotNull(response, "Response should not be null"); |
| 83 | + String actualResponseJson = objectMapper.writeValueAsString(response); |
| 84 | + String expectedResponseBody = "" |
| 85 | + + "{\n" |
| 86 | + + " \"access_token\": \"access_token\",\n" |
| 87 | + + " \"token_type\": \"token_type\",\n" |
| 88 | + + " \"expires_in\": 1\n" |
| 89 | + + "}"; |
| 90 | + JsonNode actualResponseNode = objectMapper.readTree(actualResponseJson); |
| 91 | + JsonNode expectedResponseNode = objectMapper.readTree(expectedResponseBody); |
| 92 | + Assertions.assertEquals( |
| 93 | + expectedResponseNode, actualResponseNode, "Response body structure does not match expected"); |
| 94 | + if (actualResponseNode.has("type") || actualResponseNode.has("_type") || actualResponseNode.has("kind")) { |
| 95 | + String discriminator = null; |
| 96 | + if (actualResponseNode.has("type")) |
| 97 | + discriminator = actualResponseNode.get("type").asText(); |
| 98 | + else if (actualResponseNode.has("_type")) |
| 99 | + discriminator = actualResponseNode.get("_type").asText(); |
| 100 | + else if (actualResponseNode.has("kind")) |
| 101 | + discriminator = actualResponseNode.get("kind").asText(); |
| 102 | + Assertions.assertNotNull(discriminator, "Union type should have a discriminator field"); |
| 103 | + Assertions.assertFalse(discriminator.isEmpty(), "Union discriminator should not be empty"); |
| 104 | + } |
| 105 | + |
| 106 | + if (!actualResponseNode.isNull()) { |
| 107 | + Assertions.assertTrue( |
| 108 | + actualResponseNode.isObject() || actualResponseNode.isArray() || actualResponseNode.isValueNode(), |
| 109 | + "response should be a valid JSON value"); |
| 110 | + } |
| 111 | + |
| 112 | + if (actualResponseNode.isArray()) { |
| 113 | + Assertions.assertTrue(actualResponseNode.size() >= 0, "Array should have valid size"); |
| 114 | + } |
| 115 | + if (actualResponseNode.isObject()) { |
| 116 | + Assertions.assertTrue(actualResponseNode.size() >= 0, "Object should have valid field count"); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments