1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
4   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractPrompterTest;
5   import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
6   import com.atlassian.plugins.codegen.modules.jira.ActionProperties;
7   import com.atlassian.plugins.codegen.modules.jira.View;
8   import com.atlassian.plugins.codegen.modules.jira.WebworkProperties;
9   
10  import org.codehaus.plexus.components.interactivity.Prompter;
11  import org.codehaus.plexus.components.interactivity.PrompterException;
12  import org.junit.Before;
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.mockito.Mockito.mock;
17  import static org.mockito.Mockito.when;
18  
19  /**
20   * @since 3.5
21   */
22  public class WebworkPrompterTest extends AbstractPrompterTest
23  {
24      public static final String MODULE_NAME = "My Webwork";
25      public static final String MODULE_KEY = "my-webwork";
26      public static final String DESCRIPTION = "The My Webwork Plugin";
27      public static final String I18N_NAME_KEY = "my-webwork.name";
28      public static final String I18N_DESCRIPTION_KEY = "my-webwork.description";
29  
30      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
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  
36      public static final String DEFAULT_ACTION_PACKAGE = "com.example.jira.webwork";
37      public static final String DEFAULT_ACTION_NAME = "MyWebworkAction";
38      public static final String DEFAULT_ACTION_ALIAS = "MyWebwork";
39      public static final String DEFAULT_TEMPLATE_PATH = "/templates/" + MODULE_KEY + "/";
40  
41      public static final String CUSTOM_ACTION_PACKAGE = "my.custom.actions";
42      public static final String CUSTOM_ACTION_ONE_NAME = "CustomActionOne";
43      public static final String CUSTOM_ACTION_ONE_ALIAS = "ActionOne";
44  
45      public static final String CUSTOM_ACTION_TWO_NAME = "CustomActionTwo";
46      public static final String CUSTOM_ACTION_TWO_ALIAS = "ActionTwo";
47  
48      public static final String SUCCESS_VIEW_NAME = "success";
49      public static final String ACTION_ONE_VIEW_PATH_PREFIX = "/templates/actionone/";
50      public static final String SUCCESS_VIEW_PATH = ACTION_ONE_VIEW_PATH_PREFIX + "success.vm";
51  
52      public static final String ERROR_VIEW_NAME = "error";
53      public static final String ERROR_VIEW_PATH = ACTION_ONE_VIEW_PATH_PREFIX + "error.vm";
54  
55      public static final String CUSTOM_VIEW_NAME = "custom";
56      public static final String CUSTOM_VIEW_PATH = "/templates/actiontwo/custom.vm";
57  
58      Prompter prompter;
59  
60  
61      @Before
62      public void setup()
63      {
64          prompter = mock(Prompter.class);
65      }
66  
67      @Test
68      public void basicPropertiesAreValid() throws PrompterException
69      {
70          when(prompter.prompt("Enter Plugin Module Name", "My Webwork Module")).thenReturn(MODULE_NAME);
71          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
72          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
73  
74          WebworkPrompter modulePrompter = new WebworkPrompter(prompter);
75          modulePrompter.setUseAnsiColor(false);
76          WebworkProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
77  
78          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
79          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
80          assertEquals("wrong description", DESCRIPTION, props.getDescription());
81          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
82          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
83          assertEquals("wrong number of actions", 1, props.getActions()
84                  .size());
85  
86          ActionProperties action = props.getActions()
87                  .get(0);
88          assertEquals("wrong action package", DEFAULT_ACTION_PACKAGE, action.getPackage());
89          assertEquals("wrong action classname", DEFAULT_ACTION_NAME, action.getClassname());
90          assertEquals("wrong action alias", DEFAULT_ACTION_ALIAS, action.getAlias());
91  
92          assertEquals("wrong number of views", 3, action.getViews()
93                  .size());
94          View success = action.getViews()
95                  .get(0);
96          View input = action.getViews()
97                  .get(1);
98          View error = action.getViews()
99                  .get(2);
100 
101         assertEquals("wrong view name", SUCCESS_VIEW_NAME, success.getName());
102         assertEquals("wrong view path", DEFAULT_TEMPLATE_PATH + "success.vm", success.getPath());
103 
104         assertEquals("wrong view name", "input", input.getName());
105         assertEquals("wrong view path", DEFAULT_TEMPLATE_PATH + "input.vm", input.getPath());
106 
107         assertEquals("wrong view name", ERROR_VIEW_NAME, error.getName());
108         assertEquals("wrong view path", DEFAULT_TEMPLATE_PATH + "error.vm", error.getPath());
109     }
110 
111     @Test
112     public void advancedPropertiesAreValid() throws PrompterException
113     {
114         when(prompter.prompt("Enter Plugin Module Name", "My Webwork Module")).thenReturn(MODULE_NAME);
115         when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
116         when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
117         when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
118         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
119         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
120 
121         when(prompter.prompt("Enter Action Classname", "MyActionClass")).thenReturn(CUSTOM_ACTION_ONE_NAME)
122                 .thenReturn(CUSTOM_ACTION_TWO_NAME);
123         when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.webwork")).thenReturn(CUSTOM_ACTION_PACKAGE);
124         when(prompter.prompt("Enter Package Name", CUSTOM_ACTION_PACKAGE)).thenReturn(CUSTOM_ACTION_PACKAGE);
125         when(prompter.prompt("Enter Alias", CUSTOM_ACTION_ONE_NAME)).thenReturn(CUSTOM_ACTION_ONE_ALIAS);
126         when(prompter.prompt("Enter Alias", CUSTOM_ACTION_TWO_NAME)).thenReturn(CUSTOM_ACTION_TWO_ALIAS);
127 
128         when(prompter.prompt("Enter View Name", "success")).thenReturn(SUCCESS_VIEW_NAME)
129                 .thenReturn(ERROR_VIEW_NAME)
130                 .thenReturn(CUSTOM_VIEW_NAME);
131 
132         String prompterViewPathOne = "/templates/" + ADV_MODULE_KEY + "/" + CUSTOM_ACTION_ONE_NAME.toLowerCase() + "/";
133         String prompterViewPathTwo = "/templates/" + ADV_MODULE_KEY + "/" + CUSTOM_ACTION_TWO_NAME.toLowerCase() + "/";
134 
135         when(prompter.prompt("Enter Template Path", prompterViewPathOne + "success.vm")).thenReturn(SUCCESS_VIEW_PATH);
136         when(prompter.prompt("Enter Template Path", ACTION_ONE_VIEW_PATH_PREFIX + "error.vm")).thenReturn(ERROR_VIEW_PATH);
137         when(prompter.prompt("Enter Template Path", prompterViewPathTwo + "custom.vm")).thenReturn(CUSTOM_VIEW_PATH);
138 
139         when(prompter.prompt("Add Another Action?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
140                 .thenReturn("N");
141         when(prompter.prompt("Add Another View?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
142                 .thenReturn("N");
143 
144         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
145 
146         WebworkPrompter modulePrompter = new WebworkPrompter(prompter);
147         modulePrompter.setUseAnsiColor(false);
148         WebworkProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
149 
150         assertEquals("wrong adv module name", MODULE_NAME, props.getModuleName());
151         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
152         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
153         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
154         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
155 
156         assertEquals("wrong number of actions", 2, props.getActions()
157                 .size());
158 
159         ActionProperties actionOne = props.getActions()
160                 .get(0);
161         ActionProperties actionTwo = props.getActions()
162                 .get(1);
163 
164         assertEquals("wrong action package", CUSTOM_ACTION_PACKAGE, actionOne.getPackage());
165         assertEquals("wrong action classname", CUSTOM_ACTION_ONE_NAME, actionOne.getClassname());
166         assertEquals("wrong action alias", CUSTOM_ACTION_ONE_ALIAS, actionOne.getAlias());
167 
168         assertEquals("wrong action package", CUSTOM_ACTION_PACKAGE, actionOne.getPackage());
169         assertEquals("wrong action classname", CUSTOM_ACTION_TWO_NAME, actionTwo.getClassname());
170         assertEquals("wrong action alias", CUSTOM_ACTION_TWO_ALIAS, actionTwo.getAlias());
171 
172         assertEquals("wrong number of action one views", 2, actionOne.getViews()
173                 .size());
174         View success = actionOne.getViews()
175                 .get(0);
176         View error = actionOne.getViews()
177                 .get(1);
178 
179         assertEquals("wrong view name", SUCCESS_VIEW_NAME, success.getName());
180         assertEquals("wrong view path", SUCCESS_VIEW_PATH, success.getPath());
181 
182         assertEquals("wrong view name", ERROR_VIEW_NAME, error.getName());
183         assertEquals("wrong view path", ERROR_VIEW_PATH, error.getPath());
184 
185         assertEquals("wrong number of action one views", 1, actionTwo.getViews()
186                 .size());
187         View custom = actionTwo.getViews()
188                 .get(0);
189 
190         assertEquals("wrong view name", CUSTOM_VIEW_NAME, custom.getName());
191         assertEquals("wrong view path", CUSTOM_VIEW_PATH, custom.getPath());
192     }
193 }