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