Skip to content

Commit

Permalink
Unit test related to making plugin NodeAndClusterIdConverter disabled…
Browse files Browse the repository at this point in the history
… or enabled

Signed-off-by: Lukasz Soszynski <[email protected]>
  • Loading branch information
lukaszsoszynski committed Sep 5, 2022
1 parent 407bb65 commit d78b562
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
@Plugin(category = PatternConverter.CATEGORY, name = "NodeAndClusterIdConverter")
@ConverterKeys({ "node_and_cluster_id" })
public final class NodeAndClusterIdConverter extends LogEventPatternConverter {
static final String ENABLED_SYSTEM_PROPERTY = "org.opensearch.common.logging.NodeAndClusterIdConverter.enabled";
private static final SetOnce<String> nodeAndClusterId = new SetOnce<>();

/**
Expand Down Expand Up @@ -94,7 +95,7 @@ private static String formatIds(String clusterUUID, String nodeId) {
}

private static boolean isEnabled() {
String enabled = System.getProperty("org.opensearch.common.logging.NodeAndClusterIdConverter.enabled", "true");
String enabled = System.getProperty(ENABLED_SYSTEM_PROPERTY, "true");
return Boolean.valueOf(enabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.common.logging;

import org.apache.lucene.util.SetOnce;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class NodeAndClusterIdConverterTest {

public static final String NODE_ID = "node-id";
public static final String CLUSTER_UUID = "cluster-uuid";

@After
@Before
public void cleanSystemProperties() {
System.clearProperty(NodeAndClusterIdConverter.ENABLED_SYSTEM_PROPERTY);
}

@Test(expected = SetOnce.AlreadySetException.class)
public void testTryToSetClusterAndNodeIdByDefault() {
NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID);
NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID);
}

@Test(expected = SetOnce.AlreadySetException.class)
public void testTryToSetClusterAndNodeIdWhenPluginIsEnabled() {
System.setProperty(NodeAndClusterIdConverter.ENABLED_SYSTEM_PROPERTY, "true");

NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID);
NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID);
}

@Test
public void testNotSetClusterIdWhenPluginIsDisabled() {
System.setProperty(NodeAndClusterIdConverter.ENABLED_SYSTEM_PROPERTY, "false");

NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID);

// second invocation should not throw an exception
NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID);
}

}

0 comments on commit d78b562

Please sign in to comment.