From 96a2da847be40361ff8f0a27875101782fb6b80d Mon Sep 17 00:00:00 2001 From: "Smokowski, Steven" Date: Fri, 31 May 2019 16:15:23 -0400 Subject: Fix issue where request was not being updated Fix issue where request was not being updated Change-Id: Ib4b3809797449224ac417facfaa4bf48d91a1ecb Issue-ID: SO-1966 Signed-off-by: Benjamin, Max (mb388a) --- .../onap/so/openstack/utils/MsoHeatUtilsTest.java | 55 +++++++++++++++++ .../so/openstack/utils/StackStatusHandlerTest.java | 69 ++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/StackStatusHandlerTest.java (limited to 'adapters/mso-adapter-utils/src/test') diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java index 1d38bc2431..5a2515a81e 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsTest.java @@ -23,6 +23,8 @@ package org.onap.so.openstack.utils; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.isNull; @@ -43,10 +45,14 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.db.request.beans.CloudApiRequests; +import org.onap.so.db.request.beans.InfraActiveRequests; +import org.onap.so.db.request.client.RequestsDbClient; import org.onap.so.openstack.exceptions.MsoException; import org.onap.so.openstack.exceptions.MsoOpenstackException; import org.onap.so.openstack.exceptions.MsoStackAlreadyExists; import org.springframework.core.env.Environment; +import com.woorea.openstack.base.client.Entity; import com.woorea.openstack.base.client.OpenStackResponseException; import com.woorea.openstack.heat.Heat; import com.woorea.openstack.heat.StackResource; @@ -67,6 +73,9 @@ public class MsoHeatUtilsTest extends MsoHeatUtils { @InjectMocks private MsoHeatUtils heatUtils; + @Mock + private RequestsDbClient requestDbClient; + @Mock private Heat heatClient; @@ -393,6 +402,52 @@ public class MsoHeatUtilsTest extends MsoHeatUtils { Mockito.verify(stackResource, times(1)).create(createStackParam); } + @Test + public final void saveStack_Test() throws MsoException, IOException, NovaClientException { + Stack stack = new Stack(); + stack.setId("id"); + stack.setStackName("stackName"); + stack.setStackStatus("CREATE_FAILED"); + stack.setStackStatusReason( + "Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82"); + Entity entity = new Entity(stack, "application/json"); + doReturn(entity).when(mockCreateStack).entity(); + InfraActiveRequests request = new InfraActiveRequests(); + request.setRequestId("requestId"); + doReturn(request).when(requestDbClient).getInfraActiveRequestbyRequestId("requestId"); + doNothing().when(requestDbClient).updateInfraActiveRequests(request); + heatUtils.saveStackRequest(mockCreateStack, "requestId", "stackName"); + Mockito.verify(requestDbClient, times(1)).updateInfraActiveRequests(request); + assertNotNull(request.getCloudApiRequests().get(0)); + assertEquals(request.getCloudApiRequests().get(0).getRequestId(), "requestId"); + } + + @Test + public final void saveStack__Exists_Test() throws MsoException, IOException, NovaClientException { + Stack stack = new Stack(); + stack.setId("id"); + stack.setStackName("stackName"); + stack.setStackStatus("CREATE_FAILED"); + stack.setStackStatusReason( + "Resource CREATE failed: Conflict: resources.my_keypair: Key pair 'hst3bbfnm0011vm001' already exists. (HTTP 409) (Request-ID: req-941b0af6-63ae-4d6a-afbc-90b728bacf82"); + Entity entity = new Entity(stack, "application/json"); + doReturn(entity).when(mockCreateStack).entity(); + InfraActiveRequests request = new InfraActiveRequests(); + request.setRequestId("requestId"); + CloudApiRequests cloudRequest = new CloudApiRequests(); + cloudRequest.setCloudIdentifier("stackName"); + cloudRequest.setRequestBody("testMe"); + cloudRequest.setRequestId("requestId"); + request.getCloudApiRequests().add(cloudRequest); + doReturn(request).when(requestDbClient).getInfraActiveRequestbyRequestId("requestId"); + doNothing().when(requestDbClient).updateInfraActiveRequests(request); + heatUtils.saveStackRequest(mockCreateStack, "requestId", "stackName"); + Mockito.verify(requestDbClient, times(1)).updateInfraActiveRequests(request); + assertNotNull(request.getCloudApiRequests().get(0)); + assertEquals("requestId", request.getCloudApiRequests().get(0).getRequestId()); + assertNotEquals("testMe", request.getCloudApiRequests().get(0).getRequestBody()); + } + @Test public final void createStack_Error_Test() throws MsoException, IOException, NovaClientException { CreateStackParam createStackParam = new CreateStackParam(); diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/StackStatusHandlerTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/StackStatusHandlerTest.java new file mode 100644 index 0000000000..54e2bbf06d --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/StackStatusHandlerTest.java @@ -0,0 +1,69 @@ +package org.onap.so.openstack.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import java.io.IOException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.db.request.beans.RequestProcessingData; +import org.onap.so.db.request.client.RequestsDbClient; +import org.onap.so.openstack.exceptions.MsoException; +import com.woorea.openstack.heat.model.Stack; + +@RunWith(MockitoJUnitRunner.class) +public class StackStatusHandlerTest { + + @InjectMocks + StackStatusHandler statusHandler; + + @Mock + RequestsDbClient requestDBClient; + + @Test + public final void recordExists_Test() throws MsoException, IOException { + RequestProcessingData requestProcessingData = new RequestProcessingData(); + requestProcessingData.setValue("testMe"); + + doReturn(requestProcessingData).when(requestDBClient) + .getRequestProcessingDataBySoRequestIdAndNameAndGrouping(null, "id", "stackName"); + Stack latestStack = new Stack(); + latestStack.setId("id"); + latestStack.setStackName("stackName"); + latestStack.setStackStatus("CREATE_COMPLETE"); + latestStack.setStackStatusReason("Stack Finished"); + + statusHandler.updateStackStatus(latestStack); + Mockito.verify(requestDBClient, times(1)).updateRequestProcessingData(requestProcessingData); + assertNotEquals("testMe", requestProcessingData.getValue()); + } + + @Test + public final void record_Not_Exists_Test() throws MsoException, IOException { + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(RequestProcessingData.class); + doReturn(null).when(requestDBClient).getRequestProcessingDataBySoRequestIdAndNameAndGrouping(null, "id", + "stackName"); + Stack latestStack = new Stack(); + latestStack.setId("id"); + latestStack.setStackName("stackName"); + latestStack.setStackStatus("CREATE_COMPLETE"); + latestStack.setStackStatusReason("Stack Finished"); + statusHandler.updateStackStatus(latestStack); + Mockito.verify(requestDBClient, times(1)).saveRequestProcessingData(requestCaptor.capture()); + RequestProcessingData actualRequest = requestCaptor.getValue(); + assertEquals("id", actualRequest.getGroupingId()); + assertEquals("StackInformation", actualRequest.getTag()); + assertEquals("stackName", actualRequest.getName()); + assertNotNull(actualRequest.getValue()); + + } + + +} -- cgit 1.2.3-korg