23e1f52c5e639fe07fe8c4023e634942788056a6
[appc.git] / appc-dg-util / appc-dg-util-bundle / src / test / java / org / openecomp / appc / dg / util / impl / InputParameterValidationImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.dg.util.impl;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
33 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
34 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
35 import org.powermock.reflect.Whitebox;
36
37 import java.util.HashMap;
38 import java.util.Map;
39
40 public class InputParameterValidationImplTest {
41     private SvcLogicContext svcLogicContext;
42
43     private InputParameterValidationImpl inputParameterValidation;
44
45     @Before
46     public void setUp() throws Exception {
47         inputParameterValidation = new InputParameterValidationImpl();
48         svcLogicContext = new SvcLogicContext();
49         svcLogicContext.setAttribute("a", "b");
50         svcLogicContext.setAttribute("b", "c");
51     }
52
53     @Test
54     public void validateAttributeSuccess() throws Exception {
55         Map<String, String> params = new HashMap<>();
56         params.put("a", "b");
57         params.put("b", "c");
58
59         inputParameterValidation.validateAttribute(params, svcLogicContext);
60
61         Assert.assertEquals("true", svcLogicContext.getAttribute("validateAttribute"));
62     }
63
64     @Test
65     public void validateAttributeFailure() throws Exception {
66         // wrong value
67         Map<String, String> params = new HashMap<>();
68         params.put("e", "f");
69
70         inputParameterValidation.validateAttribute(params, svcLogicContext);
71
72         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttribute"));
73
74         // null value
75         params = new HashMap<>();
76         params.put("e", null);
77
78         inputParameterValidation.validateAttribute(params, svcLogicContext);
79
80         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttribute"));
81     }
82
83     @Test
84     public void validateAttributeNull() throws Exception {
85         inputParameterValidation.validateAttribute(null, svcLogicContext);
86
87         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttribute"));
88     }
89
90     @Test
91     public void validateAttributeLengthSuccess() throws Exception {
92         Map<String, String> params = new HashMap<>();
93         params.put("maximum_length_param", "2");
94         params.put("a", "1");
95
96         inputParameterValidation.validateAttributeLength(params, svcLogicContext);
97
98         Assert.assertEquals("true", svcLogicContext.getAttribute("validateAttributeLength"));
99     }
100
101     @Test
102     public void validateAttributeLengthFailure() throws Exception {
103         // wrong key
104         Map<String, String> params = new HashMap<>();
105         params.put("maximum_length_param", "2");
106         params.put("e", "1");
107
108         inputParameterValidation.validateAttributeLength(params, svcLogicContext);
109
110         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttributeLength"));
111
112         //over length
113         params = new HashMap<>();
114         params.put("maximum_length_param", "2");
115         params.put("c", "3");
116
117         inputParameterValidation.validateAttributeLength(params, svcLogicContext);
118
119         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttributeLength"));
120     }
121
122     @Test
123     public void validateAttributeLengthNull() throws Exception {
124         inputParameterValidation.validateAttributeLength(null, svcLogicContext);
125
126         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttributeLength"));
127     }
128
129     @Test
130     public void validateAttributeCharacterSuccess() throws Exception {
131         Map<String, String> params = new HashMap<>();
132         params.put("special_characters", "z");
133         params.put("a", "1");
134
135         inputParameterValidation.validateAttributeCharacter(params, svcLogicContext);
136
137         Assert.assertEquals("true", svcLogicContext.getAttribute("validateAttributeCharacter"));
138     }
139
140     @Test
141     public void validateAttributeCharacterFailure() throws Exception {
142         Map<String, String> params = new HashMap<>();
143         params.put("special_characters", "z");
144         params.put("d", "1");
145
146         inputParameterValidation.validateAttributeCharacter(params, svcLogicContext);
147
148         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttributeCharacter"));
149     }
150
151     @Test
152     public void validateAttributeCharacterNull() throws Exception {
153         inputParameterValidation.validateAttributeCharacter(null, svcLogicContext);
154
155         Assert.assertEquals("false", svcLogicContext.getAttribute("validateAttributeCharacter"));
156     }
157
158     @Test
159     public void testGetValueFromContext() throws Exception {
160         Map<String, String> result = Whitebox.invokeMethod(inputParameterValidation, "getValueFromContext",
161             svcLogicContext);
162         Assert.assertEquals("b", result.get("a"));
163         Assert.assertEquals("c", result.get("b"));
164     }
165 }