From d78b5621b6053e35ffbb630ed6b5b18f444b7b2b Mon Sep 17 00:00:00 2001 From: Lukasz Soszynski Date: Mon, 5 Sep 2022 12:17:36 +0200 Subject: [PATCH] Unit test related to making plugin NodeAndClusterIdConverter disabled or enabled Signed-off-by: Lukasz Soszynski --- .../logging/NodeAndClusterIdConverter.java | 3 +- .../NodeAndClusterIdConverterTest.java | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java diff --git a/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java b/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java index a0709257018f4..4216963932b51 100644 --- a/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java +++ b/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java @@ -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 nodeAndClusterId = new SetOnce<>(); /** @@ -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); } } diff --git a/server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java b/server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java new file mode 100644 index 0000000000000..dd8ec0ea70d24 --- /dev/null +++ b/server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java @@ -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); + } + +}