forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestExecuteOnExtensionTests.java
94 lines (74 loc) · 3.87 KB
/
RestExecuteOnExtensionTests.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.extensions.rest;
import org.opensearch.rest.RestStatus;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.io.stream.BytesStreamInput;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.test.OpenSearchTestCase;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class RestExecuteOnExtensionTests extends OpenSearchTestCase {
public void testRestExecuteOnExtensionRequest() throws Exception {
Method expectedMethod = Method.GET;
String expectedUri = "/test/uri";
RestExecuteOnExtensionRequest request = new RestExecuteOnExtensionRequest(expectedMethod, expectedUri);
assertEquals(expectedMethod, request.getMethod());
assertEquals(expectedUri, request.getUri());
try (BytesStreamOutput out = new BytesStreamOutput()) {
request.writeTo(out);
out.flush();
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) {
request = new RestExecuteOnExtensionRequest(in);
assertEquals(expectedMethod, request.getMethod());
assertEquals(expectedUri, request.getUri());
}
}
}
public void testRestExecuteOnExtensionResponse() throws Exception {
RestStatus expectedStatus = RestStatus.OK;
String expectedContentType = BytesRestResponse.TEXT_CONTENT_TYPE;
String expectedResponse = "Test response";
byte[] expectedResponseBytes = expectedResponse.getBytes(StandardCharsets.UTF_8);
RestExecuteOnExtensionResponse response = new RestExecuteOnExtensionResponse(expectedStatus, expectedResponse);
assertEquals(expectedStatus, response.getStatus());
assertEquals(expectedContentType, response.getContentType());
assertArrayEquals(expectedResponseBytes, response.getContent());
assertEquals(0, response.getHeaders().size());
String headerKey = "foo";
List<String> headerValueList = List.of("bar", "baz");
Map<String, List<String>> expectedHeaders = Map.of(headerKey, headerValueList);
response = new RestExecuteOnExtensionResponse(expectedStatus, expectedContentType, expectedResponseBytes, expectedHeaders);
assertEquals(expectedStatus, response.getStatus());
assertEquals(expectedContentType, response.getContentType());
assertArrayEquals(expectedResponseBytes, response.getContent());
assertEquals(1, expectedHeaders.keySet().size());
assertTrue(expectedHeaders.containsKey(headerKey));
List<String> fooList = expectedHeaders.get(headerKey);
assertEquals(2, fooList.size());
assertTrue(fooList.containsAll(headerValueList));
try (BytesStreamOutput out = new BytesStreamOutput()) {
response.writeTo(out);
out.flush();
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) {
response = new RestExecuteOnExtensionResponse(in);
assertEquals(expectedStatus, response.getStatus());
assertEquals(expectedContentType, response.getContentType());
assertArrayEquals(expectedResponseBytes, response.getContent());
assertEquals(1, expectedHeaders.keySet().size());
assertTrue(expectedHeaders.containsKey(headerKey));
fooList = expectedHeaders.get(headerKey);
assertEquals(2, fooList.size());
assertTrue(fooList.containsAll(headerValueList));
}
}
}
}