From 2be2e77d30a3cd547c1a37fb2f280b5fabf1ecd6 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Thu, 19 Sep 2019 13:10:26 +0300 Subject: Rename PromiseRequestIdFilter, extract isWrapNeeded and incomingRequestId methods Change-Id: Idb39f7d5e723598e34e737522c4f73d8de3df93c Issue-ID: VID-253 Signed-off-by: Ittay Stern --- .../filter/PromiseEcompRequestIdFilter.java | 123 ------------ .../controller/filter/PromiseRequestIdFilter.java | 131 +++++++++++++ .../PromiseEcompRequestIdFilterTest.java | 204 -------------------- .../vid/controller/PromiseRequestIdFilterTest.java | 210 +++++++++++++++++++++ .../vid/mso/rest/OutgoingRequestHeadersTest.java | 6 +- 5 files changed, 345 insertions(+), 329 deletions(-) delete mode 100644 vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java create mode 100644 vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java delete mode 100644 vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java (limited to 'vid-app-common') diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java deleted file mode 100644 index 37622a215..000000000 --- a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java +++ /dev/null @@ -1,123 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.controller.filter; - - -import com.google.common.collect.ImmutableList; -import org.apache.commons.lang3.StringUtils; -import org.springframework.web.filter.GenericFilterBean; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.annotation.WebFilter; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletRequestWrapper; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Collections; -import java.util.Enumeration; -import java.util.UUID; - -import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; - -@WebFilter(urlPatterns = "/*") -public class PromiseEcompRequestIdFilter extends GenericFilterBean { - - private static final String REQUEST_ID_RESPONSE_HEADER = ECOMP_REQUEST_ID + "-echo"; - - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - - if (request instanceof HttpServletRequest) { - request = wrapIfNeeded(request); - - if (response instanceof HttpServletResponse) { - final String actualRequestId = ((HttpServletRequest) request).getHeader(ECOMP_REQUEST_ID); - ((HttpServletResponse) response).addHeader(REQUEST_ID_RESPONSE_HEADER, actualRequestId); - } - } - - chain.doFilter(request, response); - } - - public static ServletRequest wrapIfNeeded(ServletRequest request) { - final HttpServletRequest httpRequest = (HttpServletRequest) request; - final String originalRequestId = httpRequest.getHeader(ECOMP_REQUEST_ID); - - if (StringUtils.isEmpty(originalRequestId) || !verifyAndValidateUuid(originalRequestId)) { - request = new PromiseEcompRequestIdRequestWrapper(httpRequest); - } - - return request; - } - - public static boolean verifyAndValidateUuid(String value) - { - String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; - return value.matches(uuidRegex); - } - - private static class PromiseEcompRequestIdRequestWrapper extends HttpServletRequestWrapper { - - private final UUID requestId; - - PromiseEcompRequestIdRequestWrapper(HttpServletRequest request) { - super(request); - requestId = UUID.randomUUID(); - } - - @Override - public String getHeader(String name) { - return isRequestIdHeaderName(name) ? - requestId.toString() : super.getHeader(name); - } - - @Override - public Enumeration getHeaders(String name) { - if (isRequestIdHeaderName(name)) { - return Collections.enumeration(Collections.singleton(requestId.toString())); - } else { - return super.getHeaders(name); - } - } - - @Override - public Enumeration getHeaderNames() { - - if (null == super.getHeader(ECOMP_REQUEST_ID)) { - return Collections.enumeration(ImmutableList.builder() - .add(ECOMP_REQUEST_ID) - .addAll(Collections.list(super.getHeaderNames())) - .build()); - } - - return super.getHeaderNames(); - } - - private boolean isRequestIdHeaderName(String name) { - return ECOMP_REQUEST_ID.equalsIgnoreCase(name); - } - } -} diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java new file mode 100644 index 000000000..c6b18785a --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java @@ -0,0 +1,131 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.controller.filter; + + +import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; + +import com.google.common.collect.ImmutableList; +import java.io.IOException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.UUID; +import java.util.regex.Pattern; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.filter.GenericFilterBean; + +@WebFilter(urlPatterns = "/*") +public class PromiseRequestIdFilter extends GenericFilterBean { + + private static final String REQUEST_ID_RESPONSE_HEADER = ECOMP_REQUEST_ID + "-echo"; + + private static final Pattern uuidRegex = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + + if (request instanceof HttpServletRequest) { + request = wrapIfNeeded(request); + + if (response instanceof HttpServletResponse) { + final String actualRequestId = ((HttpServletRequest) request).getHeader(ECOMP_REQUEST_ID); + ((HttpServletResponse) response).addHeader(REQUEST_ID_RESPONSE_HEADER, actualRequestId); + } + } + + chain.doFilter(request, response); + } + + public ServletRequest wrapIfNeeded(ServletRequest request) { + final HttpServletRequest httpRequest = (HttpServletRequest) request; + final String originalRequestId = incomingRequestId(httpRequest); + + if (isWrapNeeded(originalRequestId)) { + request = new PromiseRequestIdRequestWrapper(httpRequest); + } + + return request; + } + + private boolean verifyAndValidateUuid(String value) { + return uuidRegex.matcher(value).matches(); + } + + protected boolean isWrapNeeded(String originalRequestId) { + return StringUtils.isEmpty(originalRequestId) + || !verifyAndValidateUuid(originalRequestId); + } + + protected String incomingRequestId(HttpServletRequest httpRequest) { + return httpRequest.getHeader(ECOMP_REQUEST_ID); + } + + private static class PromiseRequestIdRequestWrapper extends HttpServletRequestWrapper { + + private final UUID requestId; + + PromiseRequestIdRequestWrapper(HttpServletRequest request) { + super(request); + requestId = UUID.randomUUID(); + } + + @Override + public String getHeader(String name) { + return isRequestIdHeaderName(name) ? + requestId.toString() : super.getHeader(name); + } + + @Override + public Enumeration getHeaders(String name) { + if (isRequestIdHeaderName(name)) { + return Collections.enumeration(Collections.singleton(requestId.toString())); + } else { + return super.getHeaders(name); + } + } + + @Override + public Enumeration getHeaderNames() { + + if (null == super.getHeader(ECOMP_REQUEST_ID)) { + return Collections.enumeration(ImmutableList.builder() + .add(ECOMP_REQUEST_ID) + .addAll(Collections.list(super.getHeaderNames())) + .build()); + } + + return super.getHeaderNames(); + } + + private boolean isRequestIdHeaderName(String name) { + return ECOMP_REQUEST_ID.equalsIgnoreCase(name); + } + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java deleted file mode 100644 index 39638c305..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java +++ /dev/null @@ -1,204 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.controller; - -import com.google.common.collect.ImmutableMap; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.mockito.stubbing.Answer; -import org.onap.portalsdk.core.web.support.UserUtils; -import org.onap.vid.controller.filter.PromiseEcompRequestIdFilter; -import org.springframework.mock.web.MockHttpServletResponse; -import org.testng.annotations.Test; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.*; -import java.util.function.Function; - -import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.argThat; -import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; - -@Test -public class PromiseEcompRequestIdFilterTest { - - private final String anotherHeader = "ANDREI_RUBLEV"; - private final String anotherValue = "foo value"; - private final String mixedCaseHeader = "x-ecomp-REQUESTID"; - - @Test - public void givenRequestIdHeader_headerValueNotChanged() throws IOException, ServletException { - - final String someTxId = "863850e2-8545-4efd-94b8-afba5f52b3d5"; - - final ImmutableMap incomingRequestHeaders = ImmutableMap.of( - anotherHeader, anotherValue, - ECOMP_REQUEST_ID, someTxId - ); - - buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, specificTxId(someTxId)); - } - - - @Test - public void givenRequestIdHeaderThatIsNotAUUID_headerValueChanged() throws IOException, ServletException { - - final String someTxId = "863850e28544efd94b8afba5f52b3d5"; - - final ImmutableMap incomingRequestHeaders = ImmutableMap.of( - anotherHeader, anotherValue, - ECOMP_REQUEST_ID, someTxId - ); - - buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, UserUtils::getRequestId); - } - - - @Test - public void givenMixedCaseRequestIdHeader_headerValueNotChanged() throws IOException, ServletException { - - final String someTxId = "729bbd8d-b0c2-4809-a794-dcccd9cda2c0"; - - final ImmutableMap incomingRequestHeaders = ImmutableMap.of( - mixedCaseHeader, someTxId, - anotherHeader, anotherValue - ); - - buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, specificTxId(someTxId)); - } - - @Test - public void givenNoRequestIdHeader_headerValueWasGenerated() throws IOException, ServletException { - - final ImmutableMap incomingRequestHeaders = ImmutableMap.of( - anotherHeader, anotherValue - ); - - buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, UserUtils::getRequestId); - } - - - private void buildRequestThenRunThroughFilterAndAssertResultRequestHeaders( - ImmutableMap originalRequestHeaders, - Function txIdExtractor - ) throws IOException, ServletException { - HttpServletRequest servletRequest = createMockedHttpServletRequest(originalRequestHeaders); - HttpServletResponse servletResponse = createMockedHttpServletResponse(); - - final FilterChain capturingFilterChain = Mockito.mock(FilterChain.class); - - ////////////////// - // - // doFilter() is the function under test - // - new PromiseEcompRequestIdFilter().doFilter(servletRequest, servletResponse, capturingFilterChain); - // - ////////////////// - - final ServletRequest capturedServletRequest = extractCapturedServletRequest(capturingFilterChain); - final ServletResponse capturedServletResponse = extractCapturedServletResponse(capturingFilterChain); - final String expectedTxId = txIdExtractor.apply((HttpServletRequest) capturedServletRequest); - - assertRequestObjectHeaders(capturedServletRequest, expectedTxId); - assertResponseObjectHeaders(capturedServletResponse, expectedTxId); - } - - - private void assertRequestObjectHeaders(ServletRequest request, String expectedTxId) { - /* - Assert that: - - Two headers are in place - - Direct value extraction is as expected - - UserUtils.getRequestId() returns correct and valid value - */ - final HttpServletRequest httpServletRequest = (HttpServletRequest) request; - - assertThat(Collections.list(httpServletRequest.getHeaderNames()), - containsInAnyOrder(equalToIgnoringCase(ECOMP_REQUEST_ID), equalToIgnoringCase(anotherHeader))); - - assertThat(httpServletRequest.getHeader(anotherHeader), is(anotherValue)); - - assertThat(httpServletRequest.getHeader(ECOMP_REQUEST_ID), is(expectedTxId)); - assertThat(httpServletRequest.getHeader(mixedCaseHeader), is(expectedTxId)); - - assertThat(UserUtils.getRequestId(httpServletRequest), is(expectedTxId)); - assertThat(UserUtils.getRequestId(httpServletRequest), is(not(emptyOrNullString()))); - } - - private void assertResponseObjectHeaders(ServletResponse response, String txId) { - final String REQUEST_ID_HEADER_NAME_IN_RESPONSE = mixedCaseHeader + "-echo"; - final HttpServletResponse httpServletResponse = (HttpServletResponse) response; - - assertThat("header " + REQUEST_ID_HEADER_NAME_IN_RESPONSE.toLowerCase() + " in response must be provided", - httpServletResponse.getHeader(REQUEST_ID_HEADER_NAME_IN_RESPONSE), is(txId)); - } - - - - private HttpServletRequest createMockedHttpServletRequest(Map requestHeaders) { - HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class); - requestHeaders.forEach((k, v) -> { - Mockito.when(servletRequest.getHeader(argThat(s -> equalsIgnoreCase(s, k)))).thenReturn(v); - Mockito.when(servletRequest.getHeaders(argThat(s -> equalsIgnoreCase(s, k)))).then(returnEnumerationAnswer(v)); - }); - Mockito.when(servletRequest.getHeaderNames()).then(returnEnumerationAnswer(requestHeaders.keySet())); - return servletRequest; - } - - private HttpServletResponse createMockedHttpServletResponse() { - return new MockHttpServletResponse(); - } - - private static Answer> returnEnumerationAnswer(String ... items) { - return returnEnumerationAnswer(Arrays.asList(items)); - } - - private static Answer> returnEnumerationAnswer(Collection items) { - return invocation -> Collections.enumeration(items); - } - - private Function specificTxId(String someTxId) { - return r -> someTxId; - } - - private ServletRequest extractCapturedServletRequest(FilterChain capturingFilterChain) throws IOException, ServletException { - ArgumentCaptor captor = ArgumentCaptor.forClass(ServletRequest.class); - Mockito.verify(capturingFilterChain).doFilter(captor.capture(), any()); - return captor.getValue(); - } - - private ServletResponse extractCapturedServletResponse(FilterChain capturingFilterChain) throws IOException, ServletException { - ArgumentCaptor captor = ArgumentCaptor.forClass(ServletResponse.class); - Mockito.verify(capturingFilterChain).doFilter(any(), captor.capture()); - return captor.getValue(); - } - -} diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java new file mode 100644 index 000000000..2304d46f3 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java @@ -0,0 +1,210 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.controller; + +import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.emptyOrNullString; +import static org.hamcrest.Matchers.equalToIgnoringCase; +import static org.hamcrest.Matchers.not; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; + +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map; +import java.util.function.Function; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.onap.vid.controller.filter.PromiseRequestIdFilter; +import org.springframework.mock.web.MockHttpServletResponse; +import org.testng.annotations.Test; + +@Test +public class PromiseRequestIdFilterTest { + + private final String anotherHeader = "ANDREI_RUBLEV"; + private final String anotherValue = "foo value"; + private final String mixedCaseHeader = "x-ecomp-REQUESTID"; + + @Test + public void givenRequestIdHeader_headerValueNotChanged() throws IOException, ServletException { + + final String someTxId = "863850e2-8545-4efd-94b8-afba5f52b3d5"; + + final ImmutableMap incomingRequestHeaders = ImmutableMap.of( + anotherHeader, anotherValue, + ECOMP_REQUEST_ID, someTxId + ); + + buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, specificTxId(someTxId)); + } + + + @Test + public void givenRequestIdHeaderThatIsNotAUUID_headerValueChanged() throws IOException, ServletException { + + final String someTxId = "863850e28544efd94b8afba5f52b3d5"; + + final ImmutableMap incomingRequestHeaders = ImmutableMap.of( + anotherHeader, anotherValue, + ECOMP_REQUEST_ID, someTxId + ); + + buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, UserUtils::getRequestId); + } + + + @Test + public void givenMixedCaseRequestIdHeader_headerValueNotChanged() throws IOException, ServletException { + + final String someTxId = "729bbd8d-b0c2-4809-a794-dcccd9cda2c0"; + + final ImmutableMap incomingRequestHeaders = ImmutableMap.of( + mixedCaseHeader, someTxId, + anotherHeader, anotherValue + ); + + buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, specificTxId(someTxId)); + } + + @Test + public void givenNoRequestIdHeader_headerValueWasGenerated() throws IOException, ServletException { + + final ImmutableMap incomingRequestHeaders = ImmutableMap.of( + anotherHeader, anotherValue + ); + + buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, UserUtils::getRequestId); + } + + + private void buildRequestThenRunThroughFilterAndAssertResultRequestHeaders( + ImmutableMap originalRequestHeaders, + Function txIdExtractor + ) throws IOException, ServletException { + HttpServletRequest servletRequest = createMockedHttpServletRequest(originalRequestHeaders); + HttpServletResponse servletResponse = createMockedHttpServletResponse(); + + final FilterChain capturingFilterChain = Mockito.mock(FilterChain.class); + + ////////////////// + // + // doFilter() is the function under test + // + new PromiseRequestIdFilter().doFilter(servletRequest, servletResponse, capturingFilterChain); + // + ////////////////// + + final ServletRequest capturedServletRequest = extractCapturedServletRequest(capturingFilterChain); + final ServletResponse capturedServletResponse = extractCapturedServletResponse(capturingFilterChain); + final String expectedTxId = txIdExtractor.apply((HttpServletRequest) capturedServletRequest); + + assertRequestObjectHeaders(capturedServletRequest, expectedTxId); + assertResponseObjectHeaders(capturedServletResponse, expectedTxId); + } + + + private void assertRequestObjectHeaders(ServletRequest request, String expectedTxId) { + /* + Assert that: + - Two headers are in place + - Direct value extraction is as expected + - UserUtils.getRequestId() returns correct and valid value + */ + final HttpServletRequest httpServletRequest = (HttpServletRequest) request; + + assertThat(Collections.list(httpServletRequest.getHeaderNames()), + containsInAnyOrder(equalToIgnoringCase(ECOMP_REQUEST_ID), equalToIgnoringCase(anotherHeader))); + + assertThat(httpServletRequest.getHeader(anotherHeader), is(anotherValue)); + + assertThat(httpServletRequest.getHeader(ECOMP_REQUEST_ID), is(expectedTxId)); + assertThat(httpServletRequest.getHeader(mixedCaseHeader), is(expectedTxId)); + + assertThat(UserUtils.getRequestId(httpServletRequest), is(expectedTxId)); + assertThat(UserUtils.getRequestId(httpServletRequest), is(not(emptyOrNullString()))); + } + + private void assertResponseObjectHeaders(ServletResponse response, String txId) { + final String REQUEST_ID_HEADER_NAME_IN_RESPONSE = mixedCaseHeader + "-echo"; + final HttpServletResponse httpServletResponse = (HttpServletResponse) response; + + assertThat("header " + REQUEST_ID_HEADER_NAME_IN_RESPONSE.toLowerCase() + " in response must be provided", + httpServletResponse.getHeader(REQUEST_ID_HEADER_NAME_IN_RESPONSE), is(txId)); + } + + + + private HttpServletRequest createMockedHttpServletRequest(Map requestHeaders) { + HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class); + requestHeaders.forEach((k, v) -> { + Mockito.when(servletRequest.getHeader(argThat(s -> equalsIgnoreCase(s, k)))).thenReturn(v); + Mockito.when(servletRequest.getHeaders(argThat(s -> equalsIgnoreCase(s, k)))).then(returnEnumerationAnswer(v)); + }); + Mockito.when(servletRequest.getHeaderNames()).then(returnEnumerationAnswer(requestHeaders.keySet())); + return servletRequest; + } + + private HttpServletResponse createMockedHttpServletResponse() { + return new MockHttpServletResponse(); + } + + private static Answer> returnEnumerationAnswer(String ... items) { + return returnEnumerationAnswer(Arrays.asList(items)); + } + + private static Answer> returnEnumerationAnswer(Collection items) { + return invocation -> Collections.enumeration(items); + } + + private Function specificTxId(String someTxId) { + return r -> someTxId; + } + + private ServletRequest extractCapturedServletRequest(FilterChain capturingFilterChain) throws IOException, ServletException { + ArgumentCaptor captor = ArgumentCaptor.forClass(ServletRequest.class); + Mockito.verify(capturingFilterChain).doFilter(captor.capture(), any()); + return captor.getValue(); + } + + private ServletResponse extractCapturedServletResponse(FilterChain capturingFilterChain) throws IOException, ServletException { + ArgumentCaptor captor = ArgumentCaptor.forClass(ServletResponse.class); + Mockito.verify(capturingFilterChain).doFilter(any(), captor.capture()); + return captor.getValue(); + } + +} 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 20a05e334..ac05ea706 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 @@ -50,7 +50,7 @@ import org.mockito.MockitoAnnotations; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.aai.util.ServletRequestHelper; import org.onap.vid.aai.util.SystemPropertyHelper; -import org.onap.vid.controller.filter.PromiseEcompRequestIdFilter; +import org.onap.vid.controller.filter.PromiseRequestIdFilter; import org.onap.vid.testUtils.TestUtils; import org.onap.vid.utils.Logging; import org.onap.vid.utils.Unchecked; @@ -65,6 +65,7 @@ import org.testng.annotations.Test; public class OutgoingRequestHeadersTest { + private static final PromiseRequestIdFilter promiseRequestIdFilter = new PromiseRequestIdFilter(); // @InjectMocks // private RestMsoImplementation restMsoImplementation; @@ -96,7 +97,8 @@ public class OutgoingRequestHeadersTest { } public static void putRequestInSpringContext() { - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest()))); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes( + (HttpServletRequest) promiseRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest()))); } // @DataProvider -- cgit 1.2.3-korg