Improve sonar test coverage
[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.junit.Before;
34 import org.junit.Test;
35 import org.onap.aai.logging.RouterCoreMsgs;
36 import org.onap.aai.restclient.client.Headers;
37
38 public class RestClientTest {
39
40     /**
41      * Test case initialization
42      * 
43      * @throws Exception the exception
44      */
45     @Before
46     public void init() throws Exception {
47     }
48
49     @Test
50     public void validate() throws Exception {
51
52         try {
53             RestClientComponent rc = new RestClientComponent();
54             RestClientEndpoint endpoint = new RestClientEndpoint("http://host.com:8443/endpoint", rc);
55
56             endpoint.setEcompClientCert("client-cert");
57             endpoint.setEcompKeystore("keystore");
58             endpoint.setEcompKeystorePassword("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10");
59             endpoint.setOp("GET");
60
61             assertTrue(endpoint.getEcompClientCert().compareTo("client-cert") == 0);
62             assertTrue(endpoint.getEcompKeystore().compareTo("keystore") == 0);
63             assertTrue(endpoint.getEcompKeystorePassword().compareTo("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10") == 0);
64             assertTrue(endpoint.getOp().compareTo("GET") == 0);
65             assertTrue(endpoint.isSingleton());
66
67             RestClientProducer producer = (RestClientProducer)endpoint.createProducer();
68             assertTrue(producer != null);
69
70             Method method = RestClientProducer.class.getDeclaredMethod("populateRestHeaders", Exchange.class);
71             method.setAccessible(true);
72
73             Exchange exchange = endpoint.createExchange();
74             exchange.setProperty(Exchange.TO_ENDPOINT, "mock://get");
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             producer.process(exchange);
90
91             System.out.println(RouterCoreMsgs.EVENT_CONSUMER_CREATION_EXCEPTION);
92         }
93         catch (Exception ex) {
94             StringWriter writer = new StringWriter();
95             PrintWriter printWriter = new PrintWriter( writer );
96             ex.printStackTrace( printWriter );
97             printWriter.flush();
98             System.out.println(writer.toString());
99             throw ex;
100         }
101     }
102
103 }