1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import java.util.List;
4   
5   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
6   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractPrompterTest;
7   import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
8   import com.atlassian.plugins.codegen.modules.common.Resource;
9   import com.atlassian.plugins.codegen.modules.jira.ReportProperties;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.codehaus.plexus.components.interactivity.Prompter;
13  import org.codehaus.plexus.components.interactivity.PrompterException;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertTrue;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.when;
21  
22  /**
23   * @since 3.5
24   */
25  public class ReportPrompterTest extends AbstractPrompterTest
26  {
27      public static final String PACKAGE = "com.atlassian.plugins.jira.reports";
28      public static final String CLASSNAME = "MyReport";
29      public static final String MODULE_NAME = "My Report";
30      public static final String MODULE_KEY = "my-report";
31      public static final String DESCRIPTION = "The My Report Plugin";
32      public static final String I18N_NAME_KEY = "my-report.name";
33      public static final String I18N_DESCRIPTION_KEY = "my-report.description";
34  
35      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
36      public static final String ADV_MODULE_KEY = "awesome-module";
37      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
38      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
39      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
40      public static final String RESOURCE_NAME = "view";
41      public static final String RESOURCE_VM_PATH = "templates/resource.vm";
42      public static final String LABEL_KEY = "item.label";
43      public static final String LABEL_VALUE = "this is my label";
44      public static final String LABEL_PARAM = "label param";
45  
46      Prompter prompter;
47  
48      @Before
49      public void setup()
50      {
51          prompter = mock(Prompter.class);
52      }
53  
54      @Test
55      public void basicPropertiesAreValid() throws PrompterException
56      {
57          when(prompter.prompt("Enter New Classname", "MyReport")).thenReturn(CLASSNAME);
58          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.reports")).thenReturn(PACKAGE);
59          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
60          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
61  
62          ReportPrompter modulePrompter = new ReportPrompter(prompter);
63          modulePrompter.setUseAnsiColor(false);
64          ReportProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
65  
66          assertEquals("wrong class", CLASSNAME, props.getClassname());
67          assertEquals("wrong class package", PACKAGE, props.getPackage());
68          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
69          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
70          assertEquals("wrong description", DESCRIPTION, props.getDescription());
71          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
72          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
73      }
74  
75      @Test
76      public void advancedPropertiesAreValid() throws PrompterException
77      {
78          when(prompter.prompt("Enter New Classname", "MyReport")).thenReturn(CLASSNAME);
79          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.reports")).thenReturn(PACKAGE);
80          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
81  
82          when(prompter.prompt("Plugin Name", MODULE_NAME)).thenReturn(ADV_MODULE_NAME);
83          when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
84          when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
85          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
86          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
87  
88          when(prompter.prompt("Add Resource", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
89                  .thenReturn("N");
90          when(prompter.prompt("Enter Resource Name")).thenReturn(RESOURCE_NAME)
91                  .thenReturn("");
92          when(prompter.prompt("Enter Location (path to resource file)")).thenReturn(RESOURCE_VM_PATH);
93  
94          when(prompter.prompt("Enter Label Key", "my-report.label")).thenReturn(LABEL_KEY);
95          when(prompter.prompt("Enter Label Value", "My Report")).thenReturn(LABEL_VALUE);
96          when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
97                  .thenReturn("N");
98          when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
99          when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
100 
101         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
102 
103         ReportPrompter modulePrompter = new ReportPrompter(prompter);
104         modulePrompter.setUseAnsiColor(false);
105         ReportProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
106 
107         assertEquals("wrong adv class", CLASSNAME, props.getClassname());
108         assertEquals("wrong adv package", PACKAGE, props.getPackage());
109         assertEquals("wrong adv module name", ADV_MODULE_NAME, props.getModuleName());
110         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
111         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
112         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
113         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
114 
115         //resources
116         List<Resource> resources = props.getResources();
117         assertTrue("resources not found", !resources.isEmpty());
118         assertEquals("wrong number of resources", 2, resources.size());
119 
120         Resource viewResource = resources.get(0);
121 
122         assertEquals("wrong css resource name", RESOURCE_NAME, viewResource.getName());
123         assertTrue("name pattern found when name is set", StringUtils.isBlank(viewResource.getNamePattern()));
124         assertEquals("wrong resource type", "velocity", viewResource.getType());
125         assertEquals("wrong resource location", RESOURCE_VM_PATH, viewResource.getLocation());
126 
127         Resource i18n = resources.get(1);
128 
129         assertEquals("wrong i18n resource name", "i18n", i18n.getName());
130         assertTrue("name pattern found when name is set", StringUtils.isBlank(i18n.getNamePattern()));
131         assertEquals("wrong resource type", "i18n", i18n.getType());
132         assertEquals("wrong resource location", CLASSNAME, i18n.getLocation());
133 
134         assertEquals("wrong label key", LABEL_KEY, props.getLabel()
135                 .getKey());
136         assertEquals("wrong label value", LABEL_VALUE, props.getLabel()
137                 .getValue());
138     }
139 }