Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / 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.openecomp.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.openecomp.sparky.security.SecurityContextFactory;
45 import org.powermock.modules.junit4.PowerMockRunner;
46
47 import ch.qos.logback.classic.Level;
48
49 /**
50  * The Class RestClientBuilderTest.
51  */
52 @RunWith(PowerMockRunner.class) 
53 public class RestClientBuilderTest {
54
55
56   /**
57    * Inits the.
58    *
59    * @throws Exception the exception
60    */
61   @Before
62   public void init() throws Exception {
63   }
64
65   /**
66    * Basic construction test.
67    *
68    * @throws Exception the exception
69    */
70   @Test
71   public void basicConstructionTest() throws Exception {
72
73     RestClientBuilder clientBuilder = new RestClientBuilder();
74
75     // test constructor defaults
76
77     assertFalse(clientBuilder.isValidateServerHostname());
78     assertEquals(60000L, clientBuilder.getConnectTimeoutInMs());
79     assertEquals(60000L, clientBuilder.getReadTimeoutInMs());
80     assertTrue(clientBuilder.isUseHttps());
81
82   }
83
84   /**
85    * Validate accessors.
86    *
87    * @throws Exception the exception
88    */
89   @Test
90   public void validateAccessors() throws Exception {
91
92     RestClientBuilder clientBuilder = new RestClientBuilder();
93
94     clientBuilder.setConnectTimeoutInMs(12345);
95     clientBuilder.setReadTimeoutInMs(54321);
96     clientBuilder.setUseHttps(true);
97     clientBuilder.setValidateServerHostname(true);
98
99     assertEquals(12345, clientBuilder.getConnectTimeoutInMs());
100     assertEquals(54321, clientBuilder.getReadTimeoutInMs());
101     assertTrue(clientBuilder.isUseHttps());
102     assertTrue(clientBuilder.isValidateServerHostname());
103
104   }
105
106   /**
107    * Validate simple client construction.
108    *
109    * @throws Exception the exception
110    */
111   @Test
112   public void validateSimpleClientConstruction() throws Exception {
113
114     RestClientBuilder clientBuilder = new RestClientBuilder();
115     clientBuilder.setUseHttps(false);
116     Client client = clientBuilder.getClient();
117
118     /*
119      * Simple client context should not contain HTTPS properties
120      */
121     assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
122
123   }
124
125   /**
126    * Validate secure client construction without host name validation.
127    *
128    * @throws Exception the exception
129    */
130   @Test
131   public void validateSecureClientConstruction_WithoutHostNameValidation() throws Exception {
132
133     RestClientBuilder clientBuilder = new RestClientBuilder();
134     clientBuilder.setUseHttps(true);
135
136     SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
137     clientBuilder.setSslContextFactory(sslContextFactory);
138
139     SSLContext sslContext = Mockito.mock(SSLContext.class);
140     doReturn(sslContext).when(sslContextFactory).getSecureContext();
141
142     Client client = clientBuilder.getClient();
143
144     /*
145      * Secure client context should contain HTTPS properties
146      */
147     assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
148     assertNotNull(clientBuilder.getSslContextFactory());
149
150   }
151
152   /**
153    * Validate secure client construction with host name validation.
154    *
155    * @throws Exception the exception
156    */
157   @Test
158   public void validateSecureClientConstruction_WithHostNameValidation() throws Exception {
159
160     RestClientBuilder clientBuilder = new RestClientBuilder();
161     clientBuilder.setUseHttps(true);
162     clientBuilder.setValidateServerHostname(true);
163
164     SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
165     clientBuilder.setSslContextFactory(sslContextFactory);
166
167     SSLContext sslContext = Mockito.mock(SSLContext.class);
168     doReturn(sslContext).when(sslContextFactory).getSecureContext();
169
170     Client client = clientBuilder.getClient();
171
172     /*
173      * Secure client context should contain HTTPS properties
174      */
175     assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
176     assertNotNull(clientBuilder.getSslContextFactory());
177
178   }
179
180 }