diff options
Diffstat (limited to 'dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogicTest.java')
-rw-r--r-- | dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogicTest.java | 131 |
1 files changed, 66 insertions, 65 deletions
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 index d421113..3c6d1dd 100644 --- 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 @@ -1,31 +1,37 @@ package org.onap.sdc.dcae.composition.impl; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +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.CompositionConfig; -import org.onap.sdc.dcae.composition.restmodels.ruleeditor.*; -import org.onap.sdc.dcae.composition.restmodels.sdc.*; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.MappingRules; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.Rule; +import org.onap.sdc.dcae.composition.restmodels.ruleeditor.SchemaInfo; +import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact; +import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed; 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.ResponseFormat; +import org.onap.sdc.dcae.errormng.ServiceException; import org.onap.sdc.dcae.rule.editor.impl.RulesBusinessLogic; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.testng.Assert; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.*; 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 justAString = "aStringForAllSeasons"; private String userId = "gc786h"; private String vfcmtUuid = "26e8d4b5-f087-4821-a75a-0b9514b5a7ab"; private String dcaeCompLabel = "dMp.DockerMap"; @@ -34,12 +40,10 @@ public class RuleEditorBusinessLogicTest { 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'}}}"; @@ -47,9 +51,7 @@ public class RuleEditorBusinessLogicTest { 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(); @@ -67,11 +69,9 @@ public class RuleEditorBusinessLogicTest { when(vfcmt.getResourceType()).thenReturn(resourceType); when(vfcmt.getCategory()).thenReturn(categoryName); - when(ruleEditorBusinessLogic.getSdcRestClient().getResource(anyString(), anyString())).thenReturn(vfcmt); + when(sdcClientMock.getResource(anyString(), anyString())).thenReturn(vfcmt); when(schemaInfo.getVersion()).thenReturn("0.2"); - /* PowerMockito.doReturn(vs).when(VesStructureLoader.class); - when(vs.getEventListenerDefinitionByVersion(anyString())).thenReturn(null);*/ } @Test @@ -79,29 +79,55 @@ public class RuleEditorBusinessLogicTest { 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); + when(sdcClientMock.getResourceArtifact(resourceUuid, artifactUuid, requestId)).thenReturn(defaultPayload); + when(rulesBusinessLogic.addOrEditRule(any(MappingRules.class), any(Rule.class), anyBoolean())).thenReturn(true); + when(rulesBusinessLogic.validateGroupDefinitions(any(MappingRules.class), anyBoolean())).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)); + assertTrue(result.getBody().toString().contains("6e0175a0-581f-11e8-82eb-53bb060b790a")); + verify(rulesBusinessLogic,times(1)).addOrEditRule(any(MappingRules.class), any(Rule.class), anyBoolean()); } + @Test + public void test_exportRules_resourceNotFound() throws Exception { + RequestError requestError = new RequestError(); + requestError.setServiceException(new ServiceException("SVC4063", "", null)); + when(sdcClientMock.getResource(resourceUuid, requestId)).thenThrow(new ASDCException(HttpStatus.NOT_FOUND, requestError)); + + ResponseEntity result = ruleEditorBusinessLogic.downloadRules(vfcmtUuid, dcaeCompLabel, nId, configParam, requestId); + assertEquals(404,result.getStatusCodeValue()); + } + + + @Test + public void invalidRuleFormatImportRulesFailureTest() throws Exception { + String invalidInput = "{rules:[blahblahblah]}"; + String expectedError = "Error - Rule format is invalid: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 9 path $.rules[0]."; + ResponseEntity<ResponseFormat> result = ruleEditorBusinessLogic.importRules(invalidInput, requestId, userId, vfcmtUuid, dcaeCompLabel, nId, configParam); + assertEquals(400,result.getStatusCodeValue()); + assertEquals(expectedError, result.getBody().getRequestError().getServiceException().getFormattedErrorMessage()); + verify(rulesBusinessLogic, times(0)).validateImportedRules(any()); + verify(sdcClientMock, times(0)).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()); + } + @Test public void test_saveRules_artifactNotFound() throws Exception { emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, false); - when(rulesBusinessLogic.addOrEditRule(any(MappingRules.class), any(Rule.class))).thenReturn(true); + when(rulesBusinessLogic.addOrEditRule(any(MappingRules.class), any(Rule.class), anyBoolean())).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)); + assertTrue(result.getBody().toString().contains("6e0175a0-581f-11e8-82eb-53bb060b790a")); + verify(rulesBusinessLogic,times(0)).addOrEditRule(any(MappingRules.class), any(Rule.class), anyBoolean()); } @@ -116,7 +142,7 @@ public class RuleEditorBusinessLogicTest { 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)); + verify(rulesBusinessLogic,times(0)).addOrEditRule(any(MappingRules.class), any(Rule.class), anyBoolean()); } @@ -125,12 +151,9 @@ public class RuleEditorBusinessLogicTest { emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); - when(ruleEditorBusinessLogic.getSdcRestClient().getResourceArtifact(resourceUuid, artifactUuid, requestId)) - .thenReturn(defaultPayload); - - ResponseEntity result = ruleEditorBusinessLogic.getRules(vfcmtUuid, dcaeCompLabel, nId, configParam, requestId); + ResponseEntity result = ruleEditorBusinessLogic.getRulesAndSchema(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'")); + assertTrue(result.getBody().toString().contains("eventType:syslogFields,version:4.1,rules:{'test':{'version':'4.1'")); } @@ -147,33 +170,13 @@ public class RuleEditorBusinessLogicTest { } - @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(sdcClientMock.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); @@ -181,18 +184,16 @@ public class RuleEditorBusinessLogicTest { } - @Test - public void test_getDefinition(){ - + @Test + public void test_deleteGroupOfRules() throws Exception { -/* - PowerMockito.mockStatic(VesStructureLoader.class); - when(VesStructureLoader.getEventListenerDefinitionByVersion(anyString())).thenReturn(null); -*/ + emulateMockListOfArtifacts(dcaeCompLabel, nId, configParam, true); + when(sdcClientMock.getResourceArtifact(resourceUuid, artifactUuid, requestId)).thenReturn(defaultPayload); + when(rulesBusinessLogic.deleteGroupOfRules(any(MappingRules.class), anyString())).thenReturn(Collections.singletonList(new Rule())); + ResponseEntity result = ruleEditorBusinessLogic.deleteGroupOfRules(userId, vfcmtUuid, dcaeCompLabel, nId, configParam, ruleUuid, requestId); + assertEquals(200,result.getStatusCodeValue()); + } - ResponseEntity res = ruleEditorBusinessLogic.getDefinition("4.1","syslogFields"); - - } private void emulateMockListOfArtifacts(String dcaeCompLabel, String nid, String configParam, boolean isApprovedArtifact) { List<Artifact> listOfArtifactCompositionYml = new ArrayList<>(); @@ -208,10 +209,10 @@ public class RuleEditorBusinessLogicTest { when(artifact.getArtifactType()).thenReturn("DCAE_TOSCA"); when(artifact.getArtifactUUID()).thenReturn(artifactUuid); when(artifact.getArtifactDescription()).thenReturn("createmapArtifact"); + when(artifact.getPayloadData()).thenReturn(defaultPayload); listOfArtifactCompositionYml.add(artifact); when(vfcmt.getArtifacts()).thenReturn(listOfArtifactCompositionYml); } - } |