diff options
author | Stone, Avi (as206k) <as206k@att.com> | 2018-05-23 11:21:11 +0300 |
---|---|---|
committer | Stone, Avi (as206k) <as206k@att.com> | 2018-05-23 11:30:13 +0300 |
commit | 3e4c18770957b55e2f80da32c3a32caa908f1386 (patch) | |
tree | 8a94c656300e75e38febfe9826ad36fc54fe14f5 /dcaedt_be/src/test/java | |
parent | da9db1b89e8c9199da4791a2ccd26d1628120a08 (diff) |
Upgrade dt-be-main
Update sources for dcae-dt-be-main to latest version
Change-Id: I3d58a2dc32611c0ca90f1c97e1294a17d5748623
Issue-ID: SDC-1359
Signed-off-by: Stone, Avi (as206k) <as206k@att.com>
Diffstat (limited to 'dcaedt_be/src/test/java')
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/BlueprintBusinessLogicTest.java | 69 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/CompositionBusinessLogicTest.java | 218 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ReferenceBusinessLogicTest.java | 9 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogicTest.java | 217 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ServiceBusinessLogicTest.java (renamed from dcaedt_be/src/test/java/org/onap/sdc/dcae/services/GetServicesTest.java) | 60 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/VfcmtBusinessLogicTest.java | 73 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/filter/LoggingFilterTest.java | 119 | ||||
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/impl/RulesBusinessLogicTest.java | 30 |
8 files changed, 748 insertions, 47 deletions
diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/BlueprintBusinessLogicTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/BlueprintBusinessLogicTest.java new file mode 100644 index 0000000..a00130f --- /dev/null +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/BlueprintBusinessLogicTest.java @@ -0,0 +1,69 @@ +package org.onap.sdc.dcae.composition.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.sdc.dcae.client.ISdcClient; +import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact; +import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed; +import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader; +import org.springframework.http.ResponseEntity; + +import java.util.ArrayList; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; +import static org.onap.sdc.dcae.composition.util.DcaeBeConstants.Composition.fileNames.COMPOSITION_YML; + +@RunWith(MockitoJUnitRunner.class) +public class BlueprintBusinessLogicTest { + + private static final String USER_ID = "UserId"; + private static final String CONTEXT = "Context"; + private static final String VFCMT_UUID = "VfcmtUuid"; + private static final String SERVICE_UUID = "ServiceUuid"; + private static final String VFI_NAME = "VfiName"; + private static final String FLOW_TYPE = "FlowType"; + private static final String REQUEST_ID = "RequestId"; + + @InjectMocks + BlueprintBusinessLogic classUnderTest; + @Mock + private ISdcClient sdcClientMock; + private ResourceDetailed resourceDetailed; + + @Before + public void setup() { + new ErrorConfigurationLoader(System.getProperty("user.dir")+"/src/main/webapp/WEB-INF"); + + resourceDetailed = new ResourceDetailed(); + resourceDetailed.setUuid(VFCMT_UUID); + classUnderTest.setSdcRestClient(sdcClientMock); + when(sdcClientMock.getResource(eq(VFCMT_UUID), eq(REQUEST_ID))).thenReturn(resourceDetailed); + when(sdcClientMock.getResourceArtifact(eq(VFCMT_UUID), anyString(), anyString())).thenReturn("\"{\\\\\\\"version\\\\\\\":0,\\\\\\\"flowType\\\\\\\":\\\\\\\"templateInfoFlowType\\\\\\\",\\\\\\\"nodes\\\\\\\":[],\\\\\\\"inputs\\\\\\\":[],\\\\\\\"outputs\\\\\\\":[],\\\\\\\"relations\\\\\\\":[]}\\\"\""); + } + + @Test + public void generateAndSaveBlueprint_compositionNotFound() { + ResponseEntity responseEntity = classUnderTest.generateAndSaveBlueprint(USER_ID, CONTEXT, VFCMT_UUID, SERVICE_UUID, VFI_NAME, FLOW_TYPE, REQUEST_ID); + Assert.assertEquals( 404, responseEntity.getStatusCodeValue()); + } + + @Test + public void generateAndSaveBlueprint_toscaFailed() { + Artifact artifact = new Artifact(); + artifact.setArtifactName(COMPOSITION_YML); + artifact.setPayloadData("{\\\"version\\\":0,\\\"flowType\\\":\\\"templateInfoFlowType\\\",\\\"nodes\\\":[],\\\"inputs\\\":[],\\\"outputs\\\":[],\\\"relations\\\":[]}\""); + ArrayList<Artifact> artifacts = new ArrayList<>(); + artifacts.add(artifact); + resourceDetailed.setArtifacts(artifacts); + ResponseEntity responseEntity = classUnderTest.generateAndSaveBlueprint(USER_ID, CONTEXT, VFCMT_UUID, SERVICE_UUID, VFI_NAME, FLOW_TYPE, REQUEST_ID); + Assert.assertEquals( 500, responseEntity.getStatusCodeValue()); + } + +} diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/CompositionBusinessLogicTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/CompositionBusinessLogicTest.java new file mode 100644 index 0000000..d9b65b2 --- /dev/null +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/CompositionBusinessLogicTest.java @@ -0,0 +1,218 @@ +package org.onap.sdc.dcae.composition.impl; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.sdc.dcae.catalog.asdc.ASDCException; +import org.onap.sdc.dcae.client.ISdcClient; +import org.onap.sdc.dcae.composition.restmodels.VfcmtData; +import org.onap.sdc.dcae.composition.restmodels.sdc.*; +import org.onap.sdc.dcae.composition.util.DcaeBeConstants; +import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader; +import org.onap.sdc.dcae.errormng.RequestError; +import org.onap.sdc.dcae.errormng.ServiceException; +import org.onap.sdc.dcae.utils.Normalizers; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.*; + +public class CompositionBusinessLogicTest { + + private ISdcClient sdcClientMock = Mockito.mock(ISdcClient.class); + private ResourceDetailed vfcmt = Mockito.mock(ResourceDetailed.class); + private ServiceDetailed service = Mockito.mock(ServiceDetailed.class); + private CompositionBusinessLogic compositionBusinessLogic = new CompositionBusinessLogic(); + private String justAString = "aStringForAllSeasons"; + + @Before + public void setup(){ + MockitoAnnotations.initMocks(this); + compositionBusinessLogic.setSdcRestClient(sdcClientMock); + new ErrorConfigurationLoader(System.getProperty("user.dir")+"/src/main/webapp/WEB-INF"); + when(vfcmt.getUuid()).thenReturn(justAString); + when(vfcmt.getName()).thenReturn(justAString); + when(vfcmt.getDescription()).thenReturn(justAString); + when(vfcmt.getInvariantUUID()).thenReturn(justAString); + } + + @Test + public void successfulSaveCompositionAndCheckInInitialStateCheckedOut() throws Exception { + emulateListOfArtifactsWithCompositionYml(); + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(vfcmt); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKOUT"); + + compositionBusinessLogic.saveComposition(justAString, justAString, justAString, justAString, false); + + verify(sdcClientMock).getResource(anyString(),anyString()); + verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString()); + verify(sdcClientMock).updateResourceArtifact(anyString(), anyString(), any(), anyString()); + verify(sdcClientMock).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + } + + @Test + public void successfulSaveCompositionAndCheckInInitialStateCheckedIn() throws Exception { + emulateListOfArtifactsWithCompositionYml(); + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(vfcmt); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKIN"); + when(sdcClientMock.changeResourceLifecycleState(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(vfcmt); + + compositionBusinessLogic.saveComposition(justAString, justAString, justAString, justAString, false); + + verify(sdcClientMock).getResource(anyString(),anyString()); + verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString()); + verify(sdcClientMock).updateResourceArtifact(anyString(), anyString(), any(), anyString()); + verify(sdcClientMock, times(2)).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + } + + @Test + public void saveCompositionCheckInFailureDoRollback() throws Exception { + + emulateListOfArtifactsWithCompositionYml(); + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(vfcmt); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKIN"); + RequestError requestError = new RequestError(); + requestError.setServiceException(new ServiceException("SVC4086", "", null)); + when(sdcClientMock.changeResourceLifecycleState(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(vfcmt).thenThrow(new ASDCException(HttpStatus.FORBIDDEN, requestError)); + + ResponseEntity result = compositionBusinessLogic.saveComposition(justAString, justAString, justAString, justAString, false); + + verify(sdcClientMock).getResource(anyString(),anyString()); + verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString()); + verify(sdcClientMock).updateResourceArtifact(anyString(), anyString(), any(), anyString()); + verify(sdcClientMock, times(3)).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + assertEquals(403, result.getStatusCodeValue()); + } + + @Test + public void saveCompositionUpdateArtifactFailureNoRollback() throws Exception { + + emulateListOfArtifactsWithCompositionYml(); + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(vfcmt); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKOUT"); + RequestError requestError = new RequestError(); + requestError.setServiceException(new ServiceException("SVC4127", "", null)); + when(sdcClientMock.updateResourceArtifact(anyString(), anyString(), any(), anyString())).thenThrow(new ASDCException(HttpStatus.BAD_REQUEST, requestError)); + ResponseEntity result = compositionBusinessLogic.saveComposition(justAString, justAString, justAString, justAString, false); + + verify(sdcClientMock).getResource(anyString(),anyString()); + verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString()); + verify(sdcClientMock).updateResourceArtifact(anyString(), anyString(), any(), anyString()); + verify(sdcClientMock, times(0)).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + assertEquals(409, result.getStatusCodeValue()); + } + + @Test + public void saveCompositionNoExistingCompositionFailure() throws Exception { + + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(vfcmt); + + ResponseEntity result = compositionBusinessLogic.saveComposition(justAString, justAString, justAString, justAString, false); + + verify(sdcClientMock).getResource(anyString(),anyString()); + verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString()); + verify(sdcClientMock, times(0)).updateResourceArtifact(anyString(), anyString(), any(), anyString()); + verify(sdcClientMock, times(0)).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + assertEquals(404, result.getStatusCodeValue()); + } + + + //backward compatibility - create new composition + @Test + public void successfulSaveNewCompositionAndCheckIn() throws Exception { + + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(vfcmt); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKOUT"); + + compositionBusinessLogic.saveComposition(justAString, justAString, justAString, justAString, true); + + verify(sdcClientMock).getResource(anyString(),anyString()); + verify(sdcClientMock).createResourceArtifact(anyString(),anyString(),any(),anyString()); + verify(sdcClientMock, times(0)).updateResourceArtifact(anyString(), anyString(), any(), anyString()); + verify(sdcClientMock).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + } + + + @Test + public void submitCompositionSuccessNoPreviousBlueprint() throws Exception { + + String vfiName = "vfiName"; + mockVfiList(vfiName); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKOUT"); + when(sdcClientMock.getAssetMetadata(anyString(),anyString(), anyString())).thenReturn(service); + VfcmtData vfcmtData = new VfcmtData(vfcmt, vfiName, justAString, justAString); + + compositionBusinessLogic.submitComposition(justAString, justAString, vfcmtData, justAString, justAString); + + verify(sdcClientMock).getAssetMetadata(anyString(), anyString(), anyString()); + verify(sdcClientMock).createInstanceArtifact(anyString(), anyString(), anyString(), anyString(), any(),anyString()); + verify(sdcClientMock, times(0)).updateInstanceArtifact(anyString(), anyString(), anyString(), anyString(), any(), anyString()); + verify(sdcClientMock, times(0)).getMonitoringReferences(anyString(), anyString(), anyString(), anyString()); + verify(sdcClientMock, times(2)).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + } + + @Test + public void submitCompositionSuccessDeletePreviousReference() throws Exception { + + String vfiName = "vfiName"; + String flowType = "flowType"; + mockVfiWithBlueprint(vfiName, flowType); + when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKIN"); + when(sdcClientMock.getAssetMetadata(anyString(),anyString(), anyString())).thenReturn(service); + when(service.getVersion()).thenReturn("0.3"); + when(service.getUuid()).thenReturn(justAString); + ExternalReferencesMap referencesMap = new ExternalReferencesMap(); + ResourceDetailed previousMcVersion = new ResourceDetailed(); + previousMcVersion.setInvariantUUID(vfcmt.getInvariantUUID()); + previousMcVersion.setUuid("previousId"); + referencesMap.put(Normalizers.normalizeComponentInstanceName(vfiName), Arrays.asList(vfcmt.getUuid(), previousMcVersion.getUuid())); + when(sdcClientMock.getMonitoringReferences(anyString(), anyString(), anyString(), anyString())).thenReturn(referencesMap); + when(sdcClientMock.getResource(previousMcVersion.getUuid(), justAString)).thenReturn(previousMcVersion); + VfcmtData vfcmtData = new VfcmtData(vfcmt, vfiName, flowType, justAString); + + compositionBusinessLogic.submitComposition(justAString, justAString, vfcmtData, justAString, justAString); + + verify(sdcClientMock).getAssetMetadata(anyString(), anyString(), anyString()); + verify(sdcClientMock).updateInstanceArtifact(anyString(), anyString(), anyString(), anyString(), any(),anyString()); + verify(sdcClientMock, times(0)).createInstanceArtifact(anyString(), anyString(), anyString(), anyString(), any(), anyString()); + verify(sdcClientMock).getMonitoringReferences(anyString(), anyString(), anyString(), anyString()); + verify(sdcClientMock).getResource(anyString(), anyString()); + verify(sdcClientMock).deleteExternalMonitoringReference(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()); + verify(sdcClientMock).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString()); + } + + private void emulateListOfArtifactsWithCompositionYml() { + List<Artifact> listOfArtifactCompositionYml = new ArrayList<>(); + Artifact compositionArtifact = Mockito.mock(Artifact.class); + when(compositionArtifact.getArtifactName()).thenReturn(DcaeBeConstants.Composition.fileNames.COMPOSITION_YML); + listOfArtifactCompositionYml.add(compositionArtifact); + when(vfcmt.getArtifacts()).thenReturn(listOfArtifactCompositionYml); + } + + private void mockVfiList(String vfiName) { + List<ResourceInstance> vfiList = new ArrayList<>(); + ResourceInstance vfi = Mockito.mock(ResourceInstance.class); + when(vfi.getResourceInstanceName()).thenReturn(vfiName); + vfiList.add(vfi); + when(service.getResources()).thenReturn(vfiList); + } + + private void mockVfiWithBlueprint(String vfiName, String flowType) { + mockVfiList(vfiName); + List<Artifact> instanceArtifacts = new ArrayList<>(); + Artifact blueprint = Mockito.mock(Artifact.class); + String artifactName = compositionBusinessLogic.generateBlueprintFileName(flowType, vfcmt.getName()); + when(blueprint.getArtifactName()).thenReturn(artifactName); + instanceArtifacts.add(blueprint); + when(service.getResources().get(0).getArtifacts()).thenReturn(instanceArtifacts); + } + +}
\ No newline at end of file diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ReferenceBusinessLogicTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ReferenceBusinessLogicTest.java index c353701..70c0577 100644 --- a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ReferenceBusinessLogicTest.java +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ReferenceBusinessLogicTest.java @@ -10,6 +10,7 @@ import org.mockito.runners.MockitoJUnitRunner; import org.onap.sdc.dcae.client.ISdcClient; import org.onap.sdc.dcae.composition.restmodels.MonitoringComponent; import org.onap.sdc.dcae.composition.restmodels.sdc.*; +import org.onap.sdc.dcae.composition.util.DcaeBeConstants; import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader; import org.onap.sdc.dcae.errormng.ResponseFormat; import org.springframework.http.HttpStatus; @@ -36,7 +37,7 @@ public class ReferenceBusinessLogicTest { private ResourceDetailed templateMC; @InjectMocks - ReferenceBusinessLogic classUnderTest; + private ReferenceBusinessLogic classUnderTest; @Before public void setup(){ @@ -86,7 +87,7 @@ public class ReferenceBusinessLogicTest { ServiceDetailed serviceDetailed = new ServiceDetailed(); ResourceInstance resourceInstance = new ResourceInstance(); Artifact artifact = new Artifact(); - artifact.setArtifactName(monitoringComponentName); + artifact.setArtifactName("." + monitoringComponentName + "." + DcaeBeConstants.Composition.fileNames.EVENT_PROC_BP_YAML); resourceInstance.setArtifacts(Collections.singletonList(artifact)); resourceInstance.setResourceInstanceName(vfiName); serviceDetailed.setResources(Collections.singletonList(resourceInstance)); @@ -98,7 +99,7 @@ public class ReferenceBusinessLogicTest { mockGetService(); ResponseEntity responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId); verify(sdcClientMock).getService(serviceUuid, requestId); - verify(sdcClientMock).deleteInstanceResourceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()); + verify(sdcClientMock).deleteInstanceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); } @@ -114,7 +115,7 @@ public class ReferenceBusinessLogicTest { @Test public void deleteVfcmtReferenceBlueprint_exceptionSdcdeleteInstanceResourceArtifact() throws Exception { mockGetService(); - doThrow(new RuntimeException("")).when(sdcClientMock).deleteInstanceResourceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()); + doThrow(new RuntimeException("")).when(sdcClientMock).deleteInstanceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()); ResponseEntity<ResponseFormat> responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId); diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogicTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogicTest.java new file mode 100644 index 0000000..d421113 --- /dev/null +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogicTest.java @@ -0,0 +1,217 @@ +package org.onap.sdc.dcae.composition.impl; + +import org.onap.sdc.dcae.client.ISdcClient; +import org.onap.sdc.dcae.composition.CompositionConfig; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.*; +import org.onap.sdc.dcae.composition.restmodels.sdc.*; +import org.onap.sdc.dcae.composition.util.DcaeBeConstants; +import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader; +import org.onap.sdc.dcae.errormng.ResponseFormat; +import org.onap.sdc.dcae.rule.editor.impl.RulesBusinessLogic; +import org.springframework.http.ResponseEntity; +import org.testng.Assert; + +import java.util.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.mockito.Mockito.*; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +public class RuleEditorBusinessLogicTest { + + // DEFAULT PROPERTIES + private String justAString = "aStringForAllSeasons"; + private String userId = "gc786h"; + private String vfcmtUuid = "26e8d4b5-f087-4821-a75a-0b9514b5a7ab"; + private String dcaeCompLabel = "dMp.DockerMap"; + private String nId = "n.1525864440166.30"; + private String resourceUuid = "26e8d4b5-f087-4821-a75a-0b9514b5a7ab"; + private String artifactUuid = "9b00ba74-da02-4706-8db0-ac3c11d1d47b"; + private String configParam = "aaf_username"; + private String requestId = "9a89b5c7-33b2-4f7e-a404-66bf4115f510"; + private String flowTypeName = "SNMP MSE"; + private String ruleUuid = "sadsads"; + private String categoryName = "Template"; + private String resourceType = "VFCMT"; + private String saveRulesJsonRequest = "{\n\"version\":\"4.1\",\n\"eventType\":\"syslogFields\",\n\"uid\":\"\",\n\"description\":\"sfasfsaf\",\n\"actions\":[\n{\n\"id\":\"6e0175a0-581f-11e8-82eb-53bb060b790a\",\n\"actionType\":\"copy\",\n\"from\":{\n\"value\":\"asfsf\",\n\"regex\":\"\",\n\"state\":\"closed\",\n\"values\":[\n{\n" + "\"value\":\"\"\n" + "},\n" + "{\n\"value\":\"\"\n}\n]\n},\n\"target\":\"event.commonEventHeader.eventType\",\n\"map\":{\n\"values\":[\n{\n\"key\":\"\",\n\"value\":\"\"\n}\n],\n\"haveDefault\":false,\n\"default\":\"\"\n},\n\"dateFormatter\":{\n\"fromFormat\":\"\",\n\"toFormat\":\"\",\n\"fromTimezone\":\"\",\n\"toTimezone\":\"\"\n},\n\"replaceText\":{\n\"find\":\"\",\n\"replace\":\"\"\n},\n\"logText\":{\n\"name\":\"\",\n\"level\":\"\",\n\"text\":\"\"\n},\n\"logEvent\":{\n\"title\":\"\"\n}\n}\n],\n\"condition\":null\n}"; + private String artifactJson = "{\n \"artifactName\":\"composition.yml\",\n \"artifactType\":\"DCAE_TOSCA\",\n \"artifactURL\":\"/sdc/v1/catalog/resources/c2877686-616a-48ca-a37b-7e311bf83adc/artifacts/9b00ba74-da02-4706-8db0-ac3c11d1d47b\",\n \"artifactDescription\":\"createReferenceArtifact\",\n \"artifactTimeout\":null,\n \"artifactChecksum\":\"MjhhYTAwOTIxZGZkMGMyMmFjYmEzYjI1NTIwYjA3YzM=\",\n \"artifactUUID\":\"9b00ba74-da02-4706-8db0-ac3c11d1d47b\",\n \"artifactVersion\":\"1\",\n \"generatedFromUUID\":null,\n \"artifactLabel\":\"servicereference\",\n \"artifactGroupType\":\"DEPLOYMENT\",\n \"payloadData\":null,\n \"description\":null\n" + "}"; + private String defaultPayload = "{eventType:syslogFields,version:4.1,rules:{'test':{'version':'4.1'}}}"; + + + // MOCKS + private ISdcClient sdcClientMock = Mockito.mock(ISdcClient.class); + private ResourceDetailed vfcmt = Mockito.mock(ResourceDetailed.class); + private SchemaInfo schemaInfo = Mockito.mock(SchemaInfo.class); + private CompositionConfig compositionConfig = Mockito.mock(CompositionConfig.class); + private RulesBusinessLogic rulesBusinessLogic = Mockito.mock(RulesBusinessLogic.class); + private CompositionConfig.FlowType flowType = Mockito.mock(CompositionConfig.FlowType.class); + + @InjectMocks + private RuleEditorBusinessLogic ruleEditorBusinessLogic = new RuleEditorBusinessLogic(); + + @Before + public void setup() { + + MockitoAnnotations.initMocks(this); + ruleEditorBusinessLogic.setSdcRestClient(sdcClientMock); + + new ErrorConfigurationLoader(System.getProperty("user.dir") + "/src/main/webapp/WEB-INF"); + when(vfcmt.getUuid()).thenReturn(vfcmtUuid); + when(vfcmt.getName()).thenReturn(justAString); + when(vfcmt.getDescription()).thenReturn(justAString); + when(vfcmt.getResourceType()).thenReturn(resourceType); + when(vfcmt.getCategory()).thenReturn(categoryName); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResource(anyString(), anyString())).thenReturn(vfcmt); + when(schemaInfo.getVersion()).thenReturn("0.2"); + + /* PowerMockito.doReturn(vs).when(VesStructureLoader.class); + when(vs.getEventListenerDefinitionByVersion(anyString())).thenReturn(null);*/ + } + + @Test + public void test_saveRules() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(resourceUuid, artifactUuid, requestId)).thenReturn(defaultPayload); + when(rulesBusinessLogic.addOrEditRule(any(MappingRules.class), any(Rule.class))).thenReturn(true); + + ResponseEntity result = ruleEditorBusinessLogic.saveRule(saveRulesJsonRequest, requestId, userId, vfcmtUuid, dcaeCompLabel, nId, configParam); + assertEquals(200,result.getStatusCodeValue()); + Assert.assertTrue(result.getBody().toString().contains("6e0175a0-581f-11e8-82eb-53bb060b790a")); + verify(rulesBusinessLogic,times(1)).addOrEditRule(any(MappingRules.class), any(Rule.class)); + + } + + @Test + public void test_saveRules_artifactNotFound() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, false); + + when(rulesBusinessLogic.addOrEditRule(any(MappingRules.class), any(Rule.class))).thenReturn(true); + String payload = "{eventType:syslogFields,version:4.1,rules:{'test':{'version':'4.1'}},\"nid\":\"n.1525864440166.30}"; + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(anyString(),anyString(), anyString())).thenReturn(payload); + + ResponseEntity result = ruleEditorBusinessLogic.saveRule(saveRulesJsonRequest, requestId, userId, vfcmtUuid, dcaeCompLabel, nId, configParam); + assertEquals(200,result.getStatusCodeValue()); + Assert.assertTrue(result.getBody().toString().contains("6e0175a0-581f-11e8-82eb-53bb060b790a")); + verify(rulesBusinessLogic,times(0)).addOrEditRule(any(MappingRules.class), any(Rule.class)); + + } + + @Test + public void test_saveRules_artifactNotFound_Error() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, false); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(anyString(),anyString(), anyString())).thenReturn(defaultPayload); + + ResponseEntity<ResponseFormat> result = ruleEditorBusinessLogic.saveRule(saveRulesJsonRequest, requestId, userId, vfcmtUuid, dcaeCompLabel, nId, configParam); + assertEquals(400,result.getStatusCodeValue()); + assertEquals("SVC6114",result.getBody().getRequestError().getServiceException().getMessageId()); + assertEquals("DCAE component %1 not found in composition",result.getBody().getRequestError().getServiceException().getText()); + verify(rulesBusinessLogic,times(0)).addOrEditRule(any(MappingRules.class), any(Rule.class)); + + } + + @Test + public void test_getRules() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(resourceUuid, artifactUuid, requestId)) + .thenReturn(defaultPayload); + + ResponseEntity result = ruleEditorBusinessLogic.getRules(vfcmtUuid, dcaeCompLabel, nId, configParam, requestId); + assertEquals(200,result.getStatusCodeValue()); + Assert.assertTrue(result.getBody().toString().contains("eventType:syslogFields,version:4.1,rules:{'test':{'version':'4.1'")); + + } + + @Test + public void test_getExistingRuleTargets() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(resourceUuid, artifactUuid, requestId)).thenReturn(defaultPayload); + + ResponseEntity result = ruleEditorBusinessLogic.getExistingRuleTargets(vfcmtUuid, requestId, dcaeCompLabel, nId); + assertEquals(200,result.getStatusCodeValue()); + assertNotEquals(null,result.getBody()); + + } + + @Test + public void test_translate() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(resourceUuid, artifactUuid, requestId)).thenReturn(defaultPayload); + Map<String, CompositionConfig.FlowType> flowTypeMap = new HashMap<>(); + flowTypeMap.put("SNMP MSE", flowType); + when(compositionConfig.getFlowTypesMap()).thenReturn(flowTypeMap); + when(compositionConfig.getFlowTypesMap().get("SNMP MSE").getEntryPointPhaseName()).thenReturn("testName"); + when(compositionConfig.getFlowTypesMap().get("SNMP MSE").getLastPhaseName()).thenReturn("testLastName"); + + when(rulesBusinessLogic.translateRules(any(MappingRules.class), anyString(), anyString(), anyString())).thenReturn("testLastName"); + ResponseEntity result = ruleEditorBusinessLogic.translateRules(vfcmtUuid, requestId, dcaeCompLabel, nId, configParam, flowTypeName); + verify(compositionConfig,times(6)).getFlowTypesMap(); + verify(rulesBusinessLogic,times(1)).translateRules(any(MappingRules.class), anyString(), anyString(), anyString()); + + assertEquals(200,result.getStatusCodeValue()); + + } + + @Test + public void test_deleteRule() throws Exception { + + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); + + when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(resourceUuid, artifactUuid, requestId)).thenReturn(defaultPayload); + + when(rulesBusinessLogic.deleteRule(any(MappingRules.class), anyString())).thenReturn(new Rule()); + ResponseEntity result = ruleEditorBusinessLogic.deleteRule(userId, vfcmtUuid, dcaeCompLabel, nId, configParam, ruleUuid, requestId); + assertEquals(200,result.getStatusCodeValue()); + + } + + @Test + public void test_getDefinition(){ + + +/* + PowerMockito.mockStatic(VesStructureLoader.class); + when(VesStructureLoader.getEventListenerDefinitionByVersion(anyString())).thenReturn(null); +*/ + + ResponseEntity res = ruleEditorBusinessLogic.getDefinition("4.1","syslogFields"); + + } + + private void emulateMockListOfArtifacts(String dcaeCompLabel, String nid, String configParam, boolean isApprovedArtifact) { + List<Artifact> listOfArtifactCompositionYml = new ArrayList<>(); + Artifact artifact = Mockito.mock(Artifact.class);//gson.fromJson(artifactJson, Artifact.class); + + if (isApprovedArtifact) { + when(artifact.getArtifactLabel()).thenReturn(dcaeCompLabel + nid + configParam); + when(artifact.getArtifactName()).thenReturn(dcaeCompLabel + "_" + nid + "_" + DcaeBeConstants.Composition.fileNames.MAPPING_RULE_POSTFIX); + }else { + when(artifact.getArtifactLabel()).thenReturn("servicereference"); + when(artifact.getArtifactName()).thenReturn("composition.yml"); + } + when(artifact.getArtifactType()).thenReturn("DCAE_TOSCA"); + when(artifact.getArtifactUUID()).thenReturn(artifactUuid); + when(artifact.getArtifactDescription()).thenReturn("createmapArtifact"); + + listOfArtifactCompositionYml.add(artifact); + when(vfcmt.getArtifacts()).thenReturn(listOfArtifactCompositionYml); + } + + +} diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/services/GetServicesTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ServiceBusinessLogicTest.java index 68f055f..3cac355 100644 --- a/dcaedt_be/src/test/java/org/onap/sdc/dcae/services/GetServicesTest.java +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ServiceBusinessLogicTest.java @@ -1,22 +1,47 @@ -package org.onap.sdc.dcae.services; +package org.onap.sdc.dcae.composition.impl; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.when; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.junit.Before; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.sdc.dcae.client.ISdcClient; import org.onap.sdc.dcae.composition.restmodels.DcaeMinimizedService; -import org.onap.sdc.dcae.composition.controller.ServicesController; +import org.onap.sdc.dcae.composition.impl.ServiceBusinessLogic; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.ActionDeserializer; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.BaseAction; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.BaseCondition; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.ConditionDeserializer; +import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact; +import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceInstance; +import org.onap.sdc.dcae.composition.restmodels.sdc.ServiceDetailed; import org.onap.sdc.dcae.composition.util.DcaeBeConstants; import org.onap.sdc.dcae.composition.util.DcaeBeConstants.LifecycleStateEnum; +import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader; import org.testng.annotations.Test; -public class GetServicesTest { - +public class ServiceBusinessLogicTest { + + private String userId = "me"; + private String requestId = "1"; + private String monitoringComponentName = "monitoringComponentName"; + private String serviceUuid = "serviceUuid"; + private String vfiName = "vfiName"; + + private static Gson gson = new GsonBuilder() + .registerTypeAdapter(BaseAction.class, new ActionDeserializer()) + .registerTypeAdapter(BaseCondition.class, new ConditionDeserializer()).create(); + + ServiceBusinessLogic target = new ServiceBusinessLogic(); + @Test public void parseAndFliterServicesByUser_nullServices_TBD() { @@ -27,7 +52,6 @@ public class GetServicesTest { @Test public void parseAndFliterServicesByUser_emptyList_emptyList() { // arrange - ServicesController target = new ServicesController(); String user_id = "test"; String lastUpdaterUserId = "test"; List<LinkedHashMap<String, String>> services = new ArrayList<LinkedHashMap<String, String>>(); @@ -49,7 +73,6 @@ public class GetServicesTest { String version = "0.1"; String serviceName = "TestService"; - ServicesController target = new ServicesController(); LinkedHashMap<String, String> service = createServiceAsMap(lastUpdaterUserId, uuid, invariantUUID, lifecycleState, version, serviceName); List<LinkedHashMap<String, String>> services = new ArrayList<LinkedHashMap<String, String>>( @@ -77,7 +100,6 @@ public class GetServicesTest { .map(x -> createServiceAsMap(lastUpdaterUserId, uuid, UUID.randomUUID().toString(), lifecycleState, version, x)) .collect(Collectors.toList()); - ServicesController target = new ServicesController(); // act List<DcaeMinimizedService> result = target.parseAndFilterServicesByUser(lastUpdaterUserId, unsortedServices, @@ -112,8 +134,6 @@ public class GetServicesTest { List<LinkedHashMap<String, String>> singleServiceWithMultiVersions = Arrays.asList("1.0", "0.3", "11.0", "2.0", "1.8").stream() .map(x -> createServiceAsMap(lastUpdaterUserId, uuid, invariantUUID, lifecycleState, x, serviceName)) .collect(Collectors.toList()); - - ServicesController target = new ServicesController(); // act List<DcaeMinimizedService> result = target.parseAndFilterServicesByUser(lastUpdaterUserId, singleServiceWithMultiVersions, user_id); @@ -139,5 +159,15 @@ public class GetServicesTest { return service; } - + private void mockGetService() + { + ServiceDetailed serviceDetailed = new ServiceDetailed(); + ResourceInstance resourceInstance = new ResourceInstance(); + Artifact artifact = new Artifact(); + artifact.setArtifactName("." + monitoringComponentName + "." + DcaeBeConstants.Composition.fileNames.EVENT_PROC_BP_YAML); + resourceInstance.setArtifacts(Collections.singletonList(artifact)); + resourceInstance.setResourceInstanceName(vfiName); + serviceDetailed.setResources(Collections.singletonList(resourceInstance)); + when(target.getSdcRestClient().getService(serviceUuid, requestId)).thenReturn(serviceDetailed); + } } diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/VfcmtBusinessLogicTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/VfcmtBusinessLogicTest.java index 12ed040..5f1ba41 100644 --- a/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/VfcmtBusinessLogicTest.java +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/VfcmtBusinessLogicTest.java @@ -7,10 +7,8 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.sdc.dcae.catalog.asdc.ASDCException; import org.onap.sdc.dcae.client.ISdcClient; -import org.onap.sdc.dcae.client.SdcRestClient; import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest; import org.onap.sdc.dcae.composition.restmodels.ImportVFCMTRequest; -import org.onap.sdc.dcae.composition.restmodels.MonitoringComponent; import org.onap.sdc.dcae.composition.restmodels.VfcmtData; import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact; import org.onap.sdc.dcae.composition.restmodels.sdc.ExternalReferencesMap; @@ -23,20 +21,20 @@ import org.onap.sdc.dcae.errormng.RequestError; import org.onap.sdc.dcae.errormng.ResponseFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.client.HttpClientErrorException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; import static org.onap.sdc.dcae.composition.util.DcaeBeConstants.LifecycleStateEnum.CERTIFIED; import static org.onap.sdc.dcae.composition.util.DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT; public class VfcmtBusinessLogicTest { - private ISdcClient sdcClientMock = Mockito.mock(SdcRestClient.class); + private ISdcClient sdcClientMock = Mockito.mock(ISdcClient.class); private ResourceDetailed templateMC = Mockito.mock(ResourceDetailed.class); private VfcmtBusinessLogic vfcmtBusinessLogic = new VfcmtBusinessLogic(); @@ -60,7 +58,7 @@ public class VfcmtBusinessLogicTest { } @Test - public void sdcIsDown_creatingVfcmt_gotResponseWithError500() throws Exception{ + public void sdcIsDown_creatingVfcmt_gotResponseWithError500() { RequestError requestError = new RequestError(); requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null)); when(sdcClientMock.createResource(userId,request,requestId)).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError)); @@ -77,7 +75,6 @@ public class VfcmtBusinessLogicTest { requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null)); when(sdcClientMock.createResourceArtifact(anyString(),anyString(),any(),anyString())).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError)); when(sdcClientMock.createResource(userId,request,requestId)).thenReturn(templateMC); - when(sdcClientMock.getResourceArtifact(anyString(), anyString(), anyString())).thenReturn("{\"flowType\":\"don't override\""); when(templateMC.getUuid()).thenReturn("3"); when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC); emulateListOfArtifactsWithCompositionYml(); @@ -113,7 +110,6 @@ public class VfcmtBusinessLogicTest { @Test public void successfulImportAndAttachmentOfVfcmtAlreadyConnectedWithoutEditDoCheckin() throws Exception { when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC); - when(sdcClientMock.getResourceArtifact(anyString(),anyString(),anyString())).thenReturn("{\"flowType\":\"don't override\"}"); when(templateMC.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKOUT"); emulateListOfArtifactsWithCompositionYmlAndSvcRef(); request.setCloneVFCMT(false); @@ -136,7 +132,6 @@ public class VfcmtBusinessLogicTest { when(templateMC.getUuid()).thenReturn("3"); when(sdcClientMock.changeResourceLifecycleState(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(templateMC); when(sdcClientMock.updateResourceArtifact(anyString(), anyString(), any(), anyString())).thenReturn(new Artifact()); - when(sdcClientMock.getResourceArtifact(anyString(),anyString(),anyString())).thenReturn("{\"cid\":\"xsssdaerrwr\"}"); when(templateMC.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKIN").thenReturn("NOT_CERTIFIED_CHECKOUT"); emulateListOfArtifactsWithCompositionYmlAndSvcRef(); request.setCloneVFCMT(false); @@ -154,46 +149,69 @@ public class VfcmtBusinessLogicTest { @Test - public void successfulFetchVfcmtDataFull() throws Exception { + public void invalidateMCRequestFields_returnError() { + ResponseEntity response = vfcmtBusinessLogic.importMC(userId, new ImportVFCMTRequest(), requestId); + Assert.assertEquals(response.getStatusCodeValue(), 400); + } + + @Test + public void cloneVfcmt_missingToscaFile_returnError() { + when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC); + request.setCloneVFCMT(true); + ResponseEntity response = vfcmtBusinessLogic.importMC(userId, request, requestId); + Assert.assertEquals(response.getStatusCodeValue(), 404); + } + + @Test + public void checkCatchingSdcExceptions_returnError() { + RequestError requestError = new RequestError(); + requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null)); + when(sdcClientMock.getResource(request.getTemplateUuid(), requestId)).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError)); + request.setCloneVFCMT(false); + request.setUpdateFlowType(true); + ResponseEntity response = vfcmtBusinessLogic.importMC(userId, request, requestId); + Assert.assertEquals(response.getStatusCodeValue(), 500); + } + + + @Test + public void successfulFetchVfcmtDataFull() { String templateUuid = "3"; when(templateMC.getUuid()).thenReturn(templateUuid); when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC); emulateListOfArtifactsWithCompositionYmlAndSvcRef(); when(sdcClientMock.getResourceArtifact(templateUuid, "svcRefArtifactUuid", requestId)).thenReturn("thisIsTheServiceId/resources/thisIsTheVfiName"); - when(sdcClientMock.getResourceArtifact(templateUuid, "compositionArtifactUuid", requestId)).thenReturn("\"flowType\":\"Syslog\""); ResponseEntity<VfcmtData> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId); verify(sdcClientMock).getResource(anyString(),anyString()); verify(sdcClientMock,times(2)).getResourceArtifact(anyString(),anyString(),anyString()); Assert.assertEquals(200, result.getStatusCodeValue()); - Assert.assertEquals("Syslog", result.getBody().getFlowType()); + Assert.assertEquals("don't override", result.getBody().getFlowType()); Assert.assertEquals("thisIsTheServiceId", result.getBody().getServiceUuid()); Assert.assertEquals("thisIsTheVfiName", result.getBody().getVfiName()); } @Test - public void successfulFetchVfcmtDataPartial() throws Exception { + public void successfulFetchVfcmtDataPartial() { String templateUuid = "3"; when(templateMC.getUuid()).thenReturn(templateUuid); when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC); emulateListOfArtifactsWithCompositionYml(); - when(sdcClientMock.getResourceArtifact(templateUuid, "compositionArtifactUuid", requestId)).thenReturn("\"flowType\":\"Syslog\""); ResponseEntity<VfcmtData> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId); verify(sdcClientMock).getResource(anyString(),anyString()); verify(sdcClientMock,times(1)).getResourceArtifact(anyString(),anyString(),anyString()); Assert.assertEquals(200, result.getStatusCodeValue()); - Assert.assertEquals("Syslog", result.getBody().getFlowType()); + Assert.assertEquals("don't override", result.getBody().getFlowType()); Assert.assertEquals(null, result.getBody().getServiceUuid()); Assert.assertEquals(null, result.getBody().getVfiName()); } @Test - public void successfulFetchVfcmtDataEmpty() throws Exception { + public void successfulFetchVfcmtDataEmpty() { String templateUuid = "3"; when(templateMC.getUuid()).thenReturn(templateUuid); when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC); - emulateListOfArtifactsWithCompositionYml(); - when(sdcClientMock.getResourceArtifact(templateUuid, "compositionArtifactUuid", requestId)).thenReturn(""); + emulateCdumpArtifactWithoutFlowtype(); ResponseEntity<VfcmtData> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId); verify(sdcClientMock).getResource(anyString(),anyString()); verify(sdcClientMock,times(1)).getResourceArtifact(anyString(),anyString(),anyString()); @@ -204,7 +222,7 @@ public class VfcmtBusinessLogicTest { } @Test - public void fetchVfcmtDataNoCompositionFound() throws Exception { + public void fetchVfcmtDataNoCompositionFound() { String templateUuid = "3"; when(templateMC.getUuid()).thenReturn(templateUuid); @@ -219,7 +237,7 @@ public class VfcmtBusinessLogicTest { } @Test - public void getVfcmtsForMigration() throws Exception { + public void getVfcmtsForMigration() { ExternalReferencesMap connectedVfcmts = new ExternalReferencesMap(); connectedVfcmts.put("11",Arrays.asList("Red", "Blue", "Yellow")); connectedVfcmts.put("22",Arrays.asList("Ibiza", "Bora Bora", "Mykonos")); @@ -281,6 +299,17 @@ public class VfcmtBusinessLogicTest { when(templateMC.getArtifacts()).thenReturn(listOfArtifactCompositionYml); } + + private void emulateCdumpArtifactWithoutFlowtype() { + List<Artifact> listOfArtifactCompositionYml = new ArrayList<>(); + Artifact compositionArtifact = Mockito.mock(Artifact.class); + when(compositionArtifact.getArtifactName()).thenReturn(DcaeBeConstants.Composition.fileNames.COMPOSITION_YML); + when(compositionArtifact.getArtifactUUID()).thenReturn("compositionArtifactUuid"); + when(compositionArtifact.getPayloadData()).thenReturn("{\"cid\":\"xsssdaerrwr\"}\""); + listOfArtifactCompositionYml.add(compositionArtifact); + when(templateMC.getArtifacts()).thenReturn(listOfArtifactCompositionYml); + } + private void emulateListOfArtifactsWithCompositionYmlAndSvcRef() { List<Artifact> listOfArtifactCompositionYml = new ArrayList<>(); Artifact compositionArtifact = Mockito.mock(Artifact.class); @@ -296,7 +325,7 @@ public class VfcmtBusinessLogicTest { } @Test - public void uiHasABug_creatingVfcmtWithBadRequestNoServiceUuid_gotResponseWithError400() throws Exception{ + public void uiHasABug_creatingVfcmtWithBadRequestNoServiceUuid_gotResponseWithError400() { RequestError requestError = new RequestError(); requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null)); when(sdcClientMock.createResource(userId,request,requestId)).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError)); diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/filter/LoggingFilterTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/filter/LoggingFilterTest.java new file mode 100644 index 0000000..cfe9f56 --- /dev/null +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/filter/LoggingFilterTest.java @@ -0,0 +1,119 @@ +package org.onap.sdc.dcae.filter; + +import org.apache.http.impl.EnglishReasonPhraseCatalog; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.sdc.common.onaplog.Enums.LogLevel; +import org.onap.sdc.common.onaplog.Enums.OnapLoggerErrorCode; +import org.onap.sdc.common.onaplog.OnapMDCWrapper; + + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; +import java.util.Enumeration; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class LoggingFilterTest { + @Mock private HttpServletRequest request; + @Mock private HttpServletResponse httpResponse; + @Mock private ServletResponse response; + @Mock private FilterChain filterChain; + + private FilterConfig filterConfig = new FilterConfig() { + @Override + public String getFilterName() { + return null; + } + + @Override + public ServletContext getServletContext() { + return null; + } + + @Override + public String getInitParameter(String name) { + return null; + } + + @Override + public Enumeration<String> getInitParameterNames() { + return null; + } + }; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void loggingFilterInstanciationLifeCycle() { + LoggingFilter loggingFilter = new LoggingFilter(); + + loggingFilter.init(filterConfig); + + loggingFilter.destroy(); + } + + @Test + public void doFilter_healthCheck_noReadingHeader(){ + LoggingFilter loggingFilter = new LoggingFilter(); + boolean exceptionThrown = false; + + when(request.getServletPath()).thenReturn("/healthCheck"); + try { + loggingFilter.doFilter(request, response, filterChain); + } + catch (Exception e){ + exceptionThrown = true; + } + verify(request,never()).getHeader("X-ECOMP-RequestID"); + assertEquals(exceptionThrown, false); + } + + @Test + public void doFilter_notHealthCheck_noReadingHeader(){ + LoggingFilter loggingFilter = new LoggingFilter(); + boolean exceptionThrown = false; + + when(request.getServletPath()).thenReturn("/notHealthCheck"); + try { + loggingFilter.doFilter(request, response, filterChain); + } + catch (Exception e){ + exceptionThrown = true; + } + verify(request).getHeader("X-ECOMP-RequestID"); + assertEquals(exceptionThrown, false); + } + @Test + public void doFilter_InternalServerError(){ + LoggingFilter loggingFilter = new LoggingFilter(); + boolean exceptionThrown = false; + + when(request.getServletPath()).thenReturn("/notHealthCheck"); + try { + + when(httpResponse.getStatus()).thenReturn(500); + when(request.getHeader("user-agent")).thenReturn("test"); + + loggingFilter.doFilter(request, httpResponse, filterChain); + } + catch (Exception e){ + exceptionThrown = true; + } + verify(request).getHeader("X-ECOMP-RequestID"); + assertEquals(exceptionThrown, false); + } + + +} diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/impl/RulesBusinessLogicTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/impl/RulesBusinessLogicTest.java index d3ae600..bc03632 100644 --- a/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/impl/RulesBusinessLogicTest.java +++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/impl/RulesBusinessLogicTest.java @@ -201,6 +201,17 @@ public class RulesBusinessLogicTest { } @Test + public void missingLogTextFailureTest() { + Rule rule = new Rule(); + rule.setDescription("description"); + rule.setActions(new ArrayList<>()); + rule.getActions().add(buildLogTextMissingTextAction()); + List<ServiceException> errors = rulesBusinessLogic.validateRule(rule); + String expectedError = "Please fill the text field of Log Text action to "; + assertEquals(expectedError, errors.get(0).getFormattedErrorMessage()); + } + + @Test public void reorderMappingRulesCircularDependencyFailureTest() { MappingRules mr = new MappingRules(buildRuleWithMultipleCopyActions()); @@ -266,24 +277,31 @@ public class RulesBusinessLogicTest { return rule; } - private BaseAction buildCopyAction(String from, String to) { - BaseAction action = new BaseAction(); + private BaseCopyAction buildCopyAction(String from, String to) { + BaseCopyAction action = new BaseCopyAction(); action.setActionType("copy"); action.setFrom(from); action.setTarget(to); return action; } - private BaseAction buildConcatAction(List<String> from, String to) { - BaseAction action = new BaseAction(); + private LogTextAction buildLogTextMissingTextAction(){ + LogTextAction logTextAction = new LogTextAction(); + logTextAction.setActionType("Log Text"); + logTextAction.setLogText("a name", "a level", ""); + return logTextAction; + } + + private BaseCopyAction buildConcatAction(List<String> from, String to) { + BaseCopyAction action = new BaseCopyAction(); action.setActionType("concat"); action.setFrom(from); action.setTarget(to); return action; } - private BaseAction buildRegexAction(String from, String to, String regex) { - BaseAction action = new BaseAction(); + private BaseCopyAction buildRegexAction(String from, String to, String regex) { + BaseCopyAction action = new BaseCopyAction(); action.setActionType("copy"); action.setFrom(from, regex); action.setTarget(to); |