Skip to content

Commit

Permalink
Add sample for validator (#421) (#550)
Browse files Browse the repository at this point in the history
* add sample for validator



* addressing comment



* address comment



* add test



* add test case for validator



* address comment



* address comment



* fix merge conflict



---------


(cherry picked from commit 0da23e1)

Signed-off-by: Frank Lou <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 7442d1a commit ec2e7b2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.sdk.sample.helloworld;

import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Setting.RegexValidator;
import org.opensearch.common.settings.Setting.Property;

/**
* {@link ExampleCustomSettingConfig} contains the custom settings value and their static declarations.
*/
public class ExampleCustomSettingConfig {
private static final String FORBIDDEN_REGEX = "forbidden";

/**
* A string setting. If the string setting matches the FORBIDDEN_REGEX string, the validation will fail.
*/
public static final Setting<String> VALIDATED_SETTING = Setting.simpleString(
"custom.validate",
new RegexValidator(FORBIDDEN_REGEX),
Property.NodeScope,
Property.Dynamic
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionResponse;
import org.opensearch.common.settings.Setting;
import org.opensearch.sdk.BaseExtension;
import org.opensearch.sdk.Extension;
import org.opensearch.sdk.ExtensionRestHandler;
Expand Down Expand Up @@ -63,6 +64,13 @@ public List<ExtensionRestHandler> getExtensionRestHandlers() {
return Arrays.asList(new ActionHandler<>(SampleAction.INSTANCE, SampleTransportAction.class));
}

/**
* A list of object that includes a single instance of Validator for Custom Setting
*/
public List<Setting<?>> getSettings() {
return Arrays.asList(ExampleCustomSettingConfig.VALIDATED_SETTING);
}

/**
* Entry point to execute an extension.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import com.google.inject.Injector;
import com.google.inject.Key;

import static org.opensearch.sdk.sample.helloworld.ExampleCustomSettingConfig.VALIDATED_SETTING;

public class TestHelloWorldExtension extends OpenSearchTestCase {

private HelloWorldExtension extension;
Expand Down Expand Up @@ -237,4 +239,16 @@ public void onFailure(Exception e) {

assertEquals("failed to find action [" + UnregisteredAction.INSTANCE + "] to execute", ex.getMessage());
}

public void testValidatedSettings() {
final String expected = randomAlphaOfLengthBetween(1, 5);
final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build());
assertEquals(expected, actual);

final IllegalArgumentException exception = expectThrows(
IllegalArgumentException.class,
() -> VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build())
);
assertEquals("Setting must not contain [forbidden]", exception.getMessage());
}
}

0 comments on commit ec2e7b2

Please sign in to comment.