Skip to content

Commit

Permalink
Added tests for RestInitializeExtensionAction
Browse files Browse the repository at this point in the history
Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>
  • Loading branch information
owaiskazi19 committed Jun 12, 2023
1 parent 4036e69 commit 4266f4e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public RestInitializeExtensionAction(ExtensionsManager extensionsManager) {
}

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String name = "";
String uniqueId = "";
String hostAddress = "";
Expand Down Expand Up @@ -113,6 +113,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli

}

logger.info("Extension has been initialized");
return channel -> {
try (XContentBuilder builder = channel.newBuilder()) {
builder.startObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,152 @@

package org.opensearch.extensions.rest;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.util.Collections.emptyMap;
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;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.bytes.BytesArray;
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;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.extensions.ExtensionsManager;
import org.opensearch.indices.breaker.NoneCircuitBreakerService;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.extensions.RestInitializeExtensionAction;
import org.opensearch.test.MockLogAppender;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestChannel;
import org.opensearch.test.rest.FakeRestRequest;
import org.opensearch.test.transport.MockTransportService;
import org.opensearch.threadpool.TestThreadPool;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportService;
import org.opensearch.transport.nio.MockNioTransport;

public class RestInitializeExtensionTests extends OpenSearchTestCase {

private TransportService transportService;
private MockNioTransport transport;
private final ThreadPool threadPool = new TestThreadPool(RestInitializeExtensionTests.class.getSimpleName());

@Before
public void setup() throws Exception {
Settings settings = Settings.builder().put("cluster.name", "test").build();
transport = new MockNioTransport(
settings,
Version.CURRENT,
threadPool,
new NetworkService(Collections.emptyList()),
PageCacheRecycler.NON_RECYCLING_INSTANCE,
new NamedWriteableRegistry(Collections.emptyList()),
new NoneCircuitBreakerService()
);
transportService = new MockTransportService(
settings,
transport,
threadPool,
TransportService.NOOP_TRANSPORT_INTERCEPTOR,
(boundAddress) -> new DiscoveryNode(
"test_node",
"test_node",
boundAddress.publishAddress(),
emptyMap(),
emptySet(),
Version.CURRENT
),
null,
Collections.emptySet()
);

}

@Override
@After
public void tearDown() throws Exception {
super.tearDown();
transportService.close();
ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
}

public void testRestInitializeExtensionActionResponse() throws Exception {
ExtensionsManager extensionsManager = mock(ExtensionsManager.class);
RestInitializeExtensionAction restInitializeExtensionAction = new RestInitializeExtensionAction(extensionsManager);

RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST).build();
FakeRestChannel channel = new FakeRestChannel(request, false, 0);
restInitializeExtensionAction.handleRequest(request, channel, null);

assertEquals(1, channel.responses().get());
assertEquals(0, channel.errors().get());
assertTrue(channel.capturedResponse().content().utf8ToString().contains("Extension has been initialized"));

}

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.rest.extensions.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);

final String content =
"{\"name\":\"ad-extension\",\"uniqueId\":\"\",\"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(
"Required field is missing in the request",
"org.opensearch.rest.extensions.RestInitializeExtensionAction",
Level.ERROR,
"Required field [uniqueId] is missing in the request"
)
);
restInitializeExtensionAction.prepareRequest(request, null);
mockLogAppender.assertAllExpectationsMatched();

}

}

public class RestInitializeExtensionTests extends OpenSearchTestCase {}
}

0 comments on commit 4266f4e

Please sign in to comment.