-
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
149 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
94 changes: 94 additions & 0 deletions
94
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,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. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
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); | ||
|
||
} | ||
|
||
} |