58d265a97b1e344be1e39a5c16bf11323b9df15f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.apex.plugins.event.carrier.restclient;
25
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.Set;
33 import org.junit.Test;
34 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
35 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
36 import org.onap.policy.apex.service.parameters.ApexParameters;
37 import org.onap.policy.common.parameters.ParameterException;
38
39 /**
40  * Test REST client carrier technology parameters.
41  */
42 public class RestClientCarrierTechnologyParametersTest {
43
44     @Test
45     public void testRestClientCarrierTechnologyParametersBadList() {
46         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
47         arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
48         arguments.setRelativeFileRoot(".");
49
50         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
51             .hasMessageContaining("httpHeaders")
52             .hasMessageContaining("item \"entry 0\" value \"null\" INVALID, is null")
53             .hasMessageContaining("item \"entry 1\" value \"null\" INVALID, is null")
54             .hasMessageContaining("item \"entry 2\" value \"null\" INVALID, is null");
55     }
56
57     @Test
58     public void testRestClientCarrierTechnologyParametersNotKvPairs() {
59         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
60         arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
61         arguments.setRelativeFileRoot(".");
62
63         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
64             .hasMessageContaining("httpHeaders")
65             .hasMessageContaining("\"entry 0\" value \"[aaa, bbb, ccc]\" INVALID, must have one key and one value")
66             .hasMessageContaining("\"entry 0\" value \"[aaa]\" INVALID, must have one key and one value");
67     }
68
69     @Test
70     public void testRestClientCarrierTechnologyParametersNulls() {
71         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
72         arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
73         arguments.setRelativeFileRoot(".");
74
75         assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments))
76             .hasMessageContaining("httpHeaders", "entry 0", "entry 1")
77             .hasMessageContaining("item \"key\" value \"null\" INVALID, is blank")
78             .hasMessageContaining("item \"value\" value \"null\" INVALID, is blank");
79     }
80
81     @Test
82     public void testRestClientCarrierTechnologyParameterFilterInvalid() {
83         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
84         arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
85         arguments.setRelativeFileRoot(".");
86
87         assertThatCode(() -> {
88             ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
89             parameters.getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
90         }).hasMessageContaining(
91                 "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ");
92     }
93
94     @Test
95     public void testRestClientCarrierTechnologyParametersOk() throws ParameterException {
96         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
97         arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
98         arguments.setRelativeFileRoot(".");
99
100         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
101
102         RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
103                 .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
104         assertEquals(0, rrctp0.getHttpHeaders().length);
105
106         RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
107                 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
108         assertEquals(3, rrctp1.getHttpHeaders().length);
109         assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
110         assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
111         assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
112
113         rrctp1.setHttpHeaders(null);
114         assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
115     }
116
117     @Test
118     public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException {
119         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
120         arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
121         arguments.setRelativeFileRoot(".");
122
123         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
124
125         RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
126                 .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
127         assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter());
128     }
129
130     @Test
131     public void testGettersAndSetters() {
132         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
133
134         rrctp.setUrl("http://some.where");
135         assertEquals("http://some.where", rrctp.getUrl());
136
137         rrctp.setHttpCodeFilter("[1-5][0][0-5]");
138         assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
139
140         String[][] httpHeaders = new String[2][2];
141         httpHeaders[0][0] = "aaa";
142         httpHeaders[0][1] = "bbb";
143         httpHeaders[1][0] = "ccc";
144         httpHeaders[1][1] = "ddd";
145
146         rrctp.setHttpHeaders(httpHeaders);
147         assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
148         assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
149         assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
150         assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
151
152         rrctp.setHttpHeaders(null);
153         assertFalse(rrctp.checkHttpHeadersSet());
154
155         String[][] httpHeadersZeroLength = new String[0][0];
156         rrctp.setHttpHeaders(httpHeadersZeroLength);
157         assertFalse(rrctp.checkHttpHeadersSet());
158
159         rrctp.setHttpHeaders(httpHeaders);
160         assertTrue(rrctp.checkHttpHeadersSet());
161
162         rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
163         assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
164
165         assertEquals("RESTCLIENTCarrierTechnologyParameters "
166                 + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], "
167                 + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
168     }
169
170     @Test
171     public void testUrlValidation() {
172         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
173
174         rrctp.setUrl("http://some.where.no.tag.in.url");
175         assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
176
177         String[][] httpHeaders = new String[2][2];
178         httpHeaders[0][0] = "aaa";
179         httpHeaders[0][1] = "bbb";
180         httpHeaders[1][0] = "ccc";
181         httpHeaders[1][1] = "ddd";
182
183         rrctp.setHttpHeaders(httpHeaders);
184         assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
185         assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
186         assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
187         assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
188
189         assertEquals(true, rrctp.validate().isValid());
190
191         rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
192         assertEquals(true, rrctp.validate().isValid());
193
194         Set<String> keymap = rrctp.getKeysFromUrl();
195         assertEquals(true, keymap.contains("place"));
196         assertEquals(true, keymap.contains("that"));
197         assertEquals(true, keymap.contains("one"));
198
199         rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}");
200         assertEquals(false, rrctp.validate().isValid());
201         rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist");
202         assertEquals(false, rrctp.validate().isValid());
203         rrctp.setUrl("http://place.that/is.not/what.{exist");
204         assertEquals(false, rrctp.validate().isValid());
205         rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}");
206         assertEquals(false, rrctp.validate().isValid());
207         rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}");
208         assertEquals(false, rrctp.validate().isValid());
209         rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}");
210         assertEquals(false, rrctp.validate().isValid());
211         rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}");
212         assertEquals(false, rrctp.validate().isValid());
213     }
214 }