1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import java.util.Arrays;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import com.atlassian.maven.plugins.amps.codegen.jira.ActionTypeFactory;
8   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
9   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractPrompterTest;
10  import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
11  import com.atlassian.plugins.codegen.modules.jira.WorkflowPostFunctionProperties;
12  
13  import org.apache.commons.lang.StringUtils;
14  import org.codehaus.plexus.components.interactivity.Prompter;
15  import org.codehaus.plexus.components.interactivity.PrompterException;
16  import org.junit.After;
17  import org.junit.Before;
18  import org.junit.Test;
19  
20  import static com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter.MODULE_DESCRIP_PROMPT;
21  import static com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter.MODULE_KEY_PROMPT;
22  import static com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter.MODULE_NAME_PROMPT;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  /**
29   * @since 3.6
30   */
31  public class WorkflowPostFunctionPrompterTest extends AbstractPrompterTest
32  {
33      public static final String PACKAGE = "com.atlassian.plugins.jira.workflow";
34      public static final String CLASSNAME = "MyPostFunction";
35      public static final String FACTORY_CLASSNAME = "MyPostFunctionFactory";
36      public static final String MODULE_NAME = "My Post Function";
37      public static final String MODULE_KEY = "my-post-function";
38      public static final String DESCRIPTION = "The My Post Function Plugin";
39      public static final String I18N_NAME_KEY = "my-post-function.name";
40      public static final String I18N_DESCRIPTION_KEY = "my-post-function.description";
41  
42      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
43      public static final String ADV_MODULE_KEY = "awesome-module";
44      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
45      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
46      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
47  
48      Prompter prompter;
49      TestingActionTypeFactory actionTypeFactory;
50  
51      @Before
52      public void setup()
53      {
54          prompter = mock(Prompter.class);
55          actionTypeFactory = new TestingActionTypeFactory();
56          actionTypeFactory.setActionTypes(Arrays.asList("common", "initial", "global", "ordinary"));
57      }
58  
59      @After
60      public void resetActionTypes()
61      {
62          actionTypeFactory.setActionTypes(Collections.<String>emptyList());
63      }
64  
65      @Test
66      public void basicPropertiesAreValid() throws PrompterException
67      {
68          when(prompter.prompt("Enter New Classname", "MyPostFunction")).thenReturn(CLASSNAME);
69          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.workflow")).thenReturn(PACKAGE);
70          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
71          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
72  
73          WorkflowPostFunctionPrompter modulePrompter = new WorkflowPostFunctionPrompter(prompter);
74          modulePrompter.setUseAnsiColor(false);
75          WorkflowPostFunctionProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
76  
77          assertEquals("wrong function class", CLASSNAME, props.getClassId().getName());
78          assertEquals("wrong factory class", FACTORY_CLASSNAME, props.getFactoryName());
79          assertEquals("wrong class package", PACKAGE, props.getClassId().getPackage());
80          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
81          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
82          assertEquals("wrong description", DESCRIPTION, props.getDescription());
83          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
84          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
85          assertTrue("orderable should be blank", StringUtils.isBlank(props.getOrderable()));
86          assertTrue("unique should be blank", StringUtils.isBlank(props.getUnique()));
87          assertTrue("deletable should be blank", StringUtils.isBlank(props.getDeletable()));
88          assertTrue("addable should be blank", StringUtils.isBlank(props.getAddable()));
89      }
90  
91      @Test
92      public void advancedPropertiesAreValid() throws PrompterException
93      {
94          when(prompter.prompt("Enter New Classname", "MyPostFunction")).thenReturn(CLASSNAME);
95          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.workflow")).thenReturn(PACKAGE);
96          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
97  
98          when(prompter.prompt(MODULE_NAME_PROMPT, MODULE_NAME)).thenReturn(ADV_MODULE_NAME);
99          when(prompter.prompt(MODULE_KEY_PROMPT, MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
100         when(prompter.prompt(MODULE_DESCRIP_PROMPT, DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
101         when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
102         when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
103 
104         when(prompter.prompt("Is Function Orderable?", PluginModulePrompter.YN_ANSWERS, "Y")).thenReturn("y");
105         when(prompter.prompt("Is Function Unique?", PluginModulePrompter.YN_ANSWERS, "Y")).thenReturn("n");
106         when(prompter.prompt("Is Function Deletable?", PluginModulePrompter.YN_ANSWERS, "Y")).thenReturn("y");
107 
108         when(prompter.prompt("Define Action Types? (addable)", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("y");
109         when(prompter.prompt("Choose A Type\n1: common\n2: global\n3: initial\n4: ordinary\nChoose a number: ", Arrays.asList("1", "2", "3", "4"), "1")).thenReturn("2");
110         when(prompter.prompt("Add Action Type?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("y")
111                 .thenReturn("n");
112         when(prompter.prompt("Choose A Type\n1: common\n2: initial\n3: ordinary\nChoose a number: ", Arrays.asList("1", "2", "3"), "1")).thenReturn("1");
113 
114         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
115 
116         WorkflowPostFunctionPrompter modulePrompter = new WorkflowPostFunctionPrompter(prompter);
117         modulePrompter.setUseAnsiColor(false);
118         WorkflowPostFunctionProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
119 
120         assertEquals("wrong function class", CLASSNAME, props.getClassId().getName());
121         assertEquals("wrong factory class", FACTORY_CLASSNAME, props.getFactoryName());
122         assertEquals("wrong adv package", PACKAGE, props.getClassId().getPackage());
123         assertEquals("wrong adv module name", ADV_MODULE_NAME, props.getModuleName());
124         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
125         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
126         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
127         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
128         assertEquals("wrong orderable", "true", props.getOrderable());
129         assertEquals("wrong unique", "false", props.getUnique());
130         assertEquals("wrong deletable", "true", props.getDeletable());
131         assertEquals("wrong addable", "global,common", props.getAddable());
132     }
133 
134     protected class TestingActionTypeFactory extends ActionTypeFactory
135     {
136         public void setActionTypes(List<String> types)
137         {
138             availableActionTypes = types;
139         }
140     }
141 }