Skip to content

Commit

Permalink
Add DynamicActionRegistryTests for tests of dynamic registry
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed May 18, 2023
1 parent fd53943 commit ccbe4b5
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 75 deletions.
75 changes: 0 additions & 75 deletions server/src/test/java/org/opensearch/action/ActionModuleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,19 @@
package org.opensearch.action;

import java.util.ArrayList;
import org.opensearch.action.ActionModule.DynamicActionRegistry;
import org.opensearch.action.main.MainAction;
import org.opensearch.action.main.TransportMainAction;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.TransportAction;
import org.opensearch.client.node.NodeClient;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.Writeable.Reader;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsFilter;
import org.opensearch.common.settings.SettingsModule;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.extensions.action.ExtensionAction;
import org.opensearch.extensions.action.ExtensionTransportAction;
import org.opensearch.identity.IdentityService;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.ActionPlugin.ActionHandler;
Expand All @@ -69,9 +64,7 @@
import org.opensearch.usage.UsageService;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -274,72 +267,4 @@ public List<Route> routes() {
threadPool.shutdown();
}
}

public void testDynamicActionRegistry() {
ActionFilters emptyFilters = new ActionFilters(Collections.emptySet());
Map<ActionType, TransportAction> testMap = Map.of(TestAction.INSTANCE, new TestTransportAction("test-action", emptyFilters, null));

DynamicActionRegistry dynamicActionRegistry = new DynamicActionRegistry();
dynamicActionRegistry.registerUnmodifiableActionMap(testMap);

// Should contain the immutable map entry
assertNotNull(dynamicActionRegistry.get(TestAction.INSTANCE));
// Should not contain anything not added
assertNull(dynamicActionRegistry.get(MainAction.INSTANCE));

// ExtensionsAction not yet registered
ExtensionAction testExtensionAction = new ExtensionAction("extensionId", "actionName");
ExtensionTransportAction testExtensionTransportAction = new ExtensionTransportAction("test-action", emptyFilters, null, null);
assertNull(dynamicActionRegistry.get(testExtensionAction));

// Register an extension action
// Should insert without problem
try {
dynamicActionRegistry.registerDynamicAction(testExtensionAction, testExtensionTransportAction);
} catch (Exception e) {
fail("Should not have thrown exception registering action: " + e);
}
assertEquals(testExtensionTransportAction, dynamicActionRegistry.get(testExtensionAction));

// Should fail inserting twice
IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> dynamicActionRegistry.registerDynamicAction(testExtensionAction, testExtensionTransportAction)
);
assertEquals("action [actionName] already registered", ex.getMessage());
// Should remove without problem
try {
dynamicActionRegistry.unregisterDynamicAction(testExtensionAction);
} catch (Exception e) {
fail("Should not have thrown exception unregistering action: " + e);
}
// Should have been removed
assertNull(dynamicActionRegistry.get(testExtensionAction));

// Should fail removing twice
ex = assertThrows(IllegalArgumentException.class, () -> dynamicActionRegistry.unregisterDynamicAction(testExtensionAction));
assertEquals("action [actionName] was not registered", ex.getMessage());
}

private static final class TestAction extends ActionType<ActionResponse> {
public static final TestAction INSTANCE = new TestAction();

private TestAction() {
super("test-action", new Reader<ActionResponse>() {
@Override
public ActionResponse read(StreamInput in) throws IOException {
return null;
}
});
}
};

