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.CustomFieldSearcherFactory;
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.CustomFieldSearcherProperties;
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 CustomFieldSearcherPrompterTest extends AbstractPrompterTest
33  {
34      public static final String PACKAGE = "com.atlassian.plugins.jira.customfields";
35      public static final String CLASSNAME = "MyCustomFieldSearcher";
36      public static final String MODULE_NAME = "My Custom Field Searcher";
37      public static final String MODULE_KEY = "my-custom-field-searcher";
38      public static final String DESCRIPTION = "The My Custom Field Searcher Plugin";
39      public static final String I18N_NAME_KEY = "my-custom-field-searcher.name";
40      public static final String I18N_DESCRIPTION_KEY = "my-custom-field-searcher.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      TestingCustomFieldSearcherFactory searcherFactory;
53  
54      @Before
55      public void setup()
56      {
57          prompter = mock(Prompter.class);
58          searcherFactory = new TestingCustomFieldSearcherFactory();
59          searcherFactory.setSearchers(EasyMap.build("TextSearcher", "com.atlassian.jira.issue.customfields.searchers.TextSearcher", "NumberSearcher", "com.atlassian.jira.issue.customfields.searchers.NumberSearcher"));
60      }
61  
62      @Test
63      public void basicPropertiesAreValid() throws PrompterException
64      {
65  
66          when(prompter.prompt("Choose A Searcher Class\n1: NumberSearcher\n2: TextSearcher\n3: Custom Searcher Class\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
67          when(prompter.prompt("Enter Valid CustomField Package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE)).thenReturn(AbstractModulePrompter.DEFAULT_BASE_PACKAGE);
68          when(prompter.prompt("Enter Valid CustomField Key")).thenReturn("cf-key");
69  
70          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
71          when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
72  
73          CustomFieldSearcherPrompter modulePrompter = new CustomFieldSearcherPrompter(prompter);
74          modulePrompter.setUseAnsiColor(false);
75          CustomFieldSearcherProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
76  
77          assertEquals("wrong class", "TextSearcher", props.getClassId().getName());
78          assertEquals("wrong class package", "com.atlassian.jira.issue.customfields.searchers", props.getClassId().getPackage());
79          assertEquals("wrong module name", "Text Searcher", props.getModuleName());
80          assertEquals("wrong module key", "text-searcher", props.getModuleKey());
81          assertEquals("wrong i18n name key", "text-searcher.name", props.getNameI18nKey());
82          assertEquals("wrong i18n desc key", "text-searcher.description", props.getDescriptionI18nKey());
83          assertEquals("wrong customfield package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE, props.getValidCustomFieldPackage());
84          assertEquals("wrong customfield key", "cf-key", props.getValidCustomFieldKey());
85      }
86  
87      @Test
88      public void advancedPropertiesAreValid() throws PrompterException
89      {
90          when(prompter.prompt("Choose A Searcher Class\n1: NumberSearcher\n2: TextSearcher\n3: Custom Searcher Class\nChoose a number: ", Arrays.asList("1", "2", "3"), "")).thenReturn("2");
91          when(prompter.prompt("Enter Valid CustomField Package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE)).thenReturn(AbstractModulePrompter.DEFAULT_BASE_PACKAGE);
92          when(prompter.prompt("Enter Valid CustomField Key")).thenReturn("cf-key");
93          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
94  
95          when(prompter.prompt(MODULE_NAME_PROMPT, "Text Searcher")).thenReturn(ADV_MODULE_NAME);
96          when(prompter.prompt(MODULE_KEY_PROMPT, "text-searcher")).thenReturn(ADV_MODULE_KEY);
97          when(prompter.prompt(MODULE_DESCRIP_PROMPT, "The Text Searcher Plugin")).thenReturn(ADV_DESCRIPTION);
98          when(prompter.prompt("i18n Name Key", "text-searcher.name")).thenReturn(ADV_I18N_NAME_KEY);
99          when(prompter.prompt("i18n Description Key", "text-searcher.description")).thenReturn(ADV_I18N_DESCRIPTION_KEY);
100 
101         when(prompter.prompt("Add Resource", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y")
102                 .thenReturn("N");
103         when(prompter.prompt("Enter Resource Name")).thenReturn(RESOURCE_NAME)
104                 .thenReturn("");
105         when(prompter.prompt("Enter Location (path to resource file)")).thenReturn(RESOURCE_VM_PATH);
106 
107         when(prompter.prompt("Include Example Code?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("N");
108 
109         CustomFieldSearcherPrompter modulePrompter = new CustomFieldSearcherPrompter(prompter);
110         modulePrompter.setUseAnsiColor(false);
111         CustomFieldSearcherProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
112 
113         assertEquals("wrong class", "TextSearcher", props.getClassId().getName());
114         assertEquals("wrong class package", "com.atlassian.jira.issue.customfields.searchers", props.getClassId().getPackage());
115         assertEquals("wrong adv module name", ADV_MODULE_NAME, props.getModuleName());
116         assertEquals("wrong adv module key", ADV_MODULE_KEY, props.getModuleKey());
117         assertEquals("wrong adv description", ADV_DESCRIPTION, props.getDescription());
118         assertEquals("wrong adv i18n name key", ADV_I18N_NAME_KEY, props.getNameI18nKey());
119         assertEquals("wrong adv i18n desc key", ADV_I18N_DESCRIPTION_KEY, props.getDescriptionI18nKey());
120         assertEquals("wrong customfield package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE, props.getValidCustomFieldPackage());
121         assertEquals("wrong customfield key", "cf-key", props.getValidCustomFieldKey());
122         //resources
123         List<Resource> resources = props.getResources();
124         assertTrue("resources not found", !resources.isEmpty());
125         assertEquals("wrong number of resources", 1, resources.size());
126 
127         Resource viewResource = resources.get(0);
128 
129         assertEquals("wrong css resource name", RESOURCE_NAME, viewResource.getName());
130         assertTrue("name pattern found when name is set", StringUtils.isBlank(viewResource.getNamePattern()));
131         assertEquals("wrong resource type", "velocity", viewResource.getType());
132         assertEquals("wrong resource location", RESOURCE_VM_PATH, viewResource.getLocation());
133 
134     }
135 
136     protected class TestingCustomFieldSearcherFactory extends CustomFieldSearcherFactory
137     {
138         public void setSearchers(Map<String, String> searcherMap)
139         {
140             searchers = searcherMap;
141         }
142     }
143 }