NIFI-12368 Clear versionedComponentId for copied Snippets

This closes #8025

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Bryan Bende 2023-11-14 11:32:48 -05:00 committed by exceptionfactory
parent 026222d157
commit 1cc346b63d
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
2 changed files with 62 additions and 1 deletions

View File

@ -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 <T extends ComponentDTO> void removeVersionedIds(final Collection<T> 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<String> existingPortNames = new ArrayList<>();

View File

@ -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<LabelDTO> 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<LabelDTO> copiedLabels = copied.getLabels();
assertNotNull(copiedLabels);
final LabelDTO copiedLabel = copiedLabels.iterator().next();
assertNotNull(copiedLabel);
assertNotNull(copiedLabel.getVersionedComponentId());
assertNull(labelDTO.getVersionedComponentId());
}
}