From eec23e4bb901a5b3ca9c09721e678d597c189f05 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 12 Jun 2023 13:53:55 -0700 Subject: [PATCH] Removed forbidden APIs from rest action and modified tests Signed-off-by: Owais Kazi --- .../rest/RestInitializeExtensionAction.java | 5 -- .../rest/RestInitializeExtensionTests.java | 55 +++---------------- 2 files changed, 9 insertions(+), 51 deletions(-) diff --git a/server/src/main/java/org/opensearch/extensions/rest/RestInitializeExtensionAction.java b/server/src/main/java/org/opensearch/extensions/rest/RestInitializeExtensionAction.java index 0269b357e38d9..2601d1e1077ab 100644 --- a/server/src/main/java/org/opensearch/extensions/rest/RestInitializeExtensionAction.java +++ b/server/src/main/java/org/opensearch/extensions/rest/RestInitializeExtensionAction.java @@ -8,8 +8,6 @@ package org.opensearch.extensions.rest; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.opensearch.client.node.NodeClient; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; @@ -34,7 +32,6 @@ public class RestInitializeExtensionAction extends BaseRestHandler { private final ExtensionsManager extensionsManager; - private static final Logger logger = LogManager.getLogger(RestInitializeExtensionAction.class); @Override public String getName() { @@ -108,12 +105,10 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client extensionsManager.loadExtension(extension); extensionsManager.initialize(); } catch (IOException e) { - logger.error(e); return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, e.getMessage())); } - logger.info("Extension has been initialized"); return channel -> { try (XContentBuilder builder = channel.newBuilder()) { builder.startObject(); diff --git a/server/src/test/java/org/opensearch/extensions/rest/RestInitializeExtensionTests.java b/server/src/test/java/org/opensearch/extensions/rest/RestInitializeExtensionTests.java index 1f27170d076ff..fb2be7343c15f 100644 --- a/server/src/test/java/org/opensearch/extensions/rest/RestInitializeExtensionTests.java +++ b/server/src/test/java/org/opensearch/extensions/rest/RestInitializeExtensionTests.java @@ -16,8 +16,6 @@ import static java.util.Collections.emptySet; import static org.mockito.Mockito.mock; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; import org.junit.After; import org.junit.Before; import org.opensearch.Version; @@ -31,7 +29,6 @@ import org.opensearch.extensions.ExtensionsManager; import org.opensearch.indices.breaker.NoneCircuitBreakerService; import org.opensearch.rest.RestRequest; -import org.opensearch.test.MockLogAppender; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.rest.FakeRestChannel; import org.opensearch.test.rest.FakeRestRequest; @@ -89,8 +86,12 @@ public void tearDown() throws Exception { public void testRestInitializeExtensionActionResponse() throws Exception { ExtensionsManager extensionsManager = mock(ExtensionsManager.class); RestInitializeExtensionAction restInitializeExtensionAction = new RestInitializeExtensionAction(extensionsManager); + final String content = + "{\"name\":\"ad-extension\",\"uniqueId\":\"ad-extension\",\"hostAddress\":\"127.0.0.1\",\"port\":\"4532\",\"version\":\"1.0\",\"opensearchVersion\":\"3.0.0\",\"minimumCompatibleVersion\":\"3.0.0\"}"; + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON) + .withMethod(RestRequest.Method.POST) + .build(); - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST).build(); FakeRestChannel channel = new FakeRestChannel(request, false, 0); restInitializeExtensionAction.handleRequest(request, channel, null); @@ -100,33 +101,6 @@ public void testRestInitializeExtensionActionResponse() throws Exception { } - public void testRestInitializeExtensionAction() throws Exception { - ExtensionsManager extensionsManager = mock(ExtensionsManager.class); - RestInitializeExtensionAction restInitializeExtensionAction = new RestInitializeExtensionAction(extensionsManager); - final String content = - "{\"name\":\"ad-extension\",\"uniqueId\":\"ad-extension\",\"hostAddress\":\"127.0.0.1\",\"port\":\"4532\",\"version\":\"1.0\",\"opensearchVersion\":\"3.0.0\",\"minimumCompatibleVersion\":\"3.0.0\"}"; - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON) - .withMethod(RestRequest.Method.POST) - .build(); - - try ( - MockLogAppender mockLogAppender = MockLogAppender.createForLoggers(LogManager.getLogger(RestInitializeExtensionAction.class)) - ) { - mockLogAppender.addExpectation( - new MockLogAppender.SeenEventExpectation( - "Extension has been initialized", - "org.opensearch.extensions.rest.RestInitializeExtensionAction", - Level.INFO, - "Extension has been initialized" - ) - ); - restInitializeExtensionAction.prepareRequest(request, null); - mockLogAppender.assertAllExpectationsMatched(); - - } - - } - public void testRestInitializeExtensionActionFailure() throws Exception { ExtensionsManager extensionsManager = new ExtensionsManager(Set.of()); RestInitializeExtensionAction restInitializeExtensionAction = new RestInitializeExtensionAction(extensionsManager); @@ -137,22 +111,11 @@ public void testRestInitializeExtensionActionFailure() throws Exception { .withMethod(RestRequest.Method.POST) .build(); - try ( - MockLogAppender mockLogAppender = MockLogAppender.createForLoggers(LogManager.getLogger(RestInitializeExtensionAction.class)) - ) { - mockLogAppender.addExpectation( - new MockLogAppender.SeenEventExpectation( - "Required field is missing in the request", - "org.opensearch.extensions.rest.RestInitializeExtensionAction", - Level.ERROR, - "Required field [uniqueId] is missing in the request" - ) - ); - restInitializeExtensionAction.prepareRequest(request, null); - mockLogAppender.assertAllExpectationsMatched(); - - } + FakeRestChannel channel = new FakeRestChannel(request, false, 0); + restInitializeExtensionAction.handleRequest(request, channel, null); + assertEquals(1, channel.errors().get()); + assertTrue(channel.capturedResponse().content().utf8ToString().contains("Required field [uniqueId] is missing in the request")); } }