PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/plugin/aboutpagepanel/AboutPagePanelModuleDescriptorImplTest.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 219 lines | 188 code | 31 blank | 0 comment | 0 complexity | 5867186c045b8d3bf08c19ca267c924e MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.plugin.aboutpagepanel;
  2. import com.atlassian.jira.config.util.EncodingConfiguration;
  3. import com.atlassian.jira.junit.rules.InitMockitoMocks;
  4. import com.atlassian.jira.license.thirdparty.BomParser;
  5. import com.atlassian.jira.mock.plugin.MockPlugin;
  6. import com.atlassian.jira.security.JiraAuthenticationContext;
  7. import com.atlassian.jira.template.soy.SoyTemplateRendererProvider;
  8. import com.atlassian.plugin.Plugin;
  9. import com.atlassian.plugin.PluginParseException;
  10. import com.atlassian.plugin.module.ModuleFactory;
  11. import com.atlassian.plugin.web.ContextProvider;
  12. import com.atlassian.plugin.web.WebFragmentHelper;
  13. import com.atlassian.plugin.web.WebInterfaceManager;
  14. import com.atlassian.soy.renderer.SoyException;
  15. import com.atlassian.soy.renderer.SoyTemplateRenderer;
  16. import com.google.common.collect.ImmutableList;
  17. import com.google.common.collect.ImmutableMap;
  18. import org.dom4j.DocumentException;
  19. import org.dom4j.Element;
  20. import org.dom4j.io.SAXReader;
  21. import org.hamcrest.Matchers;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.TestRule;
  26. import org.mockito.Mock;
  27. import java.io.StringReader;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.Map;
  31. import static org.junit.Assert.assertEquals;
  32. import static org.junit.Assert.assertThat;
  33. import static org.junit.Assert.fail;
  34. import static org.mockito.Matchers.any;
  35. import static org.mockito.Matchers.anyString;
  36. import static org.mockito.Matchers.eq;
  37. import static org.mockito.Matchers.matches;
  38. import static org.mockito.Mockito.times;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. public class AboutPagePanelModuleDescriptorImplTest {
  42. @Rule
  43. public TestRule rule = new InitMockitoMocks(this);
  44. @Mock
  45. private JiraAuthenticationContext authenticationContext;
  46. @Mock
  47. private ModuleFactory legacyModuleFactory;
  48. @Mock
  49. private EncodingConfiguration encodingConfiguration;
  50. @Mock
  51. private SoyTemplateRendererProvider soyTemplateRendererProvider;
  52. @Mock
  53. private SoyTemplateRenderer soyTemplateRenderer;
  54. @Mock
  55. private WebInterfaceManager webInterfaceManager;
  56. @Mock
  57. private WebFragmentHelper webFragmentHelper;
  58. @Mock
  59. private ContextProvider contextProvider;
  60. @Mock
  61. private BomParser bomParser;
  62. private Plugin plugin = new MockPlugin("example.key")
  63. .setClassLoader(AboutPagePanelModuleDescriptorImplTest.class.getClassLoader());
  64. private AboutPagePanelModuleDescriptorImpl descriptor;
  65. @Before
  66. public void onTestInit() throws Exception {
  67. when(webInterfaceManager.getWebFragmentHelper()).thenReturn(webFragmentHelper);
  68. when(webFragmentHelper.loadContextProvider(eq("mockContextProvider"), any(Plugin.class))).thenReturn(contextProvider);
  69. descriptor = new AboutPagePanelModuleDescriptorImpl(authenticationContext, webInterfaceManager, encodingConfiguration,
  70. soyTemplateRendererProvider, bomParser);
  71. }
  72. @Test
  73. public void getIntroduction() throws Exception {
  74. descriptor.init(plugin, readResource("licenseModuleWithIntroAndConclusion.xml"));
  75. descriptor.generateIntroduction(soyTemplateRenderer, Collections.<String, Object>emptyMap());
  76. verify(soyTemplateRenderer, times(1)).render(any(String.class), any(String.class), any(Map.class));
  77. }
  78. @Test
  79. public void getConclusion() throws Exception {
  80. descriptor.init(plugin, readResource("licenseModuleWithIntroAndConclusion.xml"));
  81. descriptor.generateConclusion(soyTemplateRenderer, Collections.<String, Object>emptyMap());
  82. verify(soyTemplateRenderer, times(1)).render(matches("exampleModuleKey"), matches("conclusion"), any(Map.class));
  83. }
  84. @Test
  85. public void getContextProvider() throws Exception {
  86. Map<String, Object> data = ImmutableMap.<String, Object>of("parameterName", "parameterValue");
  87. when(soyTemplateRendererProvider.getRenderer()).thenReturn(soyTemplateRenderer);
  88. when(contextProvider.getContextMap(any(Map.class))).thenReturn(data);
  89. descriptor.init(plugin, readResource("aboutPagePanelWithContextProvider.xml"));
  90. descriptor.enabled();
  91. descriptor.getPluginSectionHtml();
  92. verify(contextProvider, times(1)).getContextMap(any(Map.class));
  93. verify(soyTemplateRenderer, times(2)).render(any(String.class), any(String.class), any(Map.class));
  94. }
  95. @Test
  96. public void verifyConstructor() throws Exception {
  97. expectExceptionInit("<about-page-panel key=\"key\"/>");
  98. expectExceptionInit("<about-page-panel key=\"key\"><introduction/></about-page-panel>");
  99. expectExceptionInit("<about-page-panel key=\"key\"><introduction function='introduction'/></about-page-panel>");
  100. expectOkInit(
  101. "<about-page-panel key=\"key\">"
  102. + "<introduction function='introduction' module-key='exampleModuleKey'/>"
  103. + "</about-page-panel>");
  104. expectExceptionInit("<about-page-panel key=\"key\">><licenses/></about-page-panel>");
  105. expectOkInit("<about-page-panel key=\"key\">><licenses location='exampleFile'/></about-page-panel>");
  106. expectExceptionInit(
  107. "<about-page-panel key=\"key\">"
  108. + "<introduction function='introduction' module-key='exampleModuleKey'/>"
  109. + "<licenses location='exampleFile'/>"
  110. + "<conclusion/>"
  111. + "</about-page-panel>");
  112. expectExceptionInit(
  113. "<about-page-panel key=\"key\">"
  114. + "<introduction function='introduction' module-key='exampleModuleKey'/>"
  115. + "<licenses location='exampleFile'/>"
  116. + "<conclusion function='exampleConclusion'/>"
  117. + "</about-page-panel>");
  118. expectOkInit(
  119. "<about-page-panel key=\"key\">"
  120. + "<introduction function='introduction' module-key='exampleModuleKey'/>"
  121. + "<licenses location='exampleFile'/>"
  122. + "<conclusion function='exampleConclusion' module-key='exampleModuleKey'/>"
  123. + "</about-page-panel>");
  124. expectOkInit(
  125. "<about-page-panel key=\"key\">"
  126. + "<context-provider class=\"mockContextProvider\"></context-provider>"
  127. + "<introduction function='introduction' module-key='exampleModuleKey'/>"
  128. + "<licenses location='exampleFile'/>"
  129. + "<conclusion function='exampleConclusion' module-key='exampleModuleKey'/>"
  130. + "</about-page-panel>");
  131. }
  132. @Test
  133. public void loadsBomFromPlugin() throws SoyException {
  134. final AboutPagePanelModuleDescriptorImpl.Material material = createMaterial("one");
  135. when(bomParser.extractLgplMaterials("bomContents"))
  136. .thenReturn(ImmutableList.of(material));
  137. descriptor.init(plugin, readResource("licenseModuleWithBom.xml"));
  138. final List<AboutPagePanelModuleDescriptorImpl.Material> result = descriptor.getMaterials(plugin);
  139. assertThat(result, Matchers.contains(material));
  140. }
  141. @Test
  142. public void ignoresBadBomFromPlugin() throws SoyException {
  143. descriptor.init(plugin, readResource("licenseModuleWithBadBom.xml"));
  144. final List<AboutPagePanelModuleDescriptorImpl.Material> result = descriptor.getMaterials(plugin);
  145. assertThat(result.isEmpty(), Matchers.equalTo(true));
  146. }
  147. @Test
  148. public void renderItemReturnsEmptyOnException() throws Exception {
  149. when(soyTemplateRenderer.render(anyString(), anyString(), any(Map.class))).thenThrow(new SoyException(""));
  150. String s = descriptor.renderItem(soyTemplateRenderer, "a", "b", Collections.<String, Object>emptyMap());
  151. assertEquals("", s);
  152. }
  153. private AboutPagePanelModuleDescriptorImpl.Material createMaterial(String name) {
  154. return new AboutPagePanelModuleDescriptorImpl.Material(name, name, name, name, name);
  155. }
  156. private Element readResource(final String resource) {
  157. SAXReader reader = new SAXReader();
  158. try {
  159. return reader.read(getClass().getResourceAsStream(resource)).getRootElement();
  160. } catch (DocumentException e) {
  161. throw new RuntimeException(e);
  162. }
  163. }
  164. private Element readInline(String string) {
  165. SAXReader reader = new SAXReader();
  166. try {
  167. return reader.read(new StringReader(string)).getRootElement();
  168. } catch (DocumentException e) {
  169. throw new RuntimeException(e);
  170. }
  171. }
  172. private void expectExceptionInit(String element) {
  173. try {
  174. descriptor.init(plugin, readInline(element));
  175. descriptor.enabled();
  176. fail("expected PluginParseException");
  177. } catch (PluginParseException ignored) {
  178. }
  179. }
  180. private void expectOkInit(String element) {
  181. try {
  182. descriptor.init(plugin, readInline(element));
  183. descriptor.enabled();
  184. } catch (PluginParseException ignored) {
  185. fail("Did not expect plugin parse exception");
  186. }
  187. }
  188. }