private static final class TestTransportAction extends TransportAction<ActionRequest, ActionResponse> {
protected TestTransportAction(String actionName, ActionFilters actionFilters, TaskManager taskManager) {
super(actionName, actionFilters, taskManager);
}

@Override
protected void doExecute(Task task, ActionRequest request, ActionListener<ActionResponse> listener) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.action;

import org.opensearch.action.ActionModule.DynamicActionRegistry;
import org.opensearch.action.main.MainAction;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.TransportAction;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.extensions.action.ExtensionAction;
import org.opensearch.extensions.action.ExtensionTransportAction;
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.extensions.RestSendToExtensionAction;
import org.opensearch.tasks.Task;
import org.opensearch.tasks.TaskManager;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import static org.mockito.Mockito.mock;

public class DynamicActionRegistryTests extends OpenSearchTestCase {

public void testDynamicActionRegistry() {
ActionFilters emptyFilters = new ActionFilters(Collections.emptySet());
Map<ActionType, TransportAction> testMap = Map.of(TestAction.INSTANCE, new TestTransportAction("test-action", emptyFilters, null));

DynamicActionRegistry dynamicActionRegistry = new DynamicActionRegistry();
dynamicActionRegistry.registerUnmodifiableActionMap(testMap);

// Should contain the immutable map entry
assertNotNull(dynamicActionRegistry.get(TestAction.INSTANCE));
// Should not contain anything not added
assertNull(dynamicActionRegistry.get(MainAction.INSTANCE));

// ExtensionsAction not yet registered
ExtensionAction testExtensionAction = new ExtensionAction("extensionId", "actionName");
ExtensionTransportAction testExtensionTransportAction = new ExtensionTransportAction("test-action", emptyFilters, null, null);
assertNull(dynamicActionRegistry.get(testExtensionAction));

// Register an extension action
// Should insert without problem
try {
dynamicActionRegistry.registerDynamicAction(testExtensionAction, testExtensionTransportAction);
} catch (Exception e) {
fail("Should not have thrown exception registering action: " + e);
}
assertEquals(testExtensionTransportAction, dynamicActionRegistry.get(testExtensionAction));

// Should fail inserting twice
IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> dynamicActionRegistry.registerDynamicAction(testExtensionAction, testExtensionTransportAction)
);
assertEquals("action [actionName] already registered", ex.getMessage());
// Should remove without problem
try {
dynamicActionRegistry.unregisterDynamicAction(testExtensionAction);
} catch (Exception e) {
fail("Should not have thrown exception unregistering action: " + e);
}
// Should have been removed
assertNull(dynamicActionRegistry.get(testExtensionAction));

// Should fail removing twice
ex = assertThrows(IllegalArgumentException.class, () -> dynamicActionRegistry.unregisterDynamicAction(testExtensionAction));
assertEquals("action [actionName] was not registered", ex.getMessage());
}

public void testDynamicActionRegistryWithNamedRoutes() {
RestSendToExtensionAction action = mock(RestSendToExtensionAction.class);
RestSendToExtensionAction action2 = mock(RestSendToExtensionAction.class);
NamedRoute r1 = new NamedRoute(RestRequest.Method.GET, "/foo", "foo");
NamedRoute r2 = new NamedRoute(RestRequest.Method.GET, "/bar", "bar");

DynamicActionRegistry registry = new DynamicActionRegistry();
registry.registerDynamicRoute(r1, action);
registry.registerDynamicRoute(r2, action2);

assertTrue(registry.isActionRegistered("foo"));
assertTrue(registry.isActionRegistered("bar"));
}

public void testDynamicActionRegistryRegisterAndUnregisterWithNamedRoutes() {
RestSendToExtensionAction action = mock(RestSendToExtensionAction.class);
RestSendToExtensionAction action2 = mock(RestSendToExtensionAction.class);
NamedRoute r1 = new NamedRoute(RestRequest.Method.GET, "/foo", "foo");
NamedRoute r2 = new NamedRoute(RestRequest.Method.GET, "/bar", "bar");

DynamicActionRegistry registry = new DynamicActionRegistry();
registry.registerDynamicRoute(r1, action);
registry.registerDynamicRoute(r2, action2);

registry.unregisterDynamicRoute(r2);

assertTrue(registry.isActionRegistered("foo"));
assertFalse(registry.isActionRegistered("bar"));
}

private static final class TestAction extends ActionType<ActionResponse> {
public static final TestAction INSTANCE = new TestAction();

private TestAction() {
super("test-action", new Writeable.Reader<ActionResponse>() {
@Override
public ActionResponse read(StreamInput in) throws IOException {
return null;
}
});
}
};

private static final class TestTransportAction extends TransportAction<ActionRequest, ActionResponse> {
protected TestTransportAction(String actionName, ActionFilters actionFilters, TaskManager taskManager) {
super(actionName, actionFilters, taskManager);
}

@Override
protected void doExecute(Task task, ActionRequest request, ActionListener<ActionResponse> listener) {}
}
}

0 comments on commit ccbe4b5

Please sign in to comment.