Skip to content

Commit

Permalink
Added getNamedWriteableRegistry() API for extensions opensearch-proje…
Browse files Browse the repository at this point in the history
…ct#291

Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com>
  • Loading branch information
Aisara Imangaliyeva committed Mar 19, 2023
1 parent 3e5e935 commit 12da89c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ public void updateNamedWriteableRegistry() {
this.sdkNamedWriteableRegistry.updateNamedWriteableRegistry(this);
}


/**
* Gets the NamedXContentRegistry. Only valid if {@link #isInitialized()} returns true.
*
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/opensearch/sdk/NettyTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import org.opensearch.Version;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.network.NetworkService;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.PageCacheRecycler;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/opensearch/sdk/ScriptExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ default ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext
default List<ScriptContext<?>> getContexts() {
return Collections.emptyList();
}
}
}
52 changes: 26 additions & 26 deletions src/test/java/org/opensearch/sdk/TestSDKNamedWriteableRegistry.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright OpenSearch Contributors
* 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.sdk;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -12,18 +21,16 @@
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;


public class TestSDKNamedWriteableRegistry extends OpenSearchTestCase {
private TestSDKNamedWriteableRegistry.ExampleRunnerForTest runner;

private static class Example implements NamedWriteable {
public static final String NAME = "Example";
public static final NamedWriteableRegistry.Entry WRITEABLE_REGISTRY = new NamedWriteableRegistry.Entry(
TestSDKNamedWriteableRegistry.Example.class,
NAME,
null
TestSDKNamedWriteableRegistry.Example.class,
NAME,
null
);

private final String name;
Expand All @@ -32,29 +39,13 @@ public Example(String name) {
this.name = name;
}


@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
TestSDKNamedWriteableRegistry.Example that = (TestSDKNamedWriteableRegistry.Example) o;
return Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}

@Override
public String getWriteableName() {
return null;
return name;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
}
public void writeTo(StreamOutput out) throws IOException {}
}

private static class ExampleRunnerForTest extends ExtensionsRunnerForTest {
Expand Down Expand Up @@ -94,10 +85,19 @@ public void testDefaultNamedWriteableRegistry() throws IOException {
NamedWriteableRegistry registry = runner.sdkNamedWriteableRegistry.getRegistry();

XContentParseException ex = assertThrows(
XContentParseException.class,
() -> registry.getReader(TestSDKNamedWriteableRegistry.Example.class, TestSDKNamedWriteableRegistry.Example.NAME)
XContentParseException.class,
() -> registry.getReader(TestSDKNamedWriteableRegistry.Example.class, TestSDKNamedWriteableRegistry.Example.NAME)
);
assertEquals("unknown named object category [" + TestSDKNamedWriteableRegistry.Example.class.getName() + "]", ex.getMessage());
assertEquals("Unknown NamedWriteable category [" + TestSDKNamedWriteableRegistry.Example.class.getName() + "]", ex.getMessage());
}

@Test
public void testCustomNamedWriteableRegistryParse() throws IOException {
// Update the runner before testing
runner.updateNamedWriteableRegistry();
NamedWriteableRegistry registry = runner.sdkNamedWriteableRegistry.getRegistry();

TestSDKNamedWriteableRegistry.Example example = (Example) registry.getReader(Example.class, Example.NAME);
assertEquals(TestSDKNamedWriteableRegistry.Example.NAME, example.getWriteableName());
}
}

0 comments on commit 12da89c

Please sign in to comment.