Merge "coverage SdncAaiDmaapConsumer"
authorDan Timoney <dt5972@att.com>
Wed, 28 Mar 2018 14:27:23 +0000 (14:27 +0000)
committerGerrit Code Review <gerrit@onap.org>
Wed, 28 Mar 2018 14:27:23 +0000 (14:27 +0000)
1  2 
dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncPserverDmaapReceiver.java

@@@ -9,10 -9,16 +9,17 @@@
  package org.onap.ccsdk.sli.northbound.dmaapclient;
  
  import static org.junit.Assert.assertTrue;
 +import static org.junit.Assert.assertEquals;
  
+ import java.io.File;
+ import java.io.IOException;
+ import java.io.InputStream;
+ import java.lang.reflect.Field;
+ import java.util.Map;
  import java.util.Properties;
  
+ import org.apache.commons.io.FileUtils;
  import org.junit.Before;
  import org.junit.Test;
  
@@@ -79,21 -82,94 +86,104 @@@ public class TestSdncPserverDmaapReceiv
                Properties props = new Properties();
   
            String rpcMsgbody = new SdncAaiDmaapConsumer(props).publish("src/main/resources/template-pserver.vt", aaiInput);
 +        
 +          ObjectMapper oMapper = new ObjectMapper();
 +        JsonNode aaiRootNode;
 +        try {
 +            aaiRootNode = oMapper.readTree(rpcMsgbody);
 +        } catch (Exception e) {
 +            throw new InvalidMessageException("Cannot parse json object", e);
 +        }       
  
 -        assertTrue(rpcMsgbody.indexOf("input") != -1); 
 -        assertTrue(rpcMsgbody.indexOf("payload") != -1); 
 -        assertTrue(rpcMsgbody.indexOf("common-header") != -1);
 +        assertTrue(aaiRootNode.get("input").get("payload") != null); 
 +        assertTrue(aaiRootNode.get("input").get("common-header") != null); 
 +        
 +        assertEquals(aaiRootNode.get("input").get("action-identifiers").get("action-name").textValue(), "dmaap-notification");
 +        assertEquals(aaiRootNode.get("input").get("action-identifiers").get("mode").textValue(), "async");
        }
  
+       @Test
+       public void testProcessMsgFieldMap() throws Exception {
+               String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
+               File directory = new File("lib");
+               if (! directory.exists()){
+                       directory.mkdir();
+               }
+               File source = new File("src/main/resources");
+               File dest = new File("lib/");
+               try {
+                       FileUtils.copyDirectory(source, dest);
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+               try {
+                       Map<String, String> env = System.getenv();
+                       Class<?> cl = env.getClass();
+                       Field field = cl.getDeclaredField("m");
+                       field.setAccessible(true);
+                       Map<String, String> writableEnv = (Map<String, String>) field.get(env);
+                       writableEnv.put(DMAAPLISTENERROOT, ".");
+               } catch (Exception e) {
+                       throw new IllegalStateException("Failed to set environment variable", e);
+               }
+               Properties props = new Properties();
+               SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
+               InputStream propStr = TestSdncPserverDmaapReceiver.class.getResourceAsStream("/dmaap-consumer-pserver.properties");
+               props.load(propStr);
+               consumer.init(props, "src/test/resources/dmaap-consumer-pserver.properties");
+               consumer.processMsg(aaiInput);
+       }
+       @Test(expected = InvalidMessageException.class)
+       public void testProcessMsgNullMessage() throws Exception {
+               Properties props = new Properties();
+               SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
+               consumer.processMsg(null);
+       }
+       @Test(expected = InvalidMessageException.class)
+       public void testProcessMsgInvalidMessage() throws Exception {
+               Properties props = new Properties();
+               SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
+               consumer.processMsg("test");
+       }
+       @Test
+       public void testProcessMsgMissingEventHeader() throws Exception {
+               Properties props = new Properties();
+               SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
+               consumer.processMsg("{\n" +
+                               "    \"input\" : {        \n" +
+                               "    }\n" +
+                               "}");
+       }
+       @Test
+       public void testProcessMsgInvalidEventType() throws Exception {
+               Properties props = new Properties();
+               String msg = "{\"cambria.partition\": \"AAI\",\r\n" +
+                               "    \"event-header\": {\"event-type\": \"TEST-EVENT\"}}";
+               SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
+               consumer.processMsg(msg);
+       }
  
  }