2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 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 Requestor 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(".");
96 new ApexParameterHandler().getParameters(arguments);
97 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
99 parameters.getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
100 fail("test should throw an exception here");
101 } catch (ParameterException pe) {
102 assertTrue(pe.getMessage().contains(
103 "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: "));
108 public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
109 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
110 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
111 arguments.setRelativeFileRoot(".");
113 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
115 RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
116 .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
117 assertEquals(0, rrctp0.getHttpHeaders().length);
119 RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
120 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
121 assertEquals(3, rrctp1.getHttpHeaders().length);
122 assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
123 assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
124 assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
126 rrctp1.setHttpHeaders(null);
127 assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
131 public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
132 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
133 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
134 arguments.setRelativeFileRoot(".");
136 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
138 RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
139 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
140 assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
144 public void testGettersAndSetters() {
145 RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
147 rrctp.setUrl("http://some.where");
148 assertEquals("http://some.where", rrctp.getUrl());
150 rrctp.setHttpCodeFilter("[1-5][0][0-5]");
151 assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
153 String[][] httpHeaders = new String[2][2];
154 httpHeaders[0][0] = "aaa";
155 httpHeaders[0][1] = "bbb";
156 httpHeaders[1][0] = "ccc";
157 httpHeaders[1][1] = "ddd";
159 rrctp.setHttpHeaders(httpHeaders);
160 assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
161 assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
162 assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
163 assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
165 rrctp.setHttpHeaders(null);
166 assertFalse(rrctp.checkHttpHeadersSet());
168 String[][] httpHeadersZeroLength = new String[0][0];
169 rrctp.setHttpHeaders(httpHeadersZeroLength);
170 assertFalse(rrctp.checkHttpHeadersSet());
172 rrctp.setHttpHeaders(httpHeaders);
173 assertTrue(rrctp.checkHttpHeadersSet());
175 rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
176 assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
178 assertEquals("RESTCLIENTCarrierTechnologyParameters "
179 + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
180 + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
184 public void testUrlValidation() {
185 RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
187 rrctp.setUrl("http://some.where.no.tag.in.url");
188 assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
190 String[][] httpHeaders = new String[2][2];
191 httpHeaders[0][0] = "aaa";
192 httpHeaders[0][1] = "bbb";
193 httpHeaders[1][0] = "ccc";
194 httpHeaders[1][1] = "ddd";
196 rrctp.setHttpHeaders(httpHeaders);
197 assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
198 assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
199 assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
200 assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
202 assertEquals(true, rrctp.validate().isValid());
204 rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
205 assertEquals(true, rrctp.validate().isValid());
207 Set<String> keymap = rrctp.getKeysFromUrl();
208 assertEquals(true, keymap.contains("place"));
209 assertEquals(true, keymap.contains("that"));
210 assertEquals(true, keymap.contains("one"));
212 rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}");
213 assertEquals(false, rrctp.validate().isValid());
214 rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist");
215 assertEquals(false, rrctp.validate().isValid());
216 rrctp.setUrl("http://place.that/is.not/what.{exist");
217 assertEquals(false, rrctp.validate().isValid());
218 rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}");
219 assertEquals(false, rrctp.validate().isValid());
220 rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}");
221 assertEquals(false, rrctp.validate().isValid());
222 rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}");
223 assertEquals(false, rrctp.validate().isValid());
224 rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}");
225 assertEquals(false, rrctp.validate().isValid());