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