1   package com.atlassian.plugins.codegen.modules.jira;
2   
3   import java.io.File;
4   import java.util.regex.Matcher;
5   
6   import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
7   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
8   
9   import org.dom4j.Document;
10  import org.dom4j.Node;
11  import org.junit.Before;
12  import org.junit.Test;
13  
14  import static org.junit.Assert.*;
15  
16  /**
17   * @since 3.6
18   */
19  public class WorkflowPostFunctionTest extends AbstractCodegenTestCase<WorkflowPostFunctionProperties>
20  {
21      public static final String PACKAGE_NAME = "com.atlassian.plugins.workflow";
22      public static final String XPATH = "/atlassian-plugin/workflow-function[@name='My Post Function' and @key='my-post-function' and @i18n-name-key='my-post-function.name' and @class='" + PACKAGE_NAME + ".MyPostFunctionFactory']";
23  
24      protected File templatePath;
25  
26      @Before
27      public void runGenerator() throws Exception
28      {
29          setCreator(new WorkflowPostFunctionModuleCreator());
30          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
31                  .resourcesDirectory(resourcesDir)
32                  .testDirectory(testDir)
33                  .templateDirectory(templateDir)
34                  .build());
35  
36          setProps(new WorkflowPostFunctionProperties(PACKAGE_NAME + ".MyPostFunction"));
37  
38          props.setIncludeExamples(false);
39  
40          templatePath = new File(templateDir, "postfunctions");
41  
42      }
43  
44      @Test
45      public void allFilesAreGenerated() throws Exception
46      {
47  
48          creator.createModule(moduleLocation, props);
49  
50          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
51  
52          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyPostFunction.java").exists());
53          assertTrue("factory class not generated", new File(srcDir, packagePath + File.separator + "MyPostFunctionFactory.java").exists());
54          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyPostFunctionTest.java").exists());
55          assertTrue("view template not generated", new File(templatePath, "my-post-function.vm").exists());
56          assertTrue("input template not generated", new File(templatePath, "my-post-function-input.vm").exists());
57          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
58  
59      }
60  
61      @Test
62      public void moduleIsValid() throws Exception
63      {
64  
65          creator.createModule(moduleLocation, props);
66          Document pluginDoc = getXmlDocument(pluginXml);
67          Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
68  
69          assertNotNull("valid workflow-function not found", workflowFunction);
70          assertNull("found orderable but should be null", workflowFunction.selectSingleNode("orderable"));
71          assertNull("found unique but should be null", workflowFunction.selectSingleNode("unique"));
72          assertNull("found deletable but should be null", workflowFunction.selectSingleNode("deletable"));
73          assertNull("found addable but should be null", workflowFunction.selectSingleNode("addable"));
74      }
75  
76      @Test
77      public void moduleHasFunction() throws Exception
78      {
79  
80          creator.createModule(moduleLocation, props);
81          Document pluginDoc = getXmlDocument(pluginXml);
82          Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
83  
84          String functionXpath = "function-class[text() = '" + PACKAGE_NAME + ".MyPostFunction']";
85  
86          assertNotNull("valid workflow-function not found", workflowFunction);
87          assertNotNull("valid function-class not found", workflowFunction.selectSingleNode(functionXpath));
88      }
89  
90      @Test
91      public void moduleHasOrderable() throws Exception
92      {
93  
94          props.setOrderable(true);
95          creator.createModule(moduleLocation, props);
96          Document pluginDoc = getXmlDocument(pluginXml);
97          Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
98  
99          String subXpath = "orderable[text() = 'true']";
100 
101         assertNotNull("valid workflow-function not found", workflowFunction);
102         assertNotNull("valid orderable not found", workflowFunction.selectSingleNode(subXpath));
103     }
104 
105     @Test
106     public void moduleHasDeletable() throws Exception
107     {
108 
109         props.setDeletable(false);
110         creator.createModule(moduleLocation, props);
111         Document pluginDoc = getXmlDocument(pluginXml);
112         Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
113 
114         String subXpath = "deletable[text() = 'false']";
115 
116         assertNotNull("valid workflow-function not found", workflowFunction);
117         assertNotNull("valid deletable not found", workflowFunction.selectSingleNode(subXpath));
118     }
119 
120     @Test
121     public void moduleHasUnique() throws Exception
122     {
123 
124         props.setUnique(true);
125         creator.createModule(moduleLocation, props);
126         Document pluginDoc = getXmlDocument(pluginXml);
127         Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
128 
129         String subXpath = "unique[text() = 'true']";
130 
131         assertNotNull("valid workflow-function not found", workflowFunction);
132         assertNotNull("valid unique not found", workflowFunction.selectSingleNode(subXpath));
133     }
134 
135     @Test
136     public void moduleHasAddable() throws Exception
137     {
138 
139         props.setAddable("global,common");
140         creator.createModule(moduleLocation, props);
141         Document pluginDoc = getXmlDocument(pluginXml);
142         Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
143 
144         String subXpath = "addable[text() = 'global,common']";
145 
146         assertNotNull("valid workflow-function not found", workflowFunction);
147         assertNotNull("valid addable not found", workflowFunction.selectSingleNode(subXpath));
148     }
149 
150     @Test
151     public void moduleHasMultipleFlags() throws Exception
152     {
153 
154         props.setAddable("global,common");
155         props.setDeletable(false);
156         props.setUnique(true);
157 
158         creator.createModule(moduleLocation, props);
159         Document pluginDoc = getXmlDocument(pluginXml);
160         Node workflowFunction = pluginDoc.selectSingleNode(XPATH);
161 
162         String addable = "addable[text() = 'global,common']";
163         String deletable = "deletable[text() = 'false']";
164         String unique = "unique[text() = 'true']";
165 
166         assertNotNull("valid workflow-function not found", workflowFunction);
167         assertNotNull("valid addable not found", workflowFunction.selectSingleNode(addable));
168         assertNotNull("valid unique not found", workflowFunction.selectSingleNode(unique));
169         assertNotNull("valid deletable not found", workflowFunction.selectSingleNode(deletable));
170     }
171 
172 }