-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
588 - [FEATURE] serialize requests to JSON
Signed-off-by: Jai2305 <[email protected]>
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpSerializableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.client.opensearch.json; | ||
|
||
import java.util.Collections; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.opensearch.client.opensearch._types.FieldValue; | ||
import org.opensearch.client.opensearch._types.Result; | ||
import org.opensearch.client.opensearch.core.IndexResponse; | ||
import org.opensearch.client.opensearch.core.SearchRequest; | ||
|
||
public class JsonpSerializableTest extends Assert { | ||
|
||
// Test IndexResponse which extends WriteResponseBase which implements JsonpSerializable | ||
@Test | ||
public void testIndexResponse() { | ||
|
||
String expectedStringValue = | ||
"{\"_id\":\"id\",\"_index\":\"index\",\"_primary_term\":1,\"result\":\"created\",\"_seq_no\":2,\"_shards\":{\"failed\":1.0,\"successful\":1.0,\"total\":3.0,\"failures\":[{\"index\":\"index\",\"node\":\"node\",\"reason\":{\"type\":\"query_shard_exception\",\"reason\":\"Failed to create query.\"},\"shard\":1,\"status\":\"Failed\"}],\"skipped\":1.0},\"_version\":3}"; | ||
IndexResponse indexResponse = IndexResponse.of( | ||
response -> response.result(Result.Created) | ||
.index("index") | ||
.id("id") | ||
.primaryTerm(1) | ||
.seqNo(2) | ||
.version(3) | ||
.shards( | ||
shardStats -> shardStats.total(3) | ||
.successful(1) | ||
.skipped(1) | ||
.failed(1) | ||
.failures( | ||
shardFailure -> shardFailure.index("index") | ||
.node("node") | ||
.shard(1) | ||
.status("Failed") | ||
.reason(cause -> cause.type("query_shard_exception").reason("Failed to create query.")) | ||
) | ||
) | ||
); | ||
|
||
String indexResponseString = indexResponse.writeValueAsString(); | ||
assertEquals(expectedStringValue, indexResponseString); | ||
} | ||
|
||
// Test SearchRequest which implements JsonpSerializable | ||
@Test | ||
public void testSearchResponse() { | ||
|
||
String expectedStringValue = | ||
"{\"aggregations\":{},\"query\":{\"match\":{\"name\":{\"query\":\"OpenSearch\"}}},\"terminate_after\":5}"; | ||
SearchRequest searchRequest = SearchRequest.of( | ||
request -> request.index("index1", "index2") | ||
.aggregations(Collections.emptyMap()) | ||
.terminateAfter(5L) | ||
.query(q -> q.match(t -> t.field("name").query(FieldValue.of("OpenSearch")))) | ||
); | ||
String searchRequestString = searchRequest.writeValueAsString(); | ||
assertEquals(expectedStringValue, searchRequestString); | ||
|
||
} | ||
|
||
} |