|
| 1 | +package com.dotmarketing.util; |
| 2 | + |
| 3 | +import com.dotcms.api.system.event.message.SystemMessageEventUtil; |
| 4 | +import com.dotcms.concurrent.Debouncer; |
| 5 | +import com.dotcms.contenttype.model.type.ContentType; |
| 6 | +import com.dotmarketing.beans.Identifier; |
| 7 | +import com.liferay.portal.language.LanguageUtil; |
| 8 | +import org.junit.jupiter.api.AfterEach; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.MockedStatic; |
| 12 | +import org.mockito.Mockito; |
| 13 | + |
| 14 | +import java.util.Calendar; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.*; |
| 18 | +import static org.mockito.Mockito.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit tests for {@link ContentPublishDateUtil}. |
| 22 | + * <p> |
| 23 | + * Verifies behavior related to future publish date checks, system message handling, |
| 24 | + * and the use of debouncing for user notifications. |
| 25 | + */ |
| 26 | +class ContentPublishDateUtilTest { |
| 27 | + |
| 28 | + private ContentType contentType; |
| 29 | + private Identifier identifier; |
| 30 | + private Debouncer originalDebouncer; |
| 31 | + private Debouncer debouncerMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * Initializes mocks and injects a mock Debouncer into the utility class before each test. |
| 35 | + */ |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + contentType = mock(ContentType.class); |
| 39 | + identifier = mock(Identifier.class); |
| 40 | + originalDebouncer = ContentPublishDateUtil.getDebouncer(); |
| 41 | + debouncerMock = mock(Debouncer.class); |
| 42 | + ContentPublishDateUtil.setDebouncer(debouncerMock); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterEach |
| 46 | + void tearDown() { |
| 47 | + ContentPublishDateUtil.setDebouncer(originalDebouncer); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Verifies that when the publish date is in the future and the content type has a publish field, |
| 52 | + * the method returns true and attempts to send a user message. |
| 53 | + */ |
| 54 | + @Test |
| 55 | + void test_notifyIfFuturePublishDate_shouldReturnTrueAndSendMessage() { |
| 56 | + when(contentType.publishDateVar()).thenReturn("publishDate"); |
| 57 | + |
| 58 | + Calendar future = Calendar.getInstance(); |
| 59 | + future.add(Calendar.HOUR, 1); |
| 60 | + when(identifier.getSysPublishDate()).thenReturn(future.getTime()); |
| 61 | + |
| 62 | + try ( |
| 63 | + MockedStatic<LanguageUtil> langMock = Mockito.mockStatic(LanguageUtil.class); |
| 64 | + MockedStatic<SystemMessageEventUtil> msgMock = Mockito.mockStatic(SystemMessageEventUtil.class); |
| 65 | + MockedStatic<Logger> loggerMock = Mockito.mockStatic(Logger.class) |
| 66 | + ) { |
| 67 | + langMock.when(() -> LanguageUtil.get("message.contentlet.publish.future.date")) |
| 68 | + .thenReturn("Future publish message"); |
| 69 | + |
| 70 | + SystemMessageEventUtil mockUtil = mock(SystemMessageEventUtil.class); |
| 71 | + msgMock.when(SystemMessageEventUtil::getInstance).thenReturn(mockUtil); |
| 72 | + |
| 73 | + boolean result = ContentPublishDateUtil.notifyIfFuturePublishDate(contentType, identifier, "user123"); |
| 74 | + |
| 75 | + assertTrue(result); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Ensures that the method returns false when the content type does not define a publish date field. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + void test_notifyIfFuturePublishDate_shouldReturnFalseIfNoPublishDate() { |
| 84 | + when(contentType.publishDateVar()).thenReturn(null); |
| 85 | + |
| 86 | + boolean result = ContentPublishDateUtil.notifyIfFuturePublishDate(contentType, identifier, "user123"); |
| 87 | + |
| 88 | + assertFalse(result); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Ensures that the method returns false when the publish date is in the past. |
| 93 | + */ |
| 94 | + @Test |
| 95 | + void test_notifyIfFuturePublishDate_shouldReturnFalseIfPublishDateNotInFuture() { |
| 96 | + when(contentType.publishDateVar()).thenReturn("publishDate"); |
| 97 | + |
| 98 | + Calendar past = Calendar.getInstance(); |
| 99 | + past.add(Calendar.HOUR, -1); |
| 100 | + when(identifier.getSysPublishDate()).thenReturn(past.getTime()); |
| 101 | + |
| 102 | + boolean result = ContentPublishDateUtil.notifyIfFuturePublishDate(contentType, identifier, "user123"); |
| 103 | + |
| 104 | + assertFalse(result); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Verifies that the debouncer is invoked when a future publish date is detected. |
| 109 | + */ |
| 110 | + @Test |
| 111 | + void test_debouncerCalled_whenFutureDate() { |
| 112 | + when(contentType.publishDateVar()).thenReturn("publishDate"); |
| 113 | + |
| 114 | + Calendar future = Calendar.getInstance(); |
| 115 | + future.add(Calendar.MINUTE, 10); |
| 116 | + when(identifier.getSysPublishDate()).thenReturn(future.getTime()); |
| 117 | + |
| 118 | + boolean result = ContentPublishDateUtil.notifyIfFuturePublishDate(contentType, identifier, "userX"); |
| 119 | + |
| 120 | + assertTrue(result); |
| 121 | + |
| 122 | + verify(debouncerMock).debounce( |
| 123 | + eq("contentPublishDateErroruserX"), |
| 124 | + any(Runnable.class), |
| 125 | + eq(5000L), |
| 126 | + eq(TimeUnit.MILLISECONDS) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Verifies that the debouncer is not invoked when the publish date is not in the future. |
| 132 | + */ |
| 133 | + @Test |
| 134 | + void test_debouncerNotCalled_whenPublishDateIsPast() { |
| 135 | + when(contentType.publishDateVar()).thenReturn("publishDate"); |
| 136 | + |
| 137 | + Calendar past = Calendar.getInstance(); |
| 138 | + past.add(Calendar.MINUTE, -10); |
| 139 | + when(identifier.getSysPublishDate()).thenReturn(past.getTime()); |
| 140 | + |
| 141 | + boolean result = ContentPublishDateUtil.notifyIfFuturePublishDate(contentType, identifier, "userX"); |
| 142 | + |
| 143 | + assertFalse(result); |
| 144 | + verifyNoInteractions(debouncerMock); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Verifies that the Runnable passed to the debouncer sends the correct user message |
| 149 | + * when manually executed. |
| 150 | + */ |
| 151 | + @Test |
| 152 | + void test_debouncerRunnable_executesSystemMessage() { |
| 153 | + Runnable[] capturedRunnable = new Runnable[1]; |
| 154 | + |
| 155 | + when(contentType.publishDateVar()).thenReturn("publishDate"); |
| 156 | + |
| 157 | + Calendar future = Calendar.getInstance(); |
| 158 | + future.add(Calendar.MINUTE, 5); |
| 159 | + when(identifier.getSysPublishDate()).thenReturn(future.getTime()); |
| 160 | + |
| 161 | + doAnswer(invocation -> { |
| 162 | + capturedRunnable[0] = invocation.getArgument(1); |
| 163 | + return null; |
| 164 | + }).when(debouncerMock).debounce(any(), any(), anyLong(), any()); |
| 165 | + |
| 166 | + try ( |
| 167 | + MockedStatic<LanguageUtil> langMock = Mockito.mockStatic(LanguageUtil.class); |
| 168 | + MockedStatic<SystemMessageEventUtil> msgMock = Mockito.mockStatic(SystemMessageEventUtil.class); |
| 169 | + MockedStatic<Logger> loggerMock = Mockito.mockStatic(Logger.class) |
| 170 | + ) { |
| 171 | + langMock.when(() -> LanguageUtil.get("message.contentlet.publish.future.date")) |
| 172 | + .thenReturn("Scheduled in future"); |
| 173 | + |
| 174 | + SystemMessageEventUtil mockUtil = mock(SystemMessageEventUtil.class); |
| 175 | + msgMock.when(SystemMessageEventUtil::getInstance).thenReturn(mockUtil); |
| 176 | + |
| 177 | + ContentPublishDateUtil.notifyIfFuturePublishDate(contentType, identifier, "user123"); |
| 178 | + |
| 179 | + assertNotNull(capturedRunnable[0]); |
| 180 | + capturedRunnable[0].run(); |
| 181 | + |
| 182 | + verify(mockUtil).pushMessage(any(), eq(java.util.List.of("user123"))); |
| 183 | + loggerMock.verify(() -> Logger.debug(eq(ContentPublishDateUtil.class), anyString())); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments