add template node
[ccsdk/sli/plugins.git] / template-node / provider / src / test / java / org / onap / ccsdk / sli / plugins / template / TemplateNodeTest.java
1 package org.onap.ccsdk.sli.plugins.template;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Vector;
9 import org.apache.velocity.runtime.RuntimeConstants;
10 import org.junit.Test;
11 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
12 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
13
14 public class TemplateNodeTest {
15
16     @Test
17     public void sunnyDay() throws Exception {
18         String requestId = "REQ001";
19         String uniqueKey = "UNIQUE_TEST";
20         String action = "uPdaTe";
21         String serviceType = "VPN";
22
23         TemplateNode t = new MockTemplateNode();
24
25         Map<String, String> params = new HashMap<String, String>();
26         params.put(TemplateNode.PREFIX_KEY, "output");
27         params.put(TemplateNode.OUTPUT_PATH_KEY, "mycontainer");
28         params.put(TemplateNode.TEMPLATE_PATH, "src/test/resources/basic.vtl");
29         params.put("service-type", serviceType);
30         SvcLogicContext ctx = new SvcLogicContext();
31         ctx.setAttribute("input.svc-request-id", requestId);
32         ctx.setAttribute("input.unique-key", uniqueKey);
33         ctx.setAttribute("action", action);
34
35         t.evaluateTemplate(params, ctx);
36         String result = ctx.getAttribute("output.mycontainer");
37         assertNotNull(result);
38         assertTrue(result.contains(requestId));
39         assertTrue(result.contains(uniqueKey));
40         assertTrue(result.contains(action.toUpperCase()));
41         assertTrue(result.contains(serviceType));
42     }
43
44     @Test(expected = SvcLogicException.class)
45     public void parameterException() throws Exception {
46         TemplateNode t = new MockTemplateNode();
47         Map<String, String> params = new HashMap<String, String>();
48         SvcLogicContext ctx = new SvcLogicContext();
49         t.evaluateTemplate(params, ctx);
50     }
51
52     @Test(expected = SvcLogicException.class)
53     public void missingTemplate() throws Exception {
54         TemplateNode t = new MockTemplateNode();
55         Map<String, String> params = new HashMap<String, String>();
56         params.put(TemplateNode.PREFIX_KEY, "output");
57         params.put(TemplateNode.OUTPUT_PATH_KEY, "mycontainer");
58         params.put(TemplateNode.TEMPLATE_PATH, "src/test/resources/missing.vtl");
59         SvcLogicContext ctx = new SvcLogicContext();
60         t.evaluateTemplate(params, ctx);
61     }
62
63     @Test
64     public void withProperties() throws Exception {
65         System.setProperty(TemplateNode.PROPERTIES_DIR_KEY, "src/test/resources");
66         TemplateNode t = new TemplateNode();
67         Vector<String> loader = (Vector<String>) t.ve.getProperty(RuntimeConstants.RESOURCE_LOADER);
68         assertTrue(loader.contains("class"));
69         assertEquals("/home/my/example", t.ve.getProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH));
70         assertEquals("true", t.ve.getProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE));
71         assertEquals("customValue", t.ve.getProperty("custom.property"));
72     }
73
74     @Test
75     public void withNoProperties() throws Exception {
76         System.setProperty(TemplateNode.PROPERTIES_DIR_KEY, "i/do/not/exist");
77         TemplateNode t = new TemplateNode();
78         Vector<String> loader = (Vector<String>) t.ve.getProperty(RuntimeConstants.RESOURCE_LOADER);
79         assertTrue(loader.contains("file"));
80         assertEquals("/opt/onap/sdnc/restapi/templates", t.ve.getProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH));
81         assertEquals("false", t.ve.getProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE));
82         assertEquals(null, t.ve.getProperty("custom.property"));
83     }
84
85 }