From: Bansal, Nitin (nb121v) Date: Tue, 5 Sep 2017 14:31:47 +0000 (-0400) Subject: Improve SONAR coverage in aai/data-router X-Git-Tag: v1.1.0~17 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fdata-router.git;a=commitdiff_plain;h=1e6752c966b48eb2351416691c96f9858c54f978 Improve SONAR coverage in aai/data-router Change-Id: I568a2eda367e29dbf013881bdf03aeef3312e319 Issue-ID: AAI-214 Signed-off-by: Bansal, Nitin (nb121v) --- diff --git a/pom.xml b/pom.xml index e674983..08dae9d 100644 --- a/pom.xml +++ b/pom.xml @@ -41,10 +41,43 @@ DEV google_checks.xml https://nexus.onap.org + + java + jacoco + ${project.build.directory}/surefire-reports + ${project.build.directory}/code-coverage/jacoco-ut.exec + false + ${project.version} - + + org.powermock + powermock-module-junit4 + 1.6.2 + test + + + + org.powermock + powermock-api-mockito + 1.6.2 + test + + + + org.powermock + powermock-module-javaagent + 1.6.2 + test + + + + org.powermock + powermock-module-junit4-rule-agent + 1.6.2 + test + dom4j dom4j @@ -239,6 +272,7 @@ + @@ -353,6 +387,7 @@ + diff --git a/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java b/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java index 290c47f..d6107ec 100644 --- a/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java +++ b/src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java @@ -955,7 +955,7 @@ public class EntityEventPolicy implements Processor { * @param target Resource to perform the operation on * @param allowDeleteEvent Allow delete operation to be performed on resource */ - private void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { try { diff --git a/src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyStubbed.java b/src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyStubbed.java new file mode 100644 index 0000000..cdc0fad --- /dev/null +++ b/src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyStubbed.java @@ -0,0 +1,34 @@ +package org.openecomp.datarouter.policy; + +import java.io.FileNotFoundException; + +import org.openecomp.datarouter.entity.DocumentStoreDataEntity; + +public class EntityEventPolicyStubbed extends EntityEventPolicy { + + + public EntityEventPolicyStubbed(EntityEventPolicyConfig config) throws FileNotFoundException { + super(config); + + } + + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { + //Stub out the actual call to Search Data service and instead store/update documents in memory + try { + switch (action.toLowerCase()) { + case "create": + InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 + break; + case "update": + InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 + break; + case "delete": + InMemorySearchDatastore.remove(eventEntity.getId()); // they are executed if variable == c1 + break; + default: + break; + } + } catch (Exception ex) { + } + } +} diff --git a/src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyTest.java b/src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyTest.java new file mode 100644 index 0000000..0d97e1d --- /dev/null +++ b/src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyTest.java @@ -0,0 +1,79 @@ +package org.openecomp.datarouter.policy; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; + +import java.io.File; +import java.io.FileInputStream; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.Test; +import org.openecomp.datarouter.util.NodeUtils; +import org.openecomp.datarouter.util.SearchServiceAgent; +import org.powermock.api.mockito.PowerMockito; + + + +public class EntityEventPolicyTest { + EntityEventPolicy policy; + String eventJson; + + @SuppressWarnings("unchecked") + @Before + public void init() throws Exception { + EntityEventPolicyConfig config = PowerMockito.mock(EntityEventPolicyConfig.class); + PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password"); + PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT"); + + + SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class); + + PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments().thenReturn(searchServiceAgent); + + + policy = new EntityEventPolicyStubbed(config); + FileInputStream event = new FileInputStream( new File("src/test/resources/aai_event.json")); + eventJson = IOUtils.toString(event, "UTF-8"); + + } + + @Test + public void testProcess() throws Exception { + policy.process(getExchangeEvent("event1","create")); + policy.process(getExchangeEvent("event2","create")); + + assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event1"))); + assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2"))); + + policy.process(getExchangeEvent("event1","update")); + policy.process(getExchangeEvent("event2","update")); + assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event1"))); + assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2"))); + + policy.process(getExchangeEvent("event2","delete")); + assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2"))); + } + + private Exchange getExchangeEvent(String link,String action){ + Object obj = eventJson.replace("$LINK",link ).replace("$ACTION",action) ; + Exchange exchange = PowerMockito.mock(Exchange.class); + Message inMessage = PowerMockito.mock(Message.class); + Message outMessage = PowerMockito.mock(Message.class); + PowerMockito.when(exchange.getIn()).thenReturn(inMessage); + PowerMockito.when(inMessage.getBody()).thenReturn(obj); + + PowerMockito.when(exchange.getOut()).thenReturn(outMessage); + PowerMockito.doNothing().when(outMessage).setBody(anyObject()); + PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject()); + + return exchange; + + } + + + +} diff --git a/src/test/java/org/openecomp/datarouter/policy/InMemorySearchDatastore.java b/src/test/java/org/openecomp/datarouter/policy/InMemorySearchDatastore.java new file mode 100644 index 0000000..4b25f6e --- /dev/null +++ b/src/test/java/org/openecomp/datarouter/policy/InMemorySearchDatastore.java @@ -0,0 +1,24 @@ +package org.openecomp.datarouter.policy; + +import java.util.concurrent.ConcurrentHashMap; + +public final class InMemorySearchDatastore { + + private final static ConcurrentHashMap documents = new ConcurrentHashMap(); + + public static ConcurrentHashMap getAll() { + return documents; + } + + public static void put(String key, String value) { + documents.put(key, value); + } + + public static String get(String key) { + return documents.get(key); + } + + public static void remove(String key) { + documents.remove(key); + } +} diff --git a/src/test/resources/aai_event.json b/src/test/resources/aai_event.json new file mode 100644 index 0000000..dcbad5a --- /dev/null +++ b/src/test/resources/aai_event.json @@ -0,0 +1,90 @@ +{ + "cambria.partition":"AAI", + "event-header":{ + "id":"20160525111931-22f454f0-93ff-4571-bed0-573ca6c85353", + "timestamp":"20160525-11:19:31:421", + "source-name":"RO", + "domain":"JUNIT", + "sequence-number":"0", + "severity":"NORMAL", + "event-type":"AAI-EVENT", + "version":"v9", + "action":"$ACTION", + "entity-type":"generic-vnf", + "top-entity-type":"generic-vnf", + "entity-link":"$LINK" + }, + "entity":{ + "vnf-id":"davesFix301", + "vnf-name":"davesFix301", + "vnf-name2":"davesFix302", + "nf-role":"davesFixNfRole301", + "vnf-type":"example-vnf-type-val-35010", + "regional-resource-zone":"example-regional-resource-zone-val-3501", + "prov-status":"davesFixProvStatus30", + "operational-state":"example-operational-state-val-3501", + "license-key":"example-license-key-val-3501", + "equipment-role":"example-equipment-role-val-3501", + "orchestration-status":"davesFixOrchestrationStatus30", + "heat-stack-id":"example-heat-stack-id-val-3501", + "mso-catalog-key":"example-mso-catalog-key-val-3501", + "management-option":"example-management-option-val-3501", + "ipv4-oam-address":"example-ipv4-oam-address-val-3501", + "ipv4-loopback0-address":"example-ipv4-loopback0-address-val-3501", + "nm-lan-v6-address":"example-nm-lan-v6-address-val-3501", + "management-v6-address":"example-management-v6-address-val-3501", + "vcpu":1817, + "vcpu-units":"example-vcpu-units-val-3501", + "vmemory":6020, + "vmemory-units":"example-vmemory-units-val-3501", + "vdisk":5281, + "vdisk-units":"example-vdisk-units-val-3501", + "in-maint":false, + "is-closed-loop-disabled":true, + "resource-version":"1464189571", + "relationship-list":{ + "relationship":[ + { + "related-to":"logical-link", + "related-link":"https://loopback.att.com:8443/aai/v7/network/logical-links/logical-link/link-01/", + "relationship-data":[ + { + "relationship-key":"logical-link.link-name", + "relationship-value":"link-01" + } + ] + } + ] + }, + "l-interfaces":{ + + }, + "lag-interfaces":{ + "lag-interface":[ + { + "interface-name":"example-interface-name-val-3130", + "resource-version":"1464189571", + "relationship-list":{ + + }, + "l-interfaces":{ + + } + }, + { + "interface-name":"example-interface-name-val-3131", + "resource-version":"1464189571", + "relationship-list":{ + + }, + "l-interfaces":{ + + } + } + ] + }, + "vf-modules":{ + + } + } +} \ No newline at end of file