2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.vid.mso;
24 import static org.assertj.core.api.Java6Assertions.assertThat;
25 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.when;
29 import static org.mockito.MockitoAnnotations.initMocks;
31 import io.joshworks.restclient.request.HttpRequest;
32 import java.io.IOException;
33 import java.security.GeneralSecurityException;
34 import java.util.List;
35 import java.util.Optional;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.Entity;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MultivaluedHashMap;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import org.glassfish.jersey.client.ClientProperties;
43 import org.glassfish.jersey.client.JerseyInvocation;
44 import org.hamcrest.MatcherAssert;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.onap.vid.aai.util.HttpClientMode;
48 import org.onap.vid.aai.util.HttpsAuthClient;
49 import org.onap.vid.mso.rest.RequestDetails;
50 import org.onap.vid.utils.Logging;
51 import org.onap.vid.utils.SystemPropertiesWrapper;
52 import org.springframework.http.HttpMethod;
53 import org.springframework.http.HttpStatus;
54 import org.testng.annotations.BeforeClass;
55 import org.testng.annotations.Test;
57 public class RestMsoImplementationTest {
60 private HttpRequest httpRequest;
63 private Client mockClient;
66 private HttpsAuthClient mockHttpsAuthClient;
69 private WebTarget webTarget;
72 private javax.ws.rs.client.Invocation.Builder builder;
75 private Response response;
78 private JerseyInvocation jerseyInvocation;
81 private SystemPropertiesWrapper systemProperties;
84 private Logging loggingService;
87 private RestMsoImplementation restMsoImplementation;
89 private String path = "/test_path/";
90 private String rawData = "test-row-data";
93 public void setUp() throws GeneralSecurityException, IOException {
95 when(mockHttpsAuthClient.getClient(any(HttpClientMode.class))).thenReturn(mockClient);
96 when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
100 public void shouldProperlyInitMsoClient() {
102 MultivaluedHashMap<String, Object> result = restMsoImplementation.initMsoClient();
105 List<Object> authorizationHeaders = result.get("Authorization");
106 MatcherAssert.assertThat(authorizationHeaders, hasSize(1));
107 assertThat((String) authorizationHeaders.get(0)).startsWith("Basic ");
108 assertThat(result).doesNotContainKey("notExistingKey");
113 public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndAcceptCode() {
115 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"");
118 RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
121 assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value());
122 assertThat(response.getRaw()).isEqualTo(rawData);
126 public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndBadRequestCode() {
128 prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"");
131 RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
134 assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
135 assertThat(response.getRaw()).isEqualTo(rawData);
140 public void shouldProperlyPostForObject() {
142 RequestDetails requestDetails = new RequestDetails();
144 RestObject<HttpRequest> expectedResponse = new RestObject<>();
145 expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
146 expectedResponse.setRaw(rawData);
148 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST");
151 RestObject<HttpRequest> response = restMsoImplementation.PostForObject(requestDetails, path, HttpRequest.class);
154 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
158 public void shouldProperlyPrepareClient() {
160 String method = "POST";
161 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
164 javax.ws.rs.client.Invocation.Builder response = restMsoImplementation.prepareClient(path, method);
167 assertThat(response).isEqualTo(builder);
171 public void shouldCreatRestObjectOnlyWithHttpMethod() {
173 String method = "GET";
174 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
176 RestObject<String> expectedResponse = new RestObject<>();
177 expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
178 expectedResponse.setRaw(rawData);
181 RestObject<String> response = restMsoImplementation.restCall(HttpMethod.GET, String.class, null, path, Optional.empty());
184 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
187 @Test( expectedExceptions = MsoTestException.class)
188 public void shouldThrowExceptionWhenCreateRestObjectIsCalledWithoutDefinedClient() {
190 when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testNoClientException"));
193 restMsoImplementation.restCall(HttpMethod.GET, String.class, null, "", Optional.empty());
196 private void prepareMocks(String rawData,int status,String httpMethod) {
198 when(mockClient.target(any(String.class))).thenReturn(webTarget);
199 when(webTarget.request()).thenReturn(builder);
202 when(builder.accept(any(String.class))).thenReturn(builder);
203 when(builder.headers(any(MultivaluedMap.class))).thenReturn(builder);
204 when(builder.property(eq(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION), eq(true))).thenReturn(builder);
205 when(builder.get()).thenReturn(response);
207 when(builder.build( eq(httpMethod), any(Entity.class))).thenReturn(jerseyInvocation);
208 when(builder.build( eq(httpMethod))).thenReturn(jerseyInvocation);
210 when(builder.put( any(Entity.class))).thenReturn(response);
211 when(jerseyInvocation.invoke()).thenReturn(response);
214 when(response.getStatus()).thenReturn(status);
215 when(response.readEntity(String.class)).thenReturn(rawData);
218 private class MsoTestException extends RuntimeException{
219 MsoTestException(String testException) {
220 super(testException);