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