Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / dal / rest / RestClientBuilderTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.sparky.dal.rest;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33 import static org.mockito.Mockito.doReturn;
34
35 import com.sun.jersey.api.client.Client;
36 import com.sun.jersey.client.urlconnection.HTTPSProperties;
37
38 import javax.net.ssl.SSLContext;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.aai.sparky.dal.rest.RestClientBuilder;
45 import org.onap.aai.sparky.security.SecurityContextFactory;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 import ch.qos.logback.classic.Level;
49
50 /**
51  * The Class RestClientBuilderTest.
52  */
53 @RunWith(PowerMockRunner.class) 
54 public class RestClientBuilderTest {
55
56
57   /**
58    * Inits the.
59    *
60    * @throws Exception the exception
61    */
62   @Before
63   public void init() throws Exception {
64   }
65
66   /**
67    * Basic construction test.
68    *
69    * @throws Exception the exception
70    */
71   @Test
72   public void basicConstructionTest() throws Exception {
73
74     RestClientBuilder clientBuilder = new RestClientBuilder();
75
76     // test constructor defaults
77
78     assertFalse(clientBuilder.isValidateServerHostname());
79     assertEquals(60000L, clientBuilder.getConnectTimeoutInMs());
80     assertEquals(60000L, clientBuilder.getReadTimeoutInMs());
81     assertTrue(clientBuilder.isUseHttps());
82
83   }
84
85   /**
86    * Validate accessors.
87    *
88    * @throws Exception the exception
89    */
90   @Test
91   public void validateAccessors() throws Exception {
92
93     RestClientBuilder clientBuilder = new RestClientBuilder();
94
95     clientBuilder.setConnectTimeoutInMs(12345);
96     clientBuilder.setReadTimeoutInMs(54321);
97     clientBuilder.setUseHttps(true);
98     clientBuilder.setValidateServerHostname(true);
99
100     assertEquals(12345, clientBuilder.getConnectTimeoutInMs());
101     assertEquals(54321, clientBuilder.getReadTimeoutInMs());
102     assertTrue(clientBuilder.isUseHttps());
103     assertTrue(clientBuilder.isValidateServerHostname());
104
105   }
106
107   /**
108    * Validate simple client construction.
109    *
110    * @throws Exception the exception
111    */
112   @Test
113   public void validateSimpleClientConstruction() throws Exception {
114
115     RestClientBuilder clientBuilder = new RestClientBuilder();
116     clientBuilder.setUseHttps(false);
117     Client client = clientBuilder.getClient();
118
119     /*
120      * Simple client context should not contain HTTPS properties
121      */
122     assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
123
124   }
125
126   /**
127    * Validate secure client construction without host name validation.
128    *
129    * @throws Exception the exception
130    */
131   @Test
132   public void validateSecureClientConstruction_WithoutHostNameValidation() throws Exception {
133
134     RestClientBuilder clientBuilder = new RestClientBuilder();
135     clientBuilder.setUseHttps(true);
136
137     SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
138     clientBuilder.setSslContextFactory(sslContextFactory);
139
140     SSLContext sslContext = Mockito.mock(SSLContext.class);
141     doReturn(sslContext).when(sslContextFactory).getSecureContext();
142
143     Client client = clientBuilder.getClient();
144
145     /*
146      * Secure client context should contain HTTPS properties
147      */
148     assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
149     assertNotNull(clientBuilder.getSslContextFactory());
150
151   }
152
153   /**
154    * Validate secure client construction with host name validation.
155    *
156    * @throws Exception the exception
157    */
158   @Test
159   public void validateSecureClientConstruction_WithHostNameValidation() throws Exception {
160
161     RestClientBuilder clientBuilder = new RestClientBuilder();
162     clientBuilder.setUseHttps(true);
163     clientBuilder.setValidateServerHostname(true);
164
165     SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
166     clientBuilder.setSslContextFactory(sslContextFactory);
167
168     SSLContext sslContext = Mockito.mock(SSLContext.class);
169     doReturn(sslContext).when(sslContextFactory).getSecureContext();
170
171     Client client = clientBuilder.getClient();
172
173     /*
174      * Secure client context should contain HTTPS properties
175      */
176     assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
177     assertNotNull(clientBuilder.getSslContextFactory());
178
179   }
180
181 }