-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DynamicActionRegistryTests for tests of dynamic registry
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
2 changed files
with
131 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
server/src/test/java/org/opensearch/action/DynamicActionRegistryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} | ||
} |