Skip to content

Commit

Permalink
Imporve plugin Code Coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Apr 28, 2024
1 parent 1d49ab4 commit d00180b
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ dependencies {
implementation project(path: ":${rootProject.name}-spi", configuration: 'shadow')
implementation group: 'com.google.guava', name: 'guava', version:'32.1.3-jre'
implementation group: 'com.google.guava', name: 'failureaccess', version:'1.0.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.11.0'
javaRestTestImplementation project.sourceSets.main.runtimeClasspath
//spotless
implementation('com.google.googlejavaformat:google-java-format:1.22.0') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ public class JobSchedulerPlugin extends Plugin implements ActionPlugin, Extensib
public static final String JS_BASE_URI = "/_plugins/_job_scheduler";

private static final Logger log = LogManager.getLogger(JobSchedulerPlugin.class);
JobSweeper sweeper;
JobScheduler scheduler;
LockService lockService;
Map<String, ScheduledJobProvider> indexToJobProviders;
Set<String> indicesToListen;

private JobSweeper sweeper;
private JobScheduler scheduler;
private LockService lockService;
private Map<String, ScheduledJobProvider> indexToJobProviders;
private Set<String> indicesToListen;

private JobDetailsService jobDetailsService;
JobDetailsService jobDetailsService;

public JobSchedulerPlugin() {
this.indicesToListen = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public static JobDetails parse(XContentParser parser) throws IOException {
String extensionUniqueId = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);

while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void updateIndexToJobProviders(String documentId, JobDetails jobDetails) {
* @param documentId the unique Id for the job details
* @param jobDetails the jobDetails to register
*/
void updateIndexToJobDetails(String documentId, JobDetails jobDetails) {
public void updateIndexToJobDetails(String documentId, JobDetails jobDetails) {
// Register new JobDetails entry
indexToJobDetails.put(documentId, jobDetails);
updateIndicesToListen(jobDetails.getJobIndex());
Expand Down
169 changes: 169 additions & 0 deletions src/test/java/org/opensearch/jobscheduler/JobSchedulerPluginTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* 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.jobscheduler;

import org.opensearch.Version;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.SettingsFilter;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.apache.lucene.tests.index.AssertingDirectoryReader;
import org.junit.Before;
import org.mockito.Mock;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.index.Index;
import org.opensearch.env.Environment;
import org.opensearch.index.IndexModule;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.analysis.AnalysisRegistry;
import org.opensearch.index.engine.EngineConfigFactory;
import org.opensearch.jobscheduler.rest.action.RestGetJobDetailsAction;
import org.opensearch.jobscheduler.rest.action.RestGetLockAction;
import org.opensearch.jobscheduler.rest.action.RestReleaseLockAction;
import org.opensearch.jobscheduler.spi.JobSchedulerExtension;
import org.opensearch.jobscheduler.spi.ScheduledJobParser;
import org.opensearch.jobscheduler.spi.ScheduledJobRunner;
import org.opensearch.jobscheduler.utils.JobDetailsService;
import org.opensearch.plugins.ExtensiblePlugin;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.test.IndexSettingsModule;
import org.opensearch.test.OpenSearchTestCase;

import org.opensearch.test.engine.MockEngineFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JobSchedulerPluginTests extends OpenSearchTestCase {

private JobSchedulerPlugin plugin;

private Index index;
private Settings settings;
private Settings sweeperSettings;
private IndexSettings indexSettings;
private IndexModule indexModule;
private AnalysisRegistry emptyAnalysisRegistry;

@Mock
private RestController restController;
@Mock
private ClusterSettings clusterSettings;
@Mock
private IndexScopedSettings indexScopedSettings;
@Mock
private SettingsFilter settingsFilter;
@Mock
private IndexNameExpressionResolver indexNameExpressionResolver;
@Mock
private Supplier<DiscoveryNodes> nodesInCluster;

@Before
public void setup() {
plugin = new JobSchedulerPlugin();
settings = Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.build();
indexSettings = IndexSettingsModule.newIndexSettings(".opensearch-job-scheduler-job-details", settings);
index = indexSettings.getIndex();
final MockEngineFactory engineFactory = new MockEngineFactory(AssertingDirectoryReader.class);
indexModule = new IndexModule(
indexSettings,
emptyAnalysisRegistry,
engineFactory,
new EngineConfigFactory(indexSettings),
Collections.emptyMap(),
() -> true,
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
Collections.emptyMap()
);
sweeperSettings = Settings.builder()
.put(JobSchedulerSettings.SWEEP_PERIOD.getKey(), TimeValue.timeValueMinutes(1))
.put(JobSchedulerSettings.SWEEP_PAGE_SIZE.getKey(), 10)
.put(JobSchedulerSettings.SWEEP_BACKOFF_MILLIS.getKey(), TimeValue.timeValueMillis(100))
.put(JobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT.getKey(), 5)
.build();
}

public void testLoadExtensions() {
ExtensiblePlugin.ExtensionLoader mockLoader = mock(ExtensiblePlugin.ExtensionLoader.class);
JobSchedulerExtension mockExtension1 = mock(JobSchedulerExtension.class);
JobSchedulerExtension mockExtension2 = mock(JobSchedulerExtension.class);
when(mockLoader.loadExtensions(JobSchedulerExtension.class)).thenReturn(Arrays.asList(mockExtension1, mockExtension2));
when(mockExtension1.getJobType()).thenReturn("jobType1");
when(mockExtension1.getJobIndex()).thenReturn("index1");
when(mockExtension2.getJobType()).thenReturn("jobType2");
when(mockExtension2.getJobIndex()).thenReturn("index2");
ScheduledJobParser mockParser = mock(ScheduledJobParser.class);
ScheduledJobRunner mockRunner = mock(ScheduledJobRunner.class);
when(mockExtension1.getJobParser()).thenReturn(mockParser);
when(mockExtension1.getJobRunner()).thenReturn(mockRunner);
plugin.loadExtensions(mockLoader);
assertEquals(2, plugin.indexToJobProviders.size());
assertTrue(plugin.indicesToListen.contains("index1"));
assertTrue(plugin.indicesToListen.contains("index2"));
}

public void testGetSettings_returnsSettingsList() {
List<Setting<?>> settings = plugin.getSettings();
assertNotNull(settings);
assertEquals(12, settings.size());
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_PAGE_SIZE));
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.REQUEST_TIMEOUT));
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_BACKOFF_MILLIS));
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT));
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.SWEEP_PERIOD));
assertTrue(settings.contains(LegacyOpenDistroJobSchedulerSettings.JITTER_LIMIT));
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_PAGE_SIZE));
assertTrue(settings.contains(JobSchedulerSettings.REQUEST_TIMEOUT));
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_BACKOFF_MILLIS));
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT));
assertTrue(settings.contains(JobSchedulerSettings.SWEEP_PERIOD));
assertTrue(settings.contains(JobSchedulerSettings.JITTER_LIMIT));
}

