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