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