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