Update license date and text
[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 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.rest;
24
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.lang.reflect.Method;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.apache.camel.Exchange;
34 import org.apache.camel.Message;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.aai.restclient.client.Headers;
38
39 public class RestClientTest {
40
41   /**
42    * Test case initialization
43    * 
44    * @throws Exception the exception
45    */
46   @Before
47   public void init() throws Exception {
48   }
49   
50   @Test
51   public void validate() throws Exception {
52
53     try {
54       RestClientComponent rc = new RestClientComponent();
55       RestClientEndpoint endpoint = new RestClientEndpoint("http://host.com:8443/endpoint", rc);
56
57       endpoint.setEcompClientCert("client-cert");
58       endpoint.setEcompKeystore("keystore");
59       endpoint.setEcompKeystorePassword("pwd");
60       endpoint.setOp("operation");
61
62       assertTrue(endpoint.getEcompClientCert().compareTo("client-cert") == 0);
63       assertTrue(endpoint.getEcompKeystore().compareTo("keystore") == 0);
64       assertTrue(endpoint.getEcompKeystorePassword().compareTo("pwd") == 0);
65       assertTrue(endpoint.getOp().compareTo("operation") == 0);
66       assertTrue(endpoint.isSingleton());
67       
68       RestClientProducer producer = (RestClientProducer)endpoint.createProducer();
69       assertTrue(producer != null);
70       
71       Method method = RestClientProducer.class.getDeclaredMethod("populateRestHeaders", Exchange.class);
72       method.setAccessible(true);
73       
74       Exchange exchange = endpoint.createExchange();
75       Message in = exchange.getIn();
76       in.setHeader(RestClientEndpoint.IN_HEADER_URL, "svc/endpoint");
77       in.setHeader(Headers.FROM_APP_ID, "val1");
78       in.setHeader(Headers.TRANSACTION_ID, "val2");
79       in.setHeader(Headers.RESOURCE_VERSION, "val2");
80       in.setHeader(Headers.ETAG, "val2");
81       in.setHeader(Headers.IF_MATCH, "val2");
82       in.setHeader(Headers.IF_NONE_MATCH, "val2");
83       in.setHeader(Headers.ACCEPT, "val2");
84       in.setHeader("Content-Type", "val2");
85       
86       Map<String, List<String>> headers = (Map<String, List<String>>)method.invoke(producer, exchange); 
87       assertTrue(headers.size() == 8);  
88     }
89     catch (Exception ex) {
90       StringWriter writer = new StringWriter();
91       PrintWriter printWriter = new PrintWriter( writer );
92       ex.printStackTrace( printWriter );
93       printWriter.flush();
94       System.out.println(writer.toString());
95       throw ex;
96     }
97   }
98     
99 }