3d0c9b73f71184b82db2b02e822a631bfaba46d0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.restclient;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import org.junit.Test;
29 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
30 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
31 import org.onap.policy.apex.service.parameters.ApexParameters;
32 import org.onap.policy.common.parameters.ParameterException;
33
34 /**
35  * Test REST Requestor carrier technology parameters.
36  */
37 public class RestClientCarrierTechnologyParametersTest {
38
39     @Test
40     public void testRestClientCarrierTechnologyParametersBadList() {
41         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
42         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json");
43         arguments.setRelativeFileRoot(".");
44
45         try {
46             new ApexParameterHandler().getParameters(arguments);
47             fail("test should throw an exception here");
48         } catch (ParameterException pe) {
49             assertTrue(pe.getMessage().contains("HTTP header array entry is null\n    parameter"));
50             assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null"));
51         }
52     }
53
54     @Test
55     public void testRestClientCarrierTechnologyParametersNotKvPairs() {
56         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
57         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json");
58         arguments.setRelativeFileRoot(".");
59
60         try {
61             new ApexParameterHandler().getParameters(arguments);
62             fail("test should throw an exception here");
63         } catch (ParameterException pe) {
64             assertTrue(pe.getMessage()
65                             .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]"));
66             assertTrue(pe.getMessage().trim()
67                             .endsWith("HTTP header array entries must have one key and one value: [aaa]"));
68         }
69     }
70
71     @Test
72     public void testRestClientCarrierTechnologyParametersNulls() {
73         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
74         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json");
75         arguments.setRelativeFileRoot(".");
76
77         try {
78             new ApexParameterHandler().getParameters(arguments);
79             fail("test should throw an exception here");
80         } catch (ParameterException pe) {
81             assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]"));
82             assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]"));
83         }
84     }
85
86     @Test
87     public void testRestClientCarrierTechnologyParametersOk() {
88         ApexCommandLineArguments arguments = new ApexCommandLineArguments();
89         arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json");
90         arguments.setRelativeFileRoot(".");
91
92         try {
93             ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
94
95             RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters
96                             .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters();
97             assertEquals(0, rrctp0.getHttpHeaders().length);
98
99             RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters
100                             .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters();
101             assertEquals(3, rrctp1.getHttpHeaders().length);
102             assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
103             assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
104             assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
105             
106             rrctp1.setHttpHeaders(null);
107             assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap());
108         } catch (ParameterException pe) {
109             fail("test should not throw an exception");
110         }
111     }
112
113     @Test
114     public void testGettersAndSetters() {
115         RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters();
116
117         rrctp.setUrl("http://some.where");
118         assertEquals("http://some.where", rrctp.getUrl());
119
120         String[][] httpHeaders = new String[2][2];
121         httpHeaders[0][0] = "aaa";
122         httpHeaders[0][1] = "bbb";
123         httpHeaders[1][0] = "ccc";
124         httpHeaders[1][1] = "ddd";
125
126         rrctp.setHttpHeaders(httpHeaders);
127         assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
128         assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
129         assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
130         assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);
131
132         rrctp.setHttpHeaders(null);
133         assertFalse(rrctp.checkHttpHeadersSet());
134
135         String[][] httpHeadersZeroLength = new String[0][0];
136         rrctp.setHttpHeaders(httpHeadersZeroLength);
137         assertFalse(rrctp.checkHttpHeadersSet());
138
139         rrctp.setHttpHeaders(httpHeaders);
140         assertTrue(rrctp.checkHttpHeadersSet());
141
142         rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
143         assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());
144
145         assertEquals("RestClientCarrierTechnologyParameters "
146                         + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]",
147                         rrctp.toString());
148     }
149 }