PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/project/util/TestReleaseNoteManager.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 187 lines | 161 code | 26 blank | 0 comment | 8 complexity | 4ebe912a6d865427193d6bffd972b2e8 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.project.util;
  2. import com.atlassian.crowd.exception.InvalidCredentialException;
  3. import com.atlassian.crowd.exception.InvalidUserException;
  4. import com.atlassian.crowd.exception.OperationNotPermittedException;
  5. import com.atlassian.jira.bc.project.version.VersionBuilderImpl;
  6. import com.atlassian.jira.component.ComponentAccessor;
  7. import com.atlassian.jira.config.ConstantsManager;
  8. import com.atlassian.jira.config.properties.APKeys;
  9. import com.atlassian.jira.config.properties.ApplicationPropertiesImpl;
  10. import com.atlassian.jira.issue.issuetype.IssueType;
  11. import com.atlassian.jira.issue.issuetype.MockIssueType;
  12. import com.atlassian.jira.mock.component.MockComponentWorker;
  13. import com.atlassian.jira.mock.ofbiz.MockGenericValue;
  14. import com.atlassian.jira.mock.project.MockVersion;
  15. import com.atlassian.jira.ofbiz.FieldMap;
  16. import com.atlassian.jira.project.MockProject;
  17. import com.atlassian.jira.project.Project;
  18. import com.atlassian.jira.project.version.Version;
  19. import com.atlassian.jira.template.VelocityTemplatingEngine;
  20. import com.atlassian.jira.template.mocks.VelocityTemplatingEngineMocks;
  21. import com.atlassian.jira.user.ApplicationUser;
  22. import com.atlassian.jira.user.MockApplicationUser;
  23. import com.google.common.collect.ImmutableList;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.mockito.stubbing.OngoingStubbing;
  27. import java.util.Collection;
  28. import java.util.Map;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.junit.Assert.assertNotNull;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.when;
  33. public class TestReleaseNoteManager {
  34. public TestReleaseNoteManager() {
  35. ComponentAccessor.initialiseWorker(new MockComponentWorker());
  36. }
  37. Project project;
  38. @Before
  39. public void setUp() {
  40. this.project = new MockProject(0L, "ABC");
  41. }
  42. @Test
  43. public void testGetReleaseNoteStyles() {
  44. final Map expectedStyles = FieldMap.build("text", "text-template", "html", "html-template");
  45. final String releaseNoteName = "text, html";
  46. final String releaseNoteTemplate = "text-template, html-template";
  47. ApplicationPropertiesImpl applicationProperties = new MyApplicationProperties(releaseNoteName, releaseNoteTemplate);
  48. ReleaseNoteManager releaseNoteManager = new ReleaseNoteManager(applicationProperties, null, null, null, null);
  49. assertEquals(expectedStyles, releaseNoteManager.getStyles());
  50. }
  51. @Test
  52. public void testGetReleaseNoteStylesHandlesNulls() {
  53. ApplicationPropertiesImpl applicationProperties = new MyApplicationProperties(null, null);
  54. ReleaseNoteManager releaseNoteManager = new ReleaseNoteManager(applicationProperties, null, null, null, null);
  55. releaseNoteManager.getStyles(); // just check no exceptions
  56. }
  57. @Test
  58. public void testGetReleaseNoteStylesWithCorruptedProperties() {
  59. ApplicationPropertiesImpl applicationProperties = new MyApplicationProperties("text", null);
  60. ReleaseNoteManager releaseNoteManager = new ReleaseNoteManager(applicationProperties, null, null, null, null);
  61. try {
  62. releaseNoteManager.getStyles();
  63. } catch (RuntimeException re) {
  64. assertNotNull(re.getMessage());
  65. }
  66. }
  67. @Test
  68. public void testGetReleaseNoteWithInvalidStyleName()
  69. throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
  70. final ApplicationUser testUser = new MockApplicationUser("testuser");
  71. final Version version = new MockVersion(new MockGenericValue("Version", FieldMap.build("name", "Version 1", "id", (long) 1001, "sequence", (long) 1, "project", (long) 101, "released", "true", "archived", "true")));
  72. final ApplicationPropertiesImpl applicationProperties = new MyApplicationProperties("text", "text-template", "text");
  73. final VelocityTemplatingEngine templatingEngine = VelocityTemplatingEngineMocks.alwaysOutput("BODY").get();
  74. final ConstantsManager constantsManager = mock(ConstantsManager.class);
  75. returnTwoIssueTypeGvs(constantsManager);
  76. final ReleaseNoteManager releaseNoteManager = new ReleaseNoteManager(applicationProperties, templatingEngine, constantsManager, null, null);
  77. assertEquals("BODY", releaseNoteManager.getReleaseNote(null, "xml", version, testUser, project));
  78. }
  79. @Test
  80. public void testGetReleaseNoteWithInvalidStyleNameAndInvalidDefault()
  81. throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
  82. final ApplicationUser testUser = new MockApplicationUser("testuser");
  83. final Version version = new VersionBuilderImpl(
  84. new MockVersion(new MockGenericValue("Version", FieldMap.build("name", "Version 1", "id", (long) 1001, "sequence", (long) 1, "project", (long) 101, "released", "true", "archived", "true"))))
  85. .name("Ver 1")
  86. .sequence(1L).build();
  87. final ApplicationPropertiesImpl applicationProperties = new MyApplicationProperties("text", "text-template", "nicks");
  88. final VelocityTemplatingEngine templatingEngine = VelocityTemplatingEngineMocks.alwaysOutput("BODY").get();
  89. final ConstantsManager constantsManager = mock(ConstantsManager.class);
  90. returnTwoIssueTypeGvs(constantsManager);
  91. final ReleaseNoteManager releaseNoteManager = new ReleaseNoteManager(applicationProperties, templatingEngine, constantsManager, null, null);
  92. assertEquals("BODY", releaseNoteManager.getReleaseNote(null, "xml", version, testUser, project));
  93. }
  94. @Test
  95. public void testGetReleaseNote()
  96. throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
  97. final ApplicationUser testUser = new MockApplicationUser("testuser");
  98. final Version version = new MockVersion(new MockGenericValue("Version", FieldMap.build("name", "Version 1", "id", (long) 1001, "sequence", (long) 1, "project", (long) 101, "released", "true", "archived", "true")));
  99. final ApplicationPropertiesImpl applicationProperties = new MyApplicationProperties("text", "text-template");
  100. final VelocityTemplatingEngine templatingEngine = VelocityTemplatingEngineMocks.alwaysOutput("BODY").get();
  101. final ConstantsManager constantsManager = mock(ConstantsManager.class);
  102. returnTwoIssueTypeGvs(constantsManager);
  103. final ReleaseNoteManager releaseNoteManager = new ReleaseNoteManager(applicationProperties, templatingEngine, constantsManager, null, null);
  104. assertEquals("BODY", releaseNoteManager.getReleaseNote(null, "text", version, testUser, project));
  105. }
  106. private OngoingStubbing<Collection<IssueType>> returnTwoIssueTypeGvs(ConstantsManager constantsManager) {
  107. return when(constantsManager.getRegularIssueTypeObjects()).thenReturn
  108. (
  109. ImmutableList.<IssueType>of
  110. (
  111. new MockIssueType
  112. (
  113. "100", "testtype",
  114. "test issue type"
  115. ),
  116. new MockIssueType
  117. (
  118. "200", "another testtype",
  119. "another test issue type"
  120. )
  121. )
  122. );
  123. }
  124. private static class MyApplicationProperties extends ApplicationPropertiesImpl {
  125. private final String changeLogName;
  126. private final String changeLogTemplate;
  127. private final String defaultTemplate;
  128. public MyApplicationProperties(String changeLogName, String changeLogTemplate) {
  129. super(null);
  130. this.changeLogName = changeLogName;
  131. this.changeLogTemplate = changeLogTemplate;
  132. defaultTemplate = null;
  133. }
  134. public MyApplicationProperties(String changeLogName, String changeLogTemplate, String defaultTemplate) {
  135. super(null);
  136. this.changeLogName = changeLogName;
  137. this.changeLogTemplate = changeLogTemplate;
  138. this.defaultTemplate = defaultTemplate;
  139. }
  140. @Override
  141. public String getDefaultBackedString(String name) {
  142. if (ReleaseNoteManager.RELEASE_NOTE_NAME.equals(name)) {
  143. return changeLogName;
  144. } else if (ReleaseNoteManager.RELEASE_NOTE_TEMPLATE.equals(name)) {
  145. return changeLogTemplate;
  146. } else if (ReleaseNoteManager.RELEASE_NOTE_DEFAULT.equals(name)) {
  147. return defaultTemplate;
  148. } else {
  149. return null;
  150. }
  151. }
  152. @Override
  153. public String getString(String name) {
  154. if (APKeys.JIRA_BASEURL.equals(name)) {
  155. return "jira";
  156. } else {
  157. return null;
  158. }
  159. }
  160. }
  161. }