1   package com.atlassian.plugins.codegen.modules.common.web;
2   
3   import java.util.Properties;
4   
5   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
6   import com.atlassian.plugins.codegen.modules.common.Icon;
7   import com.atlassian.plugins.codegen.modules.common.Label;
8   import com.atlassian.plugins.codegen.modules.common.Link;
9   import com.atlassian.plugins.codegen.modules.common.Tooltip;
10  
11  import org.dom4j.Document;
12  import org.dom4j.Node;
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  import static org.junit.Assert.*;
17  
18  /**
19   * @since 3.6
20   */
21  public class WebItemTest extends AbstractWebFragmentTest<WebItemProperties>
22  {
23      public static final String MODULE_NAME = "My Web Item";
24      public static final String GLOBAL_SETTINGS_SECTION = "system.admin/globalsettings";
25  
26      @Before
27      public void runGenerator() throws Exception
28      {
29          setCreator(new WebItemModuleCreator());
30          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
31                  .resourcesDirectory(resourcesDir)
32                  .testDirectory(testDir)
33                  .templateDirectory(templateDir)
34                  .build());
35  
36          setProps(new WebItemProperties(MODULE_NAME, GLOBAL_SETTINGS_SECTION));
37          props.setIncludeExamples(false);
38  
39      }
40  
41      @Test
42      public void moduleIsValid() throws Exception
43      {
44          String xpath = "/atlassian-plugin/web-item[@name='My Web Item' and @key='my-web-item' and @i18n-name-key='my-web-item.name' and @section='system.admin/globalsettings' and @weight='1000']";
45  
46          creator.createModule(moduleLocation, props);
47          Document pluginDoc = getXmlDocument(pluginXml);
48  
49          assertNotNull("valid web-item not found", pluginDoc.selectSingleNode(xpath));
50      }
51  
52      @Test
53      public void moduleIsValidWithCustomWeight() throws Exception
54      {
55          props.setWeight(20);
56  
57          String xpath = "/atlassian-plugin/web-item[@name='My Web Item' and @key='my-web-item' and @i18n-name-key='my-web-item.name' and @section='system.admin/globalsettings' and @weight='20']";
58  
59          creator.createModule(moduleLocation, props);
60          Document pluginDoc = getXmlDocument(pluginXml);
61  
62          assertNotNull("valid web-item with custom weight not found", pluginDoc.selectSingleNode(xpath));
63      }
64  
65      @Test
66      public void iconAdded() throws Exception
67      {
68          String path = "/images/myicon.png";
69          Link link = new Link(path);
70          Icon icon = new Icon(16, 16);
71          icon.setLink(link);
72  
73          props.setIcon(icon);
74  
75          String xpath = "/atlassian-plugin/web-item/icon[@width='16' and @height='16']/link";
76  
77          creator.createModule(moduleLocation, props);
78          Document pluginDoc = getXmlDocument(pluginXml);
79  
80          Node linkNode = pluginDoc.selectSingleNode(xpath);
81  
82          assertNotNull("icon link not found", linkNode);
83          assertEquals("wrong icon link", path, linkNode.getStringValue());
84      }
85  
86      @Test
87      public void labelAdded() throws Exception
88      {
89          String paramVal0 = "$helper.project.name";
90          String paramVal1 = "$helper.project.description";
91          Label label = new Label("common.concepts.create.new.issue", "create new issue");
92          label.addParam(paramVal0);
93          label.addParam(paramVal1);
94  
95          props.setLabel(label);
96  
97          String labelXpath = "/atlassian-plugin/web-item/label[@key='common.concepts.create.new.issue']";
98  
99          creator.createModule(moduleLocation, props);
100         Document pluginDoc = getXmlDocument(pluginXml);
101 
102         Node labelNode = pluginDoc.selectSingleNode(labelXpath);
103 
104         assertNotNull("label not found", labelNode);
105 
106         Node param0 = labelNode.selectSingleNode("param[@name='param0' and @value='" + paramVal0 + "']");
107         Node param1 = labelNode.selectSingleNode("param[@name='param1' and @value='" + paramVal1 + "']");
108 
109         assertNotNull("param 0 not found", param0);
110         assertNotNull("param 1 not found", param1);
111 
112         Properties i18nprops = loadI18nProperties();
113         assertTrue("label i18n not found", i18nprops.containsKey(label.getKey()));
114         assertEquals("label i18n has wrong value", label.getValue(), i18nprops.getProperty(label.getKey()));
115 
116     }
117 
118     @Test
119     public void linkAdded() throws Exception
120     {
121         String path = "/secure/CreateIssue!default.jspa";
122         Link link = new Link(path);
123         link.setLinkId("create link");
124 
125         props.setLink(link);
126 
127         String linkXpath = "/atlassian-plugin/web-item/link[@linkId='create link']";
128 
129         creator.createModule(moduleLocation, props);
130         Document pluginDoc = getXmlDocument(pluginXml);
131 
132         Node linkNode = pluginDoc.selectSingleNode(linkXpath);
133 
134         assertNotNull("link not found", linkNode);
135         assertEquals("wrong link value", path, linkNode.getStringValue());
136     }
137 
138     @Test
139     public void paramsAdded() throws Exception
140     {
141         props.addParam("isPopupLink", "true");
142         props.addParam("isSuperAwesome", "false");
143 
144         String param1Xpath = "/atlassian-plugin/web-item/param[@name='isPopupLink' and @value='true']";
145         String param2Xpath = "/atlassian-plugin/web-item/param[@name='isSuperAwesome' and @value='false']";
146 
147         creator.createModule(moduleLocation, props);
148         Document pluginDoc = getXmlDocument(pluginXml);
149 
150         assertNotNull("param 1 not found", pluginDoc.selectSingleNode(param1Xpath));
151         assertNotNull("param 2 not found", pluginDoc.selectSingleNode(param2Xpath));
152     }
153 
154     @Test
155     public void tooltipAdded() throws Exception
156     {
157         Tooltip tooltip = new Tooltip("common.concepts.create.new.issue.tooltip", "creates a new issue");
158         props.setTooltip(tooltip);
159 
160         String xpath = "/atlassian-plugin/web-item/tooltip[@key='common.concepts.create.new.issue.tooltip']";
161 
162         creator.createModule(moduleLocation, props);
163         Document pluginDoc = getXmlDocument(pluginXml);
164 
165         assertNotNull("tooltip not found", pluginDoc.selectSingleNode(xpath));
166 
167         Properties i18nprops = loadI18nProperties();
168         assertTrue("tooltip i18n not found", i18nprops.containsKey(tooltip.getKey()));
169         assertEquals("tooltip i18n has wrong value", tooltip.getValue(), i18nprops.getProperty(tooltip.getKey()));
170     }
171 }