1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.plugins.event.carrier.restclient;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Set;
import org.junit.Test;
import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
import org.onap.policy.apex.service.parameters.ApexParameterHandler;
import org.onap.policy.apex.service.parameters.ApexParameters;
import org.onap.policy.common.parameters.ParameterException;
/**
* Test REST client carrier technology parameters.
*/
public class RestClientCarrierTechnologyParametersTest {
@Test
public void testRestClientCarrierTechnologyParametersBadList() {
ApexCommandLineArguments arguments = new ApexCommandLineArguments();
arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
arguments.setRelativeFileRoot(".");
assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
.hasMessageContaining("HTTP header array entry is null\n parameter");
}
@Test
public void testRestClientCarrierTechnologyParametersNotKvPairs() {
ApexCommandLineArguments arguments = new ApexCommandLineArguments();
arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
arguments.setRelativeFileRoot(".");
assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
.hasMessageContaining("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]")
.hasMessageEndingWith("HTTP header array entries must have one key and one value: [aaa]\n");
}
@Test
public void testRestClientCarrierTechnologyParametersNulls() {
ApexCommandLineArguments arguments = new ApexCommandLineArguments();
arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
arguments.setRelativeFileRoot(".");
assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
.hasMessageContaining("HTTP header key is null or blank: [null, bbb]")
.hasMessageEndingWith("HTTP header value is null or blank: [ccc, null]\n");
}
@Test
public void testRestClientCarrierTechnologyParameterFilterInvalid() {
ApexCommandLineArguments arguments = new ApexCommandLineArguments();
arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
arguments.setRelativeFileRoot(".");
assertThatCode(() -> {
ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
parameters.getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
}).hasMessageContaining(
"Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ");
}
@Test
public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
ApexCommandLineArguments arguments = new ApexCommandLineArguments();
arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
arguments.setRelativeFileRoot(".");
ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
.getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
assertEquals(0, rrctp0.getHttpHeaders().length);
RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
.getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
assertEquals(3, rrctp1.getHttpHeaders().length);
assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
rrctp1.setHttpHeaders(null);
assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
}
@Test
public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
ApexCommandLineArguments arguments = new ApexCommandLineArguments();
arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
arguments.setRelativeFileRoot(".");
ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
.getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
}
@Test
public void testGettersAndSetters() {
RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
rrctp.setUrl("http://some.where");
assertEquals("http://some.where", rrctp.getUrl());
rrctp.setHttpCodeFilter("[1-5][0][0-5]");
assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
String[][] httpHeaders = new String[2][2];
httpHeaders[0][0] = "aaa";
httpHeaders[0][1] = "bbb";
httpHeaders[1][0] = "ccc";
httpHeaders[1][1] = "ddd";
rrctp.setHttpHeaders(httpHeaders);
assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
rrctp.setHttpHeaders(null);
assertFalse(rrctp.checkHttpHeadersSet());
String[][] httpHeadersZeroLength = new String[0][0];
rrctp.setHttpHeaders(httpHeadersZeroLength);
assertFalse(rrctp.checkHttpHeadersSet());
rrctp.setHttpHeaders(httpHeaders);
assertTrue(rrctp.checkHttpHeadersSet());
rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
assertEquals("RESTCLIENTCarrierTechnologyParameters "
+ "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
+ "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
}
@Test
public void testUrlValidation() {
RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
rrctp.setUrl("http://some.where.no.tag.in.url");
assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
String[][] httpHeaders = new String[2][2];
httpHeaders[0][0] = "aaa";
httpHeaders[0][1] = "bbb";
httpHeaders[1][0] = "ccc";
httpHeaders[1][1] = "ddd";
rrctp.setHttpHeaders(httpHeaders);
assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
assertEquals(true, rrctp.validate().isValid());
rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
assertEquals(true, rrctp.validate().isValid());
Set<String> keymap = rrctp.getKeysFromUrl();
assertEquals(true, keymap.contains("place"));
assertEquals(true, keymap.contains("that"));
assertEquals(true, keymap.contains("one"));
rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}");
assertEquals(false, rrctp.validate().isValid());
rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist");
assertEquals(false, rrctp.validate().isValid());
rrctp.setUrl("http://place.that/is.not/what.{exist");
assertEquals(false, rrctp.validate().isValid());
rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}");
assertEquals(false, rrctp.validate().isValid());
rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}");
assertEquals(false, rrctp.validate().isValid());
rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}");
assertEquals(false, rrctp.validate().isValid());
rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}");
assertEquals(false, rrctp.validate().isValid());
}
}
|