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