66e73653f6a6aea779a80c3d3717ab16ce6d201d
[aai/router-core.git] / src / test / java / org / onap / aai / rest / RestClientTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.rest;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.lang.reflect.Method;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.camel.Exchange;
32 import org.apache.camel.Message;
33 import org.apache.camel.impl.DefaultMessage;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.aai.event.TestCamelContext;
37 import org.onap.aai.logging.RouterCoreMsgs;
38 import org.onap.aai.restclient.client.Headers;
39
40 public class RestClientTest {
41
42     /**
43      * Test case initialization
44      * 
45      * @throws Exception the exception
46      */
47     @Before
48     public void init() throws Exception {
49     }
50
51     @Test
52     public void validate() throws Exception {
53
54         try {
55             RestClientComponent rc = new RestClientComponent();
56             RestClientEndpoint endpoint = new RestClientEndpoint("http://host.com:8443/endpoint", rc);
57
58             endpoint.setEcompClientCert("client-cert");
59             endpoint.setEcompKeystore("keystore");
60             endpoint.setEcompKeystorePassword("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10");
61             endpoint.setOp("GET");
62
63             assertTrue(endpoint.getEcompClientCert().compareTo("client-cert") == 0);
64             assertTrue(endpoint.getEcompKeystore().compareTo("keystore") == 0);
65             assertTrue(endpoint.getEcompKeystorePassword().compareTo("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10") == 0);
66             assertTrue(endpoint.getOp().compareTo("GET") == 0);
67             assertTrue(endpoint.isSingleton());
68
69             RestClientProducer producer = (RestClientProducer)endpoint.createProducer();
70             assertTrue(producer != null);
71
72             Method method = RestClientProducer.class.getDeclaredMethod("populateRestHeaders", Exchange.class);
73             method.setAccessible(true);
74
75             Exchange exchange = endpoint.createExchange();
76             exchange.setProperty(Exchange.TO_ENDPOINT, "mock://get");
77             DefaultMessage in = (DefaultMessage)exchange.getIn();
78             in.setCamelContext(new TestCamelContext());
79             
80             in.setHeader(RestClientEndpoint.IN_HEADER_URL, "svc/endpoint");
81             in.setHeader(Headers.FROM_APP_ID, "val1");
82             in.setHeader(Headers.TRANSACTION_ID, "val2");
83             in.setHeader(Headers.RESOURCE_VERSION, "val2");
84             in.setHeader(Headers.ETAG, "val2");
85             in.setHeader(Headers.IF_MATCH, "val2");
86             in.setHeader(Headers.IF_NONE_MATCH, "val2");
87             in.setHeader(Headers.ACCEPT, "val2");
88             in.setHeader("Content-Type", "val2");
89
90             Map<String, List<String>> headers = (Map<String, List<String>>)method.invoke(producer, exchange); 
91             assertTrue(headers.size() == 8);  
92
93             producer.process(exchange);
94
95             System.out.println(RouterCoreMsgs.EVENT_CONSUMER_CREATION_EXCEPTION);
96         }
97         catch (Exception ex) {
98             StringWriter writer = new StringWriter();
99             PrintWriter printWriter = new PrintWriter( writer );
100             ex.printStackTrace( printWriter );
101             printWriter.flush();
102             System.out.println(writer.toString());
103             throw ex;
104         }
105     }
106
107 }