ef0384f9c3b36c4016f971d6f275756f3f3093be
[policy/apex-pdp.git] /
1 /*-
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
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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.Set;
30
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;
36
37 /**
38  * Test REST Requestor carrier technology parameters.
39  */
40 public class RestClientCarrierTechnologyParametersTest {
41
42     @Test
43     public void testRestClientCarrierTechnologyParametersBadList() {
44         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
45         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
46         arguments.setRelativeFileRoot(".");
47
48         try {
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"));
54         }
55     }
56
57     @Test
58     public void testRestClientCarrierTechnologyParametersNotKvPairs() {
59         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
60         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
61         arguments.setRelativeFileRoot(".");
62
63         try {
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]"));
71         }
72     }
73
74     @Test
75     public void testRestClientCarrierTechnologyParametersNulls() {
76         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
77         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
78         arguments.setRelativeFileRoot(".");
79
80         try {
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]"));
86         }
87     }
88
89     @Test
90     public void testRestClientCarrierTechnologyParameterFilterInvalid() {
91         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
92         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
93         arguments.setRelativeFileRoot(".");
94
95         try {
96             new ApexParameterHandler().getParameters(arguments);
97             ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
98
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: "));
104         }
105     }
106
107     @Test
108     public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
109         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
110         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
111         arguments.setRelativeFileRoot(".");
112
113         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
114
115         RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
116                 .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
117         assertEquals(0, rrctp0.getHttpHeaders().length);
118
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));
125
126         rrctp1.setHttpHeaders(null);
127         assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
128     }
129
130     @Test
131     public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
132         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
133         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
134         arguments.setRelativeFileRoot(".");
135
136         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
137
138         RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
139                 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
140         assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
141     }
142
143     @Test
144     public void testGettersAndSetters() {
145         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
146
147         rrctp.setUrl("http://some.where");
148         assertEquals("http://some.where", rrctp.getUrl());
149
150         rrctp.setHttpCodeFilter("[1-5][0][0-5]");
151         assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
152
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";
158
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]);
164
165         rrctp.setHttpHeaders(null);
166         assertFalse(rrctp.checkHttpHeadersSet());
167
168         String[][] httpHeadersZeroLength = new String[0][0];
169         rrctp.setHttpHeaders(httpHeadersZeroLength);
170         assertFalse(rrctp.checkHttpHeadersSet());
171
172         rrctp.setHttpHeaders(httpHeaders);
173         assertTrue(rrctp.checkHttpHeadersSet());
174
175         rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
176         assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
177
178         assertEquals("RestClientCarrierTechnologyParameters "
179                 + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
180                 + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
181     }
182
183     @Test
184     public void testUrlValidation() {
185         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
186
187         rrctp.setUrl("http://some.where.no.tag.in.url");
188         assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
189
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";
195
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]);
201
202         assertEquals(true, rrctp.validate().isValid());
203
204         rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
205         assertEquals(true, rrctp.validate().isValid());
206
207         Set<String> keymap = rrctp.getKeysFromUrl();
208         assertEquals(true, keymap.contains("place"));
209         assertEquals(true, keymap.contains("that"));
210         assertEquals(true, keymap.contains("one"));
211
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());
226     }
227 }