-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and share use of deprecationn logger
Signed-off-by: Peter Nied <[email protected]>
- Loading branch information
Showing
7 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/org/opensearch/security/setting/DeprecatedSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
package org.opensearch.security.setting; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import org.opensearch.common.logging.DeprecationLogger; | ||
import org.opensearch.common.settings.Settings; | ||
|
||
/** | ||
* Functionality around settings that have been deprecated | ||
*/ | ||
public class DeprecatedSettings { | ||
|
||
static DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(DeprecatedSettings.class); | ||
|
||
/** | ||
* Checks for an deprecated key found in a setting, logs that it should be replaced with the another key | ||
*/ | ||
public static void checkForDeprecatedSetting(final Settings settings, final String legacySettingKey, final String validSettingKey) { | ||
if (settings.hasValue(legacySettingKey)) { | ||
DEPRECATION_LOGGER.deprecate(legacySettingKey, "Found deprecated setting '{}', please replace with '{}'", legacySettingKey, validSettingKey); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/org/opensearch/security/setting/DeprecatedSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.opensearch.security.setting; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import org.opensearch.common.logging.DeprecationLogger; | ||
import org.opensearch.common.settings.Settings; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.opensearch.security.setting.DeprecatedSettings.checkForDeprecatedSetting; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class DeprecatedSettingsTest { | ||
|
||
@Mock | ||
private DeprecationLogger logger; | ||
|
||
private DeprecationLogger original; | ||
|
||
@Before | ||
public void before() { | ||
original = DeprecatedSettings.DEPRECATION_LOGGER; | ||
DeprecatedSettings.DEPRECATION_LOGGER = logger; | ||
} | ||
|
||
@After | ||
public void after() { | ||
DeprecatedSettings.DEPRECATION_LOGGER = original; | ||
verifyNoMoreInteractions(logger); | ||
} | ||
|
||
@Test | ||
public void testCheckForDeprecatedSettingNoLegacy() { | ||
final Settings settings = Settings.builder().put("properKey", "value").build(); | ||
|
||
checkForDeprecatedSetting(settings, "legacyKey", "properKey"); | ||
|
||
verifyNoInteractions(logger); | ||
} | ||
|
||
@Test | ||
public void testCheckForDeprecatedSettingFoundLegacy() { | ||
final Settings settings = Settings.builder().put("legacyKey", "value").build(); | ||
|
||
checkForDeprecatedSetting(settings, "legacyKey", "properKey"); | ||
|
||
verify(logger).deprecate(eq("legacyKey"), anyString(), any()); | ||
} | ||
} |