diff options
Diffstat (limited to 'common-parameters/src/test')
11 files changed, 202 insertions, 282 deletions
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java index b65db953..b874db69 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/ExceptionTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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. * 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========================================================= */ @@ -27,32 +28,32 @@ import java.io.IOException; import org.junit.Test; public class ExceptionTest { + private static final String PARAMETER_EXCEPTION = "Parameter Exception"; + private static final String EXCEPTION_OBJECT = "Exception Object"; @Test public void testParameterException() { - assertEquals("Parameter Exception", new ParameterException("Parameter Exception").getMessage()); + assertEquals(PARAMETER_EXCEPTION, new ParameterException(PARAMETER_EXCEPTION).getMessage()); - String exceptionObject = "Exception Object"; - assertEquals("Exception Object", - new ParameterException("Parameter Exception", exceptionObject).getObject().toString()); + assertEquals(EXCEPTION_OBJECT, + new ParameterException(PARAMETER_EXCEPTION, EXCEPTION_OBJECT).getObject().toString()); Exception testException = new IOException("IO Exception"); assertEquals("Parameter Exception\ncaused by: Parameter Exception\ncaused by: IO Exception", - new ParameterException("Parameter Exception", testException, exceptionObject) + new ParameterException(PARAMETER_EXCEPTION, testException, EXCEPTION_OBJECT) .getCascadedMessage()); } @Test public void testParameterRuntimeException() { - assertEquals("Parameter Exception", new ParameterRuntimeException("Parameter Exception").getMessage()); + assertEquals(PARAMETER_EXCEPTION, new ParameterRuntimeException(PARAMETER_EXCEPTION).getMessage()); - String exceptionObject = "Exception Object"; - assertEquals("Exception Object", - new ParameterRuntimeException("Parameter Exception", exceptionObject).getObject().toString()); + assertEquals(EXCEPTION_OBJECT, + new ParameterRuntimeException(PARAMETER_EXCEPTION, EXCEPTION_OBJECT).getObject().toString()); Exception testException = new IOException("IO Exception"); assertEquals("Parameter Exception\ncaused by: Parameter Exception\ncaused by: IO Exception", - new ParameterRuntimeException("Parameter Exception", testException, exceptionObject) + new ParameterRuntimeException(PARAMETER_EXCEPTION, testException, EXCEPTION_OBJECT) .getCascadedMessage()); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidationResult.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidationResult.java index 30e2c0da..12cd80cb 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidationResult.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidationResult.java @@ -37,6 +37,7 @@ public class TestBeanValidationResult { private static final String NEXT_INDENT = "yy "; private static final String MID_INDENT = "xx yy "; private static final String NAME = "my-name"; + private static final String MY_LIST_INVALID = " 'my-list' INVALID, item has status INVALID\n "; private static final String BEAN_INVALID_MSG = requote("'my-name' INVALID, item has status INVALID\n"); private String cleanMsg; @@ -105,14 +106,14 @@ public class TestBeanValidationResult { list = Arrays.asList(invalid, invalid); assertFalse(bean.validateNotNullList(MY_LIST, list, item -> item)); assertFalse(bean.isValid()); - assertEquals(requote(BEAN_INVALID_MSG + " 'my-list' INVALID, item has status INVALID\n " + invalidMsg + assertEquals(requote(BEAN_INVALID_MSG + MY_LIST_INVALID + invalidMsg + " " + invalidMsg), bean.getResult()); } @Test public void testValidateNotNullList_NullList() { List<ValidationResult> list = null; - assertFalse(bean.validateNotNullList("my-list", list, item -> item)); + assertFalse(bean.validateNotNullList(MY_LIST, list, item -> item)); assertFalse(bean.isValid()); assertEquals(requote(BEAN_INVALID_MSG + " item 'my-list' value 'null' INVALID, is null\n"), bean.getResult()); @@ -137,14 +138,14 @@ public class TestBeanValidationResult { bean = new BeanValidationResult(NAME, OBJECT); assertFalse(bean.validateList(MY_LIST, list, item -> item)); assertFalse(bean.isValid()); - assertEquals(requote(BEAN_INVALID_MSG + " 'my-list' INVALID, item has status INVALID\n " + assertEquals(requote(BEAN_INVALID_MSG + MY_LIST_INVALID + "item 'item' value 'null' INVALID, null\n"), bean.getResult()); list = Arrays.asList(invalid, invalid); bean = new BeanValidationResult(NAME, OBJECT); assertFalse(bean.validateList(MY_LIST, list, item -> item)); assertFalse(bean.isValid()); - assertEquals(requote(BEAN_INVALID_MSG + " 'my-list' INVALID, item has status INVALID\n " + invalidMsg + assertEquals(requote(BEAN_INVALID_MSG + MY_LIST_INVALID + invalidMsg + " " + invalidMsg), bean.getResult()); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java index ae4dbd2c..abef5528 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestJsonInput.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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,16 +23,13 @@ package org.onap.policy.common.parameters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import com.google.gson.Gson; import com.google.gson.GsonBuilder; - import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; - import org.junit.Test; import org.onap.policy.common.parameters.testclasses.TestParametersL00; @@ -42,15 +40,10 @@ public class TestJsonInput { public void testJsonInput() throws IOException { TestParametersL00 testParameterGroup = null; - // Read the parameters - try { - // Read the parameters from JSON using Gson - final Gson gson = new GsonBuilder().create(); - testParameterGroup = gson.fromJson(new FileReader("src/test/resources/parameters/TestParameters.json"), - TestParametersL00.class); - } catch (final Exception e) { - fail("test should not throw an exception here: " + e.getMessage()); - } + // Read the parameters from JSON using Gson + final Gson gson = new GsonBuilder().create(); + testParameterGroup = gson.fromJson(new FileReader("src/test/resources/parameters/TestParameters.json"), + TestParametersL00.class); GroupValidationResult validationResult = testParameterGroup.validate(); assertTrue(validationResult.isValid()); diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java index 2dad4283..fda37816 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestParameterService.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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. @@ -20,99 +21,73 @@ package org.onap.policy.common.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; -import org.onap.policy.common.parameters.ParameterRuntimeException; -import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.parameters.testclasses.EmptyParameterGroup; public class TestParameterService { + private static final String EMPTY_GROUP = "Empty Group"; @Test public void testParameterService() { ParameterService.clear(); assertFalse(ParameterService.contains("EmptyGroup")); - try { - ParameterService.get("EmptyGroup"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("\"EmptyGroup\" not found in parameter service", e.getMessage()); - } - - ParameterService.register(new EmptyParameterGroup("Empty Group")); - assertTrue(ParameterService.contains("Empty Group")); - assertNotNull(ParameterService.get("Empty Group")); - - try { - ParameterService.register(new EmptyParameterGroup("Empty Group")); - fail("this test should throw an exception"); - } - catch (ParameterRuntimeException e) { - assertEquals("\"Empty Group\" already registered in parameter service", e.getMessage()); - } - - try { - ParameterService.register(new EmptyParameterGroup("Empty Group"), false); - fail("this test should throw an exception"); - } - catch (ParameterRuntimeException e) { - assertEquals("\"Empty Group\" already registered in parameter service", e.getMessage()); - } - - ParameterService.register(new EmptyParameterGroup("Empty Group"), true); - assertTrue(ParameterService.contains("Empty Group")); - - ParameterService.deregister("Empty Group"); - assertFalse(ParameterService.contains("Empty Group")); - - ParameterService.register(new EmptyParameterGroup("Empty Group"), true); - assertTrue(ParameterService.contains("Empty Group")); - - ParameterService.deregister("Empty Group"); - assertFalse(ParameterService.contains("Empty Group")); - - EmptyParameterGroup epg = new EmptyParameterGroup("Empty Group"); + + assertThatThrownBy(() -> ParameterService.get("EmptyGroup")) + .hasMessage("\"EmptyGroup\" not found in parameter service"); + + ParameterService.register(new EmptyParameterGroup(EMPTY_GROUP)); + assertTrue(ParameterService.contains(EMPTY_GROUP)); + assertNotNull(ParameterService.get(EMPTY_GROUP)); + + assertThatThrownBy(() -> ParameterService.register(new EmptyParameterGroup(EMPTY_GROUP))) + .hasMessage("\"Empty Group\" already registered in parameter service"); + + assertThatThrownBy(() -> ParameterService.register(new EmptyParameterGroup(EMPTY_GROUP), false)) + .hasMessage("\"Empty Group\" already registered in parameter service"); + + ParameterService.register(new EmptyParameterGroup(EMPTY_GROUP), true); + assertTrue(ParameterService.contains(EMPTY_GROUP)); + + ParameterService.deregister(EMPTY_GROUP); + assertFalse(ParameterService.contains(EMPTY_GROUP)); + + ParameterService.register(new EmptyParameterGroup(EMPTY_GROUP), true); + assertTrue(ParameterService.contains(EMPTY_GROUP)); + + ParameterService.deregister(EMPTY_GROUP); + assertFalse(ParameterService.contains(EMPTY_GROUP)); + + EmptyParameterGroup epg = new EmptyParameterGroup(EMPTY_GROUP); ParameterService.register(epg); - assertTrue(ParameterService.contains("Empty Group")); - assertNotNull(ParameterService.get("Empty Group")); + assertTrue(ParameterService.contains(EMPTY_GROUP)); + assertNotNull(ParameterService.get(EMPTY_GROUP)); ParameterService.deregister(epg); - assertFalse(ParameterService.contains("Empty Group")); - - try { - ParameterService.deregister("Empty Group"); - fail("this test should throw an exception"); - } - catch (ParameterRuntimeException e) { - assertEquals("\"Empty Group\" not registered in parameter service", e.getMessage()); - } - - try { - ParameterService.get("Empty Group"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("\"Empty Group\" not found in parameter service", e.getMessage()); - } - - ParameterService.register(new EmptyParameterGroup("Empty Group")); - assertTrue(ParameterService.contains("Empty Group")); - assertNotNull(ParameterService.get("Empty Group")); + assertFalse(ParameterService.contains(EMPTY_GROUP)); + + assertThatThrownBy(() -> ParameterService.deregister(EMPTY_GROUP)) + .hasMessage("\"Empty Group\" not registered in parameter service"); + + assertThatThrownBy(() -> ParameterService.get(EMPTY_GROUP)) + .hasMessage("\"Empty Group\" not found in parameter service"); + + ParameterService.register(new EmptyParameterGroup(EMPTY_GROUP)); + assertTrue(ParameterService.contains(EMPTY_GROUP)); + assertNotNull(ParameterService.get(EMPTY_GROUP)); assertEquals(1, ParameterService.getAll().size()); ParameterService.clear(); assertEquals(0, ParameterService.getAll().size()); - assertFalse(ParameterService.contains("Empty Group")); - try { - ParameterService.get("Empty Group"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("\"Empty Group\" not found in parameter service", e.getMessage()); - } + assertFalse(ParameterService.contains(EMPTY_GROUP)); + + assertThatThrownBy(() -> ParameterService.get(EMPTY_GROUP)) + .hasMessage("\"Empty Group\" not found in parameter service"); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java index eb673222..9e7121cd 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java @@ -38,6 +38,8 @@ import org.onap.policy.common.parameters.testclasses.TestParametersL00; import org.onap.policy.common.parameters.testclasses.TestParametersL10; public class TestValidation { + private static final String L0_PARAMETERS = "l0Parameters"; + private static final String NOT_BLANK_STRING_MESSAGE = "field 'notBlankString' type 'java.lang.String' value '' INVALID, must be a non-blank string\n" .replace('\'', '"'); @@ -73,7 +75,7 @@ public class TestValidation { @Test public void testValidationOk() throws IOException { - TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + TestParametersL00 l0Parameters = new TestParametersL00(L0_PARAMETERS); GroupValidationResult validationResult = l0Parameters.validate(); assertTrue(validationResult.isValid()); @@ -90,7 +92,7 @@ public class TestValidation { @Test public void testValidationObservation() throws IOException { - TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + TestParametersL00 l0Parameters = new TestParametersL00(L0_PARAMETERS); l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 3); @@ -135,7 +137,7 @@ public class TestValidation { @Test public void testValidationWarning() throws IOException { - TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + TestParametersL00 l0Parameters = new TestParametersL00(L0_PARAMETERS); l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 3); @@ -179,7 +181,7 @@ public class TestValidation { @Test public void testValidationInvalid() throws IOException { - TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); + TestParametersL00 l0Parameters = new TestParametersL00(L0_PARAMETERS); l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 3); @@ -222,7 +224,7 @@ public class TestValidation { } @Test - public void testValidationEmptySubGroup() throws IOException { + public void testValidationEmptySubGroup() { TestParametersL10 l10Parameters = new TestParametersL10("l10Parameters"); l10Parameters.setL10LGenericNested0(null); @@ -234,7 +236,7 @@ public class TestValidation { } @Test - public void testGetValidationResult() throws Exception { + public void testGetValidationResult() { Contained item = new Contained(); item.setName("item"); diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java index 2c1e2f18..b02022d7 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationErrors.java @@ -1,29 +1,29 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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. * 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; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.common.parameters.testclasses.ParameterGroupMissingGetter; @@ -51,25 +51,18 @@ public class TestValidationErrors { ParameterGroupWithParameterGroupCollection illegalCollection = new ParameterGroupWithParameterGroupCollection( "Illegal Collection"); - try { - illegalCollection.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("collection parameter \"parameterGroupArrayList\" is illegal," - + " parameter groups are not allowed as collection members", e.getMessage()); - } + + assertThatThrownBy(illegalCollection::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("collection parameter \"parameterGroupArrayList\" is illegal," + + " parameter groups are not allowed as collection members"); } @Test public void testNullCollection() { ParameterGroupWithNullCollection nullCollection = new ParameterGroupWithNullCollection("Null Collection"); - try { - nullCollection.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("collection parameter \"nullList\" is null", e.getMessage()); - } + assertThatThrownBy(nullCollection::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("collection parameter \"nullList\" is null"); } @Test @@ -83,59 +76,42 @@ public class TestValidationErrors { @Test public void testMapNullValueValidation() { ParameterGroupWithNullMapValue nullMap = new ParameterGroupWithNullMapValue("Null Map value"); - try { - nullMap.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("map parameter \"nullMap\" is null", e.getMessage()); - } + + assertThatThrownBy(nullMap::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("map parameter \"nullMap\" is null"); } @Test public void testBadMapKeyValidation() { ParameterGroupWithIllegalMapKey illegalMap = new ParameterGroupWithIllegalMapKey("Illegal Map"); - try { - illegalMap.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("map entry is not a parameter group keyed by a string, key \"1\" " - + "in map \"badMap\" is not a string", e.getMessage()); - } + + assertThatThrownBy(illegalMap::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("map entry is not a parameter group keyed by a string, key \"1\" " + + "in map \"badMap\" is not a string"); } @Test public void testBadMapValueValidation() { ParameterGroupWithIllegalMapValue illegalMap = new ParameterGroupWithIllegalMapValue("Illegal Map"); - try { - illegalMap.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("map entry is not a parameter group keyed by a string, value \"1\" in " - + "map \"intMap\" is not a parameter group", e.getMessage()); - } + + assertThatThrownBy(illegalMap::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("map entry is not a parameter group keyed by a string, value \"1\" in " + + "map \"intMap\" is not a parameter group"); } - + @Test public void testMissingGetter() { ParameterGroupMissingGetter badGetterName = new ParameterGroupMissingGetter("BGN"); - try { - badGetterName.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("could not get getter method for parameter \"value\"", e.getMessage()); - } - + + assertThatThrownBy(badGetterName::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("could not get getter method for parameter \"value\""); } - + @Test public void testPrivateGetter() { ParameterGroupPrivateGetter privateGetter = new ParameterGroupPrivateGetter("privateGetter"); - try { - privateGetter.isValid(); - fail("test should throw an exception"); - } catch (ParameterRuntimeException e) { - assertEquals("could not get getter method for parameter \"value\"", e.getMessage()); - } - + + assertThatThrownBy(privateGetter::isValid).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("could not get getter method for parameter \"value\""); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java index f7451051..46360ef9 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidationResults.java @@ -1,53 +1,61 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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. * 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; +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.util.LinkedHashMap; import java.util.Map; - import org.junit.Before; import org.junit.Test; import org.onap.policy.common.parameters.testclasses.TestParametersL10; import org.onap.policy.common.parameters.testclasses.TestParametersLGeneric; public class TestValidationResults { + private static final String NON_EXISTANT_PARAMETER = "nonExistantParameter"; + private static final String L10L_GENERIC_NESTED_MAP_VAL0 = "l10LGenericNestedMapVal0"; + private static final String L10_INT_FIELD = "l10IntField"; + private static final String ENTRY0 = "entry0"; + private static final String THIS_VALUE_IS_INVALID = "This value is invalid"; + private static final String SOMETHING_WAS_OBSERVED = "Something was observed"; + private static final String PG_MAP = "pgMap"; + private Map<String, ParameterGroup> pgMap = new LinkedHashMap<>(); private ParameterGroup pg = new TestParametersL10("pg"); @Before public void initMap() { - pgMap.put("entry0", new TestParametersLGeneric("entry0")); + pgMap.put(ENTRY0, new TestParametersLGeneric(ENTRY0)); } @Test - public void testGroupMapValidationResult() throws NoSuchFieldException, SecurityException { - GroupMapValidationResult result = new GroupMapValidationResult(this.getClass().getDeclaredField("pgMap"), + public void testGroupMapValidationResult() throws NoSuchFieldException { + GroupMapValidationResult result = new GroupMapValidationResult(this.getClass().getDeclaredField(PG_MAP), pgMap); assertTrue(result.isValid()); - assertEquals("pgMap", result.getName()); + assertEquals(PG_MAP, result.getName()); result.setResult(ValidationStatus.OBSERVATION); assertTrue(result.isValid()); @@ -58,26 +66,22 @@ public class TestValidationResults { assertTrue(result.isValid()); assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); - result.setResult(ValidationStatus.OBSERVATION, "Something was observed"); + result.setResult(ValidationStatus.OBSERVATION, SOMETHING_WAS_OBSERVED); assertTrue(result.isValid()); assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); assertEquals("parameter group map \"pgMap\" OBSERVATION, Something was observed", result.getResult().trim()); - result.setResult("entry0", new GroupValidationResult(pgMap.get("entry0"))); + result.setResult(ENTRY0, new GroupValidationResult(pgMap.get(ENTRY0))); assertTrue(result.isValid()); assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); assertEquals("parameter group map \"pgMap\" OBSERVATION, Something was observed", result.getResult().trim()); - try { - result.setResult("nonExistantEntry", new GroupValidationResult(pgMap.get("entry0"))); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("no entry with name \"nonExistantEntry\" exists", e.getMessage()); - } + assertThatThrownBy(() -> result.setResult("nonExistantEntry", new GroupValidationResult(pgMap.get(ENTRY0)))) + .hasMessage("no entry with name \"nonExistantEntry\" exists"); } @Test - public void testGroupValidationResult() throws NoSuchFieldException, SecurityException { + public void testGroupValidationResult() throws NoSuchFieldException { GroupValidationResult result = new GroupValidationResult(pg); assertTrue(result.isValid()); @@ -93,79 +97,47 @@ public class TestValidationResults { assertTrue(result.isValid()); assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); - result.setResult(ValidationStatus.OBSERVATION, "Something was observed"); + result.setResult(ValidationStatus.OBSERVATION, SOMETHING_WAS_OBSERVED); assertTrue(result.isValid()); assertEquals(ValidationStatus.OBSERVATION, result.getStatus()); assertEquals("parameter group \"pg\" type \"org.onap.policy.common.parameters.testclasses.TestParametersL10\"" + " OBSERVATION, Something was observed", result.getResult().trim()); - try { - result.setResult("nonExistantParameter", ValidationStatus.OBSERVATION, "Something was observed"); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("no parameter field exists for parameter: nonExistantParameter", e.getMessage()); - } + assertThatThrownBy(() -> result.setResult(NON_EXISTANT_PARAMETER, ValidationStatus.OBSERVATION, + SOMETHING_WAS_OBSERVED)) + .hasMessage("no parameter field exists for parameter: nonExistantParameter"); - result.setResult("l10IntField", ValidationStatus.OBSERVATION, "Something was observed"); + result.setResult(L10_INT_FIELD, ValidationStatus.OBSERVATION, SOMETHING_WAS_OBSERVED); assertTrue(result.isValid()); - try { - result.setResult("nonExistantParameter", new GroupValidationResult(pg)); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("no nested parameter field exists for parameter: nonExistantParameter", e.getMessage()); - } + assertThatThrownBy(() -> result.setResult(NON_EXISTANT_PARAMETER, new GroupValidationResult(pg))) + .hasMessage("no nested parameter field exists for parameter: nonExistantParameter"); - try { - result.setResult("l10IntField", new GroupValidationResult(pg)); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("parameter is not a nested group parameter: l10IntField", e.getMessage()); - } + assertThatThrownBy(() -> result.setResult(L10_INT_FIELD, new GroupValidationResult(pg))) + .hasMessage("parameter is not a nested group parameter: l10IntField"); GroupMapValidationResult groupMapResult = new GroupMapValidationResult( - this.getClass().getDeclaredField("pgMap"), pgMap); - - try { - result.setResult("nonExistantParameter", "entry0", groupMapResult); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("no group map parameter field exists for parameter: nonExistantParameter", e.getMessage()); - } - - try { - result.setResult("l10IntField", "entry0", groupMapResult); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("parameter is not a nested group map parameter: l10IntField", e.getMessage()); - } - - result.setResult("l10LGenericNestedMap", "l10LGenericNestedMapVal0", ValidationStatus.INVALID, - "This value is invalid"); + this.getClass().getDeclaredField(PG_MAP), pgMap); + + assertThatThrownBy(() -> result.setResult(NON_EXISTANT_PARAMETER, ENTRY0, groupMapResult)) + .hasMessage("no group map parameter field exists for parameter: nonExistantParameter"); + + assertThatThrownBy(() -> result.setResult(L10_INT_FIELD, ENTRY0, groupMapResult)) + .hasMessage("parameter is not a nested group map parameter: l10IntField"); + + result.setResult("l10LGenericNestedMap", L10L_GENERIC_NESTED_MAP_VAL0, ValidationStatus.INVALID, + THIS_VALUE_IS_INVALID); assertEquals(ValidationStatus.INVALID, result.getStatus()); - - try { - result.setResult("l10IntField", "l10LGenericNestedMapVal0", ValidationStatus.INVALID, - "This value is invalid"); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("parameter is not a nested group map parameter: l10IntField", e.getMessage()); - } - - try { - result.setResult("nonExistantParameter", "l10LGenericNestedMapVal0", ValidationStatus.INVALID, - "This value is invalid"); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("no group map parameter field exists for parameter: nonExistantParameter", e.getMessage()); - } - - try { - result.setResult("l10LGenericNestedMap", "NonExistantKey", ValidationStatus.INVALID, - "This value is invalid"); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("no entry with name \"NonExistantKey\" exists", e.getMessage()); - } + + assertThatThrownBy(() -> result.setResult(L10_INT_FIELD, L10L_GENERIC_NESTED_MAP_VAL0, ValidationStatus.INVALID, + THIS_VALUE_IS_INVALID)) + .hasMessage("parameter is not a nested group map parameter: l10IntField"); + + assertThatThrownBy(() -> result.setResult(NON_EXISTANT_PARAMETER, L10L_GENERIC_NESTED_MAP_VAL0, + ValidationStatus.INVALID, THIS_VALUE_IS_INVALID)).hasMessage( + "no group map parameter field exists for parameter: nonExistantParameter"); + + assertThatThrownBy(() -> result.setResult("l10LGenericNestedMap", "NonExistantKey", ValidationStatus.INVALID, + THIS_VALUE_IS_INVALID)).hasMessage("no entry with name \"NonExistantKey\" exists"); } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java index e24f1c8e..66656844 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestYamlInput.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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,13 +23,11 @@ package org.onap.policy.common.parameters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; - import org.junit.Test; import org.onap.policy.common.parameters.testclasses.TestParametersL00; import org.yaml.snakeyaml.Yaml; @@ -38,15 +37,10 @@ public class TestYamlInput { public void testYamlInput() throws IOException { TestParametersL00 testParameterGroup = null; - // Read the parameters - try { - // Read the parameters from JSON using Gson - final Yaml yaml = new Yaml(); - testParameterGroup = yaml.loadAs(new FileReader("src/test/resources/parameters/TestParameters.yaml"), - TestParametersL00.class); - } catch (final Exception e) { - fail("test should not throw an exception here: " + e.getMessage()); - } + // Read the parameters from JSON using Gson + final Yaml yaml = new Yaml(); + testParameterGroup = yaml.loadAs(new FileReader("src/test/resources/parameters/TestParameters.yaml"), + TestParametersL00.class); GroupValidationResult validationResult = testParameterGroup.validate(); assertTrue(validationResult.isValid()); diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java index 748eb902..ea5ae853 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL00.java @@ -30,6 +30,9 @@ import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.ValidationStatus; public class TestParametersL00 extends ParameterGroupImpl { + private static final String L00_INT_FIELD = "l00IntField"; + private static final String L00_STRING_FIELD = "l00StringField"; + private static final String A_CONSTANT = "A Constant"; private int l00IntField = 0; @@ -136,7 +139,7 @@ public class TestParametersL00 extends ParameterGroupImpl { l00IntField = 2; break; case WARNING: - l00StringField = "l00StringField"; + l00StringField = L00_STRING_FIELD; l00IntField = 3; break; case INVALID: @@ -165,29 +168,29 @@ public class TestParametersL00 extends ParameterGroupImpl { } if (l00StringField == null || l00StringField.trim().length() == 0) { - validationResult.setResult("l00StringField", ValidationStatus.INVALID, + validationResult.setResult(L00_STRING_FIELD, ValidationStatus.INVALID, "l00StringField must be a non-blank string"); - } else if (l00StringField.equals("l00StringField")) { - validationResult.setResult("l00StringField", ValidationStatus.WARNING, + } else if (l00StringField.equals(L00_STRING_FIELD)) { + validationResult.setResult(L00_STRING_FIELD, ValidationStatus.WARNING, "using the field name for the parameter value is dangerous"); } else if (l00StringField.equals("aString")) { - validationResult.setResult("l00StringField", ValidationStatus.OBSERVATION, + validationResult.setResult(L00_STRING_FIELD, ValidationStatus.OBSERVATION, "this value for name is unhelpful"); } else { - validationResult.setResult("l00StringField", ValidationStatus.CLEAN, + validationResult.setResult(L00_STRING_FIELD, ValidationStatus.CLEAN, ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); } if (l00IntField < 0) { - validationResult.setResult("l00IntField", ValidationStatus.INVALID, + validationResult.setResult(L00_INT_FIELD, ValidationStatus.INVALID, "l00IntField must be a positive integer"); } else if (l00IntField > 2) { - validationResult.setResult("l00IntField", ValidationStatus.WARNING, + validationResult.setResult(L00_INT_FIELD, ValidationStatus.WARNING, "values greater than 2 are not recommended"); } else if (l00IntField == 2) { - validationResult.setResult("l00IntField", ValidationStatus.OBSERVATION, "this field has been set to 2"); + validationResult.setResult(L00_INT_FIELD, ValidationStatus.OBSERVATION, "this field has been set to 2"); } else { - validationResult.setResult("l00IntField", ValidationStatus.CLEAN, + validationResult.setResult(L00_INT_FIELD, ValidationStatus.CLEAN, ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java index 8ad682ff..2fba9ba2 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersL10.java @@ -30,6 +30,9 @@ import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.ValidationStatus; public class TestParametersL10 extends ParameterGroupImpl { + private static final String L10_INT_FIELD = "l10IntField"; + private static final String L10_STRING_FIELD = "l10StringField"; + private int l10IntField = 0; private String l10StringField = "Legal " + this.getClass().getCanonicalName(); private TestParametersLGeneric l10LGenericNested0 = new TestParametersLGeneric("l10LGenericNested0"); @@ -120,7 +123,7 @@ public class TestParametersL10 extends ParameterGroupImpl { l10IntField = 2; break; case WARNING: - l10StringField = "l10StringField"; + l10StringField = L10_STRING_FIELD; l10IntField = 3; break; case INVALID: @@ -144,29 +147,29 @@ public class TestParametersL10 extends ParameterGroupImpl { GroupValidationResult validationResult = super.validate(); if (l10StringField == null || l10StringField.trim().length() == 0) { - validationResult.setResult("l10StringField", ValidationStatus.INVALID, + validationResult.setResult(L10_STRING_FIELD, ValidationStatus.INVALID, "l10StringField must be a non-blank string"); - } else if (l10StringField.equals("l10StringField")) { - validationResult.setResult("l10StringField", ValidationStatus.WARNING, + } else if (l10StringField.equals(L10_STRING_FIELD)) { + validationResult.setResult(L10_STRING_FIELD, ValidationStatus.WARNING, "using the field name for the parameter value is dangerous"); } else if (l10StringField.equals("aString")) { - validationResult.setResult("l10StringField", ValidationStatus.OBSERVATION, + validationResult.setResult(L10_STRING_FIELD, ValidationStatus.OBSERVATION, "this value for name is unhelpful"); } else { - validationResult.setResult("l10StringField", ValidationStatus.CLEAN, + validationResult.setResult(L10_STRING_FIELD, ValidationStatus.CLEAN, ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); } if (l10IntField < 0) { - validationResult.setResult("l10IntField", ValidationStatus.INVALID, + validationResult.setResult(L10_INT_FIELD, ValidationStatus.INVALID, "l10IntField must be a positive integer"); } else if (l10IntField > 2) { - validationResult.setResult("l10IntField", ValidationStatus.WARNING, + validationResult.setResult(L10_INT_FIELD, ValidationStatus.WARNING, "values greater than 2 are not recommended"); } else if (l10IntField == 2) { - validationResult.setResult("l10IntField", ValidationStatus.OBSERVATION, "this field has been set to 2"); + validationResult.setResult(L10_INT_FIELD, ValidationStatus.OBSERVATION, "this field has been set to 2"); } else { - validationResult.setResult("l10IntField", ValidationStatus.CLEAN, + validationResult.setResult(L10_INT_FIELD, ValidationStatus.CLEAN, ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString()); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java index 2fc04180..44ec7139 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java @@ -28,6 +28,9 @@ import org.onap.policy.common.parameters.annotations.NotBlank; import org.onap.policy.common.parameters.annotations.NotNull; public class TestParametersLGeneric extends ParameterGroupImpl { + private static final String LGENERIC_INT_FIELD = "lgenericIntField"; + private static final String LGENERIC_STRING_FIELD = "lgenericStringField"; + private int lgenericIntField = 0; @NotNull @NotBlank @@ -74,9 +77,6 @@ public class TestParametersLGeneric extends ParameterGroupImpl { if (level == 0) { return; } - else { - level--; - } switch (triggerStatus) { case CLEAN: @@ -88,7 +88,7 @@ public class TestParametersLGeneric extends ParameterGroupImpl { lgenericIntField = 2; break; case WARNING: - lgenericStringField = "lgenericStringField"; + lgenericStringField = LGENERIC_STRING_FIELD; lgenericIntField = 3; break; case INVALID: @@ -105,22 +105,22 @@ public class TestParametersLGeneric extends ParameterGroupImpl { public GroupValidationResult validate() { GroupValidationResult validationResult = super.validate(); - if ("lgenericStringField".equals(lgenericStringField)) { - validationResult.setResult("lgenericStringField", ValidationStatus.WARNING, + if (LGENERIC_STRING_FIELD.equals(lgenericStringField)) { + validationResult.setResult(LGENERIC_STRING_FIELD, ValidationStatus.WARNING, "using the field name for the parameter value is dangerous"); } else if ("aString".equals(lgenericStringField)) { - validationResult.setResult("lgenericStringField", ValidationStatus.OBSERVATION, + validationResult.setResult(LGENERIC_STRING_FIELD, ValidationStatus.OBSERVATION, "this value for name is unhelpful"); } if (lgenericIntField < 0) { - validationResult.setResult("lgenericIntField", ValidationStatus.INVALID, + validationResult.setResult(LGENERIC_INT_FIELD, ValidationStatus.INVALID, "lgenericIntField must be a positive integer"); } else if (lgenericIntField > 2) { - validationResult.setResult("lgenericIntField", ValidationStatus.WARNING, + validationResult.setResult(LGENERIC_INT_FIELD, ValidationStatus.WARNING, "values greater than 2 are not recommended"); } else if (lgenericIntField == 2) { - validationResult.setResult("lgenericIntField", ValidationStatus.OBSERVATION, + validationResult.setResult(LGENERIC_INT_FIELD, ValidationStatus.OBSERVATION, "this field has been set to 2"); } |