/* * 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 under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch 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.action.get; import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.index.VersionType; import org.opensearch.search.fetch.subphase.FetchSourceContext; import org.opensearch.test.OpenSearchTestCase; import java.io.IOException; import static org.opensearch.test.VersionUtils.randomVersion; import static org.hamcrest.CoreMatchers.equalTo; public class MultiGetShardRequestTests extends OpenSearchTestCase { public void testSerialization() throws IOException { MultiGetRequest multiGetRequest = new MultiGetRequest(); if (randomBoolean()) { multiGetRequest.preference(randomAlphaOfLength(randomIntBetween(1, 10))); } if (randomBoolean()) { multiGetRequest.realtime(false); } if (randomBoolean()) { multiGetRequest.refresh(true); } MultiGetShardRequest multiGetShardRequest = new MultiGetShardRequest(multiGetRequest, "index", 0); int numItems = iterations(10, 30); for (int i = 0; i < numItems; i++) { MultiGetRequest.Item item = new MultiGetRequest.Item("alias-" + randomAlphaOfLength(randomIntBetween(1, 10)), "id-" + i); if (randomBoolean()) { int numFields = randomIntBetween(1, 5); String[] fields = new String[numFields]; for (int j = 0; j < fields.length; j++) { fields[j] = randomAlphaOfLength(randomIntBetween(1, 10)); } item.storedFields(fields); } if (randomBoolean()) { item.version(randomIntBetween(1, Integer.MAX_VALUE)); item.versionType(randomFrom(VersionType.values())); } if (randomBoolean()) { item.fetchSourceContext(new FetchSourceContext(randomBoolean())); } multiGetShardRequest.add(0, item); } BytesStreamOutput out = new BytesStreamOutput(); out.setVersion(randomVersion(random())); multiGetShardRequest.writeTo(out); StreamInput in = out.bytes().streamInput(); in.setVersion(out.getVersion()); MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest(in); assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index())); assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference())); assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime())); assertThat(multiGetShardRequest2.refresh(), equalTo(multiGetShardRequest.refresh())); assertThat(multiGetShardRequest2.items.size(), equalTo(multiGetShardRequest.items.size())); for (int i = 0; i < multiGetShardRequest2.items.size(); i++) { MultiGetRequest.Item item = multiGetShardRequest.items.get(i); MultiGetRequest.Item item2 = multiGetShardRequest2.items.get(i); assertThat(item2.index(), equalTo(item.index())); assertThat(item2.id(), equalTo(item.id())); assertThat(item2.storedFields(), equalTo(item.storedFields())); assertThat(item2.version(), equalTo(item.version())); assertThat(item2.versionType(), equalTo(item.versionType())); assertThat(item2.fetchSourceContext(), equalTo(item.fetchSourceContext())); } assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices())); assertThat(multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions())); } }