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.onap.aai.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 javax.net.ssl.SSLContext;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.mockito.runners.MockitoJUnitRunner;
42 import org.onap.aai.sparky.dal.rest.RestClientBuilder;
43 import org.onap.aai.sparky.security.SecurityContextFactory;
45 import com.sun.jersey.api.client.Client;
46 import com.sun.jersey.client.urlconnection.HTTPSProperties;
49 * The Class RestClientBuilderTest.
51 @RunWith(MockitoJUnitRunner.class)
52 public class RestClientBuilderTest {
58 * @throws Exception the exception
61 public void init() throws Exception {}
64 * Basic construction test.
66 * @throws Exception the exception
69 public void basicConstructionTest() throws Exception {
71 RestClientBuilder clientBuilder = new RestClientBuilder();
73 // test constructor defaults
75 assertFalse(clientBuilder.isValidateServerHostname());
76 assertEquals(60000L, clientBuilder.getConnectTimeoutInMs());
77 assertEquals(60000L, clientBuilder.getReadTimeoutInMs());
78 assertTrue(clientBuilder.isUseHttps());
85 * @throws Exception the exception
88 public void validateAccessors() throws Exception {
90 RestClientBuilder clientBuilder = new RestClientBuilder();
92 clientBuilder.setConnectTimeoutInMs(12345);
93 clientBuilder.setReadTimeoutInMs(54321);
94 clientBuilder.setUseHttps(true);
95 clientBuilder.setValidateServerHostname(true);
97 assertEquals(12345, clientBuilder.getConnectTimeoutInMs());
98 assertEquals(54321, clientBuilder.getReadTimeoutInMs());
99 assertTrue(clientBuilder.isUseHttps());
100 assertTrue(clientBuilder.isValidateServerHostname());
105 * Validate simple client construction.
107 * @throws Exception the exception
110 public void validateSimpleClientConstruction() throws Exception {
112 RestClientBuilder clientBuilder = new RestClientBuilder();
113 clientBuilder.setUseHttps(false);
114 Client client = clientBuilder.getClient();
117 * Simple client context should not contain HTTPS properties
119 assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
124 * Validate secure client construction without host name validation.
126 * @throws Exception the exception
129 public void validateSecureClientConstruction_WithoutHostNameValidation() throws Exception {
131 RestClientBuilder clientBuilder = new RestClientBuilder();
132 clientBuilder.setUseHttps(true);
134 SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
135 clientBuilder.setSslContextFactory(sslContextFactory);
137 SSLContext sslContext = Mockito.mock(SSLContext.class);
138 doReturn(sslContext).when(sslContextFactory).getSecureContext();
140 Client client = clientBuilder.getClient();
143 * Secure client context should contain HTTPS properties
145 assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
146 assertNotNull(clientBuilder.getSslContextFactory());
151 * Validate secure client construction with host name validation.
153 * @throws Exception the exception
156 public void validateSecureClientConstruction_WithHostNameValidation() throws Exception {
158 RestClientBuilder clientBuilder = new RestClientBuilder();
159 clientBuilder.setUseHttps(true);
160 clientBuilder.setValidateServerHostname(true);
162 SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
163 clientBuilder.setSslContextFactory(sslContextFactory);
165 SSLContext sslContext = Mockito.mock(SSLContext.class);
166 doReturn(sslContext).when(sslContextFactory).getSecureContext();
168 Client client = clientBuilder.getClient();
171 * Secure client context should contain HTTPS properties
173 assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
174 assertNotNull(clientBuilder.getSslContextFactory());