From 5b1af0c59861e81f501e8b304572faf2ed6799e0 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Wed, 18 Feb 2026 13:01:17 +0000 Subject: [PATCH] Refactor Parameter Group classes Remove unneeded ParameterGroup, ParameterGroupImpl classes. Replace ParameterGroupConstraint with standard annotations. Issue-ID: POLICY-5509 Change-Id: I329bad819d9930e68ece0ac7a6bb986462f75fe3 Signed-off-by: danielhanrahan --- .../clamp/acm/element/service/ConfigService.java | 5 +- .../acm/element/service/ConfigServiceTest.java | 10 ++- .../ParticipantIntermediaryParameters.java | 5 +- .../policy/common/parameters/ParameterGroup.java | 61 -------------- .../common/parameters/ParameterGroupImpl.java | 48 ----------- .../common/parameters/topic/BusTopicParams.java | 6 +- .../parameters/topic/TopicParameterGroup.java | 54 +------------ .../validation/ParameterGroupConstraint.java | 57 ------------- .../validation/ParameterGroupValidator.java | 42 ---------- .../common/message/bus/event/CommonTestData.java | 11 ++- .../message/bus/event/TopicParameterGroupTest.java | 31 +++----- .../common/parameters/ParameterGroupTest.java | 93 ---------------------- .../validation/ParameterGroupValidatorTest.java | 91 --------------------- .../annotations/VerifyKeyValidatorTest.java | 14 ++-- .../main/parameters/AcRuntimeParameterGroup.java | 3 +- 15 files changed, 46 insertions(+), 485 deletions(-) delete mode 100644 policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java delete mode 100644 policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java delete mode 100644 policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java delete mode 100644 policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java delete mode 100644 policy-common/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java delete mode 100644 policy-common/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java diff --git a/participant/participant-impl/participant-impl-acelement/src/main/java/org/onap/policy/clamp/acm/element/service/ConfigService.java b/participant/participant-impl/participant-impl-acelement/src/main/java/org/onap/policy/clamp/acm/element/service/ConfigService.java index 902f76c31..e7bacb0d3 100644 --- a/participant/participant-impl/participant-impl-acelement/src/main/java/org/onap/policy/clamp/acm/element/service/ConfigService.java +++ b/participant/participant-impl/participant-impl-acelement/src/main/java/org/onap/policy/clamp/acm/element/service/ConfigService.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022,2024 Nordix Foundation. + * Copyright (C) 2022-2026 OpenInfra Foundation Europe. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.onap.policy.clamp.acm.element.handler.MessageHandler; import org.onap.policy.clamp.acm.element.main.concepts.ElementConfig; import org.onap.policy.clamp.acm.element.main.parameters.ElementTopicParameters; import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException; +import org.onap.policy.common.parameters.BeanValidator; import org.onap.policy.common.parameters.topic.TopicParameterGroup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,7 +63,7 @@ public class ConfigService { parameters.setTopicSinks(List.of(publisherTopicParameters)); parameters.setTopicSources(List.of(listenerTopicParameters)); - if (!parameters.isValid()) { + if (!BeanValidator.validate(parameters).isValid()) { throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, "Validation failed for topic parameter group. Kafka config not activated"); } diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java index b0e823c1c..984148ec7 100644 --- a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022,2024 Nordix Foundation. + * Copyright (C) 2022-2026 OpenInfra Foundation Europe. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ package org.onap.policy.clamp.acm.element.service; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -30,6 +31,7 @@ import org.onap.policy.clamp.acm.element.handler.MessageActivator; import org.onap.policy.clamp.acm.element.handler.MessageHandler; import org.onap.policy.clamp.acm.element.main.concepts.ElementConfig; import org.onap.policy.clamp.acm.element.main.concepts.KafkaConfig; +import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException; import org.onap.policy.common.parameters.topic.TopicParameterGroup; class ConfigServiceTest { @@ -59,5 +61,11 @@ class ConfigServiceTest { verify(handler).deactivateElement(); assertThat(configService.getElementConfig()).isNotEqualTo(elementConfig); + + // validation fails due to blank parameter + elementConfig.getTopicParameterGroup().setTopicCommInfrastructure(""); + assertThatThrownBy(() -> configService.activateElement(elementConfig)) + .isInstanceOf(AutomationCompositionRuntimeException.class) + .hasMessageContaining("Validation failed for topic parameter group"); } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/parameters/ParticipantIntermediaryParameters.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/parameters/ParticipantIntermediaryParameters.java index aea2d7da3..fb4d18bdb 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/parameters/ParticipantIntermediaryParameters.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/parameters/ParticipantIntermediaryParameters.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved. + * Copyright (C) 2021-2026 OpenInfra Foundation Europe. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,6 @@ import lombok.Setter; import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType; import org.onap.policy.common.parameters.topic.TopicParameterGroup; import org.onap.policy.common.parameters.topic.TopicParameters; -import org.onap.policy.common.parameters.validation.ParameterGroupConstraint; /** * Class to hold all parameters needed for participant component. @@ -58,7 +57,7 @@ public class ParticipantIntermediaryParameters { private int threadPoolSize = 10; @NotNull - @ParameterGroupConstraint + @Valid private TopicParameterGroup clampAutomationCompositionTopics; @NotNull diff --git a/policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java b/policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java deleted file mode 100644 index c26b7b44d..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java +++ /dev/null @@ -1,61 +0,0 @@ -/*- - * ============LICENSE_START========================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * Modifications 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters; - -/** - * This interface acts as a base interface for all parameter groups in the ONAP Policy Framework. All parameter group - * POJOs are implementations of the parameter group interface and can be used with the {@link ParameterService}. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public interface ParameterGroup { - /** - * Get the group name. - * - * @return the group name - */ - String getName(); - - /** - * Set the group name. - * - * @param name the group name - */ - void setName(final String name); - - /** - * Validate parameters. - * - * @return the result of the parameter validation - */ - BeanValidationResult validate(); - - /** - * Check if the parameters are valid. - * - * @return true if the parameters are valid - */ - default boolean isValid() { - return validate().getStatus().isValid(); - } -} diff --git a/policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java b/policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java deleted file mode 100644 index 7fc0e5cf8..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2026 OpenInfra Foundation Europe. All rights reserved. - * ================================================================================ - * 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 jakarta.validation.constraints.NotBlank; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -/** - * Implementation of a parameter group. - */ -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -public class ParameterGroupImpl implements ParameterGroup { - /** - * Group name. - */ - @NotBlank - private String name; - - @Override - public BeanValidationResult validate() { - return BeanValidator.validate(this); - } -} diff --git a/policy-common/src/main/java/org/onap/policy/common/parameters/topic/BusTopicParams.java b/policy-common/src/main/java/org/onap/policy/common/parameters/topic/BusTopicParams.java index e83154c4a..adbe67e1b 100644 --- a/policy-common/src/main/java/org/onap/policy/common/parameters/topic/BusTopicParams.java +++ b/policy-common/src/main/java/org/onap/policy/common/parameters/topic/BusTopicParams.java @@ -4,7 +4,7 @@ * ================================================================================ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. * Modifications Copyright (C) 2018-2019, 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019, 2023-2024,2026 OpenInfra Foundation Europe. All rights reserved. + * Modifications Copyright (C) 2019-2026 OpenInfra Foundation Europe. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,8 @@ package org.onap.policy.common.parameters.topic; import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; import java.util.List; import java.util.Map; import lombok.AccessLevel; @@ -57,8 +59,10 @@ import org.apache.commons.lang3.StringUtils; public class BusTopicParams { private int port; + @NotEmpty private List servers; private Map additionalProps; + @NotBlank private String topic; private String effectiveTopic; private String apiKey; diff --git a/policy-common/src/main/java/org/onap/policy/common/parameters/topic/TopicParameterGroup.java b/policy-common/src/main/java/org/onap/policy/common/parameters/topic/TopicParameterGroup.java index 5e4f662aa..c410883a0 100644 --- a/policy-common/src/main/java/org/onap/policy/common/parameters/topic/TopicParameterGroup.java +++ b/policy-common/src/main/java/org/onap/policy/common/parameters/topic/TopicParameterGroup.java @@ -25,11 +25,8 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import java.util.List; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; -import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.common.parameters.ParameterGroupImpl; -import org.onap.policy.common.parameters.ValidationStatus; /** * Class to hold all parameters needed for topic properties. @@ -38,55 +35,10 @@ import org.onap.policy.common.parameters.ValidationStatus; */ @Getter @Setter -public class TopicParameterGroup extends ParameterGroupImpl { - +@NoArgsConstructor +public class TopicParameterGroup { @NotNull private List<@Valid TopicParameters> topicSources; @NotNull private List<@Valid TopicParameters> topicSinks; - - public TopicParameterGroup() { - super(TopicParameterGroup.class.getSimpleName()); - } - - /** - * {@inheritDoc}. - */ - @Override - public BeanValidationResult validate() { - BeanValidationResult result = super.validate(); - if (result.isValid()) { - var errorMsg = new StringBuilder(); - StringBuilder missingSourceParams = checkMissingMandatoryParams(topicSources); - if (!missingSourceParams.isEmpty()) { - errorMsg.append(missingSourceParams.append("missing in topicSources. ")); - } - StringBuilder missingSinkParams = checkMissingMandatoryParams(topicSinks); - if (!missingSinkParams.isEmpty()) { - errorMsg.append(missingSinkParams.append("missing in topicSinks.")); - } - - if (!errorMsg.isEmpty()) { - errorMsg.insert(0, "Mandatory parameters are missing. "); - result.setResult(ValidationStatus.INVALID, errorMsg.toString()); - } - } - return result; - } - - private StringBuilder checkMissingMandatoryParams(List topicParametersList) { - var missingParams = new StringBuilder(); - for (TopicParameters topicParameters : topicParametersList) { - if (StringUtils.isBlank(topicParameters.getTopic())) { - missingParams.append("topic, "); - } - if (StringUtils.isBlank(topicParameters.getTopicCommInfrastructure())) { - missingParams.append("topicCommInfrastructure, "); - } - if (null == topicParameters.getServers() || topicParameters.getServers().isEmpty()) { - missingParams.append("servers, "); - } - } - return missingParams; - } } diff --git a/policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java b/policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java deleted file mode 100644 index c73d135a0..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021, 2023 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters.validation; - -import jakarta.validation.Constraint; -import jakarta.validation.Payload; -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Documented -@Constraint(validatedBy = ParameterGroupValidator.class) -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface ParameterGroupConstraint { - - /** - * Get error Message. - * - * @return error Message - */ - String message() default "validation error(s) on parameters"; - - /** - * Get groups. - * - * @return Class arrays - */ - Class[] groups() default {}; - - /** - * Get payload. - * - * @return Class arrays - */ - Class[] payload() default {}; -} diff --git a/policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java b/policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java deleted file mode 100644 index 024ca1391..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java +++ /dev/null @@ -1,42 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021, 2023 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.parameters.validation; - -import jakarta.validation.ConstraintValidator; -import jakarta.validation.ConstraintValidatorContext; -import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; - -public class ParameterGroupValidator implements ConstraintValidator { - - @Override - public boolean isValid(ParameterGroup value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } - final BeanValidationResult result = value.validate(); - if (!result.isValid()) { - context.buildConstraintViolationWithTemplate(result.getMessage()).addConstraintViolation(); - } - return result.isValid(); - } - -} diff --git a/policy-common/src/test/java/org/onap/policy/common/message/bus/event/CommonTestData.java b/policy-common/src/test/java/org/onap/policy/common/message/bus/event/CommonTestData.java index ecd2f20cd..d2b1a36ff 100644 --- a/policy-common/src/test/java/org/onap/policy/common/message/bus/event/CommonTestData.java +++ b/policy-common/src/test/java/org/onap/policy/common/message/bus/event/CommonTestData.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019, 2024 Nordix Foundation. + * Copyright (C) 2019-2026 OpenInfra Foundation Europe. All rights reserved. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,7 +27,7 @@ import java.nio.file.Files; import java.util.List; import java.util.Map; import java.util.TreeMap; -import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.topic.TopicParameterGroup; import org.onap.policy.common.parameters.topic.TopicParameters; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -69,15 +69,14 @@ public class CommonTestData { * Converts the contents of a map to a parameter class. * * @param source property map - * @param clazz class of object to be created from the map * @return a new object represented by the map */ - public T toObject(final Map source, final Class clazz) { + public TopicParameterGroup toObject(final Map source) { try { - return coder.decode(coder.encode(source), clazz); + return coder.decode(coder.encode(source), TopicParameterGroup.class); } catch (final CoderException e) { - throw new RuntimeException("cannot create " + clazz.getName() + " from map", e); + throw new RuntimeException("cannot create TopicParameterGroup from map", e); } } diff --git a/policy-common/src/test/java/org/onap/policy/common/message/bus/event/TopicParameterGroupTest.java b/policy-common/src/test/java/org/onap/policy/common/message/bus/event/TopicParameterGroupTest.java index db28892ef..e50d78f41 100644 --- a/policy-common/src/test/java/org/onap/policy/common/message/bus/event/TopicParameterGroupTest.java +++ b/policy-common/src/test/java/org/onap/policy/common/message/bus/event/TopicParameterGroupTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2024 Nordix Foundation. + * Copyright (C) 2019-2026 OpenInfra Foundation Europe. All rights reserved. * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,6 @@ package org.onap.policy.common.message.bus.event; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.beans.PropertyDescriptor; @@ -32,6 +31,7 @@ import java.lang.reflect.Modifier; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; +import org.onap.policy.common.parameters.BeanValidator; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.topic.BusTopicParams; import org.onap.policy.common.parameters.topic.TopicParameterGroup; @@ -53,9 +53,8 @@ class TopicParameterGroupTest { @Test void test() throws CoderException { final TopicParameterGroup topicParameterGroup = - testData.toObject(testData.getTopicParameterGroupMap(false), TopicParameterGroup.class); - final ValidationResult validationResult = topicParameterGroup.validate(); - assertTrue(validationResult.isValid()); + testData.toObject(testData.getTopicParameterGroupMap(false)); + assertTrue(BeanValidator.isValid(topicParameterGroup)); assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSinks()); assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSources()); @@ -70,10 +69,8 @@ class TopicParameterGroupTest { @Test void testValidate() { final TopicParameterGroup topicParameterGroup = - testData.toObject(testData.getTopicParameterGroupMap(false), TopicParameterGroup.class); - final ValidationResult result = topicParameterGroup.validate(); - assertNull(result.getResult()); - assertTrue(result.isValid()); + testData.toObject(testData.getTopicParameterGroupMap(false)); + assertTrue(BeanValidator.isValid(topicParameterGroup)); } @Test @@ -81,9 +78,7 @@ class TopicParameterGroupTest { String json = testData.getParameterGroupAsString( packageDir + "TopicParameters_valid.json"); TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class); - final ValidationResult result = topicParameterGroup.validate(); - assertNull(result.getResult()); - assertTrue(result.isValid()); + assertTrue(BeanValidator.isValid(topicParameterGroup)); } @Test @@ -91,9 +86,7 @@ class TopicParameterGroupTest { String json = testData.getParameterGroupAsString( packageDir + "TopicParameters_invalid.json"); TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class); - final ValidationResult result = topicParameterGroup.validate(); - assertFalse(result.isValid()); - assertTrue(result.getResult().contains("INVALID")); + assertFalse(BeanValidator.isValid(topicParameterGroup)); } @Test @@ -101,8 +94,8 @@ class TopicParameterGroupTest { String json = testData.getParameterGroupAsString( packageDir + "TopicParameters_missing_mandatory.json"); TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class); - final ValidationResult result = topicParameterGroup.validate(); - assertTrue(result.getResult().contains("Mandatory parameters are missing")); + final ValidationResult result = BeanValidator.validate("", topicParameterGroup); + assertTrue(result.getResult().contains("\"topicSources[0].servers\" value \"[]\" INVALID, must not be empty")); assertFalse(result.isValid()); } @@ -111,9 +104,7 @@ class TopicParameterGroupTest { String json = testData.getParameterGroupAsString( packageDir + "TopicParameters_all_params.json"); TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class); - final ValidationResult result = topicParameterGroup.validate(); - assertNull(result.getResult()); - assertTrue(result.isValid()); + assertTrue(BeanValidator.isValid(topicParameterGroup)); assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSinks())); assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSources())); } diff --git a/policy-common/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java b/policy-common/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java deleted file mode 100644 index d90b13d4a..000000000 --- a/policy-common/src/test/java/org/onap/policy/common/parameters/ParameterGroupTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/*- - * ============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/policy-common/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java b/policy-common/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java deleted file mode 100644 index 0c7f29b7b..000000000 --- a/policy-common/src/test/java/org/onap/policy/common/parameters/validation/ParameterGroupValidatorTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/*- - * ============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(); - } -} diff --git a/policy-models/src/test/java/org/onap/policy/models/base/validation/annotations/VerifyKeyValidatorTest.java b/policy-models/src/test/java/org/onap/policy/models/base/validation/annotations/VerifyKeyValidatorTest.java index 886686593..2db9b58d2 100644 --- a/policy-models/src/test/java/org/onap/policy/models/base/validation/annotations/VerifyKeyValidatorTest.java +++ b/policy-models/src/test/java/org/onap/policy/models/base/validation/annotations/VerifyKeyValidatorTest.java @@ -40,10 +40,10 @@ class VerifyKeyValidatorTest { void testStandardAnnotation() { StdAnnotation data = new StdAnnotation(); data.strValue = STRING_VALUE; - assertThat(BeanValidator.validate("", data).getResult()).isNull(); + assertThat(BeanValidator.validate(data).getResult()).isNull(); data.strValue = null; - assertThat(BeanValidator.validate("", data).getResult()).contains("strValue", "null"); + assertThat(BeanValidator.validate(data).getResult()).contains("strValue", "null"); } @Test @@ -52,7 +52,7 @@ class VerifyKeyValidatorTest { // null key - Jakarta validation will include all constraint violations data.key = new PfConceptKey(); - assertThat(BeanValidator.validate("", data).getResult()) + assertThat(BeanValidator.validate(data).getResult()) .contains(KEY_FIELD, IS_A_NULL_KEY); // invalid version - should invoke cascade validation @@ -71,11 +71,11 @@ class VerifyKeyValidatorTest { // null name data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4"); - assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "name", IS_NULL); + assertThat(BeanValidator.validate(data).getResult()).contains(KEY_FIELD, "name", IS_NULL); // null version data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION); - assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "version", IS_NULL); + assertThat(BeanValidator.validate(data).getResult()).contains(KEY_FIELD, "version", IS_NULL); // null name, invalid version - should get two messages // Create object with invalid version using reflection to bypass setter validation @@ -97,14 +97,14 @@ class VerifyKeyValidatorTest { data.key = new PfConceptKey(); // totally invalid key // should be ok, since no validations are performed - assertThat(BeanValidator.validate("", data).getResult()).isNull(); + assertThat(BeanValidator.validate(data).getResult()).isNull(); } @Test void testVerifyKeyOnGetters() { GetterKeyAnnot data = new GetterKeyAnnot(); - var result = BeanValidator.validate("", data); + var result = BeanValidator.validate(data); assertThat(result.getResult()) .contains("nullKey", IS_A_NULL_KEY) .contains("nullVersionKey", "version", IS_NULL) diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/parameters/AcRuntimeParameterGroup.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/parameters/AcRuntimeParameterGroup.java index 67befe394..f1633c2f9 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/parameters/AcRuntimeParameterGroup.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/parameters/AcRuntimeParameterGroup.java @@ -25,7 +25,6 @@ import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; import org.onap.policy.common.parameters.topic.TopicParameterGroup; -import org.onap.policy.common.parameters.validation.ParameterGroupConstraint; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.validation.annotation.Validated; @@ -43,8 +42,8 @@ public class AcRuntimeParameterGroup { @NotNull private ParticipantParameters participantParameters; + @Valid @NotNull - @ParameterGroupConstraint private TopicParameterGroup topicParameterGroup; @Valid -- 2.16.6