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