make Logging a service and inject it to SyncRestClient
[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.Logging;
56 import org.onap.vid.utils.Unchecked;
57 import org.springframework.mock.web.MockHttpServletRequest;
58 import org.springframework.web.context.request.RequestContextHolder;
59 import org.springframework.web.context.request.ServletRequestAttributes;
60 import org.testng.annotations.BeforeClass;
61 import org.testng.annotations.BeforeMethod;
62 import org.testng.annotations.DataProvider;
63 import org.testng.annotations.Test;
64
65
66 public class OutgoingRequestHeadersTest {
67
68
69 //    @InjectMocks
70 //    private RestMsoImplementation restMsoImplementation;
71
72     @Mock
73     private SystemPropertyHelper systemPropertyHelper;
74
75     @Mock
76     private ServletRequestHelper servletRequestHelper;
77
78     @Mock
79     private Logging loggingService;
80
81     @InjectMocks
82     private AAIRestInterface aaiRestInterface;
83
84     @Captor
85     private ArgumentCaptor<MultivaluedMap<String, Object>> multivaluedMapArgumentCaptor;
86
87     @BeforeClass
88     public void initMocks() {
89         MockitoAnnotations.initMocks(this);
90         when(servletRequestHelper.extractOrGenerateRequestId()).thenAnswer(invocation -> UUID.randomUUID().toString());
91     }
92
93     @BeforeMethod
94     private void setup() {
95         putRequestInSpringContext();
96     }
97
98     public static void putRequestInSpringContext() {
99         RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest())));
100     }
101
102 //    @DataProvider
103 //    public Object[][] msoMethods() {
104 //        return Stream.<ThrowingConsumer<RestMsoImplementation>>of(
105 //
106 //                client -> client.Get(new Object(), "/any path", new RestObject<>(), false),
107 //                client -> client.GetForObject("/any path", Object.class),
108 //                client -> client.Post("", "some payload", "/any path", new RestObject<>()),
109 //                client -> client.PostForObject("some payload", "/any path", Object.class),
110 //                client -> client.Put(Object.class, new RequestDetailsWrapper(), "/any path", new RestObject<>())
111 //
112 //        ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
113 //    }
114 //
115 //    @Test(dataProvider = "msoMethods")
116 //    public void mso(Consumer<RestMsoImplementation> f) throws Exception {
117 //        final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
118 //
119 //        f.accept(restMsoImplementation);
120 //
121 //        Invocation.Builder fakeBuilder = mocks.getFakeBuilder();
122 //        Object requestIdValue = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder);
123 //        assertEquals(requestIdValue, captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-RequestID"));
124 //
125 //        assertThat((String) captureHeaderKeyAndReturnItsValue(fakeBuilder, "Authorization"), startsWith("Basic "));
126 //        assertThat(captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-PartnerName"), equalTo("VID"));
127 //    }
128 //
129 //    @Test
130 //    public void whenProvideMsoRestCallUserId_builderHasXRequestorIDHeader() throws Exception {
131 //
132 //        final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
133 //        String randomUserName = randomAlphabetic(10);
134 //
135 //        restMsoImplementation.restCall(HttpMethod.DELETE, String.class, null, "abc", Optional.of(randomUserName));
136 //        assertEquals(randomUserName, captureHeaderKeyAndReturnItsValue(mocks.getFakeBuilder(), "X-RequestorID"));
137 //    }
138
139     @DataProvider
140     public Object[][] aaiMethods() {
141         return Stream.<ThrowingConsumer<AAIRestInterface>>of(
142
143                 client -> client.RestGet("from app id", "some transId", Unchecked.toURI("/any path"), false),
144                 client -> client.Delete("whatever source id", "some transId", "/any path"),
145                 client -> client.RestPost("from app id", "/any path", "some payload", false),
146                 client -> client.RestPut("from app id", "/any path", "some payload", false, false)
147
148         ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
149     }
150
151     @Test(dataProvider = "aaiMethods")
152     public void aai(Consumer<AAIRestInterface> f) throws Exception {
153         final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(aaiRestInterface);
154
155         f.accept(aaiRestInterface);
156
157         verifyXEcompRequestIdHeaderWasAdded(mocks.getFakeBuilder());
158     }
159
160 //    @Test(dataProvider = "schedulerMethods")
161 //    public void scheduler(Consumer<AAIRestInterface> f) throws Exception {
162 //
163 //        This test os not feasible in the wat acheduler is implemented today,
164 //        as Scheduler's client is rewritten in every call.
165 //
166 //        :-(
167 //
168 //    }
169
170     private Object verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) {
171         final String requestIdHeader = "x-ecomp-requestid";
172         final String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
173         Object requestId = captureHeaderKeyAndReturnItsValue(fakeBuilder, requestIdHeader);
174
175         assertThat("header '" + requestIdHeader + "' should be a uuid", requestId,
176                 allOf(instanceOf(String.class), hasToString(matchesPattern(uuidRegex))));
177         return requestId;
178     }
179
180     private Object captureHeaderKeyAndReturnItsValue(Invocation.Builder fakeBuilder, String headerName) {
181         // Checks that the builder was called with either one of header("x-ecomp-requestid", uuid)
182         // or the plural brother: headers(Map.of("x-ecomp-requestid", Set.of(uuid))
183
184         Object requestId;
185         // The 'verify()' will capture the request id. If no match -- AssertionError will
186         // catch for a second chance -- another 'verify()'.
187         try {
188             ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
189             Mockito.verify(fakeBuilder)
190                     .header(
191                             Matchers.argThat(s -> equalsIgnoreCase(s, headerName)),
192                             argumentCaptor.capture()
193                     );
194             requestId = argumentCaptor.getValue();
195
196         } catch (AssertionError e) {
197             Mockito.verify(fakeBuilder).headers(multivaluedMapArgumentCaptor.capture());
198
199             final MultivaluedMap<String, Object> headersMap = multivaluedMapArgumentCaptor.getValue();
200             final String thisRequestIdHeader = getFromSetCaseInsensitive(headersMap.keySet(), headerName);
201
202             assertThat(headersMap.keySet(), hasItem(thisRequestIdHeader));
203             requestId = headersMap.getFirst(thisRequestIdHeader);
204         }
205         return requestId;
206     }
207
208     private String getFromSetCaseInsensitive(Set<String> set, String key) {
209         return set.stream()
210                 .filter(anotherString -> anotherString.equalsIgnoreCase(key))
211                 .findFirst()
212                 .orElse(key);
213     }
214
215     private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Class<?> clazz) throws IllegalAccessException {
216         TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
217         Client fakeClient = mocks.getFakeClient();
218
219         FieldUtils.writeStaticField(clazz, "client", fakeClient, true);
220
221         return mocks;
222     }
223
224     private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Object instance) throws IllegalAccessException {
225         TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
226         Client fakeClient = mocks.getFakeClient();
227
228         FieldUtils.writeField(instance, "client", fakeClient, true);
229
230         return mocks;
231     }
232
233     @FunctionalInterface
234     public interface ThrowingConsumer<T> extends Consumer<T> {
235         @Override
236         default void accept(T t) {
237             try {
238                 acceptThrows(t);
239             } catch (Exception e) {
240                 throw new RuntimeException(e);
241             }
242         }
243
244         void acceptThrows(T t) throws Exception;
245     }
246
247 }