9218347df2d45c49b13958747c5f45efc5f97368
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.apex.client.deployment.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import org.junit.Test;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30
31 /**
32  * Test the parameter check class.
33  *
34  */
35 public class ParameterCheckTest {
36
37     @Test
38     public void testStartStop() {
39         assertEquals("START", ParameterCheck.StartStop.START.name());
40         assertEquals("STOP", ParameterCheck.StartStop.STOP.name());
41     }
42
43     @Test
44     public void testHostName() {
45         assertNull(ParameterCheck.getHostName(null));
46
47         Map<String, String[]> parameterMap = new LinkedHashMap<>();
48         assertNull(ParameterCheck.getHostName(parameterMap));
49         parameterMap.put("hostname", null);
50         assertNull(ParameterCheck.getHostName(parameterMap));
51
52         String[] hostnameBlankValue0 = {"", ""};
53         parameterMap.put("hostname", hostnameBlankValue0);
54         assertNull(ParameterCheck.getHostName(parameterMap));
55
56         String[] hostnameBlankValue1 = {" ", " "};
57         parameterMap.put("hostname", hostnameBlankValue1);
58         assertNull(ParameterCheck.getHostName(parameterMap));
59
60         String[] hostnameValue = {"hostname0", "hostname1"};
61         parameterMap.put("hostname", hostnameValue);
62         assertEquals("hostname0", ParameterCheck.getHostName(parameterMap));
63     }
64
65     @Test
66     public void testPort() {
67         assertEquals(-1, ParameterCheck.getPort(null));
68
69         Map<String, String[]> parameterMap = new LinkedHashMap<>();
70         assertEquals(-1, ParameterCheck.getPort(parameterMap));
71
72         String[] portBlankValue0 = {"", ""};
73         parameterMap.put("port", portBlankValue0);
74         assertEquals(-1, ParameterCheck.getPort(parameterMap));
75
76         String[] portBlankValue1 = {" ", " "};
77         parameterMap.put("port", portBlankValue1);
78         assertEquals(-1, ParameterCheck.getPort(parameterMap));
79
80         String[] portValueBad = {"port", "value"};
81         parameterMap.put("port", portValueBad);
82         assertEquals(-1, ParameterCheck.getPort(parameterMap));
83
84         String[] portValueRange0 = {"-1", "-1"};
85         parameterMap.put("port", portValueRange0);
86         assertEquals(-1, ParameterCheck.getPort(parameterMap));
87
88         String[] portValueRange1 = {"65536", "65536"};
89         parameterMap.put("port", portValueRange1);
90         assertEquals(-1, ParameterCheck.getPort(parameterMap));
91
92         String[] portValue = {"12344", "23221"};
93         parameterMap.put("port", portValue);
94         assertEquals(12344, ParameterCheck.getPort(parameterMap));
95     }
96
97     @Test
98     public void testEngineKey() {
99         assertEquals(null, ParameterCheck.getEngineKey(null));
100
101         Map<String, String[]> parameterMap = new LinkedHashMap<>();
102         parameterMap.put("Zooby", null);
103         assertEquals(null, ParameterCheck.getEngineKey(parameterMap));
104
105         parameterMap.put("AxArtifactKey", null);
106         assertEquals(null, ParameterCheck.getEngineKey(parameterMap));
107         parameterMap.remove("AxArtifactKey");
108
109         parameterMap.put("AxArtifactKey#zooby", null);
110         assertEquals(null, ParameterCheck.getEngineKey(parameterMap));
111         parameterMap.remove("AxArtifactKey#zooby");
112
113         parameterMap.put("AxArtifactKey#zooby#looby", null);
114         assertEquals(null, ParameterCheck.getEngineKey(parameterMap));
115         parameterMap.remove("AxArtifactKey#zooby#looby");
116
117         parameterMap.put("AxArtifactKey#Name:0.0.1", null);
118         assertEquals(new AxArtifactKey("Name", "0.0.1"), ParameterCheck.getEngineKey(parameterMap));
119     }
120
121     @Test
122     public void testStartStopValue() {
123         assertEquals(null, ParameterCheck.getStartStop(null, null));
124
125         Map<String, String[]> parameterMap = new LinkedHashMap<>();
126         assertEquals(null, ParameterCheck.getStartStop(parameterMap, null));
127
128         parameterMap.put("Zooby", null);
129         assertEquals(null, ParameterCheck.getStartStop(parameterMap, null));
130
131         AxArtifactKey engineKey = new AxArtifactKey("Engine", "0.0.1");
132
133         parameterMap.put("Zooby", null);
134         assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey));
135
136         String key = "AxArtifactKey#" + engineKey.getId();
137
138         parameterMap.put(key, null);
139         assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey));
140
141         String[] startStopBlankValue0 = {"", ""};
142         parameterMap.put(key, startStopBlankValue0);
143         assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey));
144
145         String[] startStopBlankValue1 = {" ", " "};
146         parameterMap.put(key, startStopBlankValue1);
147         assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey));
148
149         String[] startStopValueBad = {key, "value"};
150         parameterMap.put(key, startStopValueBad);
151         assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey));
152
153         String[] startValue = {"START", "STOP"};
154         parameterMap.put(key, startValue);
155         assertEquals(ParameterCheck.StartStop.START, ParameterCheck.getStartStop(parameterMap, engineKey));
156
157         String[] stopValue = {"STOP", "START"};
158         parameterMap.put(key, stopValue);
159         assertEquals(ParameterCheck.StartStop.STOP, ParameterCheck.getStartStop(parameterMap, engineKey));
160     }
161
162     @Test
163     public void testLong() {
164         assertEquals(-1, ParameterCheck.getLong(null, null));
165
166         Map<String, String[]> parameterMap = new LinkedHashMap<>();
167         assertEquals(-1, ParameterCheck.getLong(parameterMap, null));
168
169         parameterMap.put("long0", null);
170         assertEquals(-1, ParameterCheck.getLong(parameterMap, "longx"));
171         assertEquals(-1, ParameterCheck.getLong(parameterMap, "long0"));
172
173         String[] longBlankValue0 = {"", ""};
174         parameterMap.put("long1", longBlankValue0);
175         assertEquals(-1, ParameterCheck.getLong(parameterMap, "long1"));
176
177         String[] longBlankValue1 = {" ", " "};
178         parameterMap.put("long2", longBlankValue1);
179         assertEquals(-1, ParameterCheck.getLong(parameterMap, "long2"));
180
181         String[] longValueBad = {"long", "value"};
182         parameterMap.put("long3", longValueBad);
183         assertEquals(-1, ParameterCheck.getLong(parameterMap, "long3"));
184
185         String[] longValue = {"12345", "6789"};
186         parameterMap.put("long4", longValue);
187         assertEquals(12345, ParameterCheck.getLong(parameterMap, "long4"));
188     }
189 }