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 org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  /**
27   * @since 3.5
28   */
29  public class CustomFieldSearcherPrompterTest extends AbstractPrompterTest
30  {
31      public static final String PACKAGE = "com.atlassian.plugins.jira.customfields";
32      public static final String CLASSNAME = "MyCustomFieldSearcher";
33      public static final String MODULE_NAME = "My Custom Field Searcher";
34      public static final String MODULE_KEY = "my-custom-field-searcher";
35      public static final String DESCRIPTION = "The My Custom Field Searcher Plugin";
36      public static final String I18N_NAME_KEY = "my-custom-field-searcher.name";
37      public static final String I18N_DESCRIPTION_KEY = "my-custom-field-searcher.description";
38  
39      public static final String ADV_MODULE_NAME = "My Awesome Plugin";
40      public static final String ADV_MODULE_KEY = "awesome-module";
41      public static final String ADV_DESCRIPTION = "The Awesomest Plugin Ever";
42      public static final String ADV_I18N_NAME_KEY = "awesome-plugin.name";
43      public static final String ADV_I18N_DESCRIPTION_KEY = "pluginus-awesomeous.description";
44  
45      public static final String RESOURCE_NAME = "view";
46      public static final String RESOURCE_VM_PATH = "templates/resource.vm";
47  
48      Prompter prompter;
49      TestingCustomFieldSearcherFactory searcherFactory;
50  
51      @Before
52      public void setup()
53      {
54          prompter = mock(Prompter.class);
55          searcherFactory = new TestingCustomFieldSearcherFactory();
56          searcherFactory.setSearchers(EasyMap.build("TextSearcher", "com.atlassian.jira.issue.customfields.searchers.TextSearcher", "NumberSearcher", "com.atlassian.jira.issue.customfields.searchers.NumberSearcher"));
57      }
58  
59      @Test
60      public void basicPropertiesAreValid() throws PrompterException
61      {
62  
63          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");
64          when(prompter.prompt("Enter Valid CustomField Package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE)).thenReturn(AbstractModulePrompter.DEFAULT_BASE_PACKAGE);
65          when(prompter.prompt("Enter Valid CustomField Key")).thenReturn("cf-key");
66  
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          CustomFieldSearcherPrompter modulePrompter = new CustomFieldSearcherPrompter(prompter);
71          modulePrompter.setUseAnsiColor(false);
72          CustomFieldSearcherProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
73  
74          assertEquals("wrong class", "TextSearcher", props.getClassname());
75          assertEquals("wrong class package", "com.atlassian.jira.issue.customfields.searchers", props.getPackage());
76          assertEquals("wrong module name", "Text Searcher", props.getModuleName());
77          assertEquals("wrong module key", "text-searcher", props.getModuleKey());
78          assertEquals("wrong i18n name key", "text-searcher.name", props.getNameI18nKey());
79          assertEquals("wrong i18n desc key", "text-searcher.description", props.getDescriptionI18nKey());
80          assertEquals("wrong customfield package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE, props.getValidCustomFieldPackage());
81          assertEquals("wrong customfield key", "cf-key", props.getValidCustomFieldKey());
82      }
83  
84      @Test
85      public void advancedPropertiesAreValid() throws PrompterException
86      {
87          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");
88          when(prompter.prompt("Enter Valid CustomField Package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE)).thenReturn(AbstractModulePrompter.DEFAULT_BASE_PACKAGE);
89          when(prompter.prompt("Enter Valid CustomField Key")).thenReturn("cf-key");
90          when(prompter.prompt("Show Advanced Setup?", PluginModulePrompter.YN_ANSWERS, "N")).thenReturn("Y");
91  
92          when(prompter.prompt("Plugin Name", "Text Searcher")).thenReturn(ADV_MODULE_NAME);
93          when(prompter.prompt("Plugin Key", "text-searcher")).thenReturn(ADV_MODULE_KEY);
94          when(prompter.prompt("Plugin Description", "The Text Searcher Plugin")).thenReturn(ADV_DESCRIPTION);
95          when(prompter.prompt("i18n Name Key", "text-searcher.name")).thenReturn(ADV_I18N_NAME_KEY);
96          when(prompter.prompt("i18n Description Key", "text-searcher.description")).thenReturn(ADV_I18N_DESCRIPTION_KEY);
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         CustomFieldSearcherPrompter modulePrompter = new CustomFieldSearcherPrompter(prompter);
107         modulePrompter.setUseAnsiColor(false);
108         CustomFieldSearcherProperties props = modulePrompter.getModulePropertiesFromInput(moduleLocation);
109 
110         assertEquals("wrong class", "TextSearcher", props.getClassname());
111         assertEquals("wrong class package", "com.atlassian.jira.issue.customfields.searchers", props.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         assertEquals("wrong customfield package", AbstractModulePrompter.DEFAULT_BASE_PACKAGE, props.getValidCustomFieldPackage());
118         assertEquals("wrong customfield key", "cf-key", props.getValidCustomFieldKey());
119         //resources
120         List<Resource> resources = props.getResources();
121         assertTrue("resources not found", !resources.isEmpty());
122         assertEquals("wrong number of resources", 1, resources.size());
123 
124         Resource viewResource = resources.get(0);
125 
126         assertEquals("wrong css resource name", RESOURCE_NAME, viewResource.getName());
127         assertTrue("name pattern found when name is set", StringUtils.isBlank(viewResource.getNamePattern()));
128         assertEquals("wrong resource type", "velocity", viewResource.getType());
129         assertEquals("wrong resource location", RESOURCE_VM_PATH, viewResource.getLocation());
130 
131     }
132 
133     protected class TestingCustomFieldSearcherFactory extends CustomFieldSearcherFactory
134     {
135         public void setSearchers(Map<String, String> searcherMap)
136         {
137             searchers = searcherMap;
138         }
139     }
140 }