Restapicallnode with cookie based auth
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / test / java / jtest / org / onap / ccsdk / sli / plugins / restapicall / TestRestapiCallNode.java
index fd982d7..52da461 100644 (file)
 
 package jtest.org.onap.ccsdk.sli.plugins.restapicall;
 
+import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.glassfish.grizzly.http.server.HttpServer;
+import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
+import org.glassfish.jersey.media.multipart.MultiPartFeature;
 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;
+import org.glassfish.jersey.server.ResourceConfig;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
 
 public class TestRestapiCallNode {
 
@@ -224,6 +232,33 @@ public class TestRestapiCallNode {
         rcn.sendRequest(p, ctx);
     }
 
+    @Test(expected = SvcLogicException.class)
+    public void testFormData() throws SvcLogicException {
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].source-uid", "APIDOC-123");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].action", "delete");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-timestamp", "2016-09-09 16:30:35.0");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-status", "New");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].processing-status", "New");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].service-clfi", "testClfi1");
+        ctx.setAttribute("tmp.sdn-circuit-req-row[0].clci", "clci");
+
+        Map<String, String> p = new HashMap<String, String>();
+        p.put("templateFileName", "src/test/resources/test-template.json");
+        p.put("restapiUrl", "http://echo.getpostman.com");
+        p.put("restapiUser", "user1");
+        p.put("restapiPassword", "abc123");
+        p.put("format", "json");
+        p.put("httpMethod", "post");
+        p.put("responsePrefix", "response");
+        p.put("skipSending", "false");
+        p.put("multipartFormData", "true");
+        p.put("multipartFile", "src/test/resources/test-template.json");
+
+        RestapiCallNode rcn = new RestapiCallNode();
+        rcn.sendRequest(p, ctx);
+    }
 
     @Test(expected = SvcLogicException.class)
     public void testWithInvalidURI() throws SvcLogicException {
@@ -627,4 +662,46 @@ public class TestRestapiCallNode {
         RestapiCallNode rcn = new RestapiCallNode();
         rcn.sendRequest(p, ctx);
     }
+
+    @Test
+    public void testMultipartFormData() throws SvcLogicException {
+        final ResourceConfig resourceConfig = new ResourceConfig(
+                MultipartServerMock.class, MultiPartFeature.class);
+        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
+                URI.create("http://localhost:8080/"),resourceConfig);
+
+        Map<String, String> p = new HashMap<>();
+        p.put("multipartFormData", "true");
+        p.put("format", "none");
+        p.put("multipartFile", "src/test/resources/test-template.json");
+        p.put("restapiUrl", "http://localhost:8080/file-upload/upload");
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        RestapiCallNode rcn = new RestapiCallNode();
+        rcn.sendRequest(p, ctx);
+        assertThat(ctx.getAttribute("response-code"), is("200"));
+        assertThat(ctx.getAttribute("httpResponse"), is( "test-template.json"));
+        server.shutdownNow();
+    }
+
+    @Test
+    public void testCookieResponse() throws SvcLogicException {
+        final ResourceConfig resourceConfig = new ResourceConfig(
+                MockCookieAuthServer.class);
+        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
+                URI.create("http://localhost:8080/"),resourceConfig);
+
+        Map<String, String> p = new HashMap<>();
+        p.put("format", "none");
+        p.put("httpMethod", "get");
+        p.put("restapiUrl", "http://localhost:8080/get-cookie/cookie");
+        p.put("dumpHeaders", "true");
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        RestapiCallNode rcn = new RestapiCallNode();
+        rcn.sendRequest(p, ctx);
+        assertThat(ctx.getAttribute("response-code"), is("200"));
+        assertThat(ctx.getAttribute("header.Set-Cookie"), is("cookieResponse=cookieValueInReturn;Version=1"));
+        server.shutdownNow();
+    }
 }