1 package com.atlassian.plugins.codegen.modules.common;
2
3 import java.io.File;
4 import java.util.Arrays;
5 import java.util.regex.Matcher;
6
7 import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
8 import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
9
10 import org.dom4j.Document;
11 import org.dom4j.Node;
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17
18
19
20
21 public class RESTTest extends AbstractCodegenTestCase<RESTProperties>
22 {
23 public static final String PACKAGE_NAME = "com.atlassian.plugins.rest";
24
25 @Before
26 public void runGenerator() throws Exception
27 {
28 setCreator(new RESTModuleCreator());
29 setModuleLocation(new PluginModuleLocation.Builder(srcDir)
30 .resourcesDirectory(resourcesDir)
31 .testDirectory(testDir)
32 .templateDirectory(templateDir)
33 .build());
34
35 setProps(new RESTProperties(PACKAGE_NAME + ".MyRestResource"));
36
37 props.setIncludeExamples(false);
38 }
39
40 @Test
41 public void allFilesAreGenerated() throws Exception
42 {
43 creator.createModule(moduleLocation, props);
44
45 String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
46 String itPackagePath = "it" + File.separator + packagePath;
47 assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyRestResource.java").exists());
48 assertTrue("model class not generated", new File(srcDir, packagePath + File.separator + "MyRestResourceModel.java").exists());
49 assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyRestResourceTest.java").exists());
50 assertTrue("funcTest class not generated", new File(testDir, itPackagePath + File.separator + "MyRestResourceFuncTest.java").exists());
51 assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
52
53 }
54
55 @Test
56 public void moduleIsValid() throws Exception
57 {
58 String xpath = "/atlassian-plugin/rest[@name='My Rest Resource' and @key='my-rest-resource' and @i18n-name-key='my-rest-resource.name' and @path='/myrestresource' and @version='1.0']";
59
60 creator.createModule(moduleLocation, props);
61 Document pluginDoc = getXmlDocument(pluginXml);
62
63 assertNotNull("valid rest not found", pluginDoc.selectSingleNode(xpath));
64 }
65
66 @Test
67 public void customPathIsValid() throws Exception
68 {
69 String xpath = "/atlassian-plugin/rest[@name='My Rest Resource' and @key='my-rest-resource' and @i18n-name-key='my-rest-resource.name' and @path='/helloworld' and @version='1.0']";
70
71 props.setPath("/helloworld");
72 creator.createModule(moduleLocation, props);
73 Document pluginDoc = getXmlDocument(pluginXml);
74
75 assertNotNull("rest with custom path not found", pluginDoc.selectSingleNode(xpath));
76 }
77
78 @Test
79 public void customVersionIsValid() throws Exception
80 {
81 String xpath = "/atlassian-plugin/rest[@name='My Rest Resource' and @key='my-rest-resource' and @i18n-name-key='my-rest-resource.name' and @path='/myrestresource' and @version='1.1']";
82
83 props.setVersion("1.1");
84 creator.createModule(moduleLocation, props);
85 Document pluginDoc = getXmlDocument(pluginXml);
86
87 assertNotNull("rest with custom version not found", pluginDoc.selectSingleNode(xpath));
88 }
89
90 @Test
91 public void packagesAreAdded() throws Exception
92 {
93 String package1 = "com.atlassian.plugins.rest.hello";
94 String package2 = "com.atlassian.plugins.rest.message";
95
96 String xpath = "/atlassian-plugin/rest[@name='My Rest Resource' and @key='my-rest-resource' and @i18n-name-key='my-rest-resource.name' and @path='/myrestresource' and @version='1.0']";
97 String package1Xpath = "package[text() = '" + package1 + "'";
98 String package2Xpath = "package[text() = '" + package2 + "'";
99
100 props.setPackagesToScan(Arrays.asList(package1, package2));
101 creator.createModule(moduleLocation, props);
102 Document pluginDoc = getXmlDocument(pluginXml);
103
104 Node restNode = pluginDoc.selectSingleNode(xpath);
105 assertNotNull("rest not found", restNode);
106 assertNotNull("package 1 not found", restNode.selectSingleNode(package1Xpath));
107 assertNotNull("package 2 not found", restNode.selectSingleNode(package2Xpath));
108 }
109
110 @Test
111 public void dispatchersAreAdded() throws Exception
112 {
113 String requestDispatcher = "REQUEST";
114 String forwardDispatcher = "FORWARD";
115
116 String xpath = "/atlassian-plugin/rest[@name='My Rest Resource' and @key='my-rest-resource' and @i18n-name-key='my-rest-resource.name' and @path='/myrestresource' and @version='1.0']";
117 String dispatcher1Xpath = "dispatcher[text() = '" + requestDispatcher + "'";
118 String dispatcher2Xpath = "dispatcher[text() = '" + forwardDispatcher + "'";
119
120 props.setDispatchers(Arrays.asList(requestDispatcher, forwardDispatcher));
121 creator.createModule(moduleLocation, props);
122 Document pluginDoc = getXmlDocument(pluginXml);
123
124 Node restNode = pluginDoc.selectSingleNode(xpath);
125 assertNotNull("rest not found", restNode);
126 assertNotNull("request dispatcher not found", restNode.selectSingleNode(dispatcher1Xpath));
127 assertNotNull("forward dispatcher not found", restNode.selectSingleNode(dispatcher2Xpath));
128 }
129
130 }