1e0006811ed5eab8b76904a0b6450704a103c616
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restrequestor / src / test / java / org / onap / policy / apex / plugins / event / carrier / restrequestor / RestRequestorCarrierTechnologyParametersTest.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.restrequestor;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
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 RestRequestorCarrierTechnologyParametersTest {
41
42     @Test
43     public void testRestRequestorCarrierTechnologyParametersBadList() {
44         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
45         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json");
46         arguments.setRelativeFileRoot(".");
47
48         assertThatThrownBy(() -> {
49             new ApexParameterHandler().getParameters(arguments);
50         }).hasMessageContaining("HTTP header array entry is null\n    parameter");
51     }
52
53     @Test
54     public void testRestRequestorCarrierTechnologyParametersNotKvPairs() {
55         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
56         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json");
57         arguments.setRelativeFileRoot(".");
58
59         assertThatThrownBy(() -> {
60             new ApexParameterHandler().getParameters(arguments);
61         }).hasMessageContaining("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]");
62     }
63
64     @Test
65     public void testRestRequestorCarrierTechnologyParametersNulls() {
66         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
67         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json");
68         arguments.setRelativeFileRoot(".");
69
70         assertThatThrownBy(() -> {
71             new ApexParameterHandler().getParameters(arguments);
72         }).hasMessageContaining("HTTP header key is null or blank: [null, bbb]");
73     }
74
75     @Test
76     public void testRestRequestorCarrierTechnologyParametersOk() throws ParameterException {
77         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
78         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json");
79         arguments.setRelativeFileRoot(".");
80
81         ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
82
83         RestRequestorCarrierTechnologyParameters rrctp0 = (RestRequestorCarrierTechnologyParameters) parameters
84             .getEventInputParameters().get("RestRequestorConsumer0").getCarrierTechnologyParameters();
85         assertEquals(0, rrctp0.getHttpHeaders().length);
86
87         RestRequestorCarrierTechnologyParameters rrctp1 = (RestRequestorCarrierTechnologyParameters) parameters
88             .getEventInputParameters().get("RestRequestorConsumer1").getCarrierTechnologyParameters();
89         assertEquals(3, rrctp1.getHttpHeaders().length);
90         assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
91         assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
92         assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
93     }
94
95     @Test
96     public void testRestClientCarrierTechnologyParameterFilterInvalid() {
97         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
98         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json");
99         arguments.setRelativeFileRoot(".");
100
101         assertThatThrownBy(() -> {
102             new ApexParameterHandler().getParameters(arguments);
103             ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
104
105             parameters.getEventInputParameters().get("RestRequestorConsumer0").getCarrierTechnologyParameters();
106         }).hasMessageContaining(
107             "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ");
108     }
109
110     @Test
111     public void testGettersAndSetters() {
112         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
113
114         rrctp.setHttpHeaders(null);
115         assertEquals(null, rrctp.getHttpHeadersAsMultivaluedMap());
116
117         rrctp.setUrl("http://some.where");
118         assertEquals("http://some.where", rrctp.getUrl());
119
120         rrctp.setHttpCodeFilter("[1-5][0][0-5]");
121         assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter());
122
123         String[][] httpHeaders = new String[2][2];
124         httpHeaders[0][0] = "aaa";
125         httpHeaders[0][1] = "bbb";
126         httpHeaders[1][0] = "ccc";
127         httpHeaders[1][1] = "ddd";
128
129         rrctp.setHttpHeaders(httpHeaders);
130         assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
131         assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
132         assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
133         assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
134
135         rrctp.setHttpHeaders(null);
136         assertFalse(rrctp.checkHttpHeadersSet());
137
138         String[][] httpHeadersZeroLength = new String[0][0];
139         rrctp.setHttpHeaders(httpHeadersZeroLength);
140         assertFalse(rrctp.checkHttpHeadersSet());
141
142         rrctp.setHttpHeaders(httpHeaders);
143         assertTrue(rrctp.checkHttpHeadersSet());
144
145         rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE);
146         assertEquals(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
147
148         assertEquals("RESTREQUESTORCarrierTechnologyParameters "
149             + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]],"
150             + " httpCodeFilter=[1-5][0][0-5]]", rrctp.toString());
151     }
152
153     @Test
154     public void testUrlValidation() {
155         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
156
157         rrctp.setUrl("http://some.where.no.tag.in.url");
158         assertEquals("http://some.where.no.tag.in.url", rrctp.getUrl());
159
160         String[][] httpHeaders = new String[2][2];
161         httpHeaders[0][0] = "aaa";
162         httpHeaders[0][1] = "bbb";
163         httpHeaders[1][0] = "ccc";
164         httpHeaders[1][1] = "ddd";
165
166         rrctp.setHttpHeaders(httpHeaders);
167         assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
168         assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
169         assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
170         assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
171
172         assertEquals(true, rrctp.validate().isValid());
173
174         rrctp.setUrl("http://{place}.{that}/is{that}.{one}");
175         assertEquals(true, rrctp.validate().isValid());
176
177         Set<String> keymap = rrctp.getKeysFromUrl();
178         assertEquals(true, keymap.contains("place"));
179         assertEquals(true, keymap.contains("that"));
180         assertEquals(true, keymap.contains("one"));
181
182         rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}");
183         assertEquals(false, rrctp.validate().isValid());
184         rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist");
185         assertEquals(false, rrctp.validate().isValid());
186         rrctp.setUrl("http://place.that/is.not/what.{exist");
187         assertEquals(false, rrctp.validate().isValid());
188         rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}");
189         assertEquals(false, rrctp.validate().isValid());
190         rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}");
191         assertEquals(false, rrctp.validate().isValid());
192         rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}");
193         assertEquals(false, rrctp.validate().isValid());
194         rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}");
195         assertEquals(false, rrctp.validate().isValid());
196     }
197
198 }