2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.carrier.restclient;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
31 import org.junit.Test;
32 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
33 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
34 import org.onap.policy.apex.service.parameters.ApexParameters;
35 import org.onap.policy.common.parameters.ParameterException;
38 * Test REST client carrier technology parameters.
40 public class RestClientCarrierTechnologyParametersTest {
43 public void testRestClientCarrierTechnologyParametersBadList() {
44 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
45 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
46 arguments.setRelativeFileRoot(".");
49 new ApexParameterHandler().getParameters(arguments);
50 fail("test should throw an exception here");
51 } catch (ParameterException pe) {
52 assertTrue(pe.getMessage().contains("HTTP header array entry is null\n parameter"));
53 assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null"));
58 public void testRestClientCarrierTechnologyParametersNotKvPairs() {
59 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
60 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
61 arguments.setRelativeFileRoot(".");
64 new ApexParameterHandler().getParameters(arguments);
65 fail("test should throw an exception here");
66 } catch (ParameterException pe) {
67 assertTrue(pe.getMessage()
68 .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]"));
69 assertTrue(pe.getMessage().trim()
70 .endsWith("HTTP header array entries must have one key and one value: [aaa]"));
75 public void testRestClientCarrierTechnologyParametersNulls() {
76 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
77 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
78 arguments.setRelativeFileRoot(".");
81 new ApexParameterHandler().getParameters(arguments);
82 fail("test should throw an exception here");
83 } catch (ParameterException pe) {
84 assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]"));
85 assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]"));
90 public void testRestClientCarrierTechnologyParameterFilterInvalid() {
91 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
92 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
93 arguments.setRelativeFileRoot(".");
95 assertThatCode(() -> {
96 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
97 parameters.getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
98 }).hasMessageContaining(
99 "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ");
103 public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
104 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
105 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
106 arguments.setRelativeFileRoot(".");
108 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
110 RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
111 .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
112 assertEquals(0, rrctp0.getHttpHeaders().length);
114 RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
115 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
116 assertEquals(3, rrctp1.getHttpHeaders().length);
117 assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
118 assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
119 assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
121 rrctp1.setHttpHeaders(null);
122 assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
126 public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
127 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
128 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
129 arguments.setRelativeFileRoot(".");
131 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
133 RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
134 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
135 assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
139 public void testGettersAndSetters() {
140 RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
142 rrctp.setUrl("http://some.where");
143 assertEquals("http://some.where", rrctp.getUrl());
145 rrctp.setHttpCodeFilter("[1-5][0][0-5]");
146 assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
148 String[][] httpHeaders = new String[2][2];
149 httpHeaders[0][0] = "aaa";
150 httpHeaders[0][1] = "bbb";
151 httpHeaders[1][0] = "ccc";
152 httpHeaders[1][1] = "ddd";
154 rrctp.setHttpHeaders(httpHeaders);
155 assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
156 assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
157 assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
158 assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
160 rrctp.setHttpHeaders(null);
161 assertFalse(rrctp.checkHttpHeadersSet());
163 String[][] httpHeadersZeroLength = new String[0][0];
164 rrctp.setHttpHeaders(httpHeadersZeroLength);
165 assertFalse(rrctp.checkHttpHeadersSet());
167 rrctp.setHttpHeaders(httpHeaders);
168 assertTrue(rrctp.checkHttpHeadersSet());
170 rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
171 assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
173 assertEquals("RESTCLIENTCarrierTechnologyParameters "
174 + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
175 + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
179 public void testUrlValidation() {
180 RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
182 rrctp.setUrl("http://some.where.no.tag.in.url");
183 assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
185 String[][] httpHeaders = new String[2][2];
186 httpHeaders[0][0] = "aaa";
187 httpHeaders[0][1] = "bbb";
188 httpHeaders[1][0] = "ccc";
189 httpHeaders[1][1] = "ddd";
191 rrctp.setHttpHeaders(httpHeaders);
192 assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
193 assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
194 assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
195 assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
197 assertEquals(true, rrctp.validate().isValid());
199 rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
200 assertEquals(true, rrctp.validate().isValid());
202 Set<String> keymap = rrctp.getKeysFromUrl();
203 assertEquals(true, keymap.contains("place"));
204 assertEquals(true, keymap.contains("that"));
205 assertEquals(true, keymap.contains("one"));
207 rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}");
208 assertEquals(false, rrctp.validate().isValid());
209 rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist");
210 assertEquals(false, rrctp.validate().isValid());
211 rrctp.setUrl("http://place.that/is.not/what.{exist");
212 assertEquals(false, rrctp.validate().isValid());
213 rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}");
214 assertEquals(false, rrctp.validate().isValid());
215 rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}");
216 assertEquals(false, rrctp.validate().isValid());
217 rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}");
218 assertEquals(false, rrctp.validate().isValid());
219 rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}");
220 assertEquals(false, rrctp.validate().isValid());