public void testOnIndexModule() {
assertEquals(indexModule.getIndex().toString(), index.toString());
assertEquals(index.getName(), JobDetailsService.JOB_DETAILS_INDEX_NAME);
}

public void testGetRestHandlers() {
List<RestHandler> restHandlers = plugin.getRestHandlers(
settings,
restController,
clusterSettings,
indexScopedSettings,
settingsFilter,
indexNameExpressionResolver,
nodesInCluster
);
assertThat(
restHandlers,
containsInAnyOrder(
instanceOf(RestGetJobDetailsAction.class),
instanceOf(RestGetLockAction.class),
instanceOf(RestReleaseLockAction.class)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.jobscheduler;

import org.junit.Before;
import org.mockito.Mock;
import org.opensearch.jobscheduler.spi.ScheduledJobParser;
import org.opensearch.jobscheduler.spi.ScheduledJobRunner;
import org.opensearch.test.OpenSearchTestCase;

import static org.mockito.Mockito.mock;

public class ScheduledJobProviderTests extends OpenSearchTestCase {

private static final String JOB_TYPE = "test_job_type";
private static final String JOB_INDEX_NAME = "test_job_index";

@Mock
private ScheduledJobParser jobParser;

@Mock
private ScheduledJobRunner jobRunner;

private ScheduledJobProvider scheduledJobProvider;

@Before
public void setUp() throws Exception {
super.setUp();
scheduledJobProvider = new ScheduledJobProvider(JOB_TYPE, JOB_INDEX_NAME, jobParser, jobRunner);
}

public void testGetJobType() {
assertEquals(JOB_TYPE, scheduledJobProvider.getJobType());
}

public void testGetJobIndexName() {
assertEquals(JOB_INDEX_NAME, scheduledJobProvider.getJobIndexName());
}

public void testGetJobParser() {
assertEquals(jobParser, scheduledJobProvider.getJobParser());
}

public void testGetJobRunner() {
assertEquals(jobRunner, scheduledJobProvider.getJobRunner());
}

public void testConstructor() {
ScheduledJobParser parser = mock(ScheduledJobParser.class);
ScheduledJobRunner runner = mock(ScheduledJobRunner.class);
ScheduledJobProvider provider = new ScheduledJobProvider(JOB_TYPE, JOB_INDEX_NAME, parser, runner);
assertEquals(JOB_TYPE, provider.getJobType());
assertEquals(JOB_INDEX_NAME, provider.getJobIndexName());
assertEquals(parser, provider.getJobParser());
assertEquals(runner, provider.getJobRunner());
}
}
Loading

0 comments on commit d00180b

Please sign in to comment.