1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import java.util.Arrays;
4   import java.util.List;
5   import java.util.Map;
6   
7   import com.atlassian.core.util.map.EasyMap;
8   import com.atlassian.maven.plugins.amps.codegen.jira.CustomFieldTypeFactory;
9   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
10  import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractPrompterTest;
11  import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
12  import com.atlassian.plugins.codegen.modules.common.Resource;
13  import com.atlassian.plugins.codegen.modules.jira.CustomFieldProperties;
14  
15  import org.apache.commons.lang.StringUtils;
16  import org.codehaus.plexus.components.interactivity.Prompter;
17  import org.codehaus.plexus.components.interactivity.PrompterException;
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import static com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter.MODULE_DESCRIP_PROMPT;
22  import static com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter.MODULE_KEY_PROMPT;
23  import static com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter.MODULE_NAME_PROMPT;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  /**
30   * @since 3.6
31   */
32  public class CustomFieldPrompterTest extends AbstractPrompterTest
33  {
34      public static final String PACKAGE = "com.atlassian.plugins.jira.customfields";
35      public static final String CLASSNAME = "MyCustomField";
36      public static final String MODULE_NAME = "My Custom Field";
37      public static final String MODULE_KEY = "my-custom-field";
38      public static final String DESCRIPTION = "The My Custom Field Plugin";
39      public static final String I18N_NAME_KEY = "my-custom-field.name";
40      public static final String I18N_DESCRIPTION_KEY = "my-custom-field.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      public static final String RESOURCE_NAME = "view";
49      public static final String RESOURCE_VM_PATH = "templates/resource.vm";
50  
51      Prompter prompter;
52      TestingCustomFieldTypeFactory fieldTypeFactory;
53  
54      @Before
55      public void setup()
56      {
57          prompter = mock(Prompter.class);
58          fieldTypeFactory = new TestingCustomFieldTypeFactory();
59          fieldTypeFactory.setFieldTypes(EasyMap.build("AbstractCustomField", "com.example.AbstractCustomField", "AnotherCustomField", "com.example.AnotherCustomField"));
60      }
61  
62      @Test
63      public void basicPropertiesAreValid() throws PrompterException
64      {
65          when(prompter.prompt("Enter New Classname", "MyCustomField")).thenReturn(CLASSNAME);
66          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.customfields")).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          CustomFieldPrompter modulePrompter = new CustomFieldPrompter(prompter);
71          modulePrompter.setUseAnsiColor(false);
72          CustomFieldProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
73  
74          assertEquals("wrong class", CLASSNAME, props.getClassId().getName());
75          assertEquals("wrong class package", PACKAGE, props.getClassId().getPackage());
76          assertEquals("wrong module name", MODULE_NAME, props.getModuleName());
77          assertEquals("wrong module key", MODULE_KEY, props.getModuleKey());
78          assertEquals("wrong description", DESCRIPTION, props.getDescription());
79          assertEquals("wrong i18n name key", I18N_NAME_KEY, props.getNameI18nKey());
80          assertEquals("wrong i18n desc key", I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
81      }
82  
83      @Test
84      public void advancedPropertiesAreValid() throws PrompterException
85      {
86          when(prompter.prompt("Enter New Classname", "MyCustomField")).thenReturn(CLASSNAME);
87          when(prompter.prompt("Enter Package Name", AbstractModulePrompter.DEFAULT_BASE_PACKAGE + ".jira.customfields")).thenReturn(PACKAGE);
88          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
89  
90          when(prompter.prompt(MODULE_NAME_PROMPT, MODULE_NAME)).thenReturn(ADV_MODULE_NAME);
91          when(prompter.prompt(MODULE_KEY_PROMPT, MODULE_KEY)).thenReturn(ADV_MODULE_KEY);
92          when(prompter.prompt(MODULE_DESCRIP_PROMPT, DESCRIPTION)).thenReturn(ADV_DESCRIPTION);
93          when(prompter.prompt("i18n Name Key", I18N_NAME_KEY)).thenReturn(ADV_I18N_NAME_KEY);
94          when(prompter.prompt("i18n Description Key", I18N_DESCRIPTION_KEY)).thenReturn(ADV_I18N_DESCRIPTION_KEY);
95  
96          when(prompter.prompt("Choose A Custom Field Type To Extend\n1: AbstractCustomField\n2: AnotherCustomField\nChoose a number: ", Arrays.asList("1", "2"), "")).thenReturn("1");
97  
98          when(prompter.prompt("Add Resource", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
99                  .thenReturn("N");
100         when(prompter.prompt("Enter Resource Name")).thenReturn(RESOURCE_NAME)
101                 .thenReturn("");
102         when(prompter.prompt("Enter Location (path to resource file)")).thenReturn(RESOURCE_VM_PATH);
103 
104         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
105 
106         CustomFieldPrompter modulePrompter = new CustomFieldPrompter(prompter);
107         modulePrompter.setUseAnsiColor(false);
108         CustomFieldProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
109 
110         assertEquals("wrong adv class", CLASSNAME, props.getClassId().getName());
111         assertEquals("wrong adv package", PACKAGE, props.getClassId().getPackage());
112         assertEquals("wrong adv module name", ADV_MODULE_NAME, props.getModuleName());
113         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
114         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
115         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
116         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
117 
118         //resources
119         List<Resource> resources = props.getResources();
120         assertTrue("resources not found", !resources.isEmpty());
121         assertEquals("wrong number of resources", 1, resources.size());
122 
123         Resource viewResource = resources.get(0);
124 
125         assertEquals("wrong css resource name", RESOURCE_NAME, viewResource.getName());
126         assertTrue("name pattern found when name is set", StringUtils.isBlank(viewResource.getNamePattern()));
127         assertEquals("wrong resource type", "velocity", viewResource.getType());
128         assertEquals("wrong resource location", RESOURCE_VM_PATH, viewResource.getLocation());
129 
130         assertEquals("wrong super class", "com.example.AbstractCustomField", props.getFullyQualifiedClassToExtend());
131     }
132 
133     protected class TestingCustomFieldTypeFactory extends CustomFieldTypeFactory
134     {
135         public void setFieldTypes(Map<String, String> types)
136         {
137             fields = types;
138         }
139     }
140 }