|
11 | 11 |
|
12 | 12 | package org.kitodo.production.forms; |
13 | 13 |
|
14 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
15 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
16 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
17 | | - |
| 14 | +import java.io.File; |
18 | 15 | import java.io.IOException; |
| 16 | +import java.net.URI; |
| 17 | +import java.nio.charset.StandardCharsets; |
19 | 18 | import java.util.Arrays; |
20 | 19 | import java.util.List; |
21 | 20 |
|
| 21 | +import org.apache.commons.io.FileUtils; |
22 | 22 | import org.junit.jupiter.api.AfterAll; |
23 | 23 | import org.junit.jupiter.api.BeforeAll; |
24 | 24 | import org.junit.jupiter.api.Test; |
25 | 25 | import org.kitodo.MockDatabase; |
| 26 | +import org.kitodo.config.ConfigCore; |
26 | 27 | import org.kitodo.data.database.beans.DataEditorSetting; |
27 | 28 | import org.kitodo.data.database.beans.Task; |
28 | 29 | import org.kitodo.data.database.beans.Template; |
|
35 | 36 | import org.kitodo.production.services.ServiceManager; |
36 | 37 | import org.kitodo.production.services.data.DataEditorSettingService; |
37 | 38 | import org.kitodo.production.services.data.TaskService; |
| 39 | +import org.mockito.ArgumentCaptor; |
| 40 | + |
| 41 | +import javax.faces.context.ExternalContext; |
| 42 | +import javax.faces.context.FacesContext; |
| 43 | +import javax.faces.context.Flash; |
| 44 | + |
| 45 | +import static org.mockito.Mockito.*; |
| 46 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 49 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
38 | 50 |
|
39 | 51 | public class WorkflowFormIT { |
40 | 52 |
|
41 | 53 | private WorkflowForm currentWorkflowForm = new WorkflowForm(); |
42 | 54 | private static final TaskService taskService = ServiceManager.getTaskService(); |
43 | 55 | private static final DataEditorSettingService dataEditorSettingService = ServiceManager.getDataEditorSettingService(); |
| 56 | + private ExternalContext mockExternalContext; |
| 57 | + private Flash mockFlash; |
44 | 58 |
|
45 | 59 | /** |
46 | 60 | * Setup Database and start elasticsearch. |
@@ -122,6 +136,45 @@ public void shouldUpdateTemplateTasksAndDeleteOnlyAffectedDataEditorSettings() t |
122 | 136 | assertEquals(0.6f, dataEditorSettingForTaskOfSecondTemplate.get(0).getGalleryWidth(), 0); |
123 | 137 | } |
124 | 138 |
|
| 139 | + @Test |
| 140 | + public void shouldDuplicateWorkflowAndStoreInFlash() throws Exception { |
| 141 | + // Arrange: Mock ExternalContext and Flash |
| 142 | + mockExternalContext = mock(ExternalContext.class); |
| 143 | + mockFlash = mock(Flash.class); |
| 144 | + currentWorkflowForm.setExternalContext(mockExternalContext); |
| 145 | + |
| 146 | + when(mockExternalContext.getFlash()).thenReturn(mockFlash); // Ensure Flash scope is returned |
| 147 | + |
| 148 | + // Act: Call the duplicate method |
| 149 | + String resultUrl = currentWorkflowForm.duplicate(1); |
| 150 | + |
| 151 | + //Assert: Ensure redirection to the edit page |
| 152 | + assertTrue(resultUrl.contains("&id=0")); |
| 153 | + |
| 154 | + //Capture arguments that were passed to Flash |
| 155 | + ArgumentCaptor<Workflow> workflowCaptor = ArgumentCaptor.forClass(Workflow.class); |
| 156 | + ArgumentCaptor<String> xmlCaptor = ArgumentCaptor.forClass(String.class); |
| 157 | + ArgumentCaptor<String> svgCaptor = ArgumentCaptor.forClass(String.class); |
| 158 | + |
| 159 | + // ✅ Verify and capture actual method calls |
| 160 | + verify(mockFlash, times(1)).put(eq("duplicatedWorkflow"), workflowCaptor.capture()); |
| 161 | + verify(mockFlash, times(1)).put(eq("xmlDiagram"), xmlCaptor.capture()); |
| 162 | + verify(mockFlash, times(1)).put(eq("svgDiagram"), svgCaptor.capture()); |
| 163 | + |
| 164 | + // ✅ Assert the captured workflow is not null and has expected values |
| 165 | + Workflow duplicatedWorkflow = workflowCaptor.getValue(); |
| 166 | + String xmlWorkflow = xmlCaptor.getValue(); |
| 167 | + assertNotNull(duplicatedWorkflow, "Duplicated workflow should not be null"); |
| 168 | + assertEquals("gateway-test1", duplicatedWorkflow.getTitle().replaceFirst("_[^_]*$", ""), |
| 169 | + "Expected duplicated workflow title"); |
| 170 | + |
| 171 | + String diagramPath = ConfigCore.getKitodoDiagramDirectory() + "gateway-test1" + ".bpmn20.xml"; |
| 172 | + String data = FileUtils.readFileToString(new File(diagramPath), StandardCharsets.UTF_8); |
| 173 | + assertEquals(data, xmlWorkflow, "Expected duplicated workflow title"); |
| 174 | + assertNotNull(xmlCaptor.getValue(), "XML Diagram should be stored"); |
| 175 | + assertNotNull(svgCaptor.getValue(), "SVG Diagram should be stored"); |
| 176 | + } |
| 177 | + |
125 | 178 | private Task createAndSaveTemplateTask(TaskStatus taskStatus, int ordering, Template template) throws DataException { |
126 | 179 | Task task = new Task(); |
127 | 180 | task.setProcessingStatus(taskStatus); |
|
0 commit comments