df3b61bcb2a4310974581ca221407ced6ed447d4
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restclient / src / test / java / org / onap / policy / apex / plugins / event / carrier / restclient / RestClientCarrierTechnologyParametersTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.restclient;
23
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;
29
30 import java.util.Set;
31
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;
37
38 /**
39  * Test REST client carrier technology parameters.
40  */
41 public class RestClientCarrierTechnologyParametersTest {
42
43     @Test
44     public void testRestClientCarrierTechnologyParametersBadList() {
45         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
46         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
47         arguments.setRelativeFileRoot(".");
48
49         try {
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"));
55         }
56     }
57
58     @Test
59     public void testRestClientCarrierTechnologyParametersNotKvPairs() {
60         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
61         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
62         arguments.setRelativeFileRoot(".");
63
64         try {
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]"));
72         }
73     }
74
75     @Test
76     public void testRestClientCarrierTechnologyParametersNulls() {
77         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
78         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
79         arguments.setRelativeFileRoot(".");
80
81         try {
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]"));
87         }
88     }
89
90     @Test
91     public void testRestClientCarrierTechnologyParameterFilterInvalid() {
92         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
93         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
94         arguments.setRelativeFileRoot(".");
95
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: ");
101     }
102
103     @Test
104     public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
105         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
106         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
107         arguments.setRelativeFileRoot(".");
108
109         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
110
111         RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
112                 .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
113         assertEquals(0, rrctp0.getHttpHeaders().length);
114
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));
121
122         rrctp1.setHttpHeaders(null);
123         assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
124     }
125
126     @Test
127     public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
128         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
129         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
130         arguments.setRelativeFileRoot(".");
131
132         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
133
134         RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
135                 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
136         assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
137     }
138
139     @Test
140     public void testGettersAndSetters() {
141         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
142
143         rrctp.setUrl("http://some.where");
144         assertEquals("http://some.where", rrctp.getUrl());
145
146         rrctp.setHttpCodeFilter("[1-5][0][0-5]");
147         assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
148
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";
154
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]);
160
161         rrctp.setHttpHeaders(null);
162         assertFalse(rrctp.checkHttpHeadersSet());
163
164         String[][] httpHeadersZeroLength = new String[0][0];
165         rrctp.setHttpHeaders(httpHeadersZeroLength);
166         assertFalse(rrctp.checkHttpHeadersSet());
167
168         rrctp.setHttpHeaders(httpHeaders);
169         assertTrue(rrctp.checkHttpHeadersSet());
170
171         rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
172         assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
173
174         assertEquals("RESTCLIENTCarrierTechnologyParameters "
175                 + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
176                 + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
177     }
178
179     @Test
180     public void testUrlValidation() {
181         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
182
183         rrctp.setUrl("http://some.where.no.tag.in.url");
184         assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
185
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";
191
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]);
197
198         assertEquals(true, rrctp.validate().isValid());
199
200         rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
201         assertEquals(true, rrctp.validate().isValid());
202
203         Set<String> keymap = rrctp.getKeysFromUrl();
204         assertEquals(true, keymap.contains("place"));
205         assertEquals(true, keymap.contains("that"));
206         assertEquals(true, keymap.contains("one"));
207
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());
222     }
223 }