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.asdc.rest.RestfulAsdcClient;
8 import org.onap.vid.changeManagement.RequestDetailsWrapper;
9 import org.onap.vid.controller.filter.PromiseEcompRequestIdFilter;
10 import org.onap.vid.mso.RestMsoImplementation;
11 import org.onap.vid.mso.RestObject;
12 import org.onap.vid.testUtils.TestUtils;
13 import org.springframework.mock.web.MockHttpServletRequest;
14 import org.springframework.web.context.request.RequestContextHolder;
15 import org.springframework.web.context.request.ServletRequestAttributes;
16 import org.testng.annotations.BeforeClass;
17 import org.testng.annotations.BeforeMethod;
18 import org.testng.annotations.DataProvider;
19 import org.testng.annotations.Test;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.Invocation;
24 import javax.ws.rs.core.MultivaluedMap;
26 import java.util.function.Consumer;
27 import java.util.stream.Collectors;
28 import java.util.stream.Stream;
30 import static java.util.UUID.randomUUID;
31 import static org.hamcrest.MatcherAssert.assertThat;
32 import static org.hamcrest.Matchers.*;
33 import static org.mockito.Mockito.mock;
36 public class OutgoingRequestIdTest {
40 private RestMsoImplementation restMsoImplementation;
43 private AAIRestInterface aaiRestInterface;
45 private RestfulAsdcClient restfulAsdcClient = new RestfulAsdcClient.Builder(mock(Client.class), null).build();
48 private ArgumentCaptor<MultivaluedMap<String, Object>> multivaluedMapArgumentCaptor;
51 public void initMocks() {
52 MockitoAnnotations.initMocks(this);
56 private void putRequestInSpringContext() {
57 RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest())));
61 public Object[][] sdcMethods() {
62 return Stream.<ThrowingConsumer<RestfulAsdcClient>>of(
63 client -> client.getService(randomUUID()),
64 client -> client.getServiceToscaModel(randomUUID())
65 ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
68 @Test(dataProvider = "sdcMethods")
69 public void sdc(Consumer<RestfulAsdcClient> f) throws Exception {
70 final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restfulAsdcClient);
72 f.accept(restfulAsdcClient);
74 verifyRequestIdHeaderWasAdded(mocks.getFakeBuilder());
78 public Object[][] msoMethods() {
79 return Stream.<ThrowingConsumer<RestMsoImplementation>>of(
81 client -> client.Get(new Object(), "whatever source id", "/any path", new RestObject<>()),
82 client -> client.GetForObject("whatever source id", "/any path", Object.class),
83 client -> client.Post(new Object(), "some payload", "whatever source id", "/any path", new RestObject<>()),
84 client -> client.PostForObject("some payload", "whatever source id", "/any path", Object.class),
85 client -> client.Put(Object.class, new RequestDetailsWrapper(), "whatever source id", "/any path", new RestObject<>())
87 ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
90 @Test(dataProvider = "msoMethods")
91 public void mso(Consumer<RestMsoImplementation> f) throws Exception {
92 final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(restMsoImplementation);
94 f.accept(restMsoImplementation);
96 verifyRequestIdHeaderWasAdded(mocks.getFakeBuilder());
100 public Object[][] aaiMethods() {
101 return Stream.<ThrowingConsumer<AAIRestInterface>>of(
103 client -> client.RestGet("from app id", "some transId", "/any path", false),
104 client -> client.Delete("whatever source id", "some transId", "/any path"),
105 client -> client.RestPost("from app id", "/any path", "some payload", false),
106 client -> client.RestPut("from app id", "/any path", "some payload", false)
108 ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
111 //@Test(dataProvider = "aaiMethods")
112 public void aai(Consumer<AAIRestInterface> f) throws Exception {
113 final TestUtils.JavaxRsClientMocks mocks = setAndGetMocksInsideRestImpl(aaiRestInterface);
115 f.accept(aaiRestInterface);
117 verifyRequestIdHeaderWasAdded(mocks.getFakeBuilder());
120 // @Test(dataProvider = "schedulerMethods")
121 // public void scheduler(Consumer<AAIRestInterface> f) throws Exception {
123 // This test os not feasible in the wat acheduler is implemented today,
124 // as Scheduler's client is rewritten in every call.
130 private void verifyRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) {
131 final String requestIdHeader = "x-ecomp-requestid";
132 final String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
134 // Checks that the builder was called with either one of header("x-ecomp-requestid", uuid)
135 // or the plural brother: headers(Map.of("x-ecomp-requestid", Set.of(uuid))
138 // The 'verify()' will capture the request id. If no match -- AssertionError will
139 // catch for a second chance -- another 'verify()'.
141 ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
142 Mockito.verify(fakeBuilder)
144 Matchers.argThat(equalToIgnoringCase(requestIdHeader)),
145 argumentCaptor.capture()
147 requestId = argumentCaptor.getValue();
149 } catch (AssertionError e) {
150 Mockito.verify(fakeBuilder).headers(multivaluedMapArgumentCaptor.capture());
152 final MultivaluedMap<String, Object> headersMap = multivaluedMapArgumentCaptor.getValue();
153 final String thisRequestIdHeader = getFromSetCaseInsensitive(headersMap.keySet(), requestIdHeader);
155 assertThat(headersMap.keySet(), hasItem(thisRequestIdHeader));
156 requestId = headersMap.getFirst(thisRequestIdHeader);
159 assertThat("header '" + requestIdHeader + "' should be a uuid", requestId,
160 allOf(instanceOf(String.class), hasToString(matchesPattern(uuidRegex))));
163 private String getFromSetCaseInsensitive(Set<String> set, String key) {
165 .filter(anotherString -> anotherString.equalsIgnoreCase(key))
170 private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Class<?> clazz) throws IllegalAccessException {
171 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
172 Client fakeClient = mocks.getFakeClient();
174 FieldUtils.writeStaticField(clazz, "client", fakeClient, true);
179 private TestUtils.JavaxRsClientMocks setAndGetMocksInsideRestImpl(Object instance) throws IllegalAccessException {
180 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
181 Client fakeClient = mocks.getFakeClient();
183 FieldUtils.writeField(instance, "client", fakeClient, true);
189 public interface ThrowingConsumer<T> extends Consumer<T> {
191 default void accept(T t) {
194 } catch (Exception e) {
195 throw new RuntimeException(e);
199 void acceptThrows(T t) throws Exception;