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;
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.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
47 arguments.setRelativeFileRoot(".");
50 new ApexParameterHandler().getParameters(arguments);
51 fail("test should throw an exception here");
52 } catch (ParameterException pe) {
53 assertTrue(pe.getMessage().contains("HTTP header array entry is null\n parameter"));
54 assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null"));
59 public void testRestClientCarrierTechnologyParametersNotKvPairs() {
60 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
61 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
62 arguments.setRelativeFileRoot(".");
65 new ApexParameterHandler().getParameters(arguments);
66 fail("test should throw an exception here");
67 } catch (ParameterException pe) {
68 assertTrue(pe.getMessage()
69 .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]"));
70 assertTrue(pe.getMessage().trim()
71 .endsWith("HTTP header array entries must have one key and one value: [aaa]"));
76 public void testRestClientCarrierTechnologyParametersNulls() {
77 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
78 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
79 arguments.setRelativeFileRoot(".");
82 new ApexParameterHandler().getParameters(arguments);
83 fail("test should throw an exception here");
84 } catch (ParameterException pe) {
85 assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]"));
86 assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]"));
91 public void testRestClientCarrierTechnologyParameterFilterInvalid() {
92 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
93 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
94 arguments.setRelativeFileRoot(".");
96 assertThatCode(() -> {
97 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
98 parameters.getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
99 }).hasMessageContaining(
100 "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ");
104 public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
105 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
106 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
107 arguments.setRelativeFileRoot(".");
109 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
111 RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
112 .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
113 assertEquals(0, rrctp0.getHttpHeaders().length);
115 RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
116 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
117 assertEquals(3, rrctp1.getHttpHeaders().length);
118 assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
119 assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
120 assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
122 rrctp1.setHttpHeaders(null);
123 assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
127 public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
128 ApexCommandLineArguments arguments = new ApexCommandLineArguments();
129 arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
130 arguments.setRelativeFileRoot(".");
132 ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
134 RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
135 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
136 assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
140 public void testGettersAndSetters() {
141 RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
143 rrctp.setUrl("http://some.where");
144 assertEquals("http://some.where", rrctp.getUrl());
146 rrctp.setHttpCodeFilter("[1-5][0][0-5]");
147 assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
149 String[][] httpHeaders = new String[2][2];
150 httpHeaders[0][0] = "aaa";
151 httpHeaders[0][1] = "bbb";
152 httpHeaders[1][0] = "ccc";
153 httpHeaders[1][1] = "ddd";
155 rrctp.setHttpHeaders(httpHeaders);
156 assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
157 assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
158 assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
159 assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
161 rrctp.setHttpHeaders(null);
162 assertFalse(rrctp.checkHttpHeadersSet());
164 String[][] httpHeadersZeroLength = new String[0][0];
165 rrctp.setHttpHeaders(httpHeadersZeroLength);
166 assertFalse(rrctp.checkHttpHeadersSet());
168 rrctp.setHttpHeaders(httpHeaders);
169 assertTrue(rrctp.checkHttpHeadersSet());
171 rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
172 assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
174 assertEquals("RESTCLIENTCarrierTechnologyParameters "
175 + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
176 + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
180 public void testUrlValidation() {
181 RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
183 rrctp.setUrl("http://some.where.no.tag.in.url");
184 assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
186 String[][] httpHeaders = new String[2][2];
187 httpHeaders[0][0] = "aaa";
188 httpHeaders[0][1] = "bbb";
189 httpHeaders[1][0] = "ccc";
190 httpHeaders[1][1] = "ddd";
192 rrctp.setHttpHeaders(httpHeaders);
193 assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
194 assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
195 assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
196 assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
198 assertEquals(true, rrctp.validate().isValid());
200 rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
201 assertEquals(true, rrctp.validate().isValid());
203 Set<String> keymap = rrctp.getKeysFromUrl();
204 assertEquals(true, keymap.contains("place"));
205 assertEquals(true, keymap.contains("that"));
206 assertEquals(true, keymap.contains("one"));
208 rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}");
209 assertEquals(false, rrctp.validate().isValid());
210 rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist");
211 assertEquals(false, rrctp.validate().isValid());
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}/{}.{not}/{what}.{exist}");
219 assertEquals(false, rrctp.validate().isValid());
220 rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}");
221 assertEquals(false, rrctp.validate().isValid());