Skip to content

Commit

Permalink
Add unit tests for PluginSettings class
Browse files Browse the repository at this point in the history
  • Loading branch information
QU3B1M committed Jan 14, 2025
1 parent 0136b8a commit 1947e26
Showing 1 changed file with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2024, Wazuh Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.wazuh.commandmanager.settings;

import org.opensearch.common.settings.Settings;
import org.opensearch.env.Environment;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.junit.Before;

import java.util.Optional;

import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.mockito.Mockito.*;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE)
public class PluginSettingsTests extends OpenSearchIntegTestCase {
@Mock private Environment mockEnvironment;

@InjectMocks private PluginSettings pluginSettings;

Settings settings;

@Before
@Override
public void setUp() throws Exception {
settings =
Settings.builder()
.put("command_manager.timeout", 20)
.put("command_manager.job.schedule", 1)
.put("command_manager.job.page_size", 100)
.put("command_manager.job.pit_keep_alive", 30)
.put(
"command_manager.job.index.template",
"index-template-scheduled-commands")
.put("command_manager.api.prefix", "/_command_manager")
.put("command_manager.api.endpoint", "/commands")
.put("command_manager.index.name", "command_manager")
.put("command_manager.index.template", "command_manager_template")
.build();

mockEnvironment = mock(Environment.class);
when(mockEnvironment.settings()).thenReturn(settings);
pluginSettings = PluginSettings.getInstance(mockEnvironment.settings());
super.setUp();
}

public void testInitializeWithValidValues() throws Exception {
pluginSettings = PluginSettings.getInstance(mockEnvironment.settings());

assertEquals(Optional.of(20), Optional.of(pluginSettings.getTimeout()));
assertEquals(Optional.of(1), Optional.of(pluginSettings.getJobSchedule()));
assertEquals(Optional.of(100), Optional.of(pluginSettings.getJobPageSize()));
assertEquals(Optional.of(30), Optional.of(pluginSettings.getJobKeepAlive()));
assertEquals("index-template-scheduled-commands", pluginSettings.getJobIndexTemplate());
assertEquals("/_command_manager", pluginSettings.getApiPrefix());
assertEquals("/commands", pluginSettings.getApiEndpoint());
assertEquals("command_manager", pluginSettings.getIndexName());
assertEquals("command_manager_template", pluginSettings.getIndexTemplate());
}

public void testSingletonBehavior() throws Exception {
PluginSettings pluginSettings2 = PluginSettings.getInstance(mockEnvironment.settings());
assertEquals(pluginSettings, pluginSettings2);
}

public void testSIngletonMultithreadedBehavior() throws Exception {
PluginSettings[] pluginSettingsArray = new PluginSettings[10];
for (int i = 0; i < 10; i++) {
pluginSettingsArray[i] = PluginSettings.getInstance(mockEnvironment.settings());
}

for (int i = 0; i < 10; i++) {
assertEquals(pluginSettings, pluginSettingsArray[i]);
}
}
}

0 comments on commit 1947e26

Please sign in to comment.