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.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
30 import io.joshworks.restclient.request.HttpRequest;
31 import java.io.IOException;
32 import java.security.GeneralSecurityException;
33 import java.util.Optional;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.client.WebTarget;
37 import javax.ws.rs.core.MultivaluedHashMap;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import org.glassfish.jersey.client.ClientProperties;
41 import org.glassfish.jersey.client.JerseyInvocation;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.onap.vid.aai.util.HttpClientMode;
45 import org.onap.vid.aai.util.HttpsAuthClient;
46 import org.onap.vid.mso.rest.RequestDetails;
47 import org.onap.vid.utils.Logging;
48 import org.onap.vid.utils.SystemPropertiesWrapper;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.HttpStatus;
51 import org.testng.annotations.BeforeClass;
52 import org.testng.annotations.Test;
54 public class RestMsoImplementationTest {
57 private HttpRequest httpRequest;
60 private Client mockClient;
63 private HttpsAuthClient mockHttpsAuthClient;
66 private WebTarget webTarget;
69 private javax.ws.rs.client.Invocation.Builder builder;
72 private Response response;
75 private JerseyInvocation jerseyInvocation;
78 private SystemPropertiesWrapper systemProperties;
81 private Logging loggingService;
84 private RestMsoImplementation restMsoImplementation;
86 private String path = "/test_path/";
87 private String rawData = "test-row-data";
90 public void setUp() throws GeneralSecurityException, IOException {
92 when(mockHttpsAuthClient.getClient(any(HttpClientMode.class))).thenReturn(mockClient);
93 when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
97 public void shouldProperlyInitMsoClient() {
99 MultivaluedHashMap<String, Object> result = restMsoImplementation.initMsoClient();
102 assertThat(result).containsKeys("Authorization","X-ONAP-PartnerName");
103 assertThat(result).doesNotContainKey("notExistingKey");
108 public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndAcceptCode() {
110 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"");
113 RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
116 assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value());
117 assertThat(response.getRaw()).isEqualTo(rawData);
121 public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndBadRequestCode() {
123 prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"");
126 RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
129 assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
130 assertThat(response.getRaw()).isEqualTo(rawData);
135 public void shouldProperlyPostForObject() {
137 RequestDetails requestDetails = new RequestDetails();
139 RestObject<HttpRequest> expectedResponse = new RestObject<>();
140 expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
141 expectedResponse.setRaw(rawData);
143 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST");
146 RestObject<HttpRequest> response = restMsoImplementation.PostForObject(requestDetails, path, HttpRequest.class);
149 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
153 public void shouldProperlyPrepareClient() {
155 String method = "POST";
156 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
159 javax.ws.rs.client.Invocation.Builder response = restMsoImplementation.prepareClient(path, method);
162 assertThat(response).isEqualTo(builder);
166 public void shouldCreatRestObjectOnlyWithHttpMethod() {
168 String method = "GET";
169 prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
171 RestObject<String> expectedResponse = new RestObject<>();
172 expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
173 expectedResponse.setRaw(rawData);
176 RestObject<String> response = restMsoImplementation.restCall(HttpMethod.GET, String.class, null, path, Optional.empty());
179 assertThat(response).isEqualToComparingFieldByField(expectedResponse);
182 @Test( expectedExceptions = MsoTestException.class)
183 public void shouldThrowExceptionWhenCreateRestObjectIsCalledWithoutDefinedClient() {
185 when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testNoClientException"));
188 restMsoImplementation.restCall(HttpMethod.GET, String.class, null, "", Optional.empty());
191 private void prepareMocks(String rawData,int status,String httpMethod) {
193 when(mockClient.target(any(String.class))).thenReturn(webTarget);
194 when(webTarget.request()).thenReturn(builder);
197 when(builder.accept(any(String.class))).thenReturn(builder);
198 when(builder.headers(any(MultivaluedMap.class))).thenReturn(builder);
199 when(builder.property(eq(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION), eq(true))).thenReturn(builder);
200 when(builder.get()).thenReturn(response);
202 when(builder.build( eq(httpMethod), any(Entity.class))).thenReturn(jerseyInvocation);
203 when(builder.build( eq(httpMethod))).thenReturn(jerseyInvocation);
205 when(builder.put( any(Entity.class))).thenReturn(response);
206 when(jerseyInvocation.invoke()).thenReturn(response);
209 when(response.getStatus()).thenReturn(status);
210 when(response.readEntity(String.class)).thenReturn(rawData);
213 private class MsoTestException extends RuntimeException{
214 MsoTestException(String testException) {
215 super(testException);