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.Label;
7   import com.atlassian.plugins.codegen.modules.common.Tooltip;
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 WebSectionTest extends AbstractWebFragmentTest<WebSectionProperties>
20  {
21      public static final String MODULE_NAME = "Awesome Web Section";
22      public static final String CUSTOM_LOCATION = "system.admin/mysection";
23  
24      @Before
25      public void runGenerator() throws Exception
26      {
27          setCreator(new WebSectionModuleCreator());
28          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
29                  .resourcesDirectory(resourcesDir)
30                  .testDirectory(testDir)
31                  .templateDirectory(templateDir)
32                  .build());
33  
34          setProps(new WebSectionProperties(MODULE_NAME, CUSTOM_LOCATION));
35          props.setIncludeExamples(false);
36      }
37  
38      @Test
39      public void moduleIsValid() throws Exception
40      {
41          String xpath = "/atlassian-plugin/web-section[@name='Awesome Web Section' and @key='awesome-web-section' and @i18n-name-key='awesome-web-section.name' and @location='system.admin/mysection' and @weight='1000']";
42  
43          creator.createModule(moduleLocation, props);
44          Document pluginDoc = getXmlDocument(pluginXml);
45  
46          assertNotNull("valid web-section not found", pluginDoc.selectSingleNode(xpath));
47      }
48  
49      @Test
50      public void moduleIsValidWithCustomWeight() throws Exception
51      {
52          props.setWeight(20);
53  
54          String xpath = "/atlassian-plugin/web-section[@name='Awesome Web Section' and @key='awesome-web-section' and @i18n-name-key='awesome-web-section.name' and @location='system.admin/mysection' and @weight='20']";
55  
56          creator.createModule(moduleLocation, props);
57          Document pluginDoc = getXmlDocument(pluginXml);
58  
59          assertNotNull("valid web-section with custom weight not found", pluginDoc.selectSingleNode(xpath));
60      }
61  
62      @Test
63      public void labelAdded() throws Exception
64      {
65          String paramVal0 = "$helper.project.name";
66          String paramVal1 = "$helper.project.description";
67          Label label = new Label("web.section.mysection", "awesome web section");
68          label.addParam(paramVal0);
69          label.addParam(paramVal1);
70  
71          props.setLabel(label);
72  
73          String labelXpath = "/atlassian-plugin/web-section/label[@key='web.section.mysection']";
74  
75          creator.createModule(moduleLocation, props);
76          Document pluginDoc = getXmlDocument(pluginXml);
77  
78          Node labelNode = pluginDoc.selectSingleNode(labelXpath);
79  
80          assertNotNull("label not found", labelNode);
81  
82          Node param0 = labelNode.selectSingleNode("param[@name='param0' and @value='" + paramVal0 + "']");
83          Node param1 = labelNode.selectSingleNode("param[@name='param1' and @value='" + paramVal1 + "']");
84  
85          assertNotNull("param 0 not found", param0);
86          assertNotNull("param 1 not found", param1);
87  
88          Properties i18nprops = loadI18nProperties();
89          assertTrue("label i18n not found", i18nprops.containsKey(label.getKey()));
90          assertEquals("label i18n has wrong value", label.getValue(), i18nprops.getProperty(label.getKey()));
91  
92      }
93  
94      @Test
95      public void paramsAdded() throws Exception
96      {
97          props.addParam("isAwesomeSection", "true");
98          props.addParam("isSuperAwesome", "false");
99  
100         String param1Xpath = "/atlassian-plugin/web-section/param[@name='isAwesomeSection' and @value='true']";
101         String param2Xpath = "/atlassian-plugin/web-section/param[@name='isSuperAwesome' and @value='false']";
102 
103         creator.createModule(moduleLocation, props);
104         Document pluginDoc = getXmlDocument(pluginXml);
105 
106         assertNotNull("param 1 not found", pluginDoc.selectSingleNode(param1Xpath));
107         assertNotNull("param 2 not found", pluginDoc.selectSingleNode(param2Xpath));
108     }
109 
110     @Test
111     public void tooltipAdded() throws Exception
112     {
113         Tooltip tooltip = new Tooltip("web.section.mysection.tooltip", "this is an awesome section");
114         props.setTooltip(tooltip);
115 
116         String xpath = "/atlassian-plugin/web-section/tooltip[@key='web.section.mysection.tooltip']";
117 
118         creator.createModule(moduleLocation, props);
119         Document pluginDoc = getXmlDocument(pluginXml);
120 
121         assertNotNull("tooltip not found", pluginDoc.selectSingleNode(xpath));
122 
123         Properties i18nprops = loadI18nProperties();
124         assertTrue("tooltip i18n not found", i18nprops.containsKey(tooltip.getKey()));
125         assertEquals("tooltip i18n has wrong value", tooltip.getValue(), i18nprops.getProperty(tooltip.getKey()));
126     }
127 
128 }