1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import java.util.Arrays;
4   
5   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractPrompterTest;
6   import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
7   import com.atlassian.plugins.codegen.modules.jira.KeyboardShortcutProperties;
8   
9   import org.codehaus.plexus.components.interactivity.Prompter;
10  import org.codehaus.plexus.components.interactivity.PrompterException;
11  import org.junit.Before;
12  import org.junit.Test;
13  
14  import static org.junit.Assert.*;
15  import static org.mockito.Mockito.mock;
16  import static org.mockito.Mockito.when;
17  
18  /**
19   * @since 3.5
20   */
21  public class KeyboardShortcutPrompterTest extends AbstractPrompterTest
22  {
23      public static final String MODULE_NAME = "My KB Shortcut";
24      public static final String MODULE_KEY = "my-kb-shortcut";
25      public static final String DESCRIPTION = "The My KB Shortcut Plugin";
26      public static final String I18N_NAME_KEY = "my-kb-shortcut.name";
27      public static final String I18N_DESCRIPTION_KEY = "my-kb-shortcut.description";
28  
29      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
30      public static final String ADV_MODULE_KEY = "awesome-module";
31      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
32      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
33      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
34  
35      Prompter prompter;
36  
37      @Before
38      public void setup()
39      {
40          prompter = mock(Prompter.class);
41      }
42  
43      @Test
44      public void basicPropertiesAreValid() throws PrompterException
45      {
46          when(prompter.prompt("Enter Keyboard Shortcut Name", "My Keyboard Shortcut")).thenReturn(MODULE_NAME);
47          when(prompter.prompt("Enter Shortcut Character")).thenReturn("m");
48          when(prompter.prompt("Choose A Context\n1: global\n2: issueaction\n3: issuenavigation\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
49          when(prompter.prompt("Choose An Operation\n1: click\n2: evaluate\n3: execute\n4: followLink\n5: goTo\n6: moveToAndClick\n7: moveToAndFocus\n8: moveToNextItem\n9: moveToPrevItem\nChoose a number: ", Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"), "")).thenReturn("2");
50          when(prompter.prompt("Enter Operation Value")).thenReturn("some:selector");
51  
52          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
53          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
54  
55          KeyboardShortcutPrompter modulePrompter = new KeyboardShortcutPrompter(prompter);
56          modulePrompter.setUseAnsiColor(false);
57          KeyboardShortcutProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
58  
59          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
60          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
61          assertEquals("wrong description", DESCRIPTION, props.getDescription());
62          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
63          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
64          assertEquals("wrong shortcut key", "m", props.getShortcut());
65          assertEquals("wrong order", "10", props.getOrder());
66          assertFalse("wrong hidden value", props.isHidden());
67          assertEquals("wrong operation type", "evaluate", props.getOperationType());
68          assertEquals("wrong operation value", "some:selector", props.getOperationValue());
69          assertEquals("wrong context", "issueaction", props.getContext());
70  
71      }
72  
73      @Test
74      public void advancedPropertiesAreValid() throws PrompterException
75      {
76          when(prompter.prompt("Enter Keyboard Shortcut Name", "My Keyboard Shortcut")).thenReturn(MODULE_NAME);
77          when(prompter.prompt("Enter Shortcut Character")).thenReturn("m");
78          when(prompter.prompt("Choose A Context\n1: global\n2: issueaction\n3: issuenavigation\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
79          when(prompter.prompt("Choose An Operation\n1: click\n2: evaluate\n3: execute\n4: followLink\n5: goTo\n6: moveToAndClick\n7: moveToAndFocus\n8: moveToNextItem\n9: moveToPrevItem\nChoose a number: ", Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"), "")).thenReturn("2");
80          when(prompter.prompt("Enter Operation Value")).thenReturn("some:selector");
81  
82          when(prompter.prompt("Hidden?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("y");
83          when(prompter.prompt("Order", "10")).thenReturn("50");
84          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
85  
86          when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
87          when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
88          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
89          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
90  
91          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
92  
93          KeyboardShortcutPrompter modulePrompter = new KeyboardShortcutPrompter(prompter);
94          modulePrompter.setUseAnsiColor(false);
95          KeyboardShortcutProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
96  
97          assertEquals("wrong adv module name", MODULE_NAME, props.getModuleName());
98          assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
99          assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
100         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
101         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
102 
103         assertEquals("wrong shortcut key", "m", props.getShortcut());
104         assertEquals("wrong order", "50", props.getOrder());
105         assertTrue("wrong hidden value", props.isHidden());
106         assertEquals("wrong operation type", "evaluate", props.getOperationType());
107         assertEquals("wrong operation value", "some:selector", props.getOperationValue());
108         assertEquals("wrong context", "issueaction", props.getContext());
109     }
110 }