Skip to content

Commit

Permalink
TestSDKRestRequest created
Browse files Browse the repository at this point in the history
Signed-off-by: Daulet <[email protected]>
  • Loading branch information
nassipkali committed Apr 5, 2023
1 parent 3ca9c0b commit 3b5cac9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 37 deletions.
49 changes: 12 additions & 37 deletions src/test/java/org/opensearch/sdk/TestBaseExtensionRestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.junit.jupiter.api.Test;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.http.HttpRequest;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.sdk.rest.SDKRestRequest;
import org.opensearch.rest.RestStatus;
import org.opensearch.test.OpenSearchTestCase;

Expand Down Expand Up @@ -58,13 +52,16 @@ public void testHandlerDefaultRoutes() {

@Test
public void testJsonErrorResponse() {
RestRequest successfulRequest = createTestRestRequest(
RestRequest successfulRequest = TestSDKRestRequest.createTestRestRequest(
Method.GET,
"foo",
"foo",
Collections.emptyMap(),
Collections.emptyMap(),
null,
new BytesArray("bar".getBytes(StandardCharsets.UTF_8)),
""
"",
null
);
ExtensionRestResponse response = handler.handleRequest(successfulRequest);
assertEquals(RestStatus.OK, response.status());
Expand All @@ -73,13 +70,16 @@ public void testJsonErrorResponse() {

@Test
public void testErrorResponseOnException() {
RestRequest exceptionalRequest = createTestRestRequest(
RestRequest exceptionalRequest = TestSDKRestRequest.createTestRestRequest(
Method.GET,
"foo",
"foo",
Collections.emptyMap(),
Collections.emptyMap(),
null,
new BytesArray("baz".getBytes(StandardCharsets.UTF_8)),
""
"",
null
);
ExtensionRestResponse response = handler.handleRequest(exceptionalRequest);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, response.status());
Expand All @@ -88,7 +88,7 @@ public void testErrorResponseOnException() {

@Test
public void testErrorResponseOnUnhandled() {
RestRequest unhandledRequestMethod = createTestRestRequest(
RestRequest unhandledRequestMethod = TestSDKRestRequest.createTestRestRequest(
Method.PUT,
"foo",
"foo",
Expand All @@ -110,7 +110,7 @@ public void testErrorResponseOnUnhandled() {
response.content().utf8ToString()
);

RestRequest unhandledRequestPath = createTestRestRequest(
RestRequest unhandledRequestPath = TestSDKRestRequest.createTestRestRequest(
Method.GET,
"foobar",
"foobar",
Expand All @@ -132,29 +132,4 @@ public void testErrorResponseOnUnhandled() {
response.content().utf8ToString()
);
}

public static RestRequest createTestRestRequest(
final Method method,
final String uri,
final String path,
final Map<String, String> params,
final Map<String, String> headers,
final XContentType xContentType,
final BytesReference content,
final String principalIdentifier,
final HttpRequest.HttpVersion httpVersion
) {
ExtensionRestRequest request = new ExtensionRestRequest(
method,
uri,
path,
params,
headers,
xContentType,
content,
principalIdentifier,
httpVersion
);
return new SDKRestRequest(null, request.params(), request.path(), request.headers(), new SDKHttpRequest(request), null);
}
}
84 changes: 84 additions & 0 deletions src/test/java/org/opensearch/sdk/TestSDKRestRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright OpenSearch Contributors
* 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.sdk;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.http.HttpRequest;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.sdk.rest.SDKHttpRequest;
import org.opensearch.sdk.rest.SDKRestRequest;
import org.opensearch.test.OpenSearchTestCase;

import static java.util.Map.entry;

public class TestSDKRestRequest extends OpenSearchTestCase {
@Test
public void TestSDKRestRequestMethods() {
RestRequest.Method expectedMethod = Method.GET;
String expectedUri = "foobar?foo=bar&baz=42";
String expectedPath = "foo";
Map<String, String> expectedParams = Map.ofEntries(entry("foo", "bar"), entry("baz", "42"));
Map<String, List<String>> expectedHeaders = Map.ofEntries(entry("foo", Arrays.asList("hello", "world")));
XContentType exptectedXContentType = null;
BytesReference expectedContent = new BytesArray("bar");

SDKRestRequest sdkRestRequest = createTestRestRequest(
expectedMethod,
expectedUri,
expectedPath,
expectedParams,
expectedHeaders,
exptectedXContentType,
expectedContent,
"",
null
);
assertEquals(expectedMethod, sdkRestRequest.method());
assertEquals(expectedUri, sdkRestRequest.uri());
assertEquals(expectedPath, sdkRestRequest.path());
assertEquals(expectedParams, sdkRestRequest.params());
assertEquals(expectedHeaders, sdkRestRequest.getHeaders());
assertEquals(expectedContent, sdkRestRequest.content());
}

public static RestRequest createTestRestRequest(
final Method method,
final String uri,
final String path,
final Map<String, String> params,
final Map<String, String> headers,
final XContentType xContentType,
final BytesReference content,
final String principalIdentifier,
final HttpRequest.HttpVersion httpVersion
) {
ExtensionRestRequest request = new ExtensionRestRequest(
method,
uri,
path,
params,
headers,
xContentType,
content,
principalIdentifier,
httpVersion
);
return new SDKRestRequest(null, request.params(), request.path(), request.headers(), new SDKHttpRequest(request), null);
}
}

0 comments on commit 3b5cac9

Please sign in to comment.