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.WebPanelProperties;
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 WebPanelPrompterTest extends AbstractWebFragmentPrompterTest<WebPanelProperties>
22  {
23      public static final String MODULE_NAME = "My Web Panel";
24      public static final String MODULE_KEY = "my-web-panel";
25      public static final String DESCRIPTION = "The My Web Panel Plugin";
26      public static final String I18N_NAME_KEY = "my-web-panel.name";
27      public static final String I18N_DESCRIPTION_KEY = "my-web-panel.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/globalsettings";
34      public static final String WEIGHT = "20";
35  
36      @Test
37      public void basicPropertiesAreValid() throws PrompterException
38      {
39          when(prompter.prompt("Enter Plugin Module Name", "My Web Panel")).thenReturn(MODULE_NAME);
40          when(prompter.prompt("Enter Location (e.g. system.admin/globalsettings)")).thenReturn(CUSTOM_SECTION);
41  
42          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
43          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
44  
45          WebPanelPrompter modulePrompter = new WebPanelPrompter(prompter);
46          modulePrompter.setUseAnsiColor(false);
47          setProps((WebPanelProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
48  
49          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
50          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
51          assertEquals("wrong description", DESCRIPTION, props.getDescription());
52          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
53          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
54          assertEquals("wrong location", CUSTOM_SECTION, props.getLocation());
55      }
56  
57      @Test
58      public void advancedPropertiesAreValid() throws PrompterException
59      {
60          when(prompter.prompt("Enter Plugin Module Name", "My Web Panel")).thenReturn(MODULE_NAME);
61          when(prompter.prompt("Enter Location (e.g. system.admin/globalsettings)")).thenReturn(CUSTOM_SECTION);
62  
63          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
64          when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
65          when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
66          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
67          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
68          when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
69  
70          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
71  
72          WebPanelPrompter modulePrompter = new WebPanelPrompter(prompter);
73          modulePrompter.setUseAnsiColor(false);
74          setProps((WebPanelProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
75  
76          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
77          assertEquals("wrong module key", ADV_MODULE_KEY, props.getModuleKey());
78          assertEquals("wrong description", ADV_DESCRIPTION, props.getDescription());
79          assertEquals("wrong i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
80          assertEquals("wrong i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
81          assertEquals("wrong location", CUSTOM_SECTION, props.getLocation());
82          assertEquals("wrong weight", WEIGHT, props.getWeight());
83  
84          assertAdvancedCommonProps();
85  
86          //custom context-provider name check
87          assertEquals("wrong context provider", CUSTOM_CONTEXT_PROVIDER, props.getContextProvider());
88  
89          //custom condition name check
90          Condition condition = (Condition) ((Conditions) props.getConditions()
91                  .get(0)).getConditions()
92                  .get(0);
93          assertEquals("wrong condition name", CUSTOM_CONDITION, condition.getFullyQualifiedClassName());
94      }
95  
96      @Test
97      public void providerContextFromListIsValid() throws PrompterException
98      {
99          SortedMap<String, String> providersMap = new TreeMap<String, String>();
100         providersMap.put("HeightContextProvider", "com.atlassian.test.HeightContextProvider");
101         providersMap.put("WidthContextProvider", "com.atlassian.test.WidthContextProvider");
102 
103         contextProviderFactory.setProvidersMap(providersMap);
104 
105         when(prompter.prompt("Enter Plugin Module Name", "My Web Panel")).thenReturn(MODULE_NAME);
106         when(prompter.prompt("Enter Location (e.g. system.admin/globalsettings)")).thenReturn(CUSTOM_SECTION);
107 
108         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
109         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
110         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
111         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
112         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
113         when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
114 
115         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");
116 
117         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
118 
119         WebPanelPrompter modulePrompter = new WebPanelPrompter(prompter);
120         modulePrompter.setUseAnsiColor(false);
121         setProps((WebPanelProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
122 
123         assertEquals("wrong context provider", "com.atlassian.test.WidthContextProvider", props.getContextProvider());
124 
125     }
126 
127     @Test
128     public void conditionFromListIsValid() throws PrompterException
129     {
130         SortedMap<String, String> conditionMap = new TreeMap<String, String>();
131         conditionMap.put("NoFacialHairCondition", "com.atlassian.test.NoFacialHairCondition");
132         conditionMap.put("HasGlobalAdminPermissionCondition", "com.atlassian.test.HasGlobalAdminPermissionCondition");
133 
134         conditionFactory.setConditions(conditionMap);
135 
136         when(prompter.prompt("Enter Plugin Module Name", "My Web Panel")).thenReturn(MODULE_NAME);
137         when(prompter.prompt("Enter Location (e.g. system.admin/globalsettings)")).thenReturn(CUSTOM_SECTION);
138 
139         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
140         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
141         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
142         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
143         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
144         when(prompter.prompt("Weight", "1000")).thenReturn(WEIGHT);
145 
146         when(prompter.prompt("Choose A Condition\n1: HasGlobalAdminPermissionCondition\n2: NoFacialHairCondition\n3: Custom Condition\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
147 
148         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
149 
150         WebPanelPrompter modulePrompter = new WebPanelPrompter(prompter);
151         modulePrompter.setUseAnsiColor(false);
152         setProps((WebPanelProperties) modulePrompter.getModulePropertiesFromInput(moduleLocation));
153 
154         Condition condition = (Condition) ((Conditions) props.getConditions()
155                 .get(0)).getConditions()
156                 .get(0);
157         assertEquals("wrong condition name", "com.atlassian.test.NoFacialHairCondition", condition.getFullyQualifiedClassName());
158     }
159 }