diff options
author | Jim Hahn <jrh3@att.com> | 2021-04-27 09:52:51 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-04-29 11:35:23 -0400 |
commit | b64fcd93b2a5c11ffac61c9a09d52f1de18c05ff (patch) | |
tree | 7a3c65d29882705ac38997931b5d8f471a6a5ed3 /main/src/test | |
parent | 51a62bd019f1f59ee9db4872f332f1d228cef4d5 (diff) |
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>
Diffstat (limited to 'main/src/test')
4 files changed, 30 insertions, 74 deletions
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 74562260..28982489 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 static org.junit.Assert.assertTrue; 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 @@ public class TestApiParameterGroup { 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 @@ public class TestApiParameterGroup { 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 @@ public class TestApiParameterGroup { 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 @@ public class TestApiParameterGroup { 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 @@ public class TestApiParameterGroup { 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 @@ public class TestApiParameterGroup { 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 @@ public class TestApiParameterGroup { 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 5e1b7eb2..5443e8c6 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 @@ public class TestApiParameterHandler { 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 @@ public class TestApiParameterHandler { } @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 cfc2a30e..f7e43b8d 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.junit.Test; 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 @@ public class ApiDatabaseInitializerTest { 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 eca803ea..00000000 --- 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 |