|
| 1 | +/** |
| 2 | + * This file is part of the Meeds project (https://meeds.io/). |
| 3 | + * |
| 4 | + * Copyright (C) 2020 - 2026 Meeds Association contact@meeds.io |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | +package io.meeds.task.digest; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertNotNull; |
| 23 | +import static org.junit.Assert.assertNull; |
| 24 | +import static org.mockito.Mockito.lenient; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import java.time.Instant; |
| 28 | +import java.time.ZoneId; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.LinkedHashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.mockito.Mock; |
| 39 | +import org.mockito.junit.MockitoJUnitRunner; |
| 40 | + |
| 41 | +import org.exoplatform.container.xml.InitParams; |
| 42 | +import org.exoplatform.container.xml.ValuesParam; |
| 43 | +import org.exoplatform.social.core.identity.model.Identity; |
| 44 | +import org.exoplatform.social.core.identity.model.Profile; |
| 45 | +import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider; |
| 46 | +import org.exoplatform.social.core.manager.IdentityManager; |
| 47 | +import org.exoplatform.task.dto.ProjectDto; |
| 48 | +import org.exoplatform.task.dto.StatusDto; |
| 49 | +import org.exoplatform.task.dto.TaskDto; |
| 50 | +import org.exoplatform.task.exception.EntityNotFoundException; |
| 51 | +import org.exoplatform.task.service.TaskService; |
| 52 | + |
| 53 | +import io.meeds.commons.digest.model.DigestItem; |
| 54 | +import io.meeds.commons.digest.model.DigestLine; |
| 55 | +import io.meeds.commons.digest.plugin.DigestLineContext; |
| 56 | + |
| 57 | +@RunWith(MockitoJUnitRunner.class) |
| 58 | +public class TaskDigestLinePluginTest { |
| 59 | + |
| 60 | + private static final DigestLineContext CONTEXT = new DigestLineContext("ayoub", Locale.ENGLISH, ZoneId.of("Europe/Paris")); |
| 61 | + |
| 62 | + @Mock |
| 63 | + private TaskService taskService; |
| 64 | + |
| 65 | + @Mock |
| 66 | + private IdentityManager identityManager; |
| 67 | + |
| 68 | + private TaskDigestLinePlugin plugin; |
| 69 | + |
| 70 | + @Before |
| 71 | + public void setUp() throws Exception { |
| 72 | + InitParams params = new InitParams(); |
| 73 | + ValuesParam pluginIds = new ValuesParam(); |
| 74 | + pluginIds.setName("pluginIds"); |
| 75 | + pluginIds.setValues(new ArrayList<>(List.of(TaskDigestLinePlugin.TASK_ASSIGN_PLUGIN, |
| 76 | + TaskDigestLinePlugin.TASK_COWORKER_PLUGIN, |
| 77 | + TaskDigestLinePlugin.TASK_MENTIONED_PLUGIN))); |
| 78 | + params.addParameter(pluginIds); |
| 79 | + // The platform link needs the running portal: only the stored link is |
| 80 | + // exercised here |
| 81 | + plugin = new TaskDigestLinePlugin(params, taskService, identityManager); |
| 82 | + |
| 83 | + ProjectDto project = new ProjectDto(); |
| 84 | + project.setName("Website"); |
| 85 | + StatusDto status = new StatusDto(); |
| 86 | + status.setProject(project); |
| 87 | + TaskDto task = new TaskDto(); |
| 88 | + task.setId(7); |
| 89 | + task.setTitle("Write the release notes"); |
| 90 | + task.setStatus(status); |
| 91 | + lenient().when(taskService.getTask(7)).thenReturn(task); |
| 92 | + lenient().when(taskService.getTask(404)).thenThrow(new EntityNotFoundException(404, TaskDto.class)); |
| 93 | + Identity john = new Identity(OrganizationIdentityProvider.NAME, "john"); |
| 94 | + Profile profile = new Profile(john); |
| 95 | + profile.setProperty(Profile.FULL_NAME, "John Smith"); |
| 96 | + john.setProfile(profile); |
| 97 | + lenient().when(identityManager.getOrCreateUserIdentity("john")).thenReturn(john); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testAssignedLineHasNoActor() { |
| 102 | + DigestLine line = plugin.buildLine(item(TaskDigestLinePlugin.TASK_ASSIGN_PLUGIN, "taskId", "7", "creator", "john", |
| 103 | + "taskUrl", "https://platform/portal/dw/tasks/taskDetail/7"), |
| 104 | + CONTEXT); |
| 105 | + assertNotNull(line); |
| 106 | + assertEquals("digest.line.TaskAssignPlugin", line.getLabelKey()); |
| 107 | + assertEquals(List.of("Write the release notes", "Website"), line.getArgs()); |
| 108 | + assertEquals("https://platform/portal/dw/tasks/taskDetail/7", line.getUrl()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testCoworkerAndMentionLinesNameTheActor() { |
| 113 | + DigestLine coworker = plugin.buildLine(item(TaskDigestLinePlugin.TASK_COWORKER_PLUGIN, "taskId", "7", "creator", "john", |
| 114 | + "taskUrl", "https://platform/t/7"), |
| 115 | + CONTEXT); |
| 116 | + DigestLine mention = plugin.buildLine(item(TaskDigestLinePlugin.TASK_MENTIONED_PLUGIN, "taskId", "7", "creator", "john", |
| 117 | + "taskUrl", "https://platform/t/7"), |
| 118 | + CONTEXT); |
| 119 | + assertNotNull(coworker); |
| 120 | + assertNotNull(mention); |
| 121 | + assertEquals(List.of("John Smith", "Write the release notes", "Website"), coworker.getArgs()); |
| 122 | + assertEquals(List.of("John Smith", "Write the release notes", "Website"), mention.getArgs()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testDeletedTaskGivesNoLine() { |
| 127 | + assertNull(plugin.buildLine(item(TaskDigestLinePlugin.TASK_ASSIGN_PLUGIN, "taskId", "404"), CONTEXT)); |
| 128 | + assertNull(plugin.buildLine(item(TaskDigestLinePlugin.TASK_ASSIGN_PLUGIN, "taskId", "not a number"), CONTEXT)); |
| 129 | + assertNull(plugin.buildLine(item(TaskDigestLinePlugin.TASK_ASSIGN_PLUGIN), CONTEXT)); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testUnknownTypeGivesNoLine() { |
| 134 | + assertNull(plugin.buildLine(item("TaskCompletedPlugin", "taskId", "7", "taskUrl", "https://platform/t/7"), CONTEXT)); |
| 135 | + } |
| 136 | + |
| 137 | + private static DigestItem item(String pluginId, String... params) { |
| 138 | + Map<String, String> map = new LinkedHashMap<>(); |
| 139 | + for (int i = 0; i + 1 < params.length; i += 2) { |
| 140 | + map.put(params[i], params[i + 1]); |
| 141 | + } |
| 142 | + return new DigestItem(1, "ayoub", pluginId, "tasks", Instant.now(), map); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments