Skip to content

Commit

Permalink
Adding more unit test coverage (#295)
Browse files Browse the repository at this point in the history
* Adding tests for ExtensionRestHandler

Signed-off-by: Sarat Vemulapalli <vemulapallisarat@gmail.com>

* Adding unit tests for some more coverage

Signed-off-by: Sarat Vemulapalli <vemulapallisarat@gmail.com>

* Addressing comments

Signed-off-by: Sarat Vemulapalli <vemulapallisarat@gmail.com>

Signed-off-by: Sarat Vemulapalli <vemulapallisarat@gmail.com>
  • Loading branch information
saratvemulapalli authored Dec 28, 2022
1 parent 3535849 commit 850fcb2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/main/java/org/opensearch/sdk/BaseExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,22 @@ public abstract class BaseExtension implements Extension {

/**
* Instantiate this extension, initializing the connection settings and REST actions.
* @param path to extensions configuration.
*/
protected BaseExtension(String path) {
try {
this.settings = ExtensionSettings.readSettingsFromYaml(path);
if (settings == null || settings.getHostAddress() == null || settings.getHostPort() == null) {
throw new IOException("Failed to initialize Extension settings. No port bound.");
}
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}

/**
* take an ExtensionSettings object and set it directly
* take an ExtensionSettings object and set it directly.
* @param settings defined by the extension.
*/
protected BaseExtension(ExtensionSettings settings) {
this.settings = settings;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/opensearch/sdk/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public interface Extension {
*
* @return a list of REST handlers (REST actions) this extension handles.
*/
List<ExtensionRestHandler> getExtensionRestHandlers();
default List<ExtensionRestHandler> getExtensionRestHandlers() {
return Collections.emptyList();
}

/**
* Gets an optional list of custom {@link Setting} for the extension to register with OpenSearch.
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/opensearch/sdk/TestBaseExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.Test;
import org.opensearch.test.OpenSearchTestCase;

/*
* Most of the code in BaseExtension is tested by HelloWorld extension tests.
* This class tests code paths which are not tested.
*/
public class TestBaseExtension extends OpenSearchTestCase {

private static final String UNPARSEABLE_EXTENSION_CONFIG = "/bad-extension.yml";
private static final String EXTENSION_DESCRIPTOR_FILEPATH = "src/test/resources" + UNPARSEABLE_EXTENSION_CONFIG;

public class TestExtension extends BaseExtension {

public TestExtension(String path) {
super(path);
}
}

@Test
public void testBaseExtensionWithNullPath() {
// When a null path is passed, reading from YAML will fail.
assertThrows(IllegalArgumentException.class, () -> { TestExtension testExtension = new TestExtension(null); });
}

@Test
public void testBaseExtensionWithBadConfig() {
// When a bad extensions.yml config is passed, expect failing initialization.
assertThrows(
IllegalArgumentException.class,
() -> { TestExtension testExtension = new TestExtension(EXTENSION_DESCRIPTOR_FILEPATH); }
);
}
}
31 changes: 31 additions & 0 deletions src/test/java/org/opensearch/sdk/TestExtensionRestHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.Test;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.test.OpenSearchTestCase;

public class TestExtensionRestHandler extends OpenSearchTestCase {
private class NoOpExtensionRestHandler implements ExtensionRestHandler {

@Override
public ExtensionRestResponse handleRequest(ExtensionRestRequest request) {
return null;
}
}

@Test
public void testHandlerDefaultRoutes() {
NoOpExtensionRestHandler handler = new NoOpExtensionRestHandler();
assertTrue(handler.routes().isEmpty());
}
}
1 change: 1 addition & 0 deletions src/test/resources/bad-extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extensionName: bad-extension

0 comments on commit 850fcb2

Please sign in to comment.