b70ba063f331459c03cb17e7ae7f3a02dd6e8bcd
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / OutgoingRequestHeadersTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.mso.rest;
22
23 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.allOf;
26 import static org.hamcrest.Matchers.hasItem;
27 import static org.hamcrest.Matchers.hasToString;
28 import static org.hamcrest.Matchers.instanceOf;
29 import static org.hamcrest.Matchers.matchesPattern;
30 import static org.mockito.Mockito.when;
31
32 import com.google.common.collect.ImmutableList;
33 import java.util.Set;
34 import java.util.UUID;
35 import java.util.function.Consumer;
36 import java.util.stream.Collectors;
37 import java.util.stream.Stream;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.ws.rs.client.Client;
40 import javax.ws.rs.client.Invocation;
41 import javax.ws.rs.core.MultivaluedMap;
42 import org.apache.commons.lang3.reflect.FieldUtils;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Captor;
45 import org.mockito.InjectMocks;
46 import org.mockito.Matchers;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.MockitoAnnotations;
50 import org.onap.vid.aai.util.AAIRestInterface;
51 import org.onap.vid.aai.util.ServletRequestHelper;
52 import org.onap.vid.aai.util.SystemPropertyHelper;
53 import org.onap.vid.controller.filter.PromiseEcompRequestIdFilter;
54 import org.onap.vid.testUtils.TestUtils;
55 import org.onap.vid.utils.Unchecked;
56 import org.springframework.mock.web.MockHttpServletRequest;
57 import org.springframework.web.context.request.RequestContextHolder;
58 import org.springframework.web.context.request.ServletRequestAttributes;
59 import org.testng.annotations.BeforeClass;
60 import org.testng.annotations.BeforeMethod;
61 import org.testng.annotations.DataProvider;
62 import org.testng.annotations.Test;
63
64
65 public class OutgoingRequestHeadersTest {
66
67
68 //    @InjectMocks
69 //    private RestMsoImplementation restMsoImplementation;
70
71     @Mock
72     private SystemPropertyHelper systemPropertyHelper;
73
74     @Mock
75     private ServletRequestHelper servletRequestHelper;
76
77     @InjectMocks
78     private AAIRestInterface aaiRestInterface;
79
80     @Captor
81     private ArgumentCaptor<MultivaluedMap<String, Object>> multivaluedMapArgumentCaptor;
82
83     @BeforeClass
84     public void initMocks() {
85         MockitoAnnotations.initMocks(this);
86         when(servletRequestHelper.extractOrGenerateRequestId()).thenAnswer(invocation -> UUID.randomUUID().toString());
87     }
88
89     @BeforeMethod
90     private void setup() {
91         putRequestInSpringContext();
92     }
93
94     public static void putRequestInSpringContext() {
95         RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest())));
96     }
97
98 //    @DataProvider
99 //    public Object[][] msoMethods() {
100 //        return Stream.<ThrowingConsumer<RestMsoImplementation>>of(
101 //
102 //                client -> client.Get(new Object(), "/any path", new RestObject<>(), false),
103 //                client -> client.GetForObject("/any path", Object.class),
104 //                client -> client.Post("", "some payload", "/any path", new RestObject<>()),
105 //                client -> client.PostForObject("some payload", "/any path", Object.class),
106 //                client -> client.Put(Object.class, new RequestDetailsWrapper(), "/any path", new RestObject<>())
107 //
108 //        ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
109 //    }
110 //
111 //    @Test(dataProvider = "msoMethods")
112 //    public void mso(Consumer<RestMsoImplementation> f) throws Exception {
113 //        final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
114 //
115 //        f.accept(restMsoImplementation);
116 //
117 //        Invocation.Builder fakeBuilder = mocks.getFakeBuilder();
118 //        Object requestIdValue = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder);
119 //        assertEquals(requestIdValue, captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-RequestID"));
120 //
121 //        assertThat((String) captureHeaderKeyAndReturnItsValue(fakeBuilder, "Authorization"), startsWith("Basic "));
122 //        assertThat(captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-PartnerName"), equalTo("VID"));
123 //    }
124 //
125 //    @Test
126 //    public void whenProvideMsoRestCallUserId_builderHasXRequestorIDHeader() throws Exception {
127 //
128 //        final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
129 //        String randomUserName = randomAlphabetic(10);
130 //
131 //        restMsoImplementation.restCall(HttpMethod.DELETE, String.class, null, "abc", Optional.of(randomUserName));
132 //        assertEquals(randomUserName, captureHeaderKeyAndReturnItsValue(mocks.getFakeBuilder(), "X-RequestorID"));
133 //    }
134
135     @DataProvider
136     public Object[][] aaiMethods() {
137         return Stream.<ThrowingConsumer<AAIRestInterface>>of(
138
139                 client -> client.RestGet("from app id", "some transId", Unchecked.toURI("/any path"), false),
140                 client -> client.Delete("whatever source id", "some transId", "/any path"),
141                 client -> client.RestPost("from app id", "/any path", "some payload", false),
142                 client -> client.RestPut("from app id", "/any path", "some payload", false, false)
143
144         ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
145     }
146
147     @Test(dataProvider = "aaiMethods")
148     public void aai(Consumer<AAIRestInterface> f) throws Exception {
149         final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(aaiRestInterface);
150
151         f.accept(aaiRestInterface);
152
153         verifyXEcompRequestIdHeaderWasAdded(mocks.getFakeBuilder());
154     }
155
156 //    @Test(dataProvider = "schedulerMethods")
157 //    public void scheduler(Consumer<AAIRestInterface> f) throws Exception {
158 //
159 //        This test os not feasible in the wat acheduler is implemented today,
160 //        as Scheduler's client is rewritten in every call.
161 //
162 //        :-(
163 //
164 //    }
165
166     private Object verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) {
167         final String requestIdHeader = "x-ecomp-requestid";
168         final String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
169         Object requestId = captureHeaderKeyAndReturnItsValue(fakeBuilder, requestIdHeader);
170
171         assertThat("header '" + requestIdHeader + "' should be a uuid", requestId,
172                 allOf(instanceOf(String.class), hasToString(matchesPattern(uuidRegex))));
173         return requestId;
174     }
175
176     private Object captureHeaderKeyAndReturnItsValue(Invocation.Builder fakeBuilder, String headerName) {
177         // Checks that the builder was called with either one of header("x-ecomp-requestid", uuid)
178         // or the plural brother: headers(Map.of("x-ecomp-requestid", Set.of(uuid))
179
180         Object requestId;
181         // The 'verify()' will capture the request id. If no match -- AssertionError will
182         // catch for a second chance -- another 'verify()'.
183         try {
184             ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
185             Mockito.verify(fakeBuilder)
186                     .header(
187                             Matchers.argThat(s -> equalsIgnoreCase(s, headerName)),
188                             argumentCaptor.capture()
189                     );
190             requestId = argumentCaptor.getValue();
191
192         } catch (AssertionError e) {
193             Mockito.verify(fakeBuilder).headers(multivaluedMapArgumentCaptor.capture());
194
195             final MultivaluedMap<String, Object> headersMap = multivaluedMapArgumentCaptor.getValue();
196             final String thisRequestIdHeader = getFromSetCaseInsensitive(headersMap.keySet(), headerName);
197
198             assertThat(headersMap.keySet(), hasItem(thisRequestIdHeader));
199             requestId = headersMap.getFirst(thisRequestIdHeader);
200         }
201         return requestId;
202     }
203
204     private String getFromSetCaseInsensitive(Set<String> set, String key) {
205         return set.stream()
206                 .filter(anotherString -> anotherString.equalsIgnoreCase(key))
207                 .findFirst()
208                 .orElse(key);
209     }
210
211     private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Class<?> clazz) throws IllegalAccessException {
212         TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
213         Client fakeClient = mocks.getFakeClient();
214
215         FieldUtils.writeStaticField(clazz, "client", fakeClient, true);
216
217         return mocks;
218     }
219
220     private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Object instance) throws IllegalAccessException {
221         TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
222         Client fakeClient = mocks.getFakeClient();
223
224         FieldUtils.writeField(instance, "client", fakeClient, true);
225
226         return mocks;
227     }
228
229     @FunctionalInterface
230     public interface ThrowingConsumer<T> extends Consumer<T> {
231         @Override
232         default void accept(T t) {
233             try {
234                 acceptThrows(t);
235             } catch (Exception e) {
236                 throw new RuntimeException(e);
237             }
238         }
239
240         void acceptThrows(T t) throws Exception;
241     }
242
243 }