PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/web/action/admin/notification/TestAddNotification.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 165 lines | 137 code | 28 blank | 0 comment | 1 complexity | 9fddd5df352499a17f6eb1a8eea7d50a MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.admin.notification;
  2. import com.atlassian.jira.event.type.EventTypeManager;
  3. import com.atlassian.jira.junit.rules.AvailableInContainer;
  4. import com.atlassian.jira.junit.rules.MockitoContainer;
  5. import com.atlassian.jira.mock.i18n.MockI18nHelper;
  6. import com.atlassian.jira.notification.NotificationSchemeManager;
  7. import com.atlassian.jira.notification.NotificationType;
  8. import com.atlassian.jira.notification.NotificationTypeManager;
  9. import com.atlassian.jira.project.ProjectManager;
  10. import com.atlassian.jira.scheme.SchemeEntity;
  11. import com.atlassian.jira.security.JiraAuthenticationContext;
  12. import com.atlassian.jira.util.I18nHelper;
  13. import com.atlassian.jira.web.action.RedirectSanitiser;
  14. import org.junit.Before;
  15. import org.junit.Rule;
  16. import org.junit.Test;
  17. import org.mockito.Mock;
  18. import org.ofbiz.core.entity.GenericEntityException;
  19. import org.ofbiz.core.entity.GenericValue;
  20. import java.util.Map;
  21. import static org.hamcrest.Matchers.hasItem;
  22. import static org.hamcrest.Matchers.not;
  23. import static org.junit.Assert.assertArrayEquals;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertThat;
  26. import static org.mockito.Matchers.any;
  27. import static org.mockito.Matchers.anyString;
  28. import static org.mockito.Matchers.eq;
  29. import static org.mockito.Matchers.isNull;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.never;
  32. import static org.mockito.Mockito.verify;
  33. import static org.mockito.Mockito.when;
  34. public class TestAddNotification {
  35. private final static Long SELECTED_PROJECT_ID = 11l;
  36. private final static Long SCHEME_ID = 1l;
  37. private final static Long[] EVENT_TYPE_IDS = new Long[]{5l, 10l, 15l};
  38. private final static String TYPE = "EHLO";
  39. @Rule
  40. public MockitoContainer mockitoContainer = new MockitoContainer(this);
  41. @Mock
  42. @AvailableInContainer
  43. private EventTypeManager eventTypeManager;
  44. @Mock
  45. @AvailableInContainer
  46. private NotificationTypeManager notificationTypeManager;
  47. @Mock
  48. @AvailableInContainer
  49. private NotificationSchemeManager notificationSchemeManager;
  50. @Mock
  51. @AvailableInContainer
  52. private ProjectManager projectManager;
  53. @Mock
  54. @AvailableInContainer
  55. private RedirectSanitiser redirectSanitiser;
  56. @Mock
  57. @AvailableInContainer
  58. JiraAuthenticationContext jiraAuthenticationContext;
  59. private I18nHelper i18nHelper = new MockI18nHelper();
  60. @Before
  61. public void setUp() throws GenericEntityException {
  62. when(jiraAuthenticationContext.getI18nHelper()).thenReturn(i18nHelper);
  63. NotificationType notificationType = mock(NotificationType.class);
  64. when(notificationTypeManager.getNotificationType(TYPE)).thenReturn(notificationType);
  65. for (Long eventTypeId : EVENT_TYPE_IDS) {
  66. when(notificationSchemeManager.hasEntities(any(GenericValue.class), eq(eventTypeId), eq(TYPE), anyString(), isNull(Long.class))).thenReturn(true);
  67. }
  68. }
  69. @Test
  70. public void testDoExecuteDoesNotDuplicateEntities() throws Exception {
  71. AddNotification addNotification = createAddNotification(SCHEME_ID, EVENT_TYPE_IDS, TYPE, SELECTED_PROJECT_ID);
  72. final String redirectUrl = addNotification.doExecute();
  73. verify(notificationSchemeManager, never()).createSchemeEntity(any(GenericValue.class), any(SchemeEntity.class));
  74. }
  75. @Test
  76. public void testDoExecuteCalledCreateSchemeEntityWithCorrectParams() throws Exception {
  77. Long newEventTypeId = 34l;
  78. when(notificationSchemeManager.hasEntities(any(GenericValue.class), eq(newEventTypeId), eq(TYPE), anyString(), isNull(Long.class))).thenReturn(false);
  79. GenericValue mockScheme = mock(GenericValue.class);
  80. AddNotification addNotification = createAddNotification(SCHEME_ID, new Long[]{newEventTypeId}, TYPE, SELECTED_PROJECT_ID);
  81. when(notificationSchemeManager.getScheme(SCHEME_ID)).thenReturn(mockScheme);
  82. final String redirectUrl = addNotification.doExecute();
  83. verify(notificationSchemeManager).createSchemeEntity(eq(mockScheme), any(SchemeEntity.class));
  84. }
  85. @Test
  86. public void testDoValidationChecksSchemeNotNull() throws GenericEntityException {
  87. assertArrayEquals(new String[]{"admin.errors.notifications.must.select.scheme", "admin.errors.notifications.must.select.type"},
  88. validateAddNotification(null, null, null, null));
  89. when(notificationSchemeManager.getScheme(SCHEME_ID)).thenReturn(mock(GenericValue.class));
  90. assertArrayEquals(new String[]{"admin.errors.notifications.must.select.type"}, validateAddNotification(SCHEME_ID, null, null, null));
  91. }
  92. @Test
  93. public void testDoValidationChecksEventTypeIdsNotNull() {
  94. Map<String, String> errors = validateAddNotificationWithErrors(SCHEME_ID, null, TYPE, null);
  95. assertThat(errors.keySet(), hasItem("eventTypeIds"));
  96. assertEquals("admin.errors.notifications.must.select.notification.to.add", errors.get("eventTypeIds"));
  97. assertThat(validateAddNotificationWithErrors(SCHEME_ID, EVENT_TYPE_IDS, TYPE, null).keySet(), not(hasItem("eventTypeIds")));
  98. }
  99. @Test
  100. public void testDoValidationChecksTypeStringIsSet() throws GenericEntityException {
  101. when(notificationSchemeManager.getScheme(SCHEME_ID)).thenReturn(mock(GenericValue.class));
  102. assertArrayEquals(new String[]{"admin.errors.notifications.must.select.type"}, validateAddNotification(SCHEME_ID, null, null, null));
  103. assertArrayEquals(new String[]{"admin.errors.notifications.fill.out.box"}, validateAddNotification(SCHEME_ID, null, TYPE, null));
  104. }
  105. @Test
  106. public void testDoValidationValidatesNotificationType() throws GenericEntityException {
  107. when(notificationSchemeManager.getScheme(SCHEME_ID)).thenReturn(mock(GenericValue.class));
  108. assertArrayEquals(new String[]{"admin.errors.notifications.fill.out.box"}, validateAddNotification(SCHEME_ID, null, TYPE, null));
  109. NotificationType notificationType = mock(NotificationType.class);
  110. when(notificationType.doValidation(anyString(), any(Map.class))).thenReturn(true);
  111. when(notificationTypeManager.getNotificationType(TYPE)).thenReturn(notificationType);
  112. assertArrayEquals(new String[]{}, validateAddNotification(SCHEME_ID, null, TYPE, null));
  113. }
  114. @Test
  115. public void testDoValidationDoesntThrowGenericEntityException() throws Exception {
  116. when(notificationSchemeManager.getScheme(SCHEME_ID)).thenThrow(new GenericEntityException(""));
  117. assertArrayEquals(new String[]{"admin.errors.notifications.error.occured [\n]"}, validateAddNotification(SCHEME_ID, null, null, null));
  118. }
  119. private String[] validateAddNotification(Long schemeId, Long[] eventTypeIds, String type, Long selectedProjectId) {
  120. AddNotification addNotification = createAddNotification(schemeId, eventTypeIds, type, selectedProjectId);
  121. addNotification.doValidation();
  122. return addNotification.getErrorMessages().toArray(new String[]{});
  123. }
  124. private Map<String, String> validateAddNotificationWithErrors(Long schemeId, Long[] eventTypeIds, String type, Long selectedProjectId) {
  125. AddNotification addNotification = createAddNotification(schemeId, eventTypeIds, type, selectedProjectId);
  126. addNotification.doValidation();
  127. return addNotification.getErrors();
  128. }
  129. private AddNotification createAddNotification(Long schemeId, Long[] eventTypeIds, String type, Long selectedProjectId) {
  130. AddNotification addNotification = new AddNotification(eventTypeManager, notificationTypeManager, notificationSchemeManager);
  131. addNotification.setSelectedProjectId(selectedProjectId);
  132. addNotification.setSchemeId(schemeId);
  133. addNotification.setEventTypeIds(eventTypeIds);
  134. addNotification.setType(type);
  135. return addNotification;
  136. }
  137. }