diff options
Diffstat (limited to 'main/src')
4 files changed, 49 insertions, 93 deletions
diff --git a/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterGroup.java b/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterGroup.java index c5b81af1..12fec484 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterGroup.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterGroup.java @@ -22,36 +22,35 @@ package org.onap.policy.pdpx.main.parameters; import lombok.Getter; -import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.ValidationStatus; -import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.annotations.Valid; +import org.onap.policy.models.base.Validated; /** * Class to hold all parameters needed for xacml pdp component. * */ @Getter -public class XacmlPdpParameterGroup implements ParameterGroup { - private static final String PARAM_REST_SERVER = "restServerParameters"; +@NotNull +@NotBlank +public class XacmlPdpParameterGroup extends ParameterGroupImpl { private static final String PARAM_POLICY_API = "policyApiParameters"; - private static final String PARAM_TOPIC_PARAMETER_GROUP = "topicParameterGroup"; - private static final String PARAM_APPLICATION_PATH = "applicationPath"; - - private static final String ERROR_MSG = "must be a non-blank string"; - - @Setter - private String name; private String pdpGroup; private String pdpType; + @Valid private RestServerParameters restServerParameters; + @Valid private BusTopicParams policyApiParameters; + @Valid private TopicParameterGroup topicParameterGroup; private String applicationPath; @@ -64,7 +63,7 @@ public class XacmlPdpParameterGroup implements ParameterGroup { public XacmlPdpParameterGroup(final String name, final String pdpGroup, final String pdpType, final RestServerParameters restServerParameters, final BusTopicParams policyApiParameters, final TopicParameterGroup topicParameterGroup, final String applicationPath) { - this.name = name; + super(name); this.pdpGroup = pdpGroup; this.pdpType = pdpType; this.restServerParameters = restServerParameters; @@ -79,43 +78,15 @@ public class XacmlPdpParameterGroup implements ParameterGroup { * @return the result of the validation */ @Override - public GroupValidationResult validate() { - final GroupValidationResult validationResult = new GroupValidationResult(this); - if (!ParameterValidationUtils.validateStringParameter(name)) { - validationResult.setResult("name", ValidationStatus.INVALID, ERROR_MSG); - } - if (!ParameterValidationUtils.validateStringParameter(pdpGroup)) { - validationResult.setResult("pdpGroup", ValidationStatus.INVALID, ERROR_MSG); - } - if (!ParameterValidationUtils.validateStringParameter(pdpType)) { - validationResult.setResult("pdpType", ValidationStatus.INVALID, ERROR_MSG); - } - if (restServerParameters == null) { - validationResult.setResult(PARAM_REST_SERVER, ValidationStatus.INVALID, - "must have restServerParameters to configure xacml pdp rest server"); - } else { - validationResult.setResult(PARAM_REST_SERVER, restServerParameters.validate()); - } - if (policyApiParameters == null) { - validationResult.setResult(PARAM_POLICY_API, ValidationStatus.INVALID, - "must have policyApiParameters to configure xacml pdp rest server"); - } else if (StringUtils.isBlank(policyApiParameters.getHostname())) { - validationResult.setResult(PARAM_POLICY_API, ValidationStatus.INVALID, - "must have hostname to configure xacml pdp api client"); - } - if (topicParameterGroup == null) { - validationResult.setResult(PARAM_TOPIC_PARAMETER_GROUP, ValidationStatus.INVALID, - "must have topicParameterGroup to configure xacml pdp topic sink and source"); - } else { - validationResult.setResult(PARAM_TOPIC_PARAMETER_GROUP, topicParameterGroup.validate()); - } - // - // Validate the application path directory - // - if (applicationPath == null || applicationPath.isEmpty()) { - validationResult.setResult(PARAM_APPLICATION_PATH, ValidationStatus.INVALID, - "must have application path for applications to store policies and data."); + public BeanValidationResult validate() { + final BeanValidationResult validationResult = super.validate(); + + if (policyApiParameters != null && StringUtils.isBlank(policyApiParameters.getHostname())) { + BeanValidationResult sub = new BeanValidationResult(PARAM_POLICY_API, policyApiParameters); + sub.addResult("hostname", policyApiParameters.getHostname(), ValidationStatus.INVALID, Validated.IS_NULL); + validationResult.addResult(sub); } + return validationResult; } } diff --git a/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterHandler.java b/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterHandler.java index 9aaf3dae..aac05752 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterHandler.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlPdpParameterHandler.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. 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,7 +23,7 @@ package org.onap.policy.pdpx.main.parameters; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.FileReader; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.pdpx.main.PolicyXacmlPdpException; import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments; import org.slf4j.Logger; @@ -69,7 +69,7 @@ public class XacmlPdpParameterHandler { } // validate the parameters - final GroupValidationResult validationResult = xacmlPdpParameterGroup.validate(); + final ValidationResult validationResult = xacmlPdpParameterGroup.validate(); if (!validationResult.isValid()) { String returnMessage = "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n"; diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java index 1c4cf1d0..b2d1cbe3 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java @@ -36,7 +36,7 @@ import org.junit.rules.TemporaryFolder; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; -import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationResult; /** * Class to perform unit test of XacmlPdpParameterGroup. @@ -67,7 +67,7 @@ public class TestXacmlPdpParameterGroup { new XacmlPdpParameterGroup(CommonTestData.PDPX_PARAMETER_GROUP_NAME, CommonTestData.PDPX_GROUP, "flavor", restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertThat(validationResult.getResult()).isNull(); assertTrue(validationResult.isValid()); assertEquals(restServerParameters.getHost(), pdpxParameters.getRestServerParameters().getHost()); @@ -91,11 +91,10 @@ public class TestXacmlPdpParameterGroup { final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup(null, CommonTestData.PDPX_GROUP, null, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, pdpxParameters.getName()); - assertTrue(validationResult.getResult().contains( - "field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + "must be a non-blank string")); + assertThat(validationResult.getResult()).contains("\"name\" value \"null\" INVALID, is null"); } @Test @@ -109,11 +108,10 @@ public class TestXacmlPdpParameterGroup { final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup("", CommonTestData.PDPX_GROUP, CommonTestData.PDPX_TYPE, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", pdpxParameters.getName()); - assertTrue(validationResult.getResult().contains( - "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); + assertThat(validationResult.getResult()).contains("\"name\" value \"\" INVALID, is blank"); } @Test @@ -127,12 +125,10 @@ public class TestXacmlPdpParameterGroup { final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup(CommonTestData.PDPX_PARAMETER_GROUP_NAME, null, null, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, pdpxParameters.getPdpGroup()); - assertTrue(validationResult.getResult() - .contains("field \"pdpGroup\" type \"java.lang.String\" value \"null\" INVALID, " - + "must be a non-blank string")); + assertThat(validationResult.getResult()).contains("\"pdpGroup\" value \"null\" INVALID, is null"); } @Test @@ -146,12 +142,10 @@ public class TestXacmlPdpParameterGroup { final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup(CommonTestData.PDPX_PARAMETER_GROUP_NAME, "", null, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", pdpxParameters.getPdpGroup()); - assertTrue(validationResult.getResult() - .contains("field \"pdpGroup\" type \"java.lang.String\" value \"\" INVALID, " - + "must be a non-blank string")); + assertThat(validationResult.getResult()).contains("\"pdpGroup\" value \"\" INVALID, is blank"); } @Test @@ -166,10 +160,9 @@ public class TestXacmlPdpParameterGroup { new XacmlPdpParameterGroup(CommonTestData.PDPX_PARAMETER_GROUP_NAME, CommonTestData.PDPX_GROUP, null, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); - assertTrue(validationResult.getResult() - .contains("parameter group \"RestServerParameters\"")); + assertThat(validationResult.getResult()).contains("\"RestServerParameters\""); } @Test @@ -184,10 +177,9 @@ public class TestXacmlPdpParameterGroup { new XacmlPdpParameterGroup(CommonTestData.PDPX_PARAMETER_GROUP_NAME, CommonTestData.PDPX_GROUP, null, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); - assertThat(validationResult.getResult()) - .contains("field \"policyApiParameters\""); + assertThat(validationResult.getResult()).contains("\"policyApiParameters\""); } @Test @@ -202,9 +194,8 @@ public class TestXacmlPdpParameterGroup { new XacmlPdpParameterGroup(CommonTestData.PDPX_PARAMETER_GROUP_NAME, CommonTestData.PDPX_GROUP, null, restServerParameters, policyApiParameters, topicParameterGroup, applicationPath.getAbsolutePath()); - final GroupValidationResult validationResult = pdpxParameters.validate(); + final ValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); - assertTrue(validationResult.getResult() - .contains("parameter group \"TopicParameterGroup\"")); + assertThat(validationResult.getResult()).contains("\"TopicParameterGroup\""); } } diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java index 1b62965d..570b4319 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java @@ -94,18 +94,12 @@ public class TestXacmlPdpParameterHandler { noArguments.parse(noArgumentString); assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments)) - .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\n" - + "parameter group \"null\" type " - + "\"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\"" - + " INVALID, parameter group has status INVALID\n" - + " field \"name\" type \"java.lang.String\" value \"null\" INVALID, " - + "must be a non-blank string\n" - + " field \"pdpGroup\" type \"java.lang.String\" value \"null\" INVALID, " - + "must be a non-blank string\n" - + " field \"pdpType\" type \"java.lang.String\" value \"null\" INVALID, " - + "must be a non-blank string\n" - + " field \"applicationPath\" type \"java.lang.String\" value \"null\" INVALID, " - + "must have application path for applications to store policies and data.\n"); + .hasMessageContaining("validation error(s) on parameters from \"parameters/NoParameters.json\"", + "\"XacmlPdpParameterGroup\" INVALID, item has status INVALID", + "\"name\" value \"null\" INVALID, is null", + "\"pdpGroup\" value \"null\" INVALID, is null", + "\"pdpType\" value \"null\" INVALID, is null", + "\"applicationPath\" value \"null\" INVALID, is null"); } @Test @@ -142,20 +136,20 @@ public class TestXacmlPdpParameterHandler { arguments.parse(xacmlPdpConfigParameters); assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining( - "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"); + "\"name\" value \" \" INVALID, is blank"); xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_InvalidPdpGroup.json"; arguments.parse(xacmlPdpConfigParameters); assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining( - "field \"pdpGroup\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"); + "\"pdpGroup\" value \" \" INVALID, is blank"); xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_InvalidPdpType.json"; arguments.parse(xacmlPdpConfigParameters); assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining( - "field \"pdpType\" type \"java.lang.String\" value \"\" INVALID, must be a non-blank string"); + "\"pdpType\" value \"\" INVALID, is blank"); } @Test |