From e25b88b5a7a0f3bf63ca7160a441b53145484bcc Mon Sep 17 00:00:00 2001 From: Eylon Malin Date: Thu, 5 Sep 2019 23:57:37 +0300 Subject: send different request id on each mso call Don't use static headers for ECOMP_REQUEST_ID and ONAP_REQUEST_ID but getting them from request / generate them each time Issue-ID: VID-378 Signed-off-by: Eylon Malin Change-Id: I1be4e8e86195901975d613694acf0082cd555886 Signed-off-by: Eylon Malin --- .../org/onap/vid/mso/rest/MsoRestClientNew.java | 54 ++++++++++--------- .../org/onap/vid/mso/rest/MsoRestClientTest.java | 63 +++++++++++++++++++--- .../vid/mso/rest/OutgoingRequestHeadersTest.java | 47 ++++++++++------ 3 files changed, 115 insertions(+), 49 deletions(-) diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java b/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java index 743e2a16e..4b8a974e3 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java @@ -179,7 +179,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf logger.debug(EELFLoggerDelegate.debugLogger, methodName + START); String path = baseUrl + endpoint; - HttpResponse response = client.post(path, commonHeaders, requestDetails, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetails, String.class); return MsoUtil.wrapResponse(response); } @@ -223,7 +223,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf public HttpResponseWithRequestInfo getOrchestrationRequest(String endpoint, boolean warpException) { String path = baseUrl + endpoint; - HttpResponse response = client.get(path, commonHeaders, new HashMap<>(), String.class); + HttpResponse response = client.get(path, getHeaders(), new HashMap<>(), String.class); return new HttpResponseWithRequestInfo<>(response, path, HttpMethod.GET); } @@ -231,7 +231,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf public MsoResponseWrapper getOrchestrationRequest(String endpoint) { String path = baseUrl + endpoint; - HttpResponse response = client.get(path, commonHeaders, new HashMap<>(), String.class); + HttpResponse response = client.get(path, getHeaders(), new HashMap<>(), String.class); return MsoUtil.wrapResponse(response); } @@ -243,7 +243,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf try { String path = baseUrl + endpoint; - HttpResponse response = client.get(path, commonHeaders, new HashMap<>(), String.class); + HttpResponse response = client.get(path, getHeaders(), new HashMap<>(), String.class); MsoResponseWrapper w = MsoUtil.wrapResponse(response); logger.debug(EELFLoggerDelegate.debugLogger, methodName + " w=" + w.getResponse()); @@ -263,7 +263,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf try { String path = baseUrl + endpoint; - HttpResponse response = client.post(path, commonHeaders, requestDetails, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetails, String.class); MsoResponseWrapper w = MsoUtil.wrapResponse(response); logger.debug(EELFLoggerDelegate.debugLogger, methodName + " w=" + w.getResponse()); @@ -304,7 +304,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling change configuration active status, path =[" + path + "]"); - HttpResponse response = client.post(path, commonHeaders, request, String.class); + HttpResponse response = client.post(path, getHeaders(), request, String.class); return MsoUtil.wrapResponse(response); } catch (Exception e) { logger.info(EELFLoggerDelegate.errorLogger, "." + methodName + e.toString()); @@ -322,7 +322,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf String path = baseUrl + endpoint; logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling change port configuration status, path =[" + path + "]"); - HttpResponse response = client.post(path, commonHeaders, request, String.class); + HttpResponse response = client.post(path, getHeaders(), request, String.class); return MsoUtil.wrapResponse(response); } catch (Exception e) { logger.info(EELFLoggerDelegate.errorLogger, "." + methodName + e.toString()); @@ -334,7 +334,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf @Override public MsoResponseWrapperInterface changeManagementUpdate(RequestDetailsWrapper requestDetails, String endpoint) { String path = baseUrl + endpoint; - HttpResponse response = client.post(path, commonHeaders, requestDetails, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetails, String.class); return MsoUtil.wrapResponse2(response, RequestReferencesContainer.class); } @@ -347,7 +347,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); requestDetailsWrapper.requestDetails = new MsoRequestDetails(request); - HttpResponse response = client.post(path, commonHeaders, requestDetailsWrapper, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetailsWrapper, String.class); MsoResponseWrapper msoResponseWrapperObject = MsoUtil.wrapResponse(response); int status = msoResponseWrapperObject.getStatus(); if (status == 202) { @@ -388,7 +388,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); requestDetailsWrapper.requestDetails = new MsoRequestDetails(request); - HttpResponse response = client.put(path, commonHeaders, requestDetailsWrapper, String.class); + HttpResponse response = client.put(path, getHeaders(), requestDetailsWrapper, String.class); MsoResponseWrapper w = MsoUtil.wrapResponse(response); logger.debug(EELFLoggerDelegate.debugLogger, methodName + " w=" + w.getResponse()); @@ -408,7 +408,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start "); try { String path = baseUrl + endpoint; - HttpResponse response = client.post(path, commonHeaders, requestDetails, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetails, String.class); MsoResponseWrapper w = MsoUtil.wrapResponse(response); logger.debug(EELFLoggerDelegate.debugLogger, methodName + " w =" + w.getResponse()); return w; @@ -428,7 +428,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf try { logger.debug(EELFLoggerDelegate.debugLogger, methodName + " calling Remove relationship from service instance, path =[" + endpoint + "]"); String path = baseUrl + endpoint; - HttpResponse response = client.post(path, commonHeaders, requestDetails, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetails, String.class); return MsoUtil.wrapResponse(response); } catch (Exception e) { logger.info(EELFLoggerDelegate.errorLogger, "." + methodName + e.toString()); @@ -446,7 +446,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf logger.debug(EELFLoggerDelegate.debugLogger, methodName + " calling Add relationship to service instance, path =[" + addRelationshipsPath + "]"); String path = baseUrl + addRelationshipsPath; - HttpResponse response = client.post(path, commonHeaders, requestDetails, String.class); + HttpResponse response = client.post(path, getHeaders(), requestDetails, String.class); return MsoUtil.wrapResponse(response); } catch (Exception e) { logger.info(EELFLoggerDelegate.errorLogger, "." + methodName + e.toString()); @@ -460,7 +460,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf String path = baseUrl + invokeWorkflowsPath; Map finalHeader = new HashMap<>(); - finalHeader.putAll(commonHeaders); + finalHeader.putAll(getHeaders()); finalHeader.putAll(extraHeaders); RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper<>(workflowRequestDetail); @@ -472,36 +472,36 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf @Override public HttpResponse get(String endpoint, Class responseClass) { String path = baseUrl + endpoint; - return client.get(path, commonHeaders, new HashMap<>(), responseClass); + return client.get(path, getHeaders(), new HashMap<>(), responseClass); } @Override public HttpResponse post(String endpoint, RequestDetailsWrapper requestDetailsWrapper, Class responseClass) { String path = baseUrl + endpoint; - return client.post(path, commonHeaders, requestDetailsWrapper, responseClass); + return client.post(path, getHeaders(), requestDetailsWrapper, responseClass); } @Override public HttpResponse post(String endpoint, RequestDetails requestDetails, Class responseClass) { String path = baseUrl + endpoint; - return client.post(path, commonHeaders, requestDetails, responseClass); + return client.post(path, getHeaders(), requestDetails, responseClass); } public HttpResponse getWorkflowListByModelId(String endpoint){ String path = baseUrl + endpoint; - return client.get(path, commonHeaders, Maps.newHashMap(), SOWorkflowList.class); + return client.get(path, getHeaders(), Maps.newHashMap(), SOWorkflowList.class); } - private MsoResponseWrapper createInstance(Object request, String path) { + protected MsoResponseWrapper createInstance(Object request, String path) { String methodName = "createInstance"; logger.debug(methodName + START); try { - HttpResponse response = client.post(path, commonHeaders, request, String.class); + HttpResponse response = client.post(path, getHeaders(), request, String.class); return MsoUtil.wrapResponse(response); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "." + methodName + e.toString()); @@ -525,7 +525,7 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf try { logger.debug(EELFLoggerDelegate.debugLogger, methodName + " calling Delete, path =[" + path + "]"); - HttpResponse response = client.delete(path, commonHeaders, request, String.class); + HttpResponse response = client.delete(path, getHeaders(), request, String.class); MsoResponseWrapper w = MsoUtil.wrapResponse(response); logger.debug(EELFLoggerDelegate.debugLogger, methodName + " w=" + w.getResponse()); @@ -539,6 +539,15 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf } + private Map getHeaders() { + Map map = new HashMap<>(); + map.putAll(commonHeaders); + String requestIdValue = Logging.extractOrGenerateRequestId(); + map.put(SystemProperties.ECOMP_REQUEST_ID, requestIdValue); + map.put(ONAP_REQUEST_ID_HEADER_KEY, requestIdValue); + return map; + } + private Map initCommonHeaders() { String username = systemProperties.getProperty(MsoProperties.MSO_USER_NAME); String password = systemProperties.getProperty(MsoProperties.MSO_PASSWORD); @@ -555,9 +564,6 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf map.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); map.put(X_FROM_APP_ID, systemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)); map.put(X_ONAP_PARTNER_NAME, "VID"); - String requestIdValue = Logging.extractOrGenerateRequestId(); - map.put(SystemProperties.ECOMP_REQUEST_ID, requestIdValue); - map.put(ONAP_REQUEST_ID_HEADER_KEY, requestIdValue); return ImmutableMap.copyOf(map); } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java index c91e88be7..78982ef24 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java @@ -25,18 +25,32 @@ import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.hasEntry; 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.ArgumentMatchers.refEq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; +import static org.mockito.hamcrest.MockitoHamcrest.argThat; +import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; +import static org.onap.vid.utils.Logging.ONAP_REQUEST_ID_HEADER_KEY; +import static org.testng.Assert.assertNotEquals; +import static org.testng.AssertJUnit.assertEquals; import com.fasterxml.jackson.core.JsonProcessingException; import io.joshworks.restclient.http.HttpResponse; import io.joshworks.restclient.http.JsonMapper; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; import org.apache.http.ProtocolVersion; import org.apache.http.StatusLine; import org.apache.http.message.BasicHttpResponse; import org.apache.http.message.BasicStatusLine; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.Mockito; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.aai.HttpResponseWithRequestInfo; import org.onap.vid.changeManagement.RequestDetailsWrapper; @@ -54,17 +68,11 @@ import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.http.HttpMethod; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import static org.mockito.ArgumentMatchers.refEq; -import static org.mockito.hamcrest.MockitoHamcrest.argThat; -import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; - @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class}) @WebAppConfiguration @@ -138,6 +146,45 @@ public class MsoRestClientTest { assertThat(response).isEqualToComparingFieldByField(expectedResponse); } + @Test + public void whenCreateInstanceTwice_thenRequestIdHeaderIsDifferentEachTime() { + + RequestAttributes prevRequestAttributes = RequestContextHolder.getRequestAttributes(); + + try { + //given + Mockito.reset(client); + + //mocking syncRestClient + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + when( client.post( anyString() ,anyMap(), any(RequestDetails.class), eq(String.class) ) ).thenReturn(httpResponse); + + //when + //create different ECOMP_REQUEST_ID header in Spring HttpServlet each time + OutgoingRequestHeadersTest.putRequestInSpringContext(); + restClient.createInstance(requestDetails, "someEndPoint"); + + OutgoingRequestHeadersTest.putRequestInSpringContext(); + restClient.createInstance(requestDetails, "someEndPoint"); + + //then + ArgumentCaptor> requestCaptor = ArgumentCaptor.forClass(Map.class); + verify(client, times(2)).post(anyString(), requestCaptor.capture(), any(RequestDetails.class), eq(String.class)); + assertEquals(2, requestCaptor.getAllValues().size()); + assertNotEquals(requestCaptor.getAllValues().get(0).get(SystemProperties.ECOMP_REQUEST_ID), + requestCaptor.getAllValues().get(1).get(SystemProperties.ECOMP_REQUEST_ID), + SystemProperties.ECOMP_REQUEST_ID + " headers are the same"); + assertNotEquals(requestCaptor.getAllValues().get(0).get(ONAP_REQUEST_ID_HEADER_KEY), + requestCaptor.getAllValues().get(1).get(ONAP_REQUEST_ID_HEADER_KEY), + ONAP_REQUEST_ID_HEADER_KEY + " headers are the same"); + } + finally { + //make sure other test keep go smooth + RequestContextHolder.setRequestAttributes(prevRequestAttributes); + } + } + @Test public void shouldProperlyCreateVnf() { // given 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 316f200de..b70ba063f 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,9 +20,33 @@ package org.onap.vid.mso.rest; +import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasToString; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.matchesPattern; +import static org.mockito.Mockito.when; + import com.google.common.collect.ImmutableList; +import java.util.Set; +import java.util.UUID; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MultivaluedMap; import org.apache.commons.lang3.reflect.FieldUtils; -import org.mockito.*; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +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; @@ -37,21 +61,6 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.MultivaluedMap; -import java.util.Set; -import java.util.UUID; -import java.util.function.Consumer; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.mockito.Mockito.when; - public class OutgoingRequestHeadersTest { @@ -78,7 +87,11 @@ public class OutgoingRequestHeadersTest { } @BeforeMethod - private void putRequestInSpringContext() { + private void setup() { + putRequestInSpringContext(); + } + + public static void putRequestInSpringContext() { RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest()))); } -- cgit 1.2.3-korg