Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions src/test/java/org/codelibs/fesen/client/action/HttpActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2012-2025 CodeLibs Project and the Others.
*
* Licensed 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.
*/
package org.codelibs.fesen.client.action;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.opensearch.OpenSearchException;
import org.opensearch.action.support.ActiveShardCount;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaType;

class HttpActionTest {

private final HttpAction action = new HttpAction(null);

@Test
void test_fromMediaTypeOrFormat_json() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("application/json");
assertEquals(XContentType.JSON, result);
}

@Test
void test_fromMediaTypeOrFormat_jsonWithCharset() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("application/json; charset=UTF-8");
assertEquals(XContentType.JSON, result);
}

@Test
void test_fromMediaTypeOrFormat_yaml() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("application/yaml");
assertEquals(XContentType.YAML, result);
}

@Test
void test_fromMediaTypeOrFormat_cbor() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("application/cbor");
assertEquals(XContentType.CBOR, result);
}

@Test
void test_fromMediaTypeOrFormat_smile() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("application/smile");
assertEquals(XContentType.SMILE, result);
}

@Test
void test_fromMediaTypeOrFormat_null_defaultsToJson() {
final MediaType result = HttpAction.fromMediaTypeOrFormat(null);
assertEquals(XContentType.JSON, result);
}

@Test
void test_fromMediaTypeOrFormat_unknown_defaultsToJson() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("text/plain");
assertEquals(XContentType.JSON, result);
}

@Test
void test_fromMediaTypeOrFormat_subtypeOnly() {
final MediaType result = HttpAction.fromMediaTypeOrFormat("json");
assertEquals(XContentType.JSON, result);
}

@Test
void test_getActiveShardsCountValue_all() throws IOException {
final int value = action.getActiveShardsCountValue(ActiveShardCount.ALL);
assertEquals(-1, value);
}

@Test
void test_getActiveShardsCountValue_default() throws IOException {
final int value = action.getActiveShardsCountValue(ActiveShardCount.DEFAULT);
assertEquals(-2, value);
}

@Test
void test_getActiveShardsCountValue_one() throws IOException {
final int value = action.getActiveShardsCountValue(ActiveShardCount.ONE);
assertEquals(1, value);
}

@Test
void test_getActiveShardsCountValue_none() throws IOException {
final int value = action.getActiveShardsCountValue(ActiveShardCount.NONE);
assertEquals(0, value);
}

@Test
void test_getActiveShardsCountValue_customValue() throws IOException {
final int value = action.getActiveShardsCountValue(ActiveShardCount.from(3));
assertEquals(3, value);
}

@Test
void test_unwrapOpenSearchException_withOpenSearchCause() {
final OpenSearchException cause = new OpenSearchException("inner error");
final Exception wrapper = new RuntimeException("outer", cause);
final boolean[] called = { false };
action.unwrapOpenSearchException(new org.opensearch.core.action.ActionListener<>() {
@Override
public void onResponse(final Object o) {
}

@Override
public void onFailure(final Exception e) {
called[0] = true;
assertEquals(cause, e);
}
}, wrapper);
assertEquals(true, called[0]);
}

@Test
void test_unwrapOpenSearchException_withoutOpenSearchCause() {
final RuntimeException original = new RuntimeException("direct error");
final boolean[] called = { false };
action.unwrapOpenSearchException(new org.opensearch.core.action.ActionListener<>() {
@Override
public void onResponse(final Object o) {
}

@Override
public void onFailure(final Exception e) {
called[0] = true;
assertEquals(original, e);
}
}, original);
assertEquals(true, called[0]);
}

@Test
void test_parseFields_areNotNull() {
assertNotNull(HttpAction.SHARD_FIELD);
assertNotNull(HttpAction.INDEX_FIELD);
assertNotNull(HttpAction.QUERY_FIELD);
assertNotNull(HttpAction.REASON_FIELD);
assertNotNull(HttpAction.ALIASES_FIELD);
assertNotNull(HttpAction.MAPPINGS_FIELD);
assertNotNull(HttpAction.TYPE_FIELD);
assertNotNull(HttpAction.DETAILS_FIELD);
assertNotNull(HttpAction._SHARDS_FIELD);
assertNotNull(HttpAction.TASKS_FIELD);
assertNotNull(HttpAction.TOTAL_FIELD);
assertNotNull(HttpAction.SUCCESSFUL_FIELD);
assertNotNull(HttpAction.FAILED_FIELD);
assertNotNull(HttpAction.FAILURES_FIELD);
}
}
152 changes: 152 additions & 0 deletions src/test/java/org/codelibs/fesen/client/action/HttpBulkActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2012-2025 CodeLibs Project and the Others.
*
* Licensed 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.
*/
package org.codelibs.fesen.client.action;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.opensearch.action.bulk.BulkAction;
import org.opensearch.action.bulk.BulkResponse;
import org.opensearch.action.delete.DeleteRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;

class HttpBulkActionTest {

private final HttpBulkAction action = new HttpBulkAction(null, BulkAction.INSTANCE);

@Test
void test_getStringfromDocWriteRequest_indexWithId() {
final IndexRequest request = new IndexRequest("test-index").id("doc1").source("{\"field\":\"value\"}", XContentType.JSON);
final String result = action.getStringfromDocWriteRequest(request);
assertTrue(result.contains("\"index\""));
assertTrue(result.contains("\"_index\":\"test-index\""));
assertTrue(result.contains("\"_id\":\"doc1\""));
}

@Test
void test_getStringfromDocWriteRequest_indexWithoutId() {
final IndexRequest request = new IndexRequest("test-index").source("{\"field\":\"value\"}", XContentType.JSON);
request.id(null);
final String result = action.getStringfromDocWriteRequest(request);
assertTrue(result.contains("\"index\"") || result.contains("\"create\""));
assertTrue(result.contains("\"_index\":\"test-index\""));
assertFalse(result.contains("\"_id\""));
}

@Test
void test_getStringfromDocWriteRequest_delete() {
final DeleteRequest request = new DeleteRequest("test-index", "doc1");
final String result = action.getStringfromDocWriteRequest(request);
assertTrue(result.contains("\"delete\""));
assertTrue(result.contains("\"_index\":\"test-index\""));
assertTrue(result.contains("\"_id\":\"doc1\""));
}

@Test
void test_getStringfromDocWriteRequest_withRouting() {
final IndexRequest request =
new IndexRequest("test-index").id("doc1").routing("r1").source("{\"field\":\"value\"}", XContentType.JSON);
final String result = action.getStringfromDocWriteRequest(request);
assertTrue(result.contains("\"routing\":\"r1\""));
}

@Test
void test_getStringfromDocWriteRequest_withPipeline() {
final IndexRequest request =
new IndexRequest("test-index").id("doc1").setPipeline("my-pipeline").source("{\"field\":\"value\"}", XContentType.JSON);
final String result = action.getStringfromDocWriteRequest(request);
assertTrue(result.contains("\"pipeline\":\"my-pipeline\""));
}

@Test
void test_fromXContent_emptyBulkResponse() throws IOException {
final String json = "{\"took\":10,\"errors\":false,\"items\":[]}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final BulkResponse response = action.fromXContent(parser);
assertNotNull(response);
assertEquals(10, response.getTook().millis());
assertFalse(response.hasFailures());
assertEquals(0, response.getItems().length);
}
}

@Test
void test_fromXContent_withIngestTook() throws IOException {
final String json = "{\"took\":5,\"ingest_took\":3,\"errors\":false,\"items\":[]}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final BulkResponse response = action.fromXContent(parser);
assertNotNull(response);
assertEquals(5, response.getTook().millis());
assertEquals(3, response.getIngestTookInMillis());
}
}

@Test
void test_fromXContent_withIndexItem() throws IOException {
final String json = "{\"took\":1,\"errors\":false,\"items\":[{\"index\":{\"_index\":\"test\",\"_id\":\"1\","
+ "\"_version\":1,\"result\":\"created\",\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},"
+ "\"_seq_no\":0,\"_primary_term\":1,\"status\":201}}]}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final BulkResponse response = action.fromXContent(parser);
assertNotNull(response);
assertEquals(1, response.getItems().length);
assertEquals("test", response.getItems()[0].getIndex());
assertEquals("1", response.getItems()[0].getId());
assertFalse(response.getItems()[0].isFailed());
}
}

@Test
void test_fromXContent_withDeleteItem() throws IOException {
final String json = "{\"took\":1,\"errors\":false,\"items\":[{\"delete\":{\"_index\":\"test\",\"_id\":\"1\","
+ "\"_version\":2,\"result\":\"deleted\",\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},"
+ "\"_seq_no\":1,\"_primary_term\":1,\"status\":200}}]}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final BulkResponse response = action.fromXContent(parser);
assertNotNull(response);
assertEquals(1, response.getItems().length);
assertFalse(response.getItems()[0].isFailed());
}
}

@Test
void test_fromXContent_withFailedItem() throws IOException {
final String json = "{\"took\":1,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"test\",\"_id\":\"1\","
+ "\"status\":400,\"error\":{\"type\":\"mapper_parsing_exception\"," + "\"reason\":\"failed to parse\"}}}]}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final BulkResponse response = action.fromXContent(parser);
assertNotNull(response);
assertTrue(response.hasFailures());
assertEquals(1, response.getItems().length);
assertTrue(response.getItems()[0].isFailed());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2012-2025 CodeLibs Project and the Others.
*
* Licensed 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.
*/
package org.codelibs.fesen.client.action;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.opensearch.action.delete.DeleteAction;
import org.opensearch.action.delete.DeleteResponse;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;

class HttpDeleteActionTest {

private final HttpDeleteAction action = new HttpDeleteAction(null, DeleteAction.INSTANCE);

@Test
void test_fromXContent_deleted() throws IOException {
final String json = "{\"_index\":\"test\",\"_id\":\"1\",\"_version\":2,\"result\":\"deleted\","
+ "\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0}," + "\"_seq_no\":1,\"_primary_term\":1}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final DeleteResponse response = action.fromXContent(parser);
assertNotNull(response);
assertEquals("test", response.getIndex());
assertEquals("1", response.getId());
assertEquals(2, response.getVersion());
assertEquals("deleted", response.getResult().getLowercase());
}
}

@Test
void test_fromXContent_notFound() throws IOException {
final String json = "{\"_index\":\"test\",\"_id\":\"1\",\"_version\":1,\"result\":\"not_found\","
+ "\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0}," + "\"_seq_no\":2,\"_primary_term\":1}";
try (final XContentParser parser =
JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
final DeleteResponse response = action.fromXContent(parser);
assertNotNull(response);
assertEquals("not_found", response.getResult().getLowercase());
}
}
}
Loading
Loading