Remove GroupValidationResult
Removed GroupValidationResult, replacing it with BeanValidationResult.
Modified the ParameterGroup subclasses to use BeanValidator, adding
annotations where needed to trigger the validations that had been
automatically performed by GroupValidationResult.
Issue-ID: POLICY-2059
Change-Id: I774950e98c9421064bacf735e0f5a5ab18540570
Signed-off-by: Jim Hahn <jrh3@att.com>
diff --git a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java
index c063d60..c9b96e3 100644
--- a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java
+++ b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ * Modifications Copyright (C) 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.
@@ -22,12 +23,11 @@
import java.util.List;
import lombok.Getter;
-import lombok.Setter;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
-import org.onap.policy.common.parameters.GroupValidationResult;
-import org.onap.policy.common.parameters.ParameterGroup;
-import org.onap.policy.common.parameters.ValidationStatus;
-import org.onap.policy.common.utils.validation.ParameterValidationUtils;
+import org.onap.policy.common.parameters.ParameterGroupImpl;
+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.provider.PolicyModelsProviderParameters;
/**
@@ -35,14 +35,14 @@
*
*/
@Getter
-public class ApiParameterGroup implements ParameterGroup {
+public class ApiParameterGroup extends ParameterGroupImpl {
- @Setter
- private String name;
+ @NotNull @Valid
private final RestServerParameters restServerParameters;
+ @NotNull @Valid
private final PolicyModelsProviderParameters databaseProviderParameters;
- private final List<String> preloadPolicyTypes;
- private final List<String> preloadPolicies;
+ private final List<@NotNull @NotBlank String> preloadPolicyTypes;
+ private final List<@NotNull @NotBlank String> preloadPolicies;
/**
* Create the api parameter group.
@@ -56,36 +56,10 @@
public ApiParameterGroup(final String name, final RestServerParameters restServerParameters,
final PolicyModelsProviderParameters databaseProviderParameters, final List<String> preloadPolicyTypes,
final List<String> preloadPolicies) {
- this.name = name;
+ super(name);
this.restServerParameters = restServerParameters;
this.databaseProviderParameters = databaseProviderParameters;
this.preloadPolicyTypes = preloadPolicyTypes;
this.preloadPolicies = preloadPolicies;
}
-
- /**
- * Validate the parameter group.
- *
- * @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, "must be a non-blank string");
- }
- if (restServerParameters == null) {
- validationResult.setResult("restServerParameters", ValidationStatus.INVALID,
- "must have restServerParameters to configure api rest server");
- } else {
- validationResult.setResult("restServerParameters", restServerParameters.validate());
- }
- if (databaseProviderParameters == null) {
- validationResult.setResult("databaseProviderParameters", ValidationStatus.INVALID,
- "must have databaseProviderParameters to configure api rest server");
- } else {
- validationResult.setResult("databaseProviderParameters", databaseProviderParameters.validate());
- }
- return validationResult;
- }
}
diff --git a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java
index b660b88..813dd75 100644
--- a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java
+++ b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java
@@ -1,9 +1,9 @@
/*-
* ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
+ * ONAP Policy API
+ * ================================================================================
* Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * 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.
@@ -28,7 +28,7 @@
import java.io.FileReader;
import org.onap.policy.api.main.exception.PolicyApiException;
import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
-import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,7 +38,7 @@
public class ApiParameterHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiParameterHandler.class);
-
+
/**
* Read the parameters from the parameter file.
*
@@ -70,7 +70,7 @@
}
// validate the parameters
- final GroupValidationResult validationResult = apiParameterGroup.validate();
+ final ValidationResult validationResult = apiParameterGroup.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/api/main/parameters/TestApiParameterGroup.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java
index 7456226..2898248 100644
--- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java
+++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java
@@ -3,7 +3,7 @@
* ONAP Policy API
* ================================================================================
* Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * 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,6 +23,7 @@
package org.onap.policy.api.main.parameters;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -30,7 +31,7 @@
import java.util.Collections;
import org.junit.Test;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
-import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
/**
@@ -48,7 +49,7 @@
commonTestData.getDatabaseProviderParameters(false);
final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertTrue(validationResult.isValid());
assertEquals(restServerParameters.getHost(), apiParameters.getRestServerParameters().getHost());
assertEquals(restServerParameters.getPort(), apiParameters.getRestServerParameters().getPort());
@@ -69,12 +70,10 @@
commonTestData.getDatabaseProviderParameters(false);
final ApiParameterGroup apiParameters = new ApiParameterGroup(null, restServerParameters,
databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertFalse(validationResult.isValid());
assertEquals(null, apiParameters.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
@@ -84,11 +83,10 @@
commonTestData.getDatabaseProviderParameters(false);
final ApiParameterGroup apiParameters = new ApiParameterGroup("", restServerParameters,
databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertFalse(validationResult.isValid());
assertEquals("", apiParameters.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
@@ -98,11 +96,10 @@
commonTestData.getDatabaseProviderParameters(false);
final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertFalse(validationResult.isValid());
- assertTrue(validationResult.getResult()
- .contains("\"org.onap.policy.common.endpoints.parameters.RestServerParameters\" INVALID, "
- + "parameter group has status INVALID"));
+ assertThat(validationResult.getResult())
+ .contains("\"RestServerParameters\" INVALID, item has status INVALID");
}
@Test
@@ -112,10 +109,10 @@
commonTestData.getDatabaseProviderParameters(false);
final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertFalse(validationResult.isValid());
- assertTrue(validationResult.getResult()
- .contains("must have restServerParameters to configure api rest server"));
+ assertThat(validationResult.getResult())
+ .contains("item \"restServerParameters\" value \"null\" INVALID, is null");
}
@@ -126,11 +123,10 @@
commonTestData.getDatabaseProviderParameters(true);
final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertFalse(validationResult.isValid());
- assertTrue(validationResult.getResult()
- .contains("\"org.onap.policy.models.provider.PolicyModelsProviderParameters\" INVALID, "
- + "parameter group has status INVALID"));
+ assertThat(validationResult.getResult())
+ .contains("\"PolicyModelsProviderParameters\" INVALID, item has status INVALID");
}
@Test
@@ -139,10 +135,10 @@
final PolicyModelsProviderParameters databaseProviderParameters = null;
final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final GroupValidationResult validationResult = apiParameters.validate();
+ final ValidationResult validationResult = apiParameters.validate();
assertFalse(validationResult.isValid());
- assertTrue(validationResult.getResult()
- .contains("must have databaseProviderParameters to configure api rest server"));
+ assertThat(validationResult.getResult())
+ .contains("item \"databaseProviderParameters\" value \"null\" INVALID, is null");
}
@Test
diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java
index 5e1b7eb..5443e8c 100644
--- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java
+++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java
@@ -1,7 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,13 +22,11 @@
package org.onap.policy.api.main.parameters;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import org.junit.Test;
import org.onap.policy.api.main.exception.PolicyApiException;
import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
@@ -105,16 +103,8 @@
final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
noArguments.parse(noArgumentString);
- try {
- new ApiParameterHandler().getParameters(noArguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- String expMsg = "validation error(s) on parameters from \"parameters/NoParameters.json\"\nparameter group "
- + "\"null\" type \"org.onap.policy.api.main.parameters.ApiParameterGroup\" INVALID, parameter "
- + "group has status INVALID\n"
- + " field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string\n";
- assertEquals(expMsg, e.getMessage());
- }
+ assertThatThrownBy(() -> new ApiParameterHandler().getParameters(noArguments))
+ .hasMessageContaining("\"name\"", "is null");
}
@Test
@@ -137,36 +127,13 @@
}
@Test
- public void testApiParameterGroup_InvalidName() throws PolicyApiException, CommandLineException {
+ public void testApiParameterGroup_Invalid() throws PolicyApiException, CommandLineException {
final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidName.json"};
final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
arguments.parse(apiConfigParameters);
- try {
- new ApiParameterHandler().getParameters(arguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- assertTrue(e.getMessage().contains(
- "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"));
- }
- }
-
- @Test
- public void testApiParameterGroup_InvalidRestServerParameters()
- throws PolicyApiException, IOException, CommandLineException {
- final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- arguments.parse(apiConfigParameters);
-
- try {
- new ApiParameterHandler().getParameters(arguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- final String expectedResult = new String(Files.readAllBytes(
- Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")))
- .replaceAll("\\s+", "");
- assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", ""));
- }
+ assertThatThrownBy(() -> new ApiParameterHandler().getParameters(arguments)).hasMessageContaining("\"name\"",
+ "is null");
}
@Test
diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
index cfc2a30..f7e43b8 100644
--- a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
+++ b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-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.
@@ -30,7 +30,7 @@
import org.onap.policy.api.main.exception.PolicyApiException;
import org.onap.policy.api.main.parameters.ApiParameterGroup;
import org.onap.policy.api.main.parameters.CommonTestData;
-import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.provider.PolicyModelsProviderFactory;
@@ -49,7 +49,7 @@
COMMON_TEST_DATA.makeParameters(PARAM_FILE, "src/test/resources/parameters/ApiConfigParametersXXX.json", 6969);
params = new StandardCoder().decode(new File(PARAM_FILE), ApiParameterGroup.class);
- GroupValidationResult result = params.validate();
+ ValidationResult result = params.validate();
if (!result.isValid()) {
fail(result.getResult());
}
diff --git a/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt b/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt
deleted file mode 100644
index eca803e..0000000
--- a/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-validation error(s) on parameters from "parameters/ApiConfigParameters_InvalidRestServerParameters.json"
-parameter group "ApiGroup" type "org.onap.policy.api.main.parameters.ApiParameterGroup" INVALID, parameter group has status INVALID
- parameter group "RestServerParameters" type "org.onap.policy.common.endpoints.parameters.RestServerParameters" INVALID, parameter group has status INVALID
- field "host" type "java.lang.String" value "" INVALID, must be a non-blank string
- field "port" type "int" value "-1" INVALID, must be >= 1
- field "userName" type "java.lang.String" value "" INVALID, must be a non-blank string
- field "password" type "java.lang.String" value "" INVALID, must be a non-blank string
\ No newline at end of file