aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java
blob: f03b89722d856ffc9d617fc41e5f13a8015be81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.mso.rest;

import static org.apache.commons.io.IOUtils.toInputStream;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import io.joshworks.restclient.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MultivaluedMap;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.aai.util.HttpsAuthClient;
import org.onap.vid.aai.util.ServletRequestHelper;
import org.onap.vid.aai.util.SystemPropertyHelper;
import org.onap.vid.client.SyncRestClient;
import org.onap.vid.controller.filter.PromiseRequestIdFilter;
import org.onap.vid.logging.Headers;
import org.onap.vid.mso.MsoProperties;
import org.onap.vid.mso.RestMsoImplementation;
import org.onap.vid.testUtils.TestUtils;
import org.onap.vid.utils.Logging;
import org.onap.vid.utils.SystemPropertiesWrapper;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class OutgoingRequestHeadersTest {

    private static final PromiseRequestIdFilter promiseRequestIdFilter = new PromiseRequestIdFilter();

    @InjectMocks
    private RestMsoImplementation restMsoImplementation;

    private MsoRestClientNew msoRestClientNew;

    @Mock
    private SystemPropertyHelper systemPropertyHelper;

    @Mock
    private SystemPropertiesWrapper  systemPropertiesWrapper;

    @Mock
    private HttpsAuthClient httpsAuthClient;

    @Mock
    private ServletRequestHelper servletRequestHelper;

    @Mock
    private Logging loggingService;

    @Mock
    SyncRestClient syncRestClient;

    @Captor
    private ArgumentCaptor<MultivaluedMap<String, Object>> multivaluedMapArgumentCaptor;

    @BeforeClass
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
        String oneIncomingRequestId = UUID.randomUUID().toString();
        when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(oneIncomingRequestId);
        when(systemPropertiesWrapper.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1vub1ua51uh81ugi1u9d1vuz");
        when(systemPropertiesWrapper.getProperty(SystemProperties.APP_DISPLAY_NAME)).thenReturn("vid");
        //the ctor of MsoRestClientNew require the above lines as preconditions
        msoRestClientNew = new MsoRestClientNew(syncRestClient, "baseUrl",systemPropertiesWrapper);
    }

    @BeforeMethod
    private void setup() {
        putRequestInSpringContext();
    }

    public static void putRequestInSpringContext() {
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(
            (HttpServletRequest) promiseRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest())));
    }

    @DataProvider
    public Object[][] msoMethods() {
        return Stream.<ThrowingConsumer<RestMsoImplementation>>of(
                client -> client.GetForObject("/any path", Object.class),
                client -> client.restCall(HttpMethod.DELETE, Object.class, "some payload", "/any path", Optional.of("userId")),
                client -> client.PostForObject("some payload", "/any path", Object.class)
        ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
    }

    @Test
    public void whenProvideMsoRestCallUserId_builderHasXRequestorIDHeader() throws Exception {

        final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
        String randomUserName = randomAlphabetic(10);

        restMsoImplementation.restCall(HttpMethod.DELETE, String.class, null, "abc", Optional.of(randomUserName));
        assertEquals(randomUserName, captureHeaderKeyAndReturnItsValue(mocks.getFakeBuilder(), "X-RequestorID"));
    }

    @DataProvider
    public Object[][] msoRestClientNewMethods() {
        return Stream.<ThrowingConsumer<MsoRestClientNew>>of(
            client -> client.createInstance(new Object(), "/any path")
        ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
    }

    @Test(dataProvider = "msoRestClientNewMethods")
    public void msoRestClientNewHeadersTest(Consumer<MsoRestClientNew> f) throws Exception {
        Map[] captor = setMocksForMsoRestClientNew();

        f.accept(msoRestClientNew);
        Map headers = captor[0];

        String ecompRequestId = assertRequestHeaderIsUUID(headers, "X-ECOMP-RequestID");
        String onapRequestID = assertRequestHeaderIsUUID(headers, "X-ONAP-RequestID");
        assertEquals(ecompRequestId, onapRequestID);


        String invocationId1 = assertRequestHeaderIsUUID(headers, "X-InvocationID");
        assertThat((String) headers.get("Authorization"), startsWith("Basic "));
        assertThat(headers.get("X-ONAP-PartnerName"), is("VID.VID"));

        //verify requestId is same in next call but invocationId is different

        //given
        captor = setMocksForMsoRestClientNew();

        //when
        f.accept(msoRestClientNew);
        headers = captor[0];

        //then
        assertEquals(headers.get("X-ONAP-RequestID"), onapRequestID);
        String invocationId2 = assertRequestHeaderIsUUID(headers, "X-InvocationID");
        assertNotEquals(invocationId1, invocationId2);

    }

    private Map[] setMocksForMsoRestClientNew() {
        reset(syncRestClient);
        HttpResponse<String> httpResponse = mock(HttpResponse.class);
        String expectedResponse = "myResponse";
        when(httpResponse.getStatus()).thenReturn(202);
        when(httpResponse.getBody()).thenReturn(expectedResponse);
        when(httpResponse.getRawBody()).thenReturn(toInputStream(expectedResponse, StandardCharsets.UTF_8));
        final Map[] headersCapture = new Map[1];
        when(syncRestClient.post(anyString(), anyMap(), any(), eq(String.class))).thenAnswer(
            invocation -> {
                headersCapture[0] = (Map)invocation.getArguments()[1];
                return httpResponse;
            });

        return headersCapture;
    }

//    @Test(dataProvider = "schedulerMethods")
//    public void scheduler(Consumer<AAIRestInterface> f) throws Exception {
//
//        This test os not feasible in the wat acheduler is implemented today,
//        as Scheduler's client is rewritten in every call.
//
//        :-(
//
//    }

    private String verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) {
        final String requestIdHeader = "x-ecomp-requestid";
        return assertRequestHeaderIsUUID(fakeBuilder, requestIdHeader);
    }

    private String assertRequestHeaderIsUUID(Invocation.Builder fakeBuilder, String headerName) {
        Object headerValue = captureHeaderKeyAndReturnItsValue(fakeBuilder, headerName);
        return assertRequestHeaderIsUUID(headerName, headerValue);
    }

    private String assertRequestHeaderIsUUID(Map headers, String headerName) {
        return assertRequestHeaderIsUUID(headerName, headers.get(headerName));
    }

    private String assertRequestHeaderIsUUID(String headerName, Object headerValue) {
        final String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
        assertThat("header '" + headerName + "' should be a uuid", headerValue,
                allOf(instanceOf(String.class), hasToString(matchesPattern(uuidRegex))));
        return (String)headerValue;
    }

    private void verifyXOnapPartnerNameHeaderWasAdded(Invocation.Builder fakeBuilder) {
        assertThat(
            captureHeaderKeyAndReturnItsValue(fakeBuilder, Headers.PARTNER_NAME.getHeaderName()),
            is("VID.VID")
        );
    }

    private Object captureHeaderKeyAndReturnItsValue(Invocation.Builder fakeBuilder, String headerName) {
        // Checks that the builder was called with either one of header("x-ecomp-requestid", uuid)
        // or the plural brother: headers(Map.of("x-ecomp-requestid", Set.of(uuid))

        Object requestId;
        // The 'verify()' will capture the request id. If no match -- AssertionError will
        // catch for a second chance -- another 'verify()'.
        try {
            try {
                ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
                Mockito.verify(fakeBuilder)
                    .header(
                        Matchers.argThat(s -> equalsIgnoreCase(s, headerName)),
                        argumentCaptor.capture()
                    );
                requestId = argumentCaptor.getValue();

            } catch (AssertionError e) {
                Mockito.verify(fakeBuilder).headers(multivaluedMapArgumentCaptor.capture());

                final MultivaluedMap<String, Object> headersMap = multivaluedMapArgumentCaptor.getValue();
                final String thisRequestIdHeader = getFromSetCaseInsensitive(headersMap.keySet(), headerName);

                assertThat(headersMap.keySet(), hasItem(thisRequestIdHeader));
                requestId = headersMap.getFirst(thisRequestIdHeader);
            }
        } catch (AssertionError e) {
            throw new AssertionError("header not captured: " + headerName, e);
        }
        return requestId;
    }

    private String getFromSetCaseInsensitive(Set<String> set, String key) {
        return set.stream()
                .filter(anotherString -> anotherString.equalsIgnoreCase(key))
                .findFirst()
                .orElse(key);
    }

    private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Class<?> clazz) throws IllegalAccessException {
        TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
        Client fakeClient = mocks.getFakeClient();

        FieldUtils.writeStaticField(clazz, "client", fakeClient, true);

        return mocks;
    }

    private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Object instance) throws IllegalAccessException {
        TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
        Client fakeClient = mocks.getFakeClient();

        FieldUtils.writeField(instance, "client", fakeClient, true);

        return mocks;
    }

    @FunctionalInterface
    public interface ThrowingConsumer<T> extends Consumer<T> {
        @Override
        default void accept(T t) {
            try {
                acceptThrows(t);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        void acceptThrows(T t) throws Exception;
    }

    private class HeadersVerifier {

        private String firstRequestId;
        private String firstInvocationId;


        HeadersVerifier verifyFirstCall(Builder fakeBuilder) {
            firstRequestId = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder);
            assertEquals(firstRequestId, captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-RequestID"));
            firstInvocationId = assertRequestHeaderIsUUID(fakeBuilder, "X-InvocationID");
            verifyXOnapPartnerNameHeaderWasAdded(fakeBuilder);
            return this;
        }

        void verifySecondCall(Builder fakeBuilder) {
            String secondRequestId = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder);
            assertEquals(firstRequestId, secondRequestId);

            Object secondInvocationId = assertRequestHeaderIsUUID(fakeBuilder, "X-InvocationID");
            assertNotEquals(firstInvocationId, secondInvocationId);
        }
    }
}