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