1   package com.atlassian.maven.plugins.amps.codegen.prompter.common;
2   
3   import java.util.Arrays;
4   
5   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
6   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractPrompterTest;
7   import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
8   import com.atlassian.plugins.codegen.modules.common.RESTProperties;
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 RESTPrompterTest extends AbstractPrompterTest
23  {
24      public static final String PACKAGE = "com.atlassian.plugins.rest";
25      public static final String CLASSNAME = "MyRestResource";
26      public static final String MODULE_NAME = "My Rest Resource";
27      public static final String MODULE_KEY = "my-rest-resource";
28      public static final String DESCRIPTION = "The My Rest Resource Plugin";
29      public static final String I18N_NAME_KEY = "my-rest-resource.name";
30      public static final String I18N_DESCRIPTION_KEY = "my-rest-resource.description";
31  
32      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
33      public static final String ADV_MODULE_KEY = "awesome-module";
34      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
35      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
36      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
37  
38      public static final String PATH = "/helloworld";
39      public static final String VERSION = "1.1";
40  
41      Prompter prompter;
42  
43      @Before
44      public void setup()
45      {
46          prompter = mock(Prompter.class);
47      }
48  
49      @Test
50      public void basicPropertiesAreValid() throws PrompterException
51      {
52          when(prompter.prompt("Enter New Classname", "MyRestResource")).thenReturn(CLASSNAME);
53          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".rest")).thenReturn(PACKAGE);
54          when(prompter.prompt("Enter REST Path", "/myrestresource")).thenReturn(PATH);
55          when(prompter.prompt("Enter Version", "1.0")).thenReturn(VERSION);
56          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
57          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
58  
59          RESTPrompter modulePrompter = new RESTPrompter(prompter);
60          modulePrompter.setUseAnsiColor(false);
61          RESTProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
62  
63          assertEquals("wrong class", CLASSNAME, props.getClassname());
64          assertEquals("wrong class package", PACKAGE, props.getPackage());
65          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
66          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
67          assertEquals("wrong description", DESCRIPTION, props.getDescription());
68          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
69          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
70          assertEquals("wrong path", PATH, props.getPath());
71          assertEquals("wrong version", VERSION, props.getVersion());
72      }
73  
74      @Test
75      public void advancedPropertiesAreValid() throws PrompterException
76      {
77          String packageToScan = "com.atlassian.plugins.rest.helloworld";
78  
79          when(prompter.prompt("Enter New Classname", "MyRestResource")).thenReturn(CLASSNAME);
80          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".rest")).thenReturn(PACKAGE);
81          when(prompter.prompt("Enter REST Path", "/myrestresource")).thenReturn(PATH);
82          when(prompter.prompt("Enter Version", "1.0")).thenReturn(VERSION);
83          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
84  
85          when(prompter.prompt("Plugin Name", MODULE_NAME)).thenReturn(ADV_MODULE_NAME);
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          when(prompter.prompt("Add Package To Scan?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("y");
91          when(prompter.prompt("Enter Package")).thenReturn(packageToScan);
92          when(prompter.prompt("values:\n" + packageToScan + "\nAdd Package To Scan?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
93  
94          when(prompter.prompt("Add Dispatcher?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("y")
95                  .thenReturn("N");
96          when(prompter.prompt("Choose A Dispatcher\n1: REQUEST\n2: INCLUDE\n3: FORWARD\n4: ERROR\nChoose a number: ", Arrays.asList("1", "2", "3", "4"), "1")).thenReturn("3");
97          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
98  
99          RESTPrompter modulePrompter = new RESTPrompter(prompter);
100         modulePrompter.setUseAnsiColor(false);
101         RESTProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
102 
103         assertEquals("wrong adv class", CLASSNAME, props.getClassname());
104         assertEquals("wrong adv package", PACKAGE, props.getPackage());
105         assertEquals("wrong adv module name", ADV_MODULE_NAME, props.getModuleName());
106         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
107         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
108         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
109         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
110         assertEquals("wrong path", PATH, props.getPath());
111         assertEquals("wrong version", VERSION, props.getVersion());
112 
113         assertEquals("wrong number of packages", 1, props.getPackagesToScan()
114                 .size());
115         assertEquals("wrong package", packageToScan, props.getPackagesToScan()
116                 .get(0));
117 
118         assertEquals("wrong number of dispatcher", 1, props.getDispatchers()
119                 .size());
120         assertEquals("wrong dispatcher", "FORWARD", props.getDispatchers()
121                 .get(0));
122     }
123 }