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