1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import java.util.List;
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.Resource;
9   import com.atlassian.plugins.codegen.modules.jira.UserFormatProperties;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.codehaus.plexus.components.interactivity.Prompter;
13  import org.codehaus.plexus.components.interactivity.PrompterException;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertTrue;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.when;
21  
22  /**
23   * @since 3.5
24   */
25  public class UserFormatPrompterTest extends AbstractPrompterTest
26  {
27      public static final String PACKAGE = "com.atlassian.plugins.jira.userformat";
28      public static final String CLASSNAME = "MyUserFormat";
29      public static final String MODULE_NAME = "My User Format";
30      public static final String MODULE_KEY = "my-user-format";
31      public static final String DESCRIPTION = "The My User Format Plugin";
32      public static final String I18N_NAME_KEY = "my-user-format.name";
33      public static final String I18N_DESCRIPTION_KEY = "my-user-format.description";
34  
35      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
36      public static final String ADV_MODULE_KEY = "awesome-module";
37      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
38      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
39      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
40  
41      public static final String TYPE_NAME = "myType";
42      public static final String TYPE_KEY = "my.type.key";
43      public static final String RESOURCE_NAME = "view";
44      public static final String RESOURCE_VM_PATH = "templates/resource.vm";
45  
46      Prompter prompter;
47  
48      @Before
49      public void setup()
50      {
51          prompter = mock(Prompter.class);
52      }
53  
54      @Test
55      public void basicPropertiesAreValid() throws PrompterException
56      {
57          when(prompter.prompt("Enter New Classname", "MyUserFormat")).thenReturn(CLASSNAME);
58          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.userformat")).thenReturn(PACKAGE);
59          when(prompter.prompt("Enter Type Name")).thenReturn(TYPE_NAME);
60          when(prompter.prompt("Enter Type i18n Key")).thenReturn(TYPE_KEY);
61  
62          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
63          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
64  
65          UserFormatPrompter modulePrompter = new UserFormatPrompter(prompter);
66          modulePrompter.setUseAnsiColor(false);
67          UserFormatProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
68  
69          assertEquals("wrong class", CLASSNAME, props.getClassname());
70          assertEquals("wrong class package", PACKAGE, props.getPackage());
71          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
72          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
73          assertEquals("wrong description", DESCRIPTION, props.getDescription());
74          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
75          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
76          assertEquals("wrong type name", TYPE_NAME, props.getTypeName());
77          assertEquals("wrong type key", TYPE_KEY, props.getTypeKey());
78      }
79  
80      @Test
81      public void advancedPropertiesAreValid() throws PrompterException
82      {
83          when(prompter.prompt("Enter New Classname", "MyUserFormat")).thenReturn(CLASSNAME);
84          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.userformat")).thenReturn(PACKAGE);
85          when(prompter.prompt("Enter Type Name")).thenReturn(TYPE_NAME);
86          when(prompter.prompt("Enter Type i18n Key")).thenReturn(TYPE_KEY);
87          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
88  
89          when(prompter.prompt("Plugin Name", MODULE_NAME)).thenReturn(ADV_MODULE_NAME);
90          when(prompter.prompt("Plugin Key", MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
91          when(prompter.prompt("Plugin Description", DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
92          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
93          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
94  
95          when(prompter.prompt("Add Resource", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
96                  .thenReturn("N");
97          when(prompter.prompt("Enter Resource Name")).thenReturn(RESOURCE_NAME)
98                  .thenReturn("");
99          when(prompter.prompt("Enter Location (path to resource file)")).thenReturn(RESOURCE_VM_PATH);
100 
101         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
102 
103         UserFormatPrompter modulePrompter = new UserFormatPrompter(prompter);
104         modulePrompter.setUseAnsiColor(false);
105         UserFormatProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
106 
107         assertEquals("wrong adv class", CLASSNAME, props.getClassname());
108         assertEquals("wrong adv package", PACKAGE, props.getPackage());
109         assertEquals("wrong adv module name", ADV_MODULE_NAME, props.getModuleName());
110         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
111         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
112         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
113         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
114         assertEquals("wrong type name", TYPE_NAME, props.getTypeName());
115         assertEquals("wrong type key", TYPE_KEY, props.getTypeKey());
116 
117         //resources
118         List<Resource> resources = props.getResources();
119         assertTrue("resources not found", !resources.isEmpty());
120         assertEquals("wrong number of resources", 1, resources.size());
121 
122         Resource viewResource = resources.get(0);
123 
124         assertEquals("wrong css resource name", RESOURCE_NAME, viewResource.getName());
125         assertTrue("name pattern found when name is set", StringUtils.isBlank(viewResource.getNamePattern()));
126         assertEquals("wrong resource type", "velocity", viewResource.getType());
127         assertEquals("wrong resource location", RESOURCE_VM_PATH, viewResource.getLocation());
128     }
129 }