6b8460cde6953d9b7b8d4a320e6026502cdde340
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.common.parameters.testclasses;
22
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import org.onap.policy.common.parameters.ParameterGroup;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ParameterConstants;
30 import org.onap.policy.common.parameters.ValidationStatus;
31
32 public class TestParametersL00 implements ParameterGroup {
33     private String name;
34     private int l00IntField = 0;
35     private String l00StringField = "Legal " + this.getClass().getCanonicalName();
36     private TestParametersL10 l00L10Nested = new TestParametersL10("l00L10Nested");
37     private TestParametersLGeneric l00LGenericNested = new TestParametersLGeneric("l00LGenericNested");
38     private Map<String, TestParametersLGeneric> l00LGenericNestedMap = new LinkedHashMap<>();
39
40     /**
41      * Default constructor
42      */
43     public TestParametersL00() {
44     }
45     
46     /**
47      * Create a test parameter group.
48      * 
49      * @param name the parameter group name
50      */
51     public TestParametersL00(final String name) {
52         this.name = name;
53
54         TestParametersLGeneric l00LGenericNestedMapVal0 = new TestParametersLGeneric("l00LGenericNestedMapVal0");
55         l00LGenericNestedMap.put(l00LGenericNestedMapVal0.getName(), l00LGenericNestedMapVal0);
56         TestParametersLGeneric l00LGenericNestedMapVal1 = new TestParametersLGeneric("l00LGenericNestedMapVal1");
57         l00LGenericNestedMap.put(l00LGenericNestedMapVal1.getName(), l00LGenericNestedMapVal1);
58     }
59
60     public void setName(String name) {
61         this.name = name;
62     }
63
64     public void setL00IntField(int l00IntField) {
65         this.l00IntField = l00IntField;
66     }
67
68     public void setL00StringField(String l00StringField) {
69         this.l00StringField = l00StringField;
70     }
71
72     public void setL00L10Nested(TestParametersL10 l00l10Nested) {
73         l00L10Nested = l00l10Nested;
74     }
75
76     public void setL00LGenericNested(TestParametersLGeneric l00lGenericNested) {
77         l00LGenericNested = l00lGenericNested;
78     }
79
80     public void setL00LGenericNestedMap(Map<String, TestParametersLGeneric> l00lGenericNestedMap) {
81         l00LGenericNestedMap = l00lGenericNestedMap;
82     }
83
84     /**
85      * Trigger a validation message.
86      * 
87      * @param triggerStatus Validation status to trigger
88      * @param level Number of levels to recurse before stopping
89      */
90     public void triggerValidationStatus(final ValidationStatus triggerStatus, int level) {
91         if (level == 0) {
92             return;
93         } else {
94             level--;
95         }
96
97         switch (triggerStatus) {
98             case CLEAN:
99                 l00StringField = "Legal " + this.getClass().getCanonicalName();
100                 l00IntField = 0;
101                 break;
102             case OBSERVATION:
103                 l00StringField = "aString";
104                 l00IntField = 2;
105                 break;
106             case WARNING:
107                 l00StringField = "l00StringField";
108                 l00IntField = 3;
109                 break;
110             case INVALID:
111                 l00StringField = "";
112                 l00IntField = -1;
113                 break;
114             default:
115                 break;
116         }
117
118         l00L10Nested.triggerValidationStatus(triggerStatus, level);
119         l00LGenericNested.triggerValidationStatus(triggerStatus, level);
120
121         for (TestParametersLGeneric nestedParameterGroup : l00LGenericNestedMap.values()) {
122             nestedParameterGroup.triggerValidationStatus(triggerStatus, level);
123         }
124
125     }
126
127     @Override
128     public String getName() {
129         return name;
130     }
131
132     @Override
133     public GroupValidationResult validate() {
134         GroupValidationResult validationResult = new GroupValidationResult(this);
135
136         if (name == null || name.trim().length() == 0) {
137             validationResult.setResult("name", ValidationStatus.INVALID, "name must be a non-blank string");
138         }
139
140         if (l00StringField == null || l00StringField.trim().length() == 0) {
141             validationResult.setResult("l00StringField", ValidationStatus.INVALID,
142                             "l00StringField must be a non-blank string");
143         } else if (l00StringField.equals("l00StringField")) {
144             validationResult.setResult("l00StringField", ValidationStatus.WARNING,
145                             "using the field name for the parameter value is dangerous");
146         } else if (l00StringField.equals("aString")) {
147             validationResult.setResult("l00StringField", ValidationStatus.OBSERVATION,
148                             "this value for name is unhelpful");
149         } else {
150             validationResult.setResult("l00StringField", ValidationStatus.CLEAN,
151                             ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString());
152         }
153
154         if (l00IntField < 0) {
155             validationResult.setResult("l00IntField", ValidationStatus.INVALID,
156                             "l00IntField must be a positive integer");
157         } else if (l00IntField > 2) {
158             validationResult.setResult("l00IntField", ValidationStatus.WARNING,
159                             "values greater than 2 are not recommended");
160         } else if (l00IntField == 2) {
161             validationResult.setResult("l00IntField", ValidationStatus.OBSERVATION, "this field has been set to 2");
162         } else {
163             validationResult.setResult("l00IntField", ValidationStatus.CLEAN,
164                             ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString());
165         }
166
167         validationResult.setResult("l00L10Nested", l00L10Nested.validate());
168         validationResult.setResult("l00LGenericNested", l00LGenericNested.validate());
169
170         for (Entry<String, TestParametersLGeneric> nestedGroupEntry : l00LGenericNestedMap.entrySet()) {
171             validationResult.setResult("l00LGenericNestedMap", nestedGroupEntry.getKey(),
172                             nestedGroupEntry.getValue().validate());
173         }
174
175         return validationResult;
176     }
177 }