|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 |
|
| 5 | +import com.databricks.sdk.core.error.details.ErrorDetails; |
| 6 | +import com.databricks.sdk.core.error.details.ErrorInfo; |
| 7 | +import com.databricks.sdk.core.error.details.RequestInfo; |
| 8 | +import com.databricks.sdk.core.utils.SerDeUtils; |
5 | 9 | import com.fasterxml.jackson.core.JsonProcessingException; |
6 | 10 | import com.fasterxml.jackson.databind.ObjectMapper; |
7 | 11 | import java.util.*; |
|
10 | 14 | public class ApiErrorBodyDeserializationSuite { |
11 | 15 | @Test |
12 | 16 | void deserializeErrorResponse() throws JsonProcessingException { |
13 | | - ObjectMapper mapper = new ObjectMapper(); |
| 17 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
14 | 18 | String rawResponse = |
15 | 19 | "{\"error_code\":\"theerrorcode\",\"message\":\"themessage\",\"detail\":\"thescimdetail\",\"status\":\"thescimstatus\",\"scimType\":\"thescimtype\",\"error\":\"theerror\"}"; |
16 | | - ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class); |
17 | | - assertEquals(error.errorCode(), "theerrorcode"); |
18 | | - assertEquals(error.message(), "themessage"); |
19 | | - assertEquals(error.scimDetail(), "thescimdetail"); |
20 | | - assertEquals(error.scimStatus(), "thescimstatus"); |
21 | | - assertEquals(error.scimType(), "thescimtype"); |
22 | | - assertEquals(error.api12Error(), "theerror"); |
| 20 | + ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class); |
| 21 | + |
| 22 | + ApiErrorBody expected = |
| 23 | + ApiErrorBody.builder() |
| 24 | + .setErrorCode("theerrorcode") |
| 25 | + .setMessage("themessage") |
| 26 | + .setScimDetail("thescimdetail") |
| 27 | + .setScimStatus("thescimstatus") |
| 28 | + .setScimType("thescimtype") |
| 29 | + .setApi12Error("theerror") |
| 30 | + .build(); |
| 31 | + |
| 32 | + assertEquals(expected, actual); |
23 | 33 | } |
24 | 34 |
|
| 35 | + @Test |
25 | 36 | void deserializeErrorResponseWitIntErrorCode() throws JsonProcessingException { |
26 | | - ObjectMapper mapper = new ObjectMapper(); |
| 37 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
27 | 38 | String rawResponse = |
28 | 39 | "{\"error_code\":42,\"message\":\"themessage\",\"detail\":\"thescimdetail\",\"status\":\"thescimstatus\",\"scimType\":\"thescimtype\",\"error\":\"theerror\"}"; |
29 | | - ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class); |
30 | | - assertEquals(error.errorCode(), "42"); |
31 | | - assertEquals(error.message(), "themessage"); |
32 | | - assertEquals(error.scimDetail(), "thescimdetail"); |
33 | | - assertEquals(error.scimStatus(), "thescimstatus"); |
34 | | - assertEquals(error.scimType(), "thescimtype"); |
35 | | - assertEquals(error.api12Error(), "theerror"); |
| 40 | + ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class); |
| 41 | + |
| 42 | + ApiErrorBody expected = |
| 43 | + ApiErrorBody.builder() |
| 44 | + .setErrorCode("42") |
| 45 | + .setMessage("themessage") |
| 46 | + .setScimDetail("thescimdetail") |
| 47 | + .setScimStatus("thescimstatus") |
| 48 | + .setScimType("thescimtype") |
| 49 | + .setApi12Error("theerror") |
| 50 | + .build(); |
| 51 | + |
| 52 | + assertEquals(expected, actual); |
36 | 53 | } |
37 | 54 |
|
38 | 55 | @Test |
39 | 56 | void deserializeDetailedResponse() throws JsonProcessingException { |
40 | | - ObjectMapper mapper = new ObjectMapper(); |
| 57 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
41 | 58 | String rawResponse = |
42 | 59 | "{\"error_code\":\"theerrorcode\",\"message\":\"themessage\"," |
43 | 60 | + "\"details\":[" |
44 | 61 | + "{\"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\", \"reason\":\"detailreason\", \"domain\":\"detaildomain\",\"metadata\":{\"etag\":\"detailsetag\"}}" |
45 | 62 | + "]}"; |
46 | | - ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class); |
47 | | - Map<String, String> metadata = new HashMap<>(); |
48 | | - metadata.put("etag", "detailsetag"); |
49 | | - ErrorDetail errorDetails = error.getErrorDetailsList().get(0); |
50 | | - assertEquals(errorDetails.getType(), "type.googleapis.com/google.rpc.ErrorInfo"); |
51 | | - assertEquals(errorDetails.getReason(), "detailreason"); |
52 | | - assertEquals(errorDetails.getDomain(), "detaildomain"); |
53 | | - assertEquals(errorDetails.getMetadata(), metadata); |
| 63 | + ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class); |
| 64 | + |
| 65 | + ApiErrorBody expected = |
| 66 | + ApiErrorBody.builder() |
| 67 | + .setErrorCode("theerrorcode") |
| 68 | + .setMessage("themessage") |
| 69 | + .setErrorDetails( |
| 70 | + ErrorDetails.builder() |
| 71 | + .setErrorInfo( |
| 72 | + ErrorInfo.builder() |
| 73 | + .setReason("detailreason") |
| 74 | + .setDomain("detaildomain") |
| 75 | + .setMetadata(Collections.singletonMap("etag", "detailsetag")) |
| 76 | + .build()) |
| 77 | + .build()) |
| 78 | + .build(); |
| 79 | + |
| 80 | + assertEquals(expected, actual); |
54 | 81 | } |
55 | 82 |
|
56 | 83 | // Test that an ApiErrorBody can be deserialized, even if the response includes unexpected |
57 | 84 | // parameters. |
58 | 85 | @Test |
59 | 86 | void handleUnexpectedFieldsInErrorResponse() throws JsonProcessingException { |
60 | | - ObjectMapper mapper = new ObjectMapper(); |
| 87 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
61 | 88 | String rawResponse = |
62 | 89 | "{\"error_code\":\"theerrorcode\",\"message\":\"themessage\",\"unexpectedField\":[\"unexpected\"]}"; |
63 | | - ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class); |
64 | | - assertEquals(error.errorCode(), "theerrorcode"); |
65 | | - assertEquals(error.message(), "themessage"); |
| 90 | + ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class); |
| 91 | + |
| 92 | + ApiErrorBody expected = |
| 93 | + ApiErrorBody.builder().setErrorCode("theerrorcode").setMessage("themessage").build(); |
| 94 | + |
| 95 | + assertEquals(expected, actual); |
66 | 96 | } |
67 | 97 |
|
68 | 98 | @Test |
69 | 99 | void handleNullMetadataFieldInErrorResponse() throws JsonProcessingException { |
70 | | - ObjectMapper mapper = new ObjectMapper(); |
| 100 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
71 | 101 | String rawResponse = |
72 | 102 | "{\"error_code\":\"METASTORE_DOES_NOT_EXIST\",\"message\":\"No metastore assigned for the current workspace.\",\"details\":[{\"@type\":\"type.googleapis.com/google.rpc.RequestInfo\",\"request_id\":\"1888e822-f1b5-4996-85eb-0d2b5cc402e6\",\"serving_data\":\"\"}]}"; |
73 | | - ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class); |
| 103 | + ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class); |
| 104 | + |
| 105 | + ApiErrorBody expected = |
| 106 | + ApiErrorBody.builder() |
| 107 | + .setErrorCode("METASTORE_DOES_NOT_EXIST") |
| 108 | + .setMessage("No metastore assigned for the current workspace.") |
| 109 | + .setErrorDetails( |
| 110 | + ErrorDetails.builder() |
| 111 | + .setRequestInfo( |
| 112 | + RequestInfo.builder() |
| 113 | + .setRequestId("1888e822-f1b5-4996-85eb-0d2b5cc402e6") |
| 114 | + .setServingData("") |
| 115 | + .build()) |
| 116 | + .build()) |
| 117 | + .build(); |
74 | 118 |
|
75 | | - assertEquals(error.errorCode(), "METASTORE_DOES_NOT_EXIST"); |
76 | | - assertEquals(error.message(), "No metastore assigned for the current workspace."); |
| 119 | + assertEquals(expected, actual); |
77 | 120 | } |
78 | 121 | } |
0 commit comments