1 package org.onap.vid.mso.rest;
3 import com.google.common.collect.ImmutableList;
4 import org.apache.commons.lang3.reflect.FieldUtils;
6 import org.onap.vid.aai.util.AAIRestInterface;
7 import org.onap.vid.changeManagement.RequestDetailsWrapper;
8 import org.onap.vid.controller.filter.PromiseEcompRequestIdFilter;
9 import org.onap.vid.mso.RestMsoImplementation;
10 import org.onap.vid.mso.RestObject;
11 import org.onap.vid.testUtils.TestUtils;
12 import org.springframework.mock.web.MockHttpServletRequest;
13 import org.springframework.web.context.request.RequestContextHolder;
14 import org.springframework.web.context.request.ServletRequestAttributes;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.BeforeMethod;
17 import org.testng.annotations.DataProvider;
18 import org.testng.annotations.Test;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.ws.rs.client.Client;
22 import javax.ws.rs.client.Invocation;
23 import javax.ws.rs.core.MultivaluedMap;
25 import java.util.function.Consumer;
26 import java.util.stream.Collectors;
27 import java.util.stream.Stream;
29 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.Matchers.*;
34 public class OutgoingRequestIdTest {
38 private RestMsoImplementation restMsoImplementation;
41 private AAIRestInterface aaiRestInterface;
45 private ArgumentCaptor<MultivaluedMap<String, Object>> multivaluedMapArgumentCaptor;
48 public void initMocks() {
49 MockitoAnnotations.initMocks(this);
53 private void putRequestInSpringContext() {
54 RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest())));
58 public Object[][] msoMethods() {
59 return Stream.<ThrowingConsumer<RestMsoImplementation>>of(
61 client -> client.Get(new Object(), "whatever source id", "/any path", new RestObject<>()),
62 client -> client.GetForObject("whatever source id", "/any path", Object.class),
63 client -> client.Post(new Object(), "some payload", "whatever source id", "/any path", new RestObject<>()),
64 client -> client.PostForObject("some payload", "whatever source id", "/any path", Object.class),
65 client -> client.Put(Object.class, new RequestDetailsWrapper(), "whatever source id", "/any path", new RestObject<>())
67 ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
70 @Test(dataProvider = "msoMethods")
71 public void mso(Consumer<RestMsoImplementation> f) throws Exception {
72 final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
74 f.accept(restMsoImplementation);
76 verifyRequestIdHeaderWasAdded(mocks.getFakeBuilder());
80 public Object[][] aaiMethods() {
81 return Stream.<ThrowingConsumer<AAIRestInterface>>of(
83 client -> client.RestGet("from app id", "some transId", "/any path", false),
84 client -> client.Delete("whatever source id", "some transId", "/any path"),
85 client -> client.RestPost("from app id", "/any path", "some payload", false),
86 client -> client.RestPut("from app id", "/any path", "some payload", false)
88 ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
91 //@Test(dataProvider = "aaiMethods")
92 public void aai(Consumer<AAIRestInterface> f) throws Exception {
93 final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(aaiRestInterface);
95 f.accept(aaiRestInterface);
97 verifyRequestIdHeaderWasAdded(mocks.getFakeBuilder());
100 // @Test(dataProvider = "schedulerMethods")
101 // public void scheduler(Consumer<AAIRestInterface> f) throws Exception {
103 // This test os not feasible in the wat acheduler is implemented today,
104 // as Scheduler's client is rewritten in every call.
110 private void verifyRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) {
111 final String requestIdHeader = "x-ecomp-requestid";
112 final String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
114 // Checks that the builder was called with either one of header("x-ecomp-requestid", uuid)
115 // or the plural brother: headers(Map.of("x-ecomp-requestid", Set.of(uuid))
118 // The 'verify()' will capture the request id. If no match -- AssertionError will
119 // catch for a second chance -- another 'verify()'.
121 ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
122 Mockito.verify(fakeBuilder)
124 Matchers.argThat(s -> equalsIgnoreCase(s, requestIdHeader)),
125 argumentCaptor.capture()
127 requestId = argumentCaptor.getValue();
129 } catch (AssertionError e) {
130 Mockito.verify(fakeBuilder).headers(multivaluedMapArgumentCaptor.capture());
132 final MultivaluedMap<String, Object> headersMap = multivaluedMapArgumentCaptor.getValue();
133 final String thisRequestIdHeader = getFromSetCaseInsensitive(headersMap.keySet(), requestIdHeader);
135 assertThat(headersMap.keySet(), hasItem(thisRequestIdHeader));
136 requestId = headersMap.getFirst(thisRequestIdHeader);
139 assertThat("header '" + requestIdHeader + "' should be a uuid", requestId,
140 allOf(instanceOf(String.class), hasToString(matchesPattern(uuidRegex))));
143 private String getFromSetCaseInsensitive(Set<String> set, String key) {
145 .filter(anotherString -> anotherString.equalsIgnoreCase(key))
150 private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Class<?> clazz) throws IllegalAccessException {
151 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
152 Client fakeClient = mocks.getFakeClient();
154 FieldUtils.writeStaticField(clazz, "client", fakeClient, true);
159 private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Object instance) throws IllegalAccessException {
160 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
161 Client fakeClient = mocks.getFakeClient();
163 FieldUtils.writeField(instance, "client", fakeClient, true);
169 public interface ThrowingConsumer<T> extends Consumer<T> {
171 default void accept(T t) {
174 } catch (Exception e) {
175 throw new RuntimeException(e);
179 void acceptThrows(T t) throws Exception;