From 00ba7a7368e588f8d8268b40d3d5f95f3dd0b647 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Mon, 30 Dec 2024 21:09:40 -0500 Subject: [PATCH] Adds test for recipient registry Signed-off-by: Darshit Chanpura --- .../resources/RecipientTypeRegistry.java | 2 +- .../resources/RecipientTypeRegistryTests.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistryTests.java diff --git a/server/src/main/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistry.java b/server/src/main/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistry.java index 7436093e1b257..e0c56d6f7c2c1 100644 --- a/server/src/main/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistry.java +++ b/server/src/main/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistry.java @@ -26,7 +26,7 @@ public static void registerRecipientType(String key, RecipientType recipientType public static RecipientType fromValue(String value) { RecipientType type = REGISTRY.get(value); if (type == null) { - throw new IllegalArgumentException("Unknown RecipientType: " + value); + throw new IllegalArgumentException("Unknown RecipientType: " + value + ". Must be 1 of these: " + REGISTRY.values()); } return type; } diff --git a/server/src/test/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistryTests.java b/server/src/test/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistryTests.java new file mode 100644 index 0000000000000..8ed5ebaba2e2d --- /dev/null +++ b/server/src/test/java/org/opensearch/accesscontrol/resources/RecipientTypeRegistryTests.java @@ -0,0 +1,32 @@ +/* + * 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.accesscontrol.resources; + +import org.opensearch.test.OpenSearchTestCase; +import org.hamcrest.MatcherAssert; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +public class RecipientTypeRegistryTests extends OpenSearchTestCase { + + public void testFromValue() { + RecipientTypeRegistry.registerRecipientType("ble1", new RecipientType("ble1")); + RecipientTypeRegistry.registerRecipientType("ble2", new RecipientType("ble2")); + + // Valid Value + RecipientType type = RecipientTypeRegistry.fromValue("ble1"); + assertNotNull(type); + assertEquals("ble1", type.getType()); + + // Invalid Value + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> RecipientTypeRegistry.fromValue("bleble")); + MatcherAssert.assertThat("Unknown RecipientType: bleble. Must be 1 of these: [ble1, ble2]", is(equalTo(exception.getMessage()))); + } +}