change management cypress test
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / RestMsoImplementationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.vid.mso;
23
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;
29
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;
53
54 public class RestMsoImplementationTest  {
55
56     @Mock
57     private HttpRequest httpRequest;
58
59     @Mock
60     private Client mockClient;
61
62     @Mock
63     private HttpsAuthClient mockHttpsAuthClient;
64
65     @Mock
66     private WebTarget webTarget;
67
68     @Mock
69     private javax.ws.rs.client.Invocation.Builder builder;
70
71     @Mock
72     private Response response;
73
74     @Mock
75     private JerseyInvocation jerseyInvocation;
76
77     @Mock
78     private SystemPropertiesWrapper systemProperties;
79
80     @Mock
81     private Logging loggingService;
82
83     @InjectMocks
84     private RestMsoImplementation restMsoImplementation;
85
86     private String path = "/test_path/";
87     private String rawData = "test-row-data";
88
89     @BeforeClass
90     public void setUp() throws GeneralSecurityException, IOException {
91         initMocks(this);
92         when(mockHttpsAuthClient.getClient(any(HttpClientMode.class))).thenReturn(mockClient);
93         when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
94     }
95
96     @Test
97     public void shouldProperlyInitMsoClient() {
98         //  when
99         MultivaluedHashMap<String, Object> result = restMsoImplementation.initMsoClient();
100
101         //  then
102         assertThat(result).containsKeys("Authorization","X-ONAP-PartnerName");
103         assertThat(result).doesNotContainKey("notExistingKey");
104     }
105
106
107     @Test()
108     public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndAcceptCode() {
109         //  given
110         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"");
111
112         //  when
113         RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
114
115         //  then
116         assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value());
117         assertThat(response.getRaw()).isEqualTo(rawData);
118     }
119
120     @Test()
121     public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndBadRequestCode() {
122         //  given
123         prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"");
124
125         //  when
126         RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class);
127
128         //  then
129         assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
130         assertThat(response.getRaw()).isEqualTo(rawData);
131     }
132
133
134     @Test
135     public void shouldProperlyPostForObject() {
136         //  given
137         RequestDetails requestDetails = new RequestDetails();
138
139         RestObject<HttpRequest> expectedResponse = new RestObject<>();
140         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
141         expectedResponse.setRaw(rawData);
142
143         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST");
144
145         //  when
146         RestObject<HttpRequest> response = restMsoImplementation.PostForObject(requestDetails, path, HttpRequest.class);
147
148         //  then
149         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
150     }
151
152     @Test
153     public void shouldProperlyPrepareClient() {
154         //  given
155         String method = "POST";
156         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
157
158         //  when
159         javax.ws.rs.client.Invocation.Builder response = restMsoImplementation.prepareClient(path, method);
160
161         //  then
162         assertThat(response).isEqualTo(builder);
163     }
164
165     @Test
166     public void shouldCreatRestObjectOnlyWithHttpMethod() {
167         //  given
168         String method = "GET";
169         prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method);
170
171         RestObject<String> expectedResponse = new RestObject<>();
172         expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value());
173         expectedResponse.setRaw(rawData);
174
175         //  when
176         RestObject<String> response = restMsoImplementation.restCall(HttpMethod.GET, String.class, null, path, Optional.empty());
177
178         //  then
179         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
180     }
181
182     @Test( expectedExceptions = MsoTestException.class)
183     public void shouldThrowExceptionWhenCreateRestObjectIsCalledWithoutDefinedClient() {
184         //  given
185         when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testNoClientException"));
186
187         //  when
188         restMsoImplementation.restCall(HttpMethod.GET, String.class, null, "", Optional.empty());
189     }
190
191     private void prepareMocks(String rawData,int status,String httpMethod) {
192
193         when(mockClient.target(any(String.class))).thenReturn(webTarget);
194         when(webTarget.request()).thenReturn(builder);
195
196
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);
201
202         when(builder.build( eq(httpMethod), any(Entity.class))).thenReturn(jerseyInvocation);
203         when(builder.build( eq(httpMethod))).thenReturn(jerseyInvocation);
204
205         when(builder.put( any(Entity.class))).thenReturn(response);
206         when(jerseyInvocation.invoke()).thenReturn(response);
207
208
209         when(response.getStatus()).thenReturn(status);
210         when(response.readEntity(String.class)).thenReturn(rawData);
211     }
212
213     private class MsoTestException extends RuntimeException{
214         MsoTestException(String testException) {
215             super(testException);
216         }
217     }
218
219 }