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.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  /**
18   * @since 3.6
19   */
20  public class RPCTest extends AbstractCodegenTestCase<RPCProperties>
21  {
22      public static final String PACKAGE_NAME = "com.atlassian.plugins.jira.rpc";
23  
24      @Before
25      public void runGenerator() throws Exception
26      {
27          setCreator(new RPCModuleCreator());
28          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
29                  .resourcesDirectory(resourcesDir)
30                  .testDirectory(testDir)
31                  .templateDirectory(templateDir)
32                  .build());
33      }
34  
35      @Test
36      public void allSoapFilesAreGenerated() throws Exception
37      {
38          setProps(new RPCProperties(PACKAGE_NAME + ".MySoapEndpoint"));
39          props.setIncludeExamples(false);
40          props.setSoap(true);
41  
42          creator.createModule(moduleLocation, props);
43  
44          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
45          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MySoapEndpointImpl.java").exists());
46          assertTrue("interface not generated", new File(srcDir, packagePath + File.separator + "MySoapEndpoint.java").exists());
47          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MySoapEndpointTest.java").exists());
48          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
49  
50      }
51  
52      @Test
53      public void allXmlFilesAreGenerated() throws Exception
54      {
55          setProps(new RPCProperties(PACKAGE_NAME + ".MyXmlEndpoint"));
56          props.setIncludeExamples(false);
57          props.setSoap(false);
58  
59          creator.createModule(moduleLocation, props);
60  
61          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
62          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyXmlEndpointImpl.java").exists());
63          assertTrue("interface not generated", new File(srcDir, packagePath + File.separator + "MyXmlEndpoint.java").exists());
64          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyXmlEndpointTest.java").exists());
65          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
66  
67      }
68  
69      @Test
70      public void soapModuleIsValid() throws Exception
71      {
72          String xpath = "/atlassian-plugin/rpc-soap[@name='My Soap Endpoint' and @key='my-soap-endpoint' and @i18n-name-key='my-soap-endpoint.name' and @class='" + PACKAGE_NAME + ".MySoapEndpointImpl']";
73          String pathXPath = "service-path[text() = 'mysoapendpoint-v1']";
74          String interfaceXPath = "published-interface[text() = '" + PACKAGE_NAME + ".MySoapEndpoint']";
75  
76          setProps(new RPCProperties(PACKAGE_NAME + ".MySoapEndpoint"));
77          props.setIncludeExamples(false);
78          props.setSoap(true);
79  
80          creator.createModule(moduleLocation, props);
81          Document pluginDoc = getXmlDocument(pluginXml);
82  
83          Node rpcNode = pluginDoc.selectSingleNode(xpath);
84  
85          assertNotNull("valid rpc-soap not found", rpcNode);
86          assertNotNull("valid service-path not found", rpcNode.selectSingleNode(pathXPath));
87          assertNotNull("valid published-interface not found", rpcNode.selectSingleNode(interfaceXPath));
88      }
89  
90      @Test
91      public void xmlModuleIsValid() throws Exception
92      {
93          String xpath = "/atlassian-plugin/rpc-xmlrpc[@name='My Xml Endpoint' and @key='my-xml-endpoint' and @i18n-name-key='my-xml-endpoint.name' and @class='" + PACKAGE_NAME + ".MyXmlEndpointImpl']";
94  
95          String pathXPath = "service-path[text() = 'myxmlendpoint-v1']";
96  
97          setProps(new RPCProperties(PACKAGE_NAME + ".MyXmlEndpoint"));
98          props.setIncludeExamples(false);
99          props.setSoap(false);
100 
101         creator.createModule(moduleLocation, props);
102         Document pluginDoc = getXmlDocument(pluginXml);
103 
104         Node rpcNode = pluginDoc.selectSingleNode(xpath);
105 
106         assertNotNull("valid rpc-soap not found", rpcNode);
107         assertNotNull("valid service-path not found", rpcNode.selectSingleNode(pathXPath));
108     }
109 
110     @Test
111     public void soapComponentAdded() throws Exception
112     {
113         setProps(new RPCProperties(PACKAGE_NAME + ".MySoapEndpoint"));
114         props.setIncludeExamples(false);
115         props.setSoap(true);
116 
117         creator.createModule(moduleLocation, props);
118         Document pluginDoc = getXmlDocument(pluginXml);
119 
120         String compXPath = "/atlassian-plugin/component[@name='My Soap Endpoint Component' and @key='my-soap-endpoint-component' and @i18n-name-key='my-soap-endpoint.name.component' and @class='" + PACKAGE_NAME + ".MySoapEndpointImpl']";
121         String compIfaceXPath = "interface[text() = '" + PACKAGE_NAME + ".MySoapEndpoint']";
122 
123         Node compNode = pluginDoc.selectSingleNode(compXPath);
124         assertNotNull("component not found", compNode);
125         assertNotNull("interface not found", compNode.selectSingleNode(compIfaceXPath));
126 
127     }
128 
129     @Test
130     public void xmlComponentAdded() throws Exception
131     {
132         setProps(new RPCProperties(PACKAGE_NAME + ".MyXmlEndpoint"));
133         props.setIncludeExamples(false);
134         props.setSoap(true);
135 
136         creator.createModule(moduleLocation, props);
137         Document pluginDoc = getXmlDocument(pluginXml);
138 
139         String compXPath = "/atlassian-plugin/component[@name='My Xml Endpoint Component' and @key='my-xml-endpoint-component' and @i18n-name-key='my-xml-endpoint.name.component' and @class='" + PACKAGE_NAME + ".MyXmlEndpointImpl']";
140         String compIfaceXPath = "interface[text() = '" + PACKAGE_NAME + ".MyXmlEndpoint']";
141 
142         Node compNode = pluginDoc.selectSingleNode(compXPath);
143         assertNotNull("component not found", compNode);
144         assertNotNull("interface not found", compNode.selectSingleNode(compIfaceXPath));
145     }
146 }