diff options
author | Eylon Malin <eylon.malin@intl.att.com> | 2019-10-03 12:09:56 +0300 |
---|---|---|
committer | Eylon Malin <eylon.malin@intl.att.com> | 2019-10-03 14:24:08 +0300 |
commit | 979442724bf737ec30ffe7aae73cb645ea296833 (patch) | |
tree | c41b4cc8961d6e264d764004bcfdc1d002c7193d /vid-app-common/src/test | |
parent | 585494da0b36f1f4758512bc62e59e863be817ee (diff) |
send INVOCATION_ID header in MsoRestClientNew
Issue-ID: VID-253
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
Change-Id: I6f4e317571999b2bf0f37d5407c23ab79e7358b4
Diffstat (limited to 'vid-app-common/src/test')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java | 96 |
1 files changed, 90 insertions, 6 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java index 414bcf4ac..422443510 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java @@ -20,6 +20,7 @@ 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; @@ -32,9 +33,18 @@ 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; @@ -53,10 +63,12 @@ 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.AAIRestInterface; 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; @@ -82,6 +94,8 @@ public class OutgoingRequestHeadersTest { @InjectMocks private RestMsoImplementation restMsoImplementation; + private MsoRestClientNew msoRestClientNew; + @Mock private SystemPropertyHelper systemPropertyHelper; @@ -97,6 +111,9 @@ public class OutgoingRequestHeadersTest { @Mock private Logging loggingService; + @Mock + SyncRestClient syncRestClient; + @InjectMocks private AAIRestInterface aaiRestInterface; @@ -108,6 +125,9 @@ public class OutgoingRequestHeadersTest { MockitoAnnotations.initMocks(this); when(servletRequestHelper.extractOrGenerateRequestId()).thenAnswer(invocation -> UUID.randomUUID().toString()); 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 @@ -136,9 +156,9 @@ public class OutgoingRequestHeadersTest { f.accept(restMsoImplementation); Invocation.Builder fakeBuilder = mocks.getFakeBuilder(); - Object requestIdValue = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder); + String requestIdValue = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder); assertEquals(requestIdValue, captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-RequestID")); - Object invocationId1 = assertRequestHeaderIsUUID(fakeBuilder, "X-InvocationID"); + String invocationId1 = assertRequestHeaderIsUUID(fakeBuilder, "X-InvocationID"); assertThat((String) captureHeaderKeyAndReturnItsValue(fakeBuilder, "Authorization"), startsWith("Basic ")); verifyXOnapPartnerNameHeaderWasAdded(fakeBuilder); @@ -152,7 +172,7 @@ public class OutgoingRequestHeadersTest { Invocation.Builder fakeBuilder2 = mocks2.getFakeBuilder(); //then - Object requestIdValue2 = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder2); + String requestIdValue2 = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder2); assertEquals(requestIdValue, requestIdValue2); Object invocationId2 = assertRequestHeaderIsUUID(fakeBuilder2, "X-InvocationID"); @@ -170,6 +190,62 @@ public class OutgoingRequestHeadersTest { } @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")); + + //validate 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; + } + + @DataProvider public Object[][] aaiMethods() { return Stream.<ThrowingConsumer<AAIRestInterface>>of( @@ -201,17 +277,25 @@ public class OutgoingRequestHeadersTest { // // } - private Object verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) { + private String verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) { final String requestIdHeader = "x-ecomp-requestid"; return assertRequestHeaderIsUUID(fakeBuilder, requestIdHeader); } - private Object assertRequestHeaderIsUUID(Invocation.Builder fakeBuilder, String headerName) { + 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 headerValue; + return (String)headerValue; } private void verifyXOnapPartnerNameHeaderWasAdded(Invocation.Builder fakeBuilder) { |