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.WebSectionProperties;
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 WebSectionPrompterTest extends AbstractWebFragmentPrompterTest<WebSectionProperties>
22  {
23      public static final String MODULE_NAME = "My Web Section";
24      public static final String MODULE_KEY = "my-web-section";
25      public static final String DESCRIPTION = "The My Web Section Plugin";
26      public static final String I18N_NAME_KEY = "my-web-section.name";
27      public static final String I18N_DESCRIPTION_KEY = "my-web-section.description";
28  
29      public static final String ADV_MODULE_KEY = "awesome-module";
30      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
31      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
32      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
33      public static final String CUSTOM_SECTION = "system.admin/mysection";
34      public static final String WEIGHT = "20";
35      public static final String LABEL_KEY = "section.label";
36      public static final String LABEL_VALUE = "this is my label";
37      public static final String LABEL_PARAM = "label param";
38      public static final String TOOLTIP_KEY = "item.toolip";
39      public static final String TOOLTIP_VALUE = "this is a tooltip";
40  
41      @Test
42      public void basicPropertiesAreValid() throws PrompterException
43      {
44          when(prompter.prompt("Enter Plugin Module Name", "My Web Section")).thenReturn(MODULE_NAME);
45          when(prompter.prompt("Enter Location (e.g. system.admin/mynewsection)")).thenReturn(CUSTOM_SECTION);
46  
47          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
48          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
49  
50          WebSectionPrompter modulePrompter = new WebSectionPrompter(prompter);
51          modulePrompter.setUseAnsiColor(false);
52          setProps((WebSectionProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
53  
54          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
55          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
56          assertEquals("wrong description", DESCRIPTION, props.getDescription());
57          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
58          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
59          assertEquals("wrong location", CUSTOM_SECTION, props.getLocation());
60      }
61  
62      @Test
63      public void advancedPropertiesAreValid() throws PrompterException
64      {
65          when(prompter.prompt("Enter Plugin Module Name", "My Web Section")).thenReturn(MODULE_NAME);
66          when(prompter.prompt("Enter Location (e.g. system.admin/mynewsection)")).thenReturn(CUSTOM_SECTION);
67  
68          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
69          when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
70          when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
71          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
72          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
73          when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
74          when(prompter.prompt("Enter Label Key", "my-web-section.label")).thenReturn(LABEL_KEY);
75          when(prompter.prompt("Enter Label Value", "My Web Section")).thenReturn(LABEL_VALUE);
76          when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
77                  .thenReturn("N");
78          when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
79          when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
80          when(prompter.prompt("Add Tooltip?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
81                  .thenReturn("N");
82          when(prompter.prompt("Enter Tooltip Key", "awesome-module.tooltip")).thenReturn(TOOLTIP_KEY);
83          when(prompter.prompt("Enter Tooltip Value", "My Web Section Tooltip")).thenReturn(TOOLTIP_VALUE);
84          when(prompter.prompt("Add Tooltip Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
85          when(prompter.prompt("Add Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
86                  .thenReturn("N");
87          when(prompter.prompt("params:\nparamKey->paramVal\nAdd Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
88  
89  
90          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
91  
92          WebSectionPrompter modulePrompter = new WebSectionPrompter(prompter);
93          modulePrompter.setUseAnsiColor(false);
94          setProps((WebSectionProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
95  
96          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
97          assertEquals("wrong module key", ADV_MODULE_KEY, props.getModuleKey());
98          assertEquals("wrong description", ADV_DESCRIPTION, props.getDescription());
99          assertEquals("wrong i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
100         assertEquals("wrong i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
101         assertEquals("wrong location", CUSTOM_SECTION, props.getLocation());
102         assertEquals("wrong weight", WEIGHT, props.getWeight());
103 
104         assertAdvancedCommonProps();
105 
106         //custom context-provider name check
107         assertEquals("wrong context provider", CUSTOM_CONTEXT_PROVIDER, props.getContextProvider());
108 
109         //custom condition name check
110         Condition condition = (Condition) ((Conditions) props.getConditions()
111                 .get(0)).getConditions()
112                 .get(0);
113         assertEquals("wrong condition name", CUSTOM_CONDITION, condition.getFullyQualifiedClassName());
114     }
115 
116     @Test
117     public void providerContextFromListIsValid() throws PrompterException
118     {
119         SortedMap<String, String> providersMap = new TreeMap<String, String>();
120         providersMap.put("HeightContextProvider", "com.atlassian.test.HeightContextProvider");
121         providersMap.put("WidthContextProvider", "com.atlassian.test.WidthContextProvider");
122 
123         contextProviderFactory.setProvidersMap(providersMap);
124 
125         when(prompter.prompt("Enter Plugin Module Name", "My Web Section")).thenReturn(MODULE_NAME);
126         when(prompter.prompt("Enter Location (e.g. system.admin/mynewsection)")).thenReturn(CUSTOM_SECTION);
127 
128         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
129         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
130         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
131         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
132         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
133         when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
134         when(prompter.prompt("Enter Label Key", "my-web-section.label")).thenReturn(LABEL_KEY);
135         when(prompter.prompt("Enter Label Value", "My Web Section")).thenReturn(LABEL_VALUE);
136         when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
137                 .thenReturn("N");
138         when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
139         when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
140         when(prompter.prompt("Add Tooltip?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
141                 .thenReturn("N");
142         when(prompter.prompt("Enter Tooltip Key", "awesome-module.tooltip")).thenReturn(TOOLTIP_KEY);
143         when(prompter.prompt("Enter Tooltip Value", "My Web Section Tooltip")).thenReturn(TOOLTIP_VALUE);
144         when(prompter.prompt("Add Tooltip Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
145         when(prompter.prompt("Add Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
146                 .thenReturn("N");
147         when(prompter.prompt("params:\nparamKey->paramVal\nAdd Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
148 
149         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");
150 
151         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
152 
153         WebSectionPrompter modulePrompter = new WebSectionPrompter(prompter);
154         modulePrompter.setUseAnsiColor(false);
155         setProps((WebSectionProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
156 
157         assertEquals("wrong context provider", "com.atlassian.test.WidthContextProvider", props.getContextProvider());
158 
159     }
160 
161     @Test
162     public void conditionFromListIsValid() throws PrompterException
163     {
164         SortedMap<String, String> conditionMap = new TreeMap<String, String>();
165         conditionMap.put("NoFacialHairCondition", "com.atlassian.test.NoFacialHairCondition");
166         conditionMap.put("HasGlobalAdminPermissionCondition", "com.atlassian.test.HasGlobalAdminPermissionCondition");
167 
168         conditionFactory.setConditions(conditionMap);
169 
170         when(prompter.prompt("Enter Plugin Module Name", "My Web Section")).thenReturn(MODULE_NAME);
171         when(prompter.prompt("Enter Location (e.g. system.admin/mynewsection)")).thenReturn(CUSTOM_SECTION);
172 
173         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
174         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
175         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
176         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
177         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
178         when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
179         when(prompter.prompt("Enter Label Key", "my-web-section.label")).thenReturn(LABEL_KEY);
180         when(prompter.prompt("Enter Label Value", "My Web Section")).thenReturn(LABEL_VALUE);
181         when(prompter.prompt("Add Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
182                 .thenReturn("N");
183         when(prompter.prompt("values:\nlabel param\nAdd Label Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
184         when(prompter.prompt("Enter Param Value")).thenReturn(LABEL_PARAM);
185         when(prompter.prompt("Add Tooltip?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
186                 .thenReturn("N");
187         when(prompter.prompt("Enter Tooltip Key", "awesome-module.tooltip")).thenReturn(TOOLTIP_KEY);
188         when(prompter.prompt("Enter Tooltip Value", "My Web Section Tooltip")).thenReturn(TOOLTIP_VALUE);
189         when(prompter.prompt("Add Tooltip Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
190         when(prompter.prompt("Add Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
191                 .thenReturn("N");
192         when(prompter.prompt("params:\nparamKey->paramVal\nAdd Plugin Module Param?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
193 
194         when(prompter.prompt("Choose A Condition\n1: HasGlobalAdminPermissionCondition\n2: NoFacialHairCondition\n3: Custom Condition\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
195 
196         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
197 
198         WebSectionPrompter modulePrompter = new WebSectionPrompter(prompter);
199         modulePrompter.setUseAnsiColor(false);
200         setProps((WebSectionProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
201 
202         Condition condition = (Condition) ((Conditions) props.getConditions()
203                 .get(0)).getConditions()
204                 .get(0);
205         assertEquals("wrong condition name", "com.atlassian.test.NoFacialHairCondition", condition.getFullyQualifiedClassName());
206 
207     }
208 }