ce368baca6875dd674456dfa6b623b6f52e64e2d
[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 org.onap.policy.common.parameters.ParameterGroup;
24 import org.onap.policy.common.parameters.GroupValidationResult;
25 import org.onap.policy.common.parameters.ParameterConstants;
26 import org.onap.policy.common.parameters.ValidationStatus;
27
28 public class TestParametersLGeneric implements ParameterGroup {
29     private String name;
30     private int lgenericIntField = 0;
31     private String lgenericStringField = "Legal " + this.getClass().getCanonicalName();
32     
33     /**
34      * Default constructor
35      */
36     public TestParametersLGeneric() {
37     }
38     
39     /**
40      * Create a test parameter group.
41      * 
42      * @param name the parameter group name
43      */
44     public TestParametersLGeneric(final String name) {
45         this.name = name;
46     }
47
48     public void setName(String name) {
49         this.name = name;
50     }
51
52     public void setLgenericIntField(int lgenericIntField) {
53         this.lgenericIntField = lgenericIntField;
54     }
55
56     public void setLgenericStringField(String lgenericStringField) {
57         this.lgenericStringField = lgenericStringField;
58     }
59
60     /**
61      * Trigger a validation message.
62      * 
63      * @param level Number of levels to recurse before stopping
64      */
65     public void triggerValidationStatus(final ValidationStatus triggerStatus, int level) {
66         if (level == 0) {
67             return;
68         }
69         else {
70             level--;
71         }
72
73         switch (triggerStatus) {
74             case CLEAN:
75                 lgenericStringField = "Legal " + this.getClass().getCanonicalName();
76                 lgenericIntField = 0;
77                 break;
78             case OBSERVATION:
79                 lgenericStringField = "aString";
80                 lgenericIntField = 2;
81                 break;
82             case WARNING:
83                 lgenericStringField = "lgenericStringField";
84                 lgenericIntField = 3;
85                 break;
86             case INVALID:
87                 lgenericStringField = "";
88                 lgenericIntField = -1;
89                 break;
90             default:
91                 break;
92         }
93
94     }
95
96     @Override
97     public String getName() {
98         return this.name;
99     }
100
101     @Override
102     public GroupValidationResult validate() {
103         GroupValidationResult validationResult = new GroupValidationResult(this);
104
105         if (lgenericStringField == null || lgenericStringField.trim().length() == 0) {
106             validationResult.setResult("lgenericStringField", ValidationStatus.INVALID,
107                             "lgenericStringField must be a non-blank string");
108         } else if (lgenericStringField.equals("lgenericStringField")) {
109             validationResult.setResult("lgenericStringField", ValidationStatus.WARNING,
110                             "using the field name for the parameter value is dangerous");
111         } else if (lgenericStringField.equals("aString")) {
112             validationResult.setResult("lgenericStringField", ValidationStatus.OBSERVATION,
113                             "this value for name is unhelpful");
114         } else {
115             validationResult.setResult("lgenericStringField", ValidationStatus.CLEAN,
116                             ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString());
117         }
118
119         if (lgenericIntField < 0) {
120             validationResult.setResult("lgenericIntField", ValidationStatus.INVALID,
121                             "lgenericIntField must be a positive integer");
122         } else if (lgenericIntField > 2) {
123             validationResult.setResult("lgenericIntField", ValidationStatus.WARNING,
124                             "values greater than 2 are not recommended");
125         } else if (lgenericIntField == 2) {
126             validationResult.setResult("lgenericIntField", ValidationStatus.OBSERVATION,
127                             "this field has been set to 2");
128         } else {
129             validationResult.setResult("lgenericIntField", ValidationStatus.CLEAN,
130                             ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString());
131         }
132
133         return validationResult;
134     }
135 }