diff options
26 files changed, 412 insertions, 89 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java index e607ff98..3baacb1f 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java @@ -89,7 +89,7 @@ public class GroupMapValidationResult implements ValidationResult { * @param message The validation message explaining the validation status */ @Override - public void setResult(ValidationStatus status, String message) { + public void setResult(final ValidationStatus status, final String message) { setResult(status); this.message = message; } @@ -112,9 +112,28 @@ public class GroupMapValidationResult implements ValidationResult { * Set the validation result on a parameter map entry. * * @param entryName The name of the parameter map entry + * @param status The validation status for the entry + * @param message The validation message for the entry + */ + public void setResult(final String entryName, final ValidationStatus status, final String message) { + ValidationResult validationResult = validationResultMap.get(entryName); + if (validationResult == null) { + throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists"); + } + + // Set the status of the parameter group and replace the field result + validationResult.setResult(status, message); + this.setResult(status); + } + + + /** + * Set the validation result on a parameter map entry. + * + * @param entryName The name of the parameter map entry * @param mapEntryValidationResult The validation result for the entry */ - public void setResult(String entryName, ValidationResult mapEntryValidationResult) { + public void setResult(final String entryName, final ValidationResult mapEntryValidationResult) { ValidationResult validationResult = validationResultMap.get(entryName); if (validationResult == null) { throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists"); diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java index ac9dc7ba..fc2f6ca5 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java @@ -48,6 +48,11 @@ public class GroupValidationResult implements ValidationResult { public GroupValidationResult(final ParameterGroup parameterGroup) { this.parameterGroup = parameterGroup; + // Parameter group definitions may be optional + if (parameterGroup == null) { + return; + } + // Add a validation result per field for (Field field : parameterGroup.getClass().getDeclaredFields()) { // Exclude system fields @@ -86,15 +91,15 @@ public class GroupValidationResult implements ValidationResult { // Nested parameter groups are allowed if (ParameterGroup.class.isAssignableFrom(fieldType)) { - return new GroupValidationResult((ParameterGroup) field.get(parameterGroup)); + return new GroupValidationResult((ParameterGroup) fieldObject); } - + // Nested maps of parameter groups are allowed if (Map.class.isAssignableFrom(field.getType())) { checkMapIsParameterGroupMap(fieldName, fieldObject); return new GroupMapValidationResult(field, fieldObject); } - + // Collections of parameter groups are not allowed if (Collection.class.isAssignableFrom(field.getType())) { checkCollection4ParameterGroups(fieldName, fieldObject); @@ -115,7 +120,7 @@ public class GroupValidationResult implements ValidationResult { if (mapObject == null) { throw new ParameterRuntimeException("map parameter \"" + fieldName + "\" is null"); } - + Map<?, ?> incomingMap = (Map<?, ?>) mapObject; for (Entry<?, ?> mapEntry : incomingMap.entrySet()) { @@ -184,7 +189,7 @@ public class GroupValidationResult implements ValidationResult { } /** - * Set the validation result on on a parameter group. + * Set the validation result on a parameter group. * * @param status The validation status the parameter group is receiving * @param message The validation message explaining the validation status @@ -196,9 +201,8 @@ public class GroupValidationResult implements ValidationResult { } /** - * Set the validation result on on a parameter group. - * On a sequence of calls, the most serious validation status is recorded, assuming the status enum ordinal increase - * in order of severity + * Set the validation result on a parameter group. On a sequence of calls, the most serious validation status is + * recorded, assuming the status enum ordinal increase in order of severity * * @param status The validation status the parameter group is receiving */ @@ -211,27 +215,23 @@ public class GroupValidationResult implements ValidationResult { } /** - * Set the validation result on a parameter group. + * Set the validation result on a parameter in a parameter group. * - * @param parameterGroupName The name of the parameter group + * @param parameterName The name of the parameter * @param status The validation status the field is receiving * @param message The validation message explaining the validation status */ - public void setResult(final String parameterGroupName, final ValidationStatus status, final String message) { - ParameterValidationResult parameterValidationResult; - try { - parameterValidationResult = (ParameterValidationResult) validationResultMap.get(parameterGroupName); - } catch (ClassCastException e) { - throw new ParameterRuntimeException("parameter not a regular parameter: " + parameterGroupName, e); - } + public void setResult(final String parameterName, final ValidationStatus status, final String message) { + ValidationResult validationResult = validationResultMap.get(parameterName); - if (parameterValidationResult == null) { - throw new ParameterRuntimeException( - "no regular parameter field exists for parameter: " + parameterGroupName); + if (validationResult == null) { + throw new ParameterRuntimeException("no parameter field exists for parameter: " + parameterName); } - // Set the status of the parameter group and the field - parameterValidationResult.setResult(status, message); + // Set the status and the message on the result irrespective of validation result type + validationResult.setResult(status, message); + + // Set the status of this result this.setResult(status); } @@ -241,7 +241,7 @@ public class GroupValidationResult implements ValidationResult { * @param parameterName The name of the parameter field * @param nestedValidationResult The validation result from a nested field */ - public void setResult(String parameterName, ValidationResult nestedValidationResult) { + public void setResult(final String parameterName, final ValidationResult nestedValidationResult) { GroupValidationResult groupValidationResult; try { groupValidationResult = (GroupValidationResult) validationResultMap.get(parameterName); @@ -284,6 +284,32 @@ public class GroupValidationResult implements ValidationResult { } /** + * Set the validation status on a group map entry. + * + * @param parameterName The name of the parameter field + * @param key The key of the map entry + * @param status The validation status of the entry + * @param message The message for the parameter group + */ + public void setResult(final String parameterName, final String key, final ValidationStatus status, + final String message) { + GroupMapValidationResult groupMapValidationResult; + try { + groupMapValidationResult = (GroupMapValidationResult) validationResultMap.get(parameterName); + } catch (ClassCastException e) { + throw new ParameterRuntimeException("parameter is not a nested group map parameter: " + parameterName, e); + } + + if (groupMapValidationResult == null) { + throw new ParameterRuntimeException("no group map parameter field exists for parameter: " + parameterName); + } + + // Set the status of the parameter group and the field + groupMapValidationResult.setResult(key, status, message); + this.setResult(status); + } + + /** * Gets the validation result. * * @param initialIndentation the indentation to use on the main result output @@ -301,9 +327,15 @@ public class GroupValidationResult implements ValidationResult { validationResultBuilder.append(initialIndentation); validationResultBuilder.append("parameter group \""); - validationResultBuilder.append(parameterGroup.getName()); - validationResultBuilder.append("\" type \""); - validationResultBuilder.append(parameterGroup.getClass().getCanonicalName()); + + if (parameterGroup != null) { + validationResultBuilder.append(parameterGroup.getName()); + validationResultBuilder.append("\" type \""); + validationResultBuilder.append(parameterGroup.getClass().getCanonicalName()); + } + else { + validationResultBuilder.append("UNDEFINED"); + } validationResultBuilder.append("\" "); validationResultBuilder.append(status); validationResultBuilder.append(", "); diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java index 3a6e17e8..0b29a4e6 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java @@ -88,7 +88,7 @@ public class ParameterException extends Exception { * @param throwable the top level exception * @return cascaded message string */ - public static String buildCascadedMessage(Throwable throwable) { + public static String buildCascadedMessage(final Throwable throwable) { final StringBuilder builder = new StringBuilder(); builder.append(throwable.getMessage()); diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java index 48e8379f..8bfa183b 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java @@ -35,6 +35,13 @@ public interface ParameterGroup { public String getName(); /** + * Set the group name. + * + * @param name the group name + */ + public void setName(final String name); + + /** * Validate parameters. * * @return the result of the parameter validation diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java index db6995c5..358e5535 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java @@ -61,6 +61,20 @@ public abstract class ParameterService { } /** + * Register a parameter group with the parameter service. + * + * @param parameterGroup the parameter group + * @param overwrite if true, overwrite the current value if set + */ + public static void register(final ParameterGroup parameterGroup, final boolean overwrite) { + if (overwrite && parameterGroupMap.containsKey(parameterGroup.getName())) { + deregister(parameterGroup); + } + + register(parameterGroup); + } + + /** * Remove a parameter group from the parameter service. * * @param parameterGroupName the name of the parameter group @@ -74,13 +88,23 @@ public abstract class ParameterService { } /** + * Remove a parameter group from the parameter service. + * + * @param parameterGroup the parameter group + */ + public static void deregister(final ParameterGroup parameterGroup) { + deregister(parameterGroup.getName()); + } + + /** * Get a parameter group from the parameter service. * * @param parameterGroupName the name of the parameter group * @return The parameter group */ - public static ParameterGroup get(final String parameterGroupName) { - final ParameterGroup parameterGroup = parameterGroupMap.get(parameterGroupName); + public static <T extends ParameterGroup> T get(final String parameterGroupName) { + @SuppressWarnings("unchecked") + final T parameterGroup = (T) parameterGroupMap.get(parameterGroupName); if (parameterGroup == null) { throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not found in parameter service"); diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java index b97ccda0..309fd001 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java @@ -48,6 +48,15 @@ public interface ValidationResult { } /** + * Checks if the result is clean. + * + * @return true, if is clean + */ + default boolean isClean() { + return getStatus().isClean(); + } + + /** * Gets the validation result. * * @return the full validation result diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java index ff453a1b..8692506f 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java @@ -33,4 +33,12 @@ public enum ValidationStatus { public boolean isValid() { return !this.equals(INVALID); } + + /** + * Check if the validation was clean. + * @return true if the validation is clean + */ + public boolean isClean() { + return this.equals(CLEAN); + } } 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 0b7e46ca..2dad4283 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 @@ -57,9 +57,34 @@ public class TestParameterService { 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"); + ParameterService.register(epg); + 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"); 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 2ca24a05..fe750b2c 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 @@ -39,6 +39,7 @@ public class TestValidation { GroupValidationResult validationResult = l0Parameters.validate(); assertTrue(validationResult.isValid()); + assertTrue(validationResult.isClean()); assertNull(validationResult.getResult()); assertEquals(l0Parameters, validationResult.getParameterGroup()); assertEquals(l0Parameters.getName(), validationResult.getName()); @@ -61,6 +62,7 @@ public class TestValidation { GroupValidationResult validationResult = l0Parameters.validate(); assertTrue(validationResult.isValid()); + assertFalse(validationResult.isClean()); assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", "")); l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3); 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 6b781472..a489cc3f 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 @@ -21,6 +21,7 @@ package org.onap.policy.common.parameters; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -31,6 +32,7 @@ import org.onap.policy.common.parameters.testclasses.ParameterGroupWithIllegalMa import org.onap.policy.common.parameters.testclasses.ParameterGroupWithIllegalMapValue; import org.onap.policy.common.parameters.testclasses.ParameterGroupWithNullCollection; import org.onap.policy.common.parameters.testclasses.ParameterGroupWithNullMapValue; +import org.onap.policy.common.parameters.testclasses.ParameterGroupWithNullSubGroup; import org.onap.policy.common.parameters.testclasses.ParameterGroupWithParameterGroupCollection; public class TestValidationErrors { @@ -69,6 +71,14 @@ public class TestValidationErrors { } @Test + public void testMapNullSubGroupValidation() { + ParameterGroupWithNullSubGroup nullSub = new ParameterGroupWithNullSubGroup("Null sub group value"); + + nullSub.isValid(); + assertNull(nullSub.getSubGroup()); + } + + @Test public void testMapNullValueValidation() { ParameterGroupWithNullMapValue nullMap = new ParameterGroupWithNullMapValue("Null Map value"); try { 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 8f22765b..f7451051 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 @@ -103,15 +103,11 @@ public class TestValidationResults { result.setResult("nonExistantParameter", ValidationStatus.OBSERVATION, "Something was observed"); fail("test shold throw an exception here"); } catch (Exception e) { - assertEquals("no regular parameter field exists for parameter: nonExistantParameter", e.getMessage()); + assertEquals("no parameter field exists for parameter: nonExistantParameter", e.getMessage()); } - try { - result.setResult("l10LGenericNestedMap", ValidationStatus.OBSERVATION, "Something was observed"); - fail("test shold throw an exception here"); - } catch (Exception e) { - assertEquals("parameter not a regular parameter: l10LGenericNestedMap", e.getMessage()); - } + result.setResult("l10IntField", ValidationStatus.OBSERVATION, "Something was observed"); + assertTrue(result.isValid()); try { result.setResult("nonExistantParameter", new GroupValidationResult(pg)); @@ -143,5 +139,33 @@ public class TestValidationResults { } 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"); + 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()); + } } } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/EmptyParameterGroup.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/EmptyParameterGroup.java index 681eb693..505b2ce4 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/EmptyParameterGroup.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/EmptyParameterGroup.java @@ -36,6 +36,11 @@ public class EmptyParameterGroup implements ParameterGroup { } @Override + public void setName(final String name) { + this.name = name; + } + + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithArray.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithArray.java index fcb2bb1b..c448cea1 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithArray.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithArray.java @@ -34,12 +34,17 @@ public class ParameterGroupWithArray implements ParameterGroup { public int[] getIntArray() { return intArray; } - + @Override public String getName() { return name; } - + + @Override + public void setName(final String name) { + this.name = name; + } + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java index 75ae45b8..7f2c0ab5 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithCollection.java @@ -53,6 +53,11 @@ public class ParameterGroupWithCollection implements ParameterGroup { } @Override + public void setName(final String name) { + this.name = name; + } + + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java index ca441060..57a5a110 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapKey.java @@ -52,6 +52,11 @@ public class ParameterGroupWithIllegalMapKey implements ParameterGroup { } @Override + public void setName(final String name) { + this.name = name; + } + + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java index 9253f158..39e35d9c 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithIllegalMapValue.java @@ -52,6 +52,11 @@ public class ParameterGroupWithIllegalMapValue implements ParameterGroup { } @Override + public void setName(final String name) { + this.name = name; + } + + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); } diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java index 0d113dc6..9b661bd5 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullCollection.java @@ -40,12 +40,17 @@ public class ParameterGroupWithNullCollection implements ParameterGroup { public List<Integer> getNullList() { return nullList; } - + @Override public String getName() { return name; } - + + @Override + public void setName(final String name) { + this.name = name; + } + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java index 9e1601c3..ac863a42 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullMapValue.java @@ -40,12 +40,17 @@ public class ParameterGroupWithNullMapValue implements ParameterGroup { public Map<String, Integer> getNullMap() { return nullMap; } - + @Override public String getName() { return name; } - + + @Override + public void setName(final String name) { + this.name = name; + } + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullSubGroup.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullSubGroup.java new file mode 100644 index 00000000..830c8117 --- /dev/null +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithNullSubGroup.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.testclasses; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterGroup; + +public class ParameterGroupWithNullSubGroup implements ParameterGroup { + private String name; + private ParameterGroup subGroup = null; + + /** + * Create a test parameter group. + * @param name the parameter group name + */ + public ParameterGroupWithNullSubGroup(final String name) { + this.name = name; + } + + public ParameterGroup getSubGroup() { + return subGroup; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(final String name) { + this.name = name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java index 5ece07b4..dadf7273 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/ParameterGroupWithParameterGroupCollection.java @@ -52,6 +52,11 @@ public class ParameterGroupWithParameterGroupCollection implements ParameterGrou } @Override + public void setName(final String name) { + this.name = name; + } + + @Override public GroupValidationResult validate() { return new GroupValidationResult(this); } diff --git a/policy-endpoints/pom.xml b/policy-endpoints/pom.xml index aedb56d8..03b3d59a 100644 --- a/policy-endpoints/pom.xml +++ b/policy-endpoints/pom.xml @@ -139,6 +139,12 @@ <artifactId>jersey-client</artifactId> <version>${jersey.version}</version> </dependency> + + <dependency> + <groupId>org.glassfish.jersey.core</groupId> + <artifactId>jersey-common</artifactId> + <version>${jersey.version}</version> + </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java index f886e5ca..6e8865f5 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java @@ -20,6 +20,9 @@ package org.onap.policy.common.endpoints.http.client; +import java.util.Map; + +import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import org.onap.policy.common.capabilities.Startable; @@ -30,6 +33,8 @@ public interface HttpClient extends Startable { public Response get(); + public Response put(String path, Entity<?> entity, Map<String, Object> headers); + public static <T> T getBody(Response response, Class<T> entityType) { return response.readEntity(entityType); } @@ -54,4 +59,5 @@ public interface HttpClient extends Startable { public static final HttpClientFactory factory = new IndexedHttpClientFactory(); + } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java index c227071c..0be81099 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java @@ -28,12 +28,16 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.Map; +import java.util.Map.Entry; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.Response; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; @@ -66,14 +70,9 @@ public class JerseyClient implements HttpClient { /** * Constructor. * - * name the name - * https is it https or not - * selfSignedCerts are there self signed certs - * hostname the hostname - * port port being used - * basePath base context - * userName user - * password password + * name the name https is it https or not selfSignedCerts are there self signed certs hostname + * the hostname port port being used basePath base context userName user password password + * * @param busTopicParams Input parameters object * @throws KeyManagementException key exception * @throws NoSuchAlgorithmException no algorithm exception @@ -163,6 +162,14 @@ public class JerseyClient implements HttpClient { return this.client.target(this.baseUrl).request().get(); } + @Override + public Response put(String path, Entity<?> entity, Map<String, Object> headers) { + Builder builder = this.client.target(this.baseUrl).path(path).request(); + for (Entry<String, Object> header : headers.entrySet()) { + builder.header(header.getKey(), header.getValue()); + } + return builder.put(entity); + } @Override public boolean start() { diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java index 7d37521e..3b8c08fc 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java @@ -23,7 +23,9 @@ package org.onap.policy.common.endpoints.http.server.internal; import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.EnumSet; + import javax.servlet.DispatcherType; + import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.HashLoginService; @@ -164,7 +166,7 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable } else { this.connector = httpConnector(); } - + this.connector.setName(srvName); this.connector.setReuseAddress(true); this.connector.setPort(port); @@ -189,8 +191,7 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable tempFilterPath = "/*"; } - context.addFilter(filterClass, tempFilterPath, - EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); + context.addFilter(filterClass, tempFilterPath, EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); } /** diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java index 1248744d..61525c33 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java @@ -21,13 +21,17 @@ package org.onap.policy.common.endpoints.http.server.test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Properties; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.junit.AfterClass; @@ -51,7 +55,7 @@ public class HttpClientTest { * Setup before class method. * * @throws InterruptedException can be interrupted - * @throws IOException can have an IO exception + * @throws IOException can have an IO exception */ @BeforeClass public static void setUp() throws InterruptedException, IOException { @@ -76,21 +80,20 @@ public class HttpClientTest { String keyStorePasswordSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME); if (keyStorePasswordSystemProperty != null) { - savedValuesMap.put( - JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, keyStorePasswordSystemProperty); + savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, + keyStorePasswordSystemProperty); } String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME); if (trustStoreSystemProperty != null) { - savedValuesMap - .put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty); + savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty); } String trustStorePasswordSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME); if (trustStorePasswordSystemProperty != null) { - savedValuesMap - .put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, trustStorePasswordSystemProperty); + savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, + trustStorePasswordSystemProperty); } System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test"); @@ -158,14 +161,12 @@ public class HttpClientTest { } @Test - public void testHttpNoAuthClient() throws Exception { + public void testHttpGetNoAuthClient() throws Exception { logger.info("-- testHttpNoAuthClient() --"); final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient") - .useHttps(false) - .allowSelfSignedCerts(false) - .hostname("localhost") - .port(6666).basePath("junit/echo").userName(null).password(null).managed(true).build()); + .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo") + .userName(null).password(null).managed(true).build()); final Response response = client.get("hello"); final String body = HttpClient.getBody(response, String.class); @@ -174,18 +175,28 @@ public class HttpClientTest { } @Test - public void testHttpAuthClient() throws Exception { + public void testHttpPutNoAuthClient() throws Exception { + logger.info("-- testHttpNoAuthClient() --"); + + final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient") + .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo") + .userName(null).password(null).managed(true).build()); + + Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON); + final Response response = client.put("hello", entity, Collections.emptyMap()); + final String body = HttpClient.getBody(response, String.class); + + assertTrue(response.getStatus() == 200); + assertTrue(body.equals("hello:{myParameter=myValue}")); + } + + @Test + public void testHttpGetAuthClient() throws Exception { logger.info("-- testHttpAuthClient() --"); final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient") - .useHttps(true) - .allowSelfSignedCerts(true) - .hostname("localhost") - .port(6667) - .basePath("junit/echo") - .userName("x") - .password("y") - .managed(true).build()); + .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo") + .userName("x").password("y").managed(true).build()); final Response response = client.get("hello"); final String body = HttpClient.getBody(response, String.class); @@ -195,20 +206,30 @@ public class HttpClientTest { } @Test + public void testHttpPutAuthClient() throws Exception { + logger.info("-- testHttpAuthClient() --"); + + final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient") + .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo") + .userName("x").password("y").managed(true).build()); + + Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON); + final Response response = client.put("hello", entity, Collections.emptyMap()); + final String body = HttpClient.getBody(response, String.class); + + assertTrue(response.getStatus() == 200); + assertTrue(body.equals("hello:{myParameter=myValue}")); + } + + @Test public void testHttpAuthClient401() throws Exception { logger.info("-- testHttpAuthClient401() --"); final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient401") - .useHttps(true) - .allowSelfSignedCerts(true) - .hostname("localhost") - .port(6667) - .basePath("junit/echo") - .userName(null) - .password(null) - .managed(true).build()); + .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo") + .userName(null).password(null).managed(true).build()); final Response response = client.get("hello"); - assertTrue(response.getStatus() == 401); + assertEquals(new Integer(response.getStatus()).toString(), response.getStatus(), 401); } @Test @@ -298,5 +319,22 @@ public class HttpClientTest { assertTrue(response2.getStatus() == 500); } + class MyEntity { + + private String myParameter; + + public MyEntity(final String myParameter) { + this.myParameter = myParameter; + } + + public void setMyParameter(final String myParameter) { + this.myParameter = myParameter; + } + + public String getMyParameter() { + return myParameter; + } + + } } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java index fdb8770c..56ed8933 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java @@ -22,12 +22,15 @@ package org.onap.policy.common.endpoints.http.server.test; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; + import javax.ws.rs.GET; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; + @Api(value = "echo") @Path("/junit/echo") public class RestEchoService { @@ -35,11 +38,17 @@ public class RestEchoService { @GET @Path("{word}") @Produces(MediaType.TEXT_PLAIN) - @ApiOperation( - value = "echoes back whatever received" - ) - public String echo(@PathParam("word") String word) { + @ApiOperation(value = "echoes back whatever received") + public String echo(@PathParam("word") String word) { return word; } + @PUT + @Path("{word}") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "echoes back whatever received") + public String echoPut(@PathParam("word") String word, Object entity) { + return word + ":" + entity.toString(); + } + } |