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