restapicallnode fix
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / test / java / org / onap / ccsdk / sli / plugins / restapicall / TestRestapiCallNode.java
index 0704d29..a993bb9 100755 (executable)
 package org.onap.ccsdk.sli.plugins.restapicall;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.HashMap;
 import java.util.Map;
-
 import org.codehaus.jettison.json.JSONObject;
-import org.glassfish.grizzly.http.server.HttpServer;
-import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
-import org.glassfish.jersey.media.multipart.MultiPartFeature;
-import org.glassfish.jersey.server.ResourceConfig;
-
+import org.junit.Before;
 import org.junit.Test;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
-import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -46,6 +42,11 @@ public class TestRestapiCallNode {
     @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(TestRestapiCallNode.class);
 
+    @Before
+    public void init() {
+        System.setProperty("SDNC_CONFIG_DIR", "src/test/resources");
+    }
+
     @Test
     public void testDelete() throws SvcLogicException {
         SvcLogicContext ctx = new SvcLogicContext();
@@ -79,6 +80,21 @@ public class TestRestapiCallNode {
         rcn.sendRequest(p, ctx);
     }
 
+    @Test
+    public void testSendFile() throws SvcLogicException {
+        SvcLogicContext ctx = new SvcLogicContext();
+
+        Map<String, String> p = new HashMap<>();
+        p.put("fileName", "src/test/resources/test_file.txt");
+        p.put("url", "https://testurl.test");
+        p.put("user", "user");
+        p.put("password", "*******");
+        p.put("skipSending", "true"); // Set real url, user, password, when testing actual sending
+
+        RestapiCallNode rcn = new RestapiCallNode();
+        rcn.sendFile(p, ctx);
+    }
+
     @Test
     public void testJsonTemplate() throws SvcLogicException {
         SvcLogicContext ctx = new SvcLogicContext();
@@ -476,18 +492,18 @@ public class TestRestapiCallNode {
         assertEquals(partnerTwoUsername,details.username);
         assertEquals(partnerTwoPassword,details.password);
         assertNull(rcn.partnerStore.get("partnerThree"));
-        
+
         //In this scenario the caller expects username, password and url to be picked up from the partners json
-        Map<String, String> paramMap = new HashMap<String,String>();
+        Map<String, String> paramMap = new HashMap<>();
         paramMap.put("partner", partnerTwoKey);
        rcn.handlePartner(paramMap );
-        assertEquals(partnerTwoUsername,paramMap.get(rcn.restapiUserKey));
-        assertEquals(partnerTwoPassword,paramMap.get(rcn.restapiPasswordKey));
-        assertEquals("http://localhost:7002",paramMap.get(rcn.restapiUrlString));
+        assertEquals(partnerTwoUsername,paramMap.get(RestapiCallNode.restapiUserKey));
+        assertEquals(partnerTwoPassword,paramMap.get(RestapiCallNode.restapiPasswordKey));
+        assertEquals("http://localhost:7002",paramMap.get(RestapiCallNode.restapiUrlString));
 
         //In this scenario the caller expects username, password and url to be picked up from the partners json
         //the provided suffix will be appended to the default url from the partners json
-        paramMap = new HashMap<String,String>();
+        paramMap = new HashMap<>();
         paramMap.put("partner", partnerTwoKey);
         paramMap.put("restapiUrlSuffix", "/networking/v1/instance/3");
        rcn.handlePartner(paramMap);
@@ -497,7 +513,7 @@ public class TestRestapiCallNode {
         assertEquals(partnerTwoPassword,p.restapiPassword);
         assertEquals("http://localhost:7002/networking/v1/instance/3",p.restapiUrl);
     }
-    
+
     @Test
     public void retryPolicyBean() throws Exception {
        Integer retries = 3;
@@ -527,7 +543,62 @@ public class TestRestapiCallNode {
         RestapiCallNode rcn = new RestapiCallNode();
         String request = rcn.buildXmlJsonRequest(ctx, rcn.readFile("src/test/resources/testEmbeddedTemplate.json"), Format.JSON);
         //This will throw a JSONException and fail the test case if rest api call node doesn't form valid JSON
-        JSONObject requestObj = new JSONObject(request);
+        assertNotNull(new JSONObject(request));
+    }
+
+    @Test
+    public void testMultiLineEmbeddedJsonTemplate() throws Exception {
+        SvcLogicContext ctx = new SvcLogicContext();
+        String complexObj = "{\n"
+                            + "  \"image_name\": \"Ubuntu 14.04\",\n"
+                            + "  \"service-instance-id\": \"1\",\n"
+                            + "  \"vnf-model-customization-uuid\": \"2f\",\n"
+                            + "  \"vnf-id\": \"3b\"\n"
+                            + "}";
+        ctx.setAttribute("reqId", "1235");
+        ctx.setAttribute("subReqId", "054243");
+        ctx.setAttribute("actionName", "CREATE");
+        ctx.setAttribute("myPrefix", "2016-09-09 16:30:35.0");
+        ctx.setAttribute("complexObj", complexObj);
+        RestapiCallNode rcn = new RestapiCallNode();
+        String request = rcn.buildXmlJsonRequest(ctx, rcn.readFile("src/test/resources/testMultiLineEmbeddedTemplate.json"), Format.JSON);
+        //This will throw a JSONException and fail the test case if rest api call node doesn't form valid JSON
+        assertNotNull(new JSONObject(request));
+    }
+    
+    @Test
+    public void testGetMultipleUrls() throws Exception{
+       String[] urls =  RestapiCallNode.getMultipleUrls("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana,https://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Potato");
+       assertEquals("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana",urls[0]);
+       assertEquals("https://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Potato",urls[1]);
+
+       urls =  RestapiCallNode.getMultipleUrls("https://wiki.onap.org/,http://localhost:7001/,http://wiki.onap.org/");
+       assertEquals("https://wiki.onap.org/",urls[0]);
+       assertEquals("http://localhost:7001/",urls[1]);
+       assertEquals("http://wiki.onap.org/",urls[2]);
+       
+       urls =  RestapiCallNode.getMultipleUrls("https://wiki.onap.org/test=4,5,6,http://localhost:7001/test=1,2,3,http://wiki.onap.org/test=7,8,9,10");
+       assertEquals("https://wiki.onap.org/test=4,5,6",urls[0]);
+       assertEquals("http://localhost:7001/test=1,2,3",urls[1]);
+       assertEquals("http://wiki.onap.org/test=7,8,9,10",urls[2]);
+
+       urls =  RestapiCallNode.getMultipleUrls("https://wiki.onap.org/,https://readthedocs.org/projects/onap/");
+       assertEquals("https://wiki.onap.org/",urls[0]);
+       assertEquals("https://readthedocs.org/projects/onap/",urls[1]);
+    }
+    
+    @Test
+    public void testContainsMultipleUrls() throws Exception{
+        assertFalse(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/"));
+        assertFalse(RestapiCallNode.containsMultipleUrls("http://wiki.onap.org/"));
+        assertFalse(RestapiCallNode.containsMultipleUrls("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana"));
+        assertFalse(RestapiCallNode.containsMultipleUrls("https://localhost:8008/params=1,2,3,4,5,6"));
+
+        assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/,https://readthedocs.org/projects/onap/"));
+        assertTrue(RestapiCallNode.containsMultipleUrls("http://localhost:7001/,http://localhost:7002"));
+        assertTrue(RestapiCallNode.containsMultipleUrls("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana,https://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Potato"));
+        assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/,http://localhost:7001/,http://wiki.onap.org/"));
+        assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/test=4,5,6,http://localhost:7001/test=1,2,3,http://wiki.onap.org/test=7,8,9,10"));
     }
 
 }