aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java
blob: 7be42e264d6fb0c39686ca2dc650396a2edceb34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.plugins.event.carrier.restrequestor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
import org.onap.policy.apex.service.parameters.ApexParameterHandler;
import org.onap.policy.apex.service.parameters.ApexParameters;
import org.onap.policy.common.parameters.ParameterException;

/**
 * Test REST Requestor carrier technology parameters.
 */
public class RestRequestorCarrierTechnologyParametersTest {

    @Test
    public void testRestRequestorCarrierTechnologyParametersBadList() {
        ApexCommandLineArguments arguments = new ApexCommandLineArguments();
        arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json");
        arguments.setRelativeFileRoot(".");

        try {
            new ApexParameterHandler().getParameters(arguments);
            fail("test should throw an exception here");
        } catch (ParameterException pe) {
            assertTrue(pe.getMessage().contains("HTTP header array entry is null\n    parameter"));
            assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null"));
        }
    }

    @Test
    public void testRestRequestorCarrierTechnologyParametersNotKvPairs() {
        ApexCommandLineArguments arguments = new ApexCommandLineArguments();
        arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json");
        arguments.setRelativeFileRoot(".");

        try {
            new ApexParameterHandler().getParameters(arguments);
            fail("test should throw an exception here");
        } catch (ParameterException pe) {
            assertTrue(pe.getMessage()
                            .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]"));
            assertTrue(pe.getMessage().trim()
                            .endsWith("HTTP header array entries must have one key and one value: [aaa]"));
        }
    }

    @Test
    public void testRestRequestorCarrierTechnologyParametersNulls() {
        ApexCommandLineArguments arguments = new ApexCommandLineArguments();
        arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json");
        arguments.setRelativeFileRoot(".");

        try {
            new ApexParameterHandler().getParameters(arguments);
            fail("test should throw an exception here");
        } catch (ParameterException pe) {
            assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]"));
            assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]"));
        }
    }

    @Test
    public void testRestRequestorCarrierTechnologyParametersOk() {
        ApexCommandLineArguments arguments = new ApexCommandLineArguments();
        arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json");
        arguments.setRelativeFileRoot(".");

        try {
            ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);

            RestRequestorCarrierTechnologyParameters rrctp0 = (RestRequestorCarrierTechnologyParameters) parameters
                            .getEventInputParameters().get("RestRequestorConsumer0").getCarrierTechnologyParameters();
            assertEquals(0, rrctp0.getHttpHeaders().length);

            RestRequestorCarrierTechnologyParameters rrctp1 = (RestRequestorCarrierTechnologyParameters) parameters
                            .getEventInputParameters().get("RestRequestorConsumer1").getCarrierTechnologyParameters();
            assertEquals(3, rrctp1.getHttpHeaders().length);
            assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0));
            assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0));
            assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0));
        } catch (ParameterException pe) {
            fail("test should not throw an exception");
        }
    }

    @Test
    public void testGettersAndSetters() {
        RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();

        rrctp.setUrl("http://some.where");
        assertEquals("http://some.where", rrctp.getUrl());

        String[][] httpHeaders = new String[2][2];
        httpHeaders[0][0] = "aaa";
        httpHeaders[0][1] = "bbb";
        httpHeaders[1][0] = "ccc";
        httpHeaders[1][1] = "ddd";

        rrctp.setHttpHeaders(httpHeaders);
        assertEquals("aaa", rrctp.getHttpHeaders()[0][0]);
        assertEquals("bbb", rrctp.getHttpHeaders()[0][1]);
        assertEquals("ccc", rrctp.getHttpHeaders()[1][0]);
        assertEquals("ddd", rrctp.getHttpHeaders()[1][1]);

        rrctp.setHttpHeaders(null);
        assertFalse(rrctp.checkHttpHeadersSet());

        String[][] httpHeadersZeroLength = new String[0][0];
        rrctp.setHttpHeaders(httpHeadersZeroLength);
        assertFalse(rrctp.checkHttpHeadersSet());

        rrctp.setHttpHeaders(httpHeaders);
        assertTrue(rrctp.checkHttpHeadersSet());

        rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE);
        assertEquals(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod());

        assertEquals("RESTRequestorCarrierTechnologyParameters "
                        + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]",
                        rrctp.toString());
    }
}