Update apache camel from 2.x to 3.x
[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.ExtendedCamelContext;
33 import org.apache.camel.support.DefaultMessage;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.aai.logging.RouterCoreMsgs;
40 import org.onap.aai.restclient.client.Headers;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class RestClientTest {
44
45
46     @Mock
47     public ExtendedCamelContext context;
48
49     /**
50      * Test case initialization
51      * 
52      * @throws Exception the exception
53      */
54     @Before
55     public void init() throws Exception {
56     }
57
58     @Test
59     public void validate() throws Exception {
60
61         try {
62             RestClientComponent rc = new RestClientComponent(context);
63             RestClientEndpoint endpoint = new RestClientEndpoint("http://host.com:8443/endpoint", rc);
64
65             endpoint.setEcompClientCert("client-cert");
66             endpoint.setEcompKeystore("keystore");
67             endpoint.setEcompKeystorePassword("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10");
68             endpoint.setOp("GET");
69
70
71             assertTrue(endpoint.getEcompClientCert().compareTo("client-cert") == 0);
72             assertTrue(endpoint.getEcompKeystore().compareTo("keystore") == 0);
73             assertTrue(endpoint.getEcompKeystorePassword().compareTo("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10") == 0);
74             assertTrue(endpoint.getOp().compareTo("GET") == 0);
75             assertTrue(endpoint.isSingleton());
76
77             RestClientProducer producer = (RestClientProducer)endpoint.createProducer();
78             assertTrue(producer != null);
79
80             Method method = RestClientProducer.class.getDeclaredMethod("populateRestHeaders", Exchange.class);
81             method.setAccessible(true);
82
83             Exchange exchange = endpoint.createExchange();
84             exchange.setProperty(Exchange.TO_ENDPOINT, "mock://get");
85             DefaultMessage in = (DefaultMessage)exchange.getIn();
86             
87             in.setHeader(RestClientEndpoint.IN_HEADER_URL, "svc/endpoint");
88             in.setHeader(Headers.FROM_APP_ID, "val1");
89             in.setHeader(Headers.TRANSACTION_ID, "val2");
90             in.setHeader(Headers.RESOURCE_VERSION, "val2");
91             in.setHeader(Headers.ETAG, "val2");
92             in.setHeader(Headers.IF_MATCH, "val2");
93             in.setHeader(Headers.IF_NONE_MATCH, "val2");
94             in.setHeader(Headers.ACCEPT, "val2");
95             in.setHeader("Content-Type", "val2");
96
97             Map<String, List<String>> headers = (Map<String, List<String>>)method.invoke(producer, exchange); 
98             assertTrue(headers.size() == 8);  
99
100             producer.process(exchange);
101
102             System.out.println(RouterCoreMsgs.EVENT_CONSUMER_CREATION_EXCEPTION);
103         }
104         catch (Exception ex) {
105             StringWriter writer = new StringWriter();
106             PrintWriter printWriter = new PrintWriter( writer );
107             ex.printStackTrace( printWriter );
108             printWriter.flush();
109             System.out.println(writer.toString());
110             throw ex;
111         }
112     }
113
114 }