2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.dal.rest;
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;
35 import com.sun.jersey.api.client.Client;
36 import com.sun.jersey.client.urlconnection.HTTPSProperties;
38 import javax.net.ssl.SSLContext;
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;
47 import ch.qos.logback.classic.Level;
50 * The Class RestClientBuilderTest.
52 @RunWith(PowerMockRunner.class)
53 public class RestClientBuilderTest {
59 * @throws Exception the exception
62 public void init() throws Exception {
66 * Basic construction test.
68 * @throws Exception the exception
71 public void basicConstructionTest() throws Exception {
73 RestClientBuilder clientBuilder = new RestClientBuilder();
75 // test constructor defaults
77 assertFalse(clientBuilder.isValidateServerHostname());
78 assertEquals(60000L, clientBuilder.getConnectTimeoutInMs());
79 assertEquals(60000L, clientBuilder.getReadTimeoutInMs());
80 assertTrue(clientBuilder.isUseHttps());
87 * @throws Exception the exception
90 public void validateAccessors() throws Exception {
92 RestClientBuilder clientBuilder = new RestClientBuilder();
94 clientBuilder.setConnectTimeoutInMs(12345);
95 clientBuilder.setReadTimeoutInMs(54321);
96 clientBuilder.setUseHttps(true);
97 clientBuilder.setValidateServerHostname(true);
99 assertEquals(12345, clientBuilder.getConnectTimeoutInMs());
100 assertEquals(54321, clientBuilder.getReadTimeoutInMs());
101 assertTrue(clientBuilder.isUseHttps());
102 assertTrue(clientBuilder.isValidateServerHostname());
107 * Validate simple client construction.
109 * @throws Exception the exception
112 public void validateSimpleClientConstruction() throws Exception {
114 RestClientBuilder clientBuilder = new RestClientBuilder();
115 clientBuilder.setUseHttps(false);
116 Client client = clientBuilder.getClient();
119 * Simple client context should not contain HTTPS properties
121 assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
126 * Validate secure client construction without host name validation.
128 * @throws Exception the exception
131 public void validateSecureClientConstruction_WithoutHostNameValidation() throws Exception {
133 RestClientBuilder clientBuilder = new RestClientBuilder();
134 clientBuilder.setUseHttps(true);
136 SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
137 clientBuilder.setSslContextFactory(sslContextFactory);
139 SSLContext sslContext = Mockito.mock(SSLContext.class);
140 doReturn(sslContext).when(sslContextFactory).getSecureContext();
142 Client client = clientBuilder.getClient();
145 * Secure client context should contain HTTPS properties
147 assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
148 assertNotNull(clientBuilder.getSslContextFactory());
153 * Validate secure client construction with host name validation.
155 * @throws Exception the exception
158 public void validateSecureClientConstruction_WithHostNameValidation() throws Exception {
160 RestClientBuilder clientBuilder = new RestClientBuilder();
161 clientBuilder.setUseHttps(true);
162 clientBuilder.setValidateServerHostname(true);
164 SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
165 clientBuilder.setSslContextFactory(sslContextFactory);
167 SSLContext sslContext = Mockito.mock(SSLContext.class);
168 doReturn(sslContext).when(sslContextFactory).getSecureContext();
170 Client client = clientBuilder.getClient();
173 * Secure client context should contain HTTPS properties
175 assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
176 assertNotNull(clientBuilder.getSslContextFactory());