Merge changes from topics "VID-30", "VID-37"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / MsoRestClientTest.java
index 839e6e6..dd05a62 100644 (file)
@@ -25,45 +25,55 @@ 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.jetbrains.annotations.NotNull;
+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;
-import org.onap.vid.changeManagement.RequestParameters;
 import org.onap.vid.changeManagement.WorkflowRequestDetail;
 import org.onap.vid.client.SyncRestClient;
 import org.onap.vid.controller.LocalWebConfig;
 import org.onap.vid.model.RequestReferencesContainer;
+import org.onap.vid.mso.MsoProperties;
 import org.onap.vid.mso.MsoResponseWrapper;
 import org.onap.vid.mso.MsoResponseWrapperInterface;
 import org.onap.vid.mso.MsoUtil;
 import org.onap.vid.mso.RestObject;
-import org.onap.vid.mso.model.CloudConfiguration;
 import org.onap.vid.mso.model.RequestReferences;
+import org.onap.vid.utils.Logging;
+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.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-
-import static org.mockito.ArgumentMatchers.refEq;
-import static org.mockito.hamcrest.MockitoHamcrest.argThat;
-
 
 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
 @WebAppConfiguration
@@ -75,6 +85,11 @@ public class MsoRestClientTest {
     @Mock
     private SyncRestClient client;
 
+    @Mock
+    private SystemPropertiesWrapper systemProperties;
+
+    @Mock
+    private Logging loggingService;
 
     private MsoRestClientNew restClient;
 
@@ -82,8 +97,9 @@ public class MsoRestClientTest {
     @BeforeClass
     private void setUp(){
         initMocks(this);
-        restClient = new MsoRestClientNew(client,baseUrl,null);
-
+        when(systemProperties.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1ghz1kfx1j1w1m7w1i271e8q1eas1hzj1m4i1iyy1kch1gdz");
+        when(systemProperties.getProperty("app_display_name")).thenReturn("vid");
+        restClient = new MsoRestClientNew(client, baseUrl, systemProperties);
     }
 
     @Test
@@ -134,6 +150,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<String> 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<Map<String, String>> 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
@@ -373,17 +428,15 @@ public class MsoRestClientTest {
 
     @Test
     public void shouldProperlyGetOrchestrationRequest() {
-        //  given
-        RestObject restObject = generateMockMsoRestObject();
-
         String endpoint = "testEndpoint";
         HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse");
-        MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse);
+        String expectedPath = baseUrl+endpoint;
+        HttpResponseWithRequestInfo<String> expectedResponse = new HttpResponseWithRequestInfo<>(httpResponse, expectedPath, HttpMethod.GET);
 
-        when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) )  ).thenReturn(httpResponse);
+        when( client.get( eq(expectedPath), anyMap(), anyMap(), eq(String.class) )).thenReturn(httpResponse);
 
         //  when
-        MsoResponseWrapper response = restClient.getOrchestrationRequest(null,null,endpoint,restObject,true);
+        HttpResponseWithRequestInfo<String> response = restClient.getOrchestrationRequest(endpoint, true);
 
         //  then
         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
@@ -593,25 +646,23 @@ public class MsoRestClientTest {
     }
 
     @Test
-    public void shouldProperlyChangeManagementUpdate() {
+    public void shouldProperlyChangeManagementUpdate() throws JsonProcessingException {
         //  given
         RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest();
         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
 
         String endpoint = "testEndpoint";
-        HttpResponse<RequestReferencesContainer> httpResponse = HttpResponse.fallback(
-                new RequestReferencesContainer(
-                        new RequestReferences()));
-
-        MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse);
+        RequestReferencesContainer entity = new RequestReferencesContainer(new RequestReferences());
+        HttpResponse<String> httpResponse = HttpResponse.fallback(JACKSON_OBJECT_MAPPER.writeValueAsString(entity));
 
-        when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(RequestReferencesContainer.class))).thenReturn(httpResponse);
+        when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(httpResponse);
 
         //  when
         MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint);
 
         //  then
-        assertThat(expectedResponse).isEqualToComparingFieldByField(response);
+        assertThat(response.getEntity()).isEqualToComparingFieldByField(entity);
+        assertThat(response.getStatus()).isEqualTo(0);
     }
 
     @Test
@@ -659,7 +710,7 @@ public class MsoRestClientTest {
         when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse);
 
         //  when
-        restClient.setServiceInstanceStatus(requestDetails,"", "", endpoint, restObject);
+        restClient.setServiceInstanceStatus(requestDetails, endpoint);
     }
 
     @Test( expectedExceptions = MsoTestException.class)
@@ -670,7 +721,7 @@ public class MsoRestClientTest {
         when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception"));
 
         //  when
-        restClient.setServiceInstanceStatus(null,"", "", endpoint, null);
+        restClient.setServiceInstanceStatus(null, endpoint);
     }
 
     @Test
@@ -781,7 +832,7 @@ public class MsoRestClientTest {
 
         Map<String,String> extraHeaders = new HashMap<>();
         extraHeaders.put("X-ONAP-RequestID",requestId.toString());
-        extraHeaders.put("X-ONAP-PartnerName","VID");
+        extraHeaders.put("X-ONAP-PartnerName","VID.VID");
         extraHeaders.put("X-RequestorID","testRequester");
 
         //  when