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