1   package com.atlassian.maven.plugins.amps.codegen.prompter.common.web;
2   
3   import java.util.Arrays;
4   import java.util.SortedMap;
5   import java.util.TreeMap;
6   
7   import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
8   import com.atlassian.plugins.codegen.modules.common.Condition;
9   import com.atlassian.plugins.codegen.modules.common.Conditions;
10  import com.atlassian.plugins.codegen.modules.common.web.WebItemProperties;
11  
12  import org.codehaus.plexus.components.interactivity.PrompterException;
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.mockito.Mockito.when;
17  
18  /**
19   * @since 3.5
20   */
21  public class WebItemPrompterTest extends AbstractWebFragmentPrompterTest<WebItemProperties>
22  {
23      public static final String MODULE_NAME = "My Web Item";
24      public static final String MODULE_KEY = "my-web-item";
25      public static final String LINK_ID = "item-link";
26      public static final String DESCRIPTION = "The My Web Item Plugin";
27      public static final String I18N_NAME_KEY = "my-web-item.name";
28      public static final String I18N_DESCRIPTION_KEY = "my-web-item.description";
29  
30      public static final String ADV_MODULE_KEY = "awesome-module";
31      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
32      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
33      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
34      public static final String SYSTEM_ADMIN_SETTINGS = "system.admin/settings";
35      public static final String LINK_URL = "/secure/MyAction.jspa";
36      public static final String WEIGHT = "20";
37      public static final String LABEL_KEY = "item.label";
38      public static final String LABEL_VALUE = "this is my label";
39      public static final String LABEL_PARAM = "label param";
40      public static final String ICON_PATH = "/images/icon.png";
41      public static final String ICON_WIDTH = "32";
42      public static final String ICON_HEIGHT = "20";
43      public static final String TOOLTIP_KEY = "item.toolip";
44      public static final String TOOLTIP_VALUE = "this is a tooltip";
45  
46      @Test
47      public void basicPropertiesAreValid() throws PrompterException
48      {
49          when(prompter.prompt("Enter Plugin Module Name", "My Web Item")).thenReturn(MODULE_NAME);
50          when(prompter.prompt("Enter Section (e.g. system.admin/globalsettings)")).thenReturn(SYSTEM_ADMIN_SETTINGS);
51          when(prompter.prompt("Enter Link URL (e.g. /secure/CreateIssue!default.jspa)")).thenReturn(LINK_URL);
52  
53          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
54          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
55  
56          WebItemPrompter modulePrompter = new WebItemPrompter(prompter);
57          modulePrompter.setUseAnsiColor(false);
58          setProps((WebItemProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
59  
60          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
61          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
62          assertEquals("wrong description", DESCRIPTION, props.getDescription());
63          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
64          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
65          assertEquals("wrong section", SYSTEM_ADMIN_SETTINGS, props.getSection());
66          assertEquals("wrong link", LINK_URL, props.getLink()
67                  .getValue());
68      }
69  
70      @Test
71      public void advancedPropertiesAreValid() throws PrompterException
72      {
73          when(prompter.prompt("Enter Plugin Module Name", "My Web Item")).thenReturn(MODULE_NAME);
74          when(prompter.prompt("Enter Section (e.g. system.admin/globalsettings)")).thenReturn(SYSTEM_ADMIN_SETTINGS);
75          when(prompter.prompt("Enter Link URL (e.g. /secure/CreateIssue!default.jspa)")).thenReturn(LINK_URL);
76  
77          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
78          when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
79          when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
80          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
81          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
82          when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
83          when(prompter.prompt("Link Id", "my-web-item-link")).thenReturn(LINK_ID);
84          when(prompter.prompt("Enter Label Key", "my-web-item.label")).thenReturn(LABEL_KEY);
85          when(prompter.prompt("Enter Label Value", "My Web Item")).thenReturn(LABEL_VALUE);
86          when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
87                  .thenReturn("N");
88          when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
89          when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
90          when(prompter.prompt("Add Icon?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
91                  .thenReturn("N");
92          when(prompter.prompt("Icon Location (e.g. /images/icons/print.gif)")).thenReturn(ICON_PATH);
93          when(prompter.prompt("Icon Width", "16")).thenReturn(ICON_WIDTH);
94          when(prompter.prompt("Icon Height", "16")).thenReturn(ICON_HEIGHT);
95          when(prompter.prompt("Add Tooltip?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
96                  .thenReturn("N");
97          when(prompter.prompt("Enter Tooltip Key", "awesome-module.tooltip")).thenReturn(TOOLTIP_KEY);
98          when(prompter.prompt("Enter Tooltip Value", "My Web Item Tooltip")).thenReturn(TOOLTIP_VALUE);
99          when(prompter.prompt("Add Tooltip Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
100         when(prompter.prompt("Add Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
101                 .thenReturn("N");
102         when(prompter.prompt("params:\nparamKey->paramVal\nAdd Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
103 
104 
105         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
106 
107         WebItemPrompter modulePrompter = new WebItemPrompter(prompter);
108         modulePrompter.setUseAnsiColor(false);
109         setProps((WebItemProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
110 
111         assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
112         assertEquals("wrong module key", ADV_MODULE_KEY, props.getModuleKey());
113         assertEquals("wrong description", ADV_DESCRIPTION, props.getDescription());
114         assertEquals("wrong i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
115         assertEquals("wrong i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
116         assertEquals("wrong section", SYSTEM_ADMIN_SETTINGS, props.getSection());
117         assertEquals("wrong link", LINK_URL, props.getLink()
118                 .getValue());
119         assertEquals("wrong weight", WEIGHT, props.getWeight());
120 
121         assertAdvancedCommonProps();
122 
123         //custom context-provider name check
124         assertEquals("wrong context provider", CUSTOM_CONTEXT_PROVIDER, props.getContextProvider());
125 
126         //custom condition name check
127         Condition condition = (Condition) ((Conditions) props.getConditions()
128                 .get(0)).getConditions()
129                 .get(0);
130         assertEquals("wrong condition name", CUSTOM_CONDITION, condition.getFullyQualifiedClassName());
131     }
132 
133     @Test
134     public void providerContextFromListIsValid() throws PrompterException
135     {
136         SortedMap<String, String> providersMap = new TreeMap<String, String>();
137         providersMap.put("HeightContextProvider", "com.atlassian.test.HeightContextPRovider");
138         providersMap.put("WidthContextProvider", "com.atlassian.test.WidthContextProvider");
139 
140         contextProviderFactory.setProvidersMap(providersMap);
141 
142         when(prompter.prompt("Enter Plugin Module Name", "My Web Item")).thenReturn(MODULE_NAME);
143         when(prompter.prompt("Enter Section (e.g. system.admin/globalsettings)")).thenReturn(SYSTEM_ADMIN_SETTINGS);
144         when(prompter.prompt("Enter Link URL (e.g. /secure/CreateIssue!default.jspa)")).thenReturn(LINK_URL);
145 
146         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
147         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
148         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
149         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
150         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
151         when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
152         when(prompter.prompt("Link Id", "my-web-item-link")).thenReturn(LINK_ID);
153         when(prompter.prompt("Enter Label Key", "my-web-item.label")).thenReturn(LABEL_KEY);
154         when(prompter.prompt("Enter Label Value", "My Web Item")).thenReturn(LABEL_VALUE);
155         when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
156                 .thenReturn("N");
157         when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
158         when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
159         when(prompter.prompt("Add Icon?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
160                 .thenReturn("N");
161         when(prompter.prompt("Icon Location (e.g. /images/icons/print.gif)")).thenReturn(ICON_PATH);
162         when(prompter.prompt("Icon Width", "16")).thenReturn(ICON_WIDTH);
163         when(prompter.prompt("Icon Height", "16")).thenReturn(ICON_HEIGHT);
164         when(prompter.prompt("Add Tooltip?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
165                 .thenReturn("N");
166         when(prompter.prompt("Enter Tooltip Key", "awesome-module.tooltip")).thenReturn(TOOLTIP_KEY);
167         when(prompter.prompt("Enter Tooltip Value", "My Web Item Tooltip")).thenReturn(TOOLTIP_VALUE);
168         when(prompter.prompt("Add Tooltip Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
169         when(prompter.prompt("Add Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
170                 .thenReturn("N");
171         when(prompter.prompt("params:\nparamKey->paramVal\nAdd Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
172 
173         when(prompter.prompt("Choose A Context Provider\n1: HeightContextProvider\n2: WidthContextProvider\n3: Custom Context Provider\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
174 
175         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
176 
177         WebItemPrompter modulePrompter = new WebItemPrompter(prompter);
178         modulePrompter.setUseAnsiColor(false);
179         setProps((WebItemProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
180 
181         assertEquals("wrong context provider", "com.atlassian.test.WidthContextProvider", props.getContextProvider());
182     }
183 
184     @Test
185     public void conditionFromListIsValid() throws PrompterException
186     {
187         SortedMap<String, String> conditionMap = new TreeMap<String, String>();
188         conditionMap.put("NoFacialHairCondition", "com.atlassian.test.NoFacialHairCondition");
189         conditionMap.put("HasGlobalAdminPermissionCondition", "com.atlassian.test.HasGlobalAdminPermissionCondition");
190 
191         conditionFactory.setConditions(conditionMap);
192 
193         when(prompter.prompt("Enter Plugin Module Name", "My Web Item")).thenReturn(MODULE_NAME);
194         when(prompter.prompt("Enter Section (e.g. system.admin/globalsettings)")).thenReturn(SYSTEM_ADMIN_SETTINGS);
195         when(prompter.prompt("Enter Link URL (e.g. /secure/CreateIssue!default.jspa)")).thenReturn(LINK_URL);
196 
197         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
198         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
199         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
200         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
201         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
202         when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
203         when(prompter.prompt("Link Id", "my-web-item-link")).thenReturn(LINK_ID);
204         when(prompter.prompt("Enter Label Key", "my-web-item.label")).thenReturn(LABEL_KEY);
205         when(prompter.prompt("Enter Label Value", "My Web Item")).thenReturn(LABEL_VALUE);
206         when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
207                 .thenReturn("N");
208         when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
209         when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
210         when(prompter.prompt("Add Icon?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
211                 .thenReturn("N");
212         when(prompter.prompt("Icon Location (e.g. /images/icons/print.gif)")).thenReturn(ICON_PATH);
213         when(prompter.prompt("Icon Width", "16")).thenReturn(ICON_WIDTH);
214         when(prompter.prompt("Icon Height", "16")).thenReturn(ICON_HEIGHT);
215         when(prompter.prompt("Add Tooltip?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
216                 .thenReturn("N");
217         when(prompter.prompt("Enter Tooltip Key", "awesome-module.tooltip")).thenReturn(TOOLTIP_KEY);
218         when(prompter.prompt("Enter Tooltip Value", "My Web Item Tooltip")).thenReturn(TOOLTIP_VALUE);
219         when(prompter.prompt("Add Tooltip Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
220         when(prompter.prompt("Add Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
221                 .thenReturn("N");
222         when(prompter.prompt("params:\nparamKey->paramVal\nAdd Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
223 
224         when(prompter.prompt("Choose A Condition\n1: HasGlobalAdminPermissionCondition\n2: NoFacialHairCondition\n3: Custom Condition\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
225 
226         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
227 
228         WebItemPrompter modulePrompter = new WebItemPrompter(prompter);
229         modulePrompter.setUseAnsiColor(false);
230         setProps((WebItemProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
231 
232         Condition condition = (Condition) ((Conditions) props.getConditions()
233                 .get(0)).getConditions()
234                 .get(0);
235         assertEquals("wrong condition name", "com.atlassian.test.NoFacialHairCondition", condition.getFullyQualifiedClassName());
236 
237     }
238 
239 }