Improve SONAR coverage in aai/data-router 57/10357/1
authorBansal, Nitin (nb121v) <nitin.bansal@amdocs.com>
Tue, 5 Sep 2017 14:31:47 +0000 (10:31 -0400)
committerBansal, Nitin (nb121v) <nitin.bansal@amdocs.com>
Tue, 5 Sep 2017 14:33:47 +0000 (10:33 -0400)
Change-Id: I568a2eda367e29dbf013881bdf03aeef3312e319
Issue-ID: AAI-214
Signed-off-by: Bansal, Nitin (nb121v) <nitin.bansal@amdocs.com>
pom.xml
src/main/java/org/openecomp/datarouter/policy/EntityEventPolicy.java
src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyStubbed.java [new file with mode: 0644]
src/test/java/org/openecomp/datarouter/policy/EntityEventPolicyTest.java [new file with mode: 0644]
src/test/java/org/openecomp/datarouter/policy/InMemorySearchDatastore.java [new file with mode: 0644]
src/test/resources/aai_event.json [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index e674983..08dae9d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <testEnv>DEV</testEnv>
         <checkstyle.config.location>google_checks.xml</checkstyle.config.location>
         <nexusproxy>https://nexus.onap.org</nexusproxy>
+                      <!-- Sonar Properties -->
+              <sonar.language>java</sonar.language>
+              <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
+              <sonar.surefire.reportsPath>${project.build.directory}/surefire-reports</sonar.surefire.reportsPath>
+              <sonar.jacoco.reportPath>${project.build.directory}/code-coverage/jacoco-ut.exec</sonar.jacoco.reportPath>
+              <sonar.jacoco.reportMissing.force.zero>false</sonar.jacoco.reportMissing.force.zero>
+              <sonar.projectVersion>${project.version}</sonar.projectVersion>
     </properties>
 
     <dependencies>
-
+<dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-module-junit4</artifactId>
+                       <version>1.6.2</version>
+                       <scope>test</scope>
+               </dependency>
+               
+               <dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-api-mockito</artifactId>
+                       <version>1.6.2</version>
+                       <scope>test</scope>
+               </dependency>
+               
+               <dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-module-javaagent</artifactId>
+                       <version>1.6.2</version>
+                       <scope>test</scope>
+               </dependency>
+               
+               <dependency>
+                       <groupId>org.powermock</groupId>
+                       <artifactId>powermock-module-junit4-rule-agent</artifactId>
+                       <version>1.6.2</version>
+                       <scope>test</scope>
+               </dependency>   
         <dependency>
             <groupId>dom4j</groupId>
             <artifactId>dom4j</artifactId>
                     </execution>
                 </executions>
             </plugin>
+            
         </plugins>
     </build>
 
                             </dependency>
                         </dependencies>
                     </plugin>
+                   
                 </plugins>
             </build>
         </profile>
index 290c47f..d6107ec 100644 (file)
@@ -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 (file)
index 0000000..cdc0fad
--- /dev/null
@@ -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 (file)
index 0000000..0d97e1d
--- /dev/null
@@ -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 (file)
index 0000000..4b25f6e
--- /dev/null
@@ -0,0 +1,24 @@
+package org.openecomp.datarouter.policy;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+public final class InMemorySearchDatastore {
+
+       private final static ConcurrentHashMap<String, String> documents = new ConcurrentHashMap<String, String>();
+
+       public static ConcurrentHashMap<String, String> 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 (file)
index 0000000..dcbad5a
--- /dev/null
@@ -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