aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/test/java
diff options
context:
space:
mode:
authorwaynedunican <wayne.dunican@est.tech>2024-08-20 08:27:23 +0100
committerwaynedunican <wayne.dunican@est.tech>2024-09-03 16:03:15 +0100
commitc780793359e31dac7d99731360cf1e575485f638 (patch)
tree897f61ba970728a80a75c35cac23033f68f941b4 /common-parameters/src/test/java
parent460b684f934776e9c4eefab00d1febb398e87bb1 (diff)
Increase coverage in common
Issue-ID: POLICY-5106 Change-Id: I54dfcc2ab1100e959ee786ce7a9791ef3465686f Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'common-parameters/src/test/java')
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java93
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterServiceTest.java161
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java91
3 files changed, 345 insertions, 0 deletions
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java
new file mode 100644
index 00000000..d90b13d4
--- /dev/null
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java
@@ -0,0 +1,93 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class ParameterGroupTest {
+
+ private ParameterGroup parameterGroup;
+
+ @BeforeEach
+ void setUp() {
+ parameterGroup = new ParameterGroup() {
+ private String name;
+ private BeanValidationResult validationResult = new BeanValidationResult("testGroup", "testObject");
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+ @Override
+ public BeanValidationResult validate() {
+ return validationResult;
+ }
+ };
+ }
+
+ @Test
+ void testGetName() {
+ String testName = "TestGroupName";
+ parameterGroup.setName(testName);
+ assertEquals(testName, parameterGroup.getName(), "The group name should match the one set");
+ }
+
+ @Test
+ void testSetName() {
+ String testName = "AnotherGroupName";
+ parameterGroup.setName(testName);
+ assertEquals(testName, parameterGroup.getName(), "The group name should match the one set");
+ }
+
+ @Test
+ void testValidate() {
+ BeanValidationResult result = parameterGroup.validate();
+ assertNotNull(result, "The validation result should not be null");
+ assertEquals("testGroup", result.getName(), "The validation result should have the correct group name");
+ }
+
+ @Test
+ void testIsValid() {
+ BeanValidationResult mockValidationResult = mock(BeanValidationResult.class);
+ ValidationStatus mockStatus = mock(ValidationStatus.class);
+
+ when(mockStatus.isValid()).thenReturn(true);
+ when(mockValidationResult.getStatus()).thenReturn(mockStatus);
+
+ ParameterGroup mockedParameterGroup = spy(parameterGroup);
+ doReturn(mockValidationResult).when(mockedParameterGroup).validate();
+
+ assertTrue(mockedParameterGroup.isValid(), "The parameters should be valid");
+ }
+}
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterServiceTest.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterServiceTest.java
new file mode 100644
index 00000000..7091f568
--- /dev/null
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/ParameterServiceTest.java
@@ -0,0 +1,161 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Map;
+import java.util.Set;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class ParameterServiceTest {
+
+ private static class TestParameterGroup implements ParameterGroup {
+ private final String name;
+
+ TestParameterGroup(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String getName() {
+ return this.name;
+ }
+
+ @Override
+ public void setName(String name) {
+ // do nothing
+ }
+
+ @Override
+ public BeanValidationResult validate() {
+ return null;
+ }
+
+ @Override
+ public boolean isValid() {
+ return ParameterGroup.super.isValid();
+ }
+ }
+
+ @BeforeEach
+ void setUp() {
+ ParameterService.clear();
+ }
+
+ @Test
+ void testRegisterAndRetrieveParameterGroup() {
+ TestParameterGroup group = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group);
+ ParameterGroup retrievedGroup = ParameterService.get("testGroup");
+
+ assertEquals(group, retrievedGroup, "The retrieved group should be the same as the registered group.");
+ }
+
+ @Test
+ void testRegisterDuplicateParameterGroupThrowsException() {
+ TestParameterGroup group = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group);
+
+ TestParameterGroup testGroup = new TestParameterGroup("testGroup");
+ assertThrows(ParameterRuntimeException.class, () -> {
+ ParameterService.register(testGroup);
+ }, "Registering a duplicate parameter group should throw an exception.");
+ }
+
+ @Test
+ void testRegisterWithOverwrite() {
+ TestParameterGroup group1 = new TestParameterGroup("testGroup");
+ TestParameterGroup group2 = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group1);
+ ParameterService.register(group2, true); // Overwrite the existing group
+
+ ParameterGroup retrievedGroup = ParameterService.get("testGroup");
+ assertEquals(group2, retrievedGroup,
+ "The retrieved group should be the newly registered group after overwrite.");
+ }
+
+ @Test
+ void testDeregisterParameterGroupByName() {
+ TestParameterGroup group = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group);
+ ParameterService.deregister("testGroup");
+
+ assertThrows(ParameterRuntimeException.class, () -> {
+ ParameterService.get("testGroup");
+ }, "Deregistering a parameter group should remove it from the service.");
+ }
+
+ @Test
+ void testDeregisterParameterGroupByInstance() {
+ TestParameterGroup group = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group);
+ ParameterService.deregister(group);
+
+ assertThrows(ParameterRuntimeException.class, () -> {
+ ParameterService.get("testGroup");
+ }, "Deregistering a parameter group by instance should remove it from the service.");
+ }
+
+ @Test
+ void testContainsParameterGroup() {
+ TestParameterGroup group = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group);
+
+ assertTrue(ParameterService.contains("testGroup"), "The parameter group should be contained in the service.");
+ assertFalse(ParameterService.contains("nonExistentGroup"),
+ "A non-existent parameter group should not be contained in the service.");
+ }
+
+ @Test
+ void testGetAllParameterGroups() {
+ TestParameterGroup group1 = new TestParameterGroup("group1");
+ TestParameterGroup group2 = new TestParameterGroup("group2");
+
+ ParameterService.register(group1);
+ ParameterService.register(group2);
+
+ Set<Map.Entry<String, ParameterGroup>> allGroups = ParameterService.getAll();
+ assertEquals(2, allGroups.size(), "There should be exactly 2 parameter groups in the service.");
+ assertTrue(allGroups.stream().anyMatch(entry -> entry.getKey().equals("group1")),
+ "The service should contain group1.");
+ assertTrue(allGroups.stream().anyMatch(entry -> entry.getKey().equals("group2")),
+ "The service should contain group2.");
+ }
+
+ @Test
+ void testClearParameterGroups() {
+ TestParameterGroup group = new TestParameterGroup("testGroup");
+
+ ParameterService.register(group);
+ ParameterService.clear();
+
+ assertFalse(ParameterService.contains("testGroup"), "All parameter groups should be cleared from the service.");
+ }
+}
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java
new file mode 100644
index 00000000..0c7f29b7
--- /dev/null
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java
@@ -0,0 +1,91 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.validation;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import jakarta.validation.ConstraintValidatorContext;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ParameterGroup;
+
+class ParameterGroupValidatorTest {
+
+ private ParameterGroupValidator validator;
+
+ @Mock
+ private ParameterGroup mockParameterGroup;
+
+ @Mock
+ private BeanValidationResult mockBeanValidationResult;
+
+ @Mock
+ private ConstraintValidatorContext mockContext;
+
+ @Mock
+ private ConstraintValidatorContext.ConstraintViolationBuilder mockViolationBuilder;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ validator = new ParameterGroupValidator();
+ }
+
+ @Test
+ void testIsValid_NullValue() {
+ boolean result = validator.isValid(null, mockContext);
+ assertTrue(result, "Expected isValid to return true when value is null");
+ }
+
+ @Test
+ void testIsValid_ValidParameterGroup() {
+ when(mockParameterGroup.validate()).thenReturn(mockBeanValidationResult);
+ when(mockBeanValidationResult.isValid()).thenReturn(true);
+
+ boolean result = validator.isValid(mockParameterGroup, mockContext);
+ assertTrue(result, "Expected isValid to return true when ParameterGroup is valid");
+
+ verify(mockContext, never()).buildConstraintViolationWithTemplate(anyString());
+ }
+
+ @Test
+ void testIsValid_InvalidParameterGroup() {
+ when(mockParameterGroup.validate()).thenReturn(mockBeanValidationResult);
+ when(mockBeanValidationResult.isValid()).thenReturn(false);
+ when(mockBeanValidationResult.getMessage()).thenReturn("Invalid parameters");
+ when(mockContext.buildConstraintViolationWithTemplate(anyString())).thenReturn(mockViolationBuilder);
+
+ boolean result = validator.isValid(mockParameterGroup, mockContext);
+ assertFalse(result, "Expected isValid to return false when ParameterGroup is invalid");
+
+ InOrder inOrder = inOrder(mockContext, mockViolationBuilder);
+ inOrder.verify(mockContext).buildConstraintViolationWithTemplate("Invalid parameters");
+ inOrder.verify(mockViolationBuilder).addConstraintViolation();
+ }
+}