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