Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 |
|
| 22 |
|
| 23 | package org.onap.osam.controller;
|
| 24 |
|
| 25 | import com.google.common.collect.ImmutableMap;
|
| 26 | import org.mockito.ArgumentCaptor;
|
| 27 | import org.mockito.Mockito;
|
| 28 | import org.mockito.stubbing.Answer;
|
| 29 | import org.onap.portalsdk.core.web.support.UserUtils;
|
| 30 | import org.onap.osam.filters.PromiseEcompRequestIdFilter;
|
| 31 | import org.springframework.mock.web.MockHttpServletResponse;
|
| 32 | import org.testng.annotations.Test;
|
| 33 |
|
| 34 | import javax.servlet.FilterChain;
|
| 35 | import javax.servlet.ServletException;
|
| 36 | import javax.servlet.ServletRequest;
|
| 37 | import javax.servlet.ServletResponse;
|
| 38 | import javax.servlet.http.HttpServletRequest;
|
| 39 | import javax.servlet.http.HttpServletResponse;
|
| 40 | import java.io.IOException;
|
| 41 | import java.util.*;
|
| 42 | import java.util.function.Function;
|
| 43 |
|
| 44 | import static org.hamcrest.CoreMatchers.is;
|
| 45 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 46 | import static org.hamcrest.Matchers.*;
|
| 47 | import static org.mockito.Matchers.any;
|
| 48 | import static org.mockito.Matchers.argThat;
|
| 49 | import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID;
|
| 50 |
|
| 51 | @Test
|
| 52 | public class PromiseEcompRequestIdFilterTest {
|
| 53 |
|
| 54 | private final String anotherHeader = "ANDREI_RUBLEV";
|
| 55 | private final String anotherValue = "foo value";
|
| 56 | private final String mixedCaseHeader = "x-ecomp-REQUESTID";
|
| 57 |
|
| 58 | @Test
|
| 59 | public void givenRequestIdHeader_headerValueNotChanged() throws IOException, ServletException {
|
| 60 |
|
| 61 | final String someTxId = "863850e2-8545-4efd-94b8-afba5f52b3d5";
|
| 62 |
|
| 63 | final ImmutableMap<String, String> incomingRequestHeaders = ImmutableMap.of(
|
| 64 | anotherHeader, anotherValue,
|
| 65 | ECOMP_REQUEST_ID, someTxId
|
| 66 | );
|
| 67 |
|
| 68 | buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, specificTxId(someTxId));
|
| 69 | }
|
| 70 |
|
| 71 | @Test
|
| 72 | public void givenMixedCaseRequestIdHeader_headerValueNotChanged() throws IOException, ServletException {
|
| 73 |
|
| 74 | final String someTxId = "729bbd8d-b0c2-4809-a794-dcccd9cda2c0";
|
| 75 |
|
| 76 | final ImmutableMap<String, String> incomingRequestHeaders = ImmutableMap.of(
|
| 77 | mixedCaseHeader, someTxId,
|
| 78 | anotherHeader, anotherValue
|
| 79 | );
|
| 80 |
|
| 81 | buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, specificTxId(someTxId));
|
| 82 | }
|
| 83 |
|
| 84 | @Test
|
| 85 | public void givenNoRequestIdHeader_headerValueWasGenerated() throws IOException, ServletException {
|
| 86 |
|
| 87 | final ImmutableMap<String, String> incomingRequestHeaders = ImmutableMap.of(
|
| 88 | anotherHeader, anotherValue
|
| 89 | );
|
| 90 |
|
| 91 | buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(incomingRequestHeaders, UserUtils::getRequestId);
|
| 92 | }
|
| 93 |
|
| 94 |
|
| 95 | private void buildRequestThenRunThroughFilterAndAssertResultRequestHeaders(
|
| 96 | ImmutableMap<String, String> originalRequestHeaders,
|
| 97 | Function<HttpServletRequest, String> txIdExtractor
|
| 98 | ) throws IOException, ServletException {
|
| 99 | HttpServletRequest servletRequest = createMockedHttpServletRequest(originalRequestHeaders);
|
| 100 | HttpServletResponse servletResponse = createMockedHttpServletResponse();
|
| 101 |
|
| 102 | final FilterChain capturingFilterChain = Mockito.mock(FilterChain.class);
|
| 103 |
|
| 104 | //////////////////
|
| 105 | //
|
| 106 | // doFilter() is the function under test
|
| 107 | //
|
| 108 | new PromiseEcompRequestIdFilter().doFilter(servletRequest, servletResponse, capturingFilterChain);
|
| 109 | //
|
| 110 | //////////////////
|
| 111 |
|
| 112 | final ServletRequest capturedServletRequest = extractCapturedServletRequest(capturingFilterChain);
|
| 113 | final ServletResponse capturedServletResponse = extractCapturedServletResponse(capturingFilterChain);
|
| 114 | final String expectedTxId = txIdExtractor.apply((HttpServletRequest) capturedServletRequest);
|
| 115 |
|
| 116 | assertRequestObjectHeaders(capturedServletRequest, expectedTxId);
|
| 117 | assertResponseObjectHeaders(capturedServletResponse, expectedTxId);
|
| 118 | }
|
| 119 |
|
| 120 |
|
| 121 | private void assertRequestObjectHeaders(ServletRequest request, String expectedTxId) {
|
| 122 | final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
| 123 |
|
| 124 | assertThat(Collections.list(httpServletRequest.getHeaderNames()),
|
| 125 | containsInAnyOrder(equalToIgnoringCase(ECOMP_REQUEST_ID), equalToIgnoringCase(anotherHeader)));
|
| 126 |
|
| 127 | assertThat(httpServletRequest.getHeader(anotherHeader), is(anotherValue));
|
| 128 |
|
| 129 | assertThat(httpServletRequest.getHeader(ECOMP_REQUEST_ID), is(expectedTxId));
|
| 130 | assertThat(httpServletRequest.getHeader(mixedCaseHeader), is(expectedTxId));
|
| 131 |
|
| 132 | assertThat(UserUtils.getRequestId(httpServletRequest), is(expectedTxId));
|
| 133 | assertThat(UserUtils.getRequestId(httpServletRequest), is(not(emptyOrNullString())));
|
| 134 | }
|
| 135 |
|
| 136 | private void assertResponseObjectHeaders(ServletResponse response, String txId) {
|
| 137 | final String REQUEST_ID_HEADER_NAME_IN_RESPONSE = mixedCaseHeader + "-echo";
|
| 138 | final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
|
| 139 |
|
| 140 | assertThat("header " + REQUEST_ID_HEADER_NAME_IN_RESPONSE.toLowerCase() + " in response must be provided",
|
| 141 | httpServletResponse.getHeader(REQUEST_ID_HEADER_NAME_IN_RESPONSE), is(txId));
|
| 142 | }
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 | private HttpServletRequest createMockedHttpServletRequest(Map<String, String> requestHeaders) {
|
| 147 | HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
|
| 148 | requestHeaders.forEach((k, v) -> {
|
| 149 | Mockito.when(servletRequest.getHeader(argThat(equalToIgnoringCase(k)))).thenReturn(v);
|
| 150 | Mockito.when(servletRequest.getHeaders(argThat(equalToIgnoringCase(k)))).then(returnEnumerationAnswer(v));
|
| 151 | });
|
| 152 | Mockito.when(servletRequest.getHeaderNames()).then(returnEnumerationAnswer(requestHeaders.keySet()));
|
| 153 | return servletRequest;
|
| 154 | }
|
| 155 |
|
| 156 | private HttpServletResponse createMockedHttpServletResponse() {
|
| 157 | return new MockHttpServletResponse();
|
| 158 | }
|
| 159 |
|
| 160 | private static Answer<Enumeration<String>> returnEnumerationAnswer(String ... items) {
|
| 161 | return returnEnumerationAnswer(Arrays.asList(items));
|
| 162 | }
|
| 163 |
|
| 164 | private static Answer<Enumeration<String>> returnEnumerationAnswer(Collection<String> items) {
|
| 165 | return invocation -> Collections.enumeration(items);
|
| 166 | }
|
| 167 |
|
| 168 | private Function<HttpServletRequest, String> specificTxId(String someTxId) {
|
| 169 | return r -> someTxId;
|
| 170 | }
|
| 171 |
|
| 172 | private ServletRequest extractCapturedServletRequest(FilterChain capturingFilterChain) throws IOException, ServletException {
|
| 173 | ArgumentCaptor<ServletRequest> captor = ArgumentCaptor.forClass(ServletRequest.class);
|
| 174 | Mockito.verify(capturingFilterChain).doFilter(captor.capture(), any());
|
| 175 | return captor.getValue();
|
| 176 | }
|
| 177 |
|
| 178 | private ServletResponse extractCapturedServletResponse(FilterChain capturingFilterChain) throws IOException, ServletException {
|
| 179 | ArgumentCaptor<ServletResponse> captor = ArgumentCaptor.forClass(ServletResponse.class);
|
| 180 | Mockito.verify(capturingFilterChain).doFilter(any(), captor.capture());
|
| 181 | return captor.getValue();
|
| 182 | }
|
| 183 |
|
| 184 | }
|