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