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 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.onap.aai.sparky.dal.rest.RestClientBuilder;
45 import org.onap.aai.sparky.security.SecurityContextFactory;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 import ch.qos.logback.classic.Level;
51 * The Class RestClientBuilderTest.
53 @RunWith(PowerMockRunner.class)
54 public class RestClientBuilderTest {
60 * @throws Exception the exception
63 public void init() throws Exception {
67 * Basic construction test.
69 * @throws Exception the exception
72 public void basicConstructionTest() throws Exception {
74 RestClientBuilder clientBuilder = new RestClientBuilder();
76 // test constructor defaults
78 assertFalse(clientBuilder.isValidateServerHostname());
79 assertEquals(60000L, clientBuilder.getConnectTimeoutInMs());
80 assertEquals(60000L, clientBuilder.getReadTimeoutInMs());
81 assertTrue(clientBuilder.isUseHttps());
88 * @throws Exception the exception
91 public void validateAccessors() throws Exception {
93 RestClientBuilder clientBuilder = new RestClientBuilder();
95 clientBuilder.setConnectTimeoutInMs(12345);
96 clientBuilder.setReadTimeoutInMs(54321);
97 clientBuilder.setUseHttps(true);
98 clientBuilder.setValidateServerHostname(true);
100 assertEquals(12345, clientBuilder.getConnectTimeoutInMs());
101 assertEquals(54321, clientBuilder.getReadTimeoutInMs());
102 assertTrue(clientBuilder.isUseHttps());
103 assertTrue(clientBuilder.isValidateServerHostname());
108 * Validate simple client construction.
110 * @throws Exception the exception
113 public void validateSimpleClientConstruction() throws Exception {
115 RestClientBuilder clientBuilder = new RestClientBuilder();
116 clientBuilder.setUseHttps(false);
117 Client client = clientBuilder.getClient();
120 * Simple client context should not contain HTTPS properties
122 assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
127 * Validate secure client construction without host name validation.
129 * @throws Exception the exception
132 public void validateSecureClientConstruction_WithoutHostNameValidation() throws Exception {
134 RestClientBuilder clientBuilder = new RestClientBuilder();
135 clientBuilder.setUseHttps(true);
137 SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
138 clientBuilder.setSslContextFactory(sslContextFactory);
140 SSLContext sslContext = Mockito.mock(SSLContext.class);
141 doReturn(sslContext).when(sslContextFactory).getSecureContext();
143 Client client = clientBuilder.getClient();
146 * Secure client context should contain HTTPS properties
148 assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
149 assertNotNull(clientBuilder.getSslContextFactory());
154 * Validate secure client construction with host name validation.
156 * @throws Exception the exception
159 public void validateSecureClientConstruction_WithHostNameValidation() throws Exception {
161 RestClientBuilder clientBuilder = new RestClientBuilder();
162 clientBuilder.setUseHttps(true);
163 clientBuilder.setValidateServerHostname(true);
165 SecurityContextFactory sslContextFactory = Mockito.mock(SecurityContextFactory.class);
166 clientBuilder.setSslContextFactory(sslContextFactory);
168 SSLContext sslContext = Mockito.mock(SSLContext.class);
169 doReturn(sslContext).when(sslContextFactory).getSecureContext();
171 Client client = clientBuilder.getClient();
174 * Secure client context should contain HTTPS properties
176 assertNotNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
177 assertNotNull(clientBuilder.getSslContextFactory());