From 4a5c9bc223242317af6c2f35149fe563dba4a449 Mon Sep 17 00:00:00 2001 From: Michael Froh Date: Thu, 18 May 2023 06:16:12 +0000 Subject: [PATCH] Add tests for SearchPipelineInfo Signed-off-by: Michael Froh --- .../pipeline/SearchPipelineInfoTests.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 server/src/test/java/org/opensearch/search/pipeline/SearchPipelineInfoTests.java diff --git a/server/src/test/java/org/opensearch/search/pipeline/SearchPipelineInfoTests.java b/server/src/test/java/org/opensearch/search/pipeline/SearchPipelineInfoTests.java new file mode 100644 index 0000000000000..6eb137cb28e8f --- /dev/null +++ b/server/src/test/java/org/opensearch/search/pipeline/SearchPipelineInfoTests.java @@ -0,0 +1,75 @@ +/* + * 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.search.pipeline; + +import org.opensearch.Version; +import org.opensearch.common.io.stream.BytesStreamOutput; +import org.opensearch.common.io.stream.StreamInput; +import org.opensearch.test.OpenSearchTestCase; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class SearchPipelineInfoTests extends OpenSearchTestCase { + public void testSerializationRoundtrip() throws IOException { + SearchPipelineInfo searchPipelineInfo = new SearchPipelineInfo( + Map.of( + "a", + List.of(new ProcessorInfo("a1"), new ProcessorInfo("a2"), new ProcessorInfo("a3")), + "b", + List.of(new ProcessorInfo("b1"), new ProcessorInfo("b2")), + "c", + List.of(new ProcessorInfo("c1")) + ) + ); + SearchPipelineInfo deserialized; + try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) { + searchPipelineInfo.writeTo(bytesStreamOutput); + try (StreamInput bytesStreamInput = bytesStreamOutput.bytes().streamInput()) { + deserialized = new SearchPipelineInfo(bytesStreamInput); + } + } + assertTrue(deserialized.containsProcessor("a", "a1")); + assertTrue(deserialized.containsProcessor("a", "a2")); + assertTrue(deserialized.containsProcessor("a", "a3")); + assertTrue(deserialized.containsProcessor("b", "b1")); + assertTrue(deserialized.containsProcessor("b", "b2")); + assertTrue(deserialized.containsProcessor("c", "c1")); + } + + /** + * When serializing / deserializing to / from old versions, processor type info is lost. + * + * Also, we only supported request/response processors. + */ + public void testSerializationRoundtripBackcompat() throws IOException { + SearchPipelineInfo searchPipelineInfo = new SearchPipelineInfo( + Map.of( + Pipeline.REQUEST_PROCESSORS_KEY, + List.of(new ProcessorInfo("a1"), new ProcessorInfo("a2"), new ProcessorInfo("a3")), + Pipeline.RESPONSE_PROCESSORS_KEY, + List.of(new ProcessorInfo("b1"), new ProcessorInfo("b2")) + ) + ); + SearchPipelineInfo deserialized; + try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) { + bytesStreamOutput.setVersion(Version.V_2_7_0); + searchPipelineInfo.writeTo(bytesStreamOutput); + try (StreamInput bytesStreamInput = bytesStreamOutput.bytes().streamInput()) { + bytesStreamInput.setVersion(Version.V_2_7_0); + deserialized = new SearchPipelineInfo(bytesStreamInput); + } + } + for (String proc : List.of("a1", "a2", "a3", "b1", "b2")) { + assertTrue(deserialized.containsProcessor(Pipeline.REQUEST_PROCESSORS_KEY, proc)); + assertTrue(deserialized.containsProcessor(Pipeline.RESPONSE_PROCESSORS_KEY, proc)); + } + } +}