From 1cc346b63d0ecea1d0b2e9079a0f31c15f035b6d Mon Sep 17 00:00:00 2001 From: Bryan Bende Date: Tue, 14 Nov 2023 11:32:48 -0500 Subject: [PATCH] NIFI-12368 Clear versionedComponentId for copied Snippets This closes #8025 Signed-off-by: David Handermann --- .../apache/nifi/web/util/SnippetUtils.java | 17 +++++++ .../nifi/web/util/SnippetUtilsTest.java | 46 ++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java index 95b918e972..b26c41b0f9 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java @@ -404,9 +404,26 @@ public final class SnippetUtils { public FlowSnippetDTO copy(final FlowSnippetDTO snippetContents, final ProcessGroup group, final String idGenerationSeed, boolean isCopy) { final FlowSnippetDTO snippetCopy = copyContentsForGroup(snippetContents, group.getIdentifier(), null, null, idGenerationSeed, isCopy); resolveNameConflicts(snippetCopy, group); + removeTopLevelVersionedIds(snippetContents); return snippetCopy; } + private void removeTopLevelVersionedIds(final FlowSnippetDTO snippetContents) { + removeVersionedIds(snippetContents.getProcessors()); + removeVersionedIds(snippetContents.getLabels()); + removeVersionedIds(snippetContents.getConnections()); + removeVersionedIds(snippetContents.getInputPorts()); + removeVersionedIds(snippetContents.getOutputPorts()); + removeVersionedIds(snippetContents.getRemoteProcessGroups()); + removeVersionedIds(snippetContents.getFunnels()); + } + + private void removeVersionedIds(final Collection components) { + if (components != null) { + components.forEach(c -> c.setVersionedComponentId(null)); + } + } + private void resolveNameConflicts(final FlowSnippetDTO snippetContents, final ProcessGroup group) { // get a list of all names of ports so that we can rename the ports as needed. final List existingPortNames = new ArrayList<>(); diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/util/SnippetUtilsTest.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/util/SnippetUtilsTest.java index 4df259dd06..b3da3edcad 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/util/SnippetUtilsTest.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/util/SnippetUtilsTest.java @@ -17,7 +17,12 @@ package org.apache.nifi.web.util; +import org.apache.nifi.groups.ProcessGroup; import org.apache.nifi.util.ComponentIdGenerator; +import org.apache.nifi.web.api.dto.DtoFactory; +import org.apache.nifi.web.api.dto.FlowSnippetDTO; +import org.apache.nifi.web.api.dto.LabelDTO; +import org.apache.nifi.web.dao.AccessPolicyDAO; import org.junit.jupiter.api.Test; import java.lang.reflect.Method; @@ -25,11 +30,16 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SnippetUtilsTest { @@ -193,7 +203,7 @@ public class SnippetUtilsTest { * sequentially correct where each subsequent ID is > previous ID. */ @Test - public void validateIdOrdering() throws Exception { + public void validateIdOrdering() { UUID seed = ComponentIdGenerator.generateId(); UUID currentId1 = ComponentIdGenerator.generateId(); UUID currentId2 = ComponentIdGenerator.generateId(); @@ -217,4 +227,38 @@ public class SnippetUtilsTest { assertEquals(id2, list.get(1)); assertEquals(id3, list.get(2)); } + + @Test + public void testCopyVersionedComponentIdRemoved() { + final FlowSnippetDTO flowSnippetDTO = new FlowSnippetDTO(); + + final String versionedComponentId = UUID.randomUUID().toString(); + + final LabelDTO labelDTO = new LabelDTO(); + labelDTO.setVersionedComponentId(versionedComponentId); + labelDTO.setId(UUID.randomUUID().toString()); + final Set labels = Collections.singleton(labelDTO); + flowSnippetDTO.setLabels(labels); + + final String processGroupId = UUID.randomUUID().toString(); + final ProcessGroup processGroup = mock(ProcessGroup.class); + when(processGroup.getIdentifier()).thenReturn(processGroupId); + + final AccessPolicyDAO accessPolicyDao = mock(AccessPolicyDAO.class); + final SnippetUtils snippetUtils = new SnippetUtils(); + snippetUtils.setAccessPolicyDAO(accessPolicyDao); + snippetUtils.setDtoFactory(new DtoFactory()); + + final String seed = ComponentIdGenerator.generateId().toString(); + + final FlowSnippetDTO copied = snippetUtils.copy(flowSnippetDTO, processGroup, seed, true); + + final Set copiedLabels = copied.getLabels(); + assertNotNull(copiedLabels); + final LabelDTO copiedLabel = copiedLabels.iterator().next(); + assertNotNull(copiedLabel); + assertNotNull(copiedLabel.getVersionedComponentId()); + + assertNull(labelDTO.getVersionedComponentId()); + } }