Search in sources :

Example 1 with ComponentDifferenceGroup

use of org.apache.nifi.registry.diff.ComponentDifferenceGroup in project nifi-registry by apache.

the class TestRegistryService method testGetDiffReturnsRemovedComponentChanges.

// -----------------Test Flow Diff Service Method---------------------
@Test
public void testGetDiffReturnsRemovedComponentChanges() {
    when(flowPersistenceProvider.getFlowContent(anyString(), anyString(), anyInt())).thenReturn(new byte[10], new byte[10]);
    final VersionedProcessGroup pgA = createVersionedProcessGroupA();
    final VersionedProcessGroup pgB = createVersionedProcessGroupB();
    when(snapshotSerializer.deserialize(any())).thenReturn(pgA, pgB);
    final VersionedFlowDifference diff = registryService.getFlowDiff("bucketIdentifier", "flowIdentifier", 1, 2);
    assertNotNull(diff);
    Optional<ComponentDifferenceGroup> removedComponent = diff.getComponentDifferenceGroups().stream().filter(p -> p.getComponentId().equals("ID-pg1")).findFirst();
    assertTrue(removedComponent.isPresent());
    assertTrue(removedComponent.get().getDifferences().iterator().next().getDifferenceType().equals("COMPONENT_REMOVED"));
}
Also used : Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Validator(javax.validation.Validator) Set(java.util.Set) Serializer(org.apache.nifi.registry.serialization.Serializer) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) List(java.util.List) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Bucket(org.apache.nifi.registry.bucket.Bucket) ValidatorFactory(javax.validation.ValidatorFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) ComponentDifference(org.apache.nifi.registry.diff.ComponentDifference) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) Validation(javax.validation.Validation) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Before(org.junit.Before) OutputStream(java.io.OutputStream) VersionedProcessor(org.apache.nifi.registry.flow.VersionedProcessor) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup) VersionedProcessGroupSerializer(org.apache.nifi.registry.serialization.VersionedProcessGroupSerializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) FlowPersistenceProvider(org.apache.nifi.registry.flow.FlowPersistenceProvider) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) Mockito.verify(org.mockito.Mockito.verify) ConstraintViolationException(javax.validation.ConstraintViolationException) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup) Test(org.junit.Test)

Example 2 with ComponentDifferenceGroup

use of org.apache.nifi.registry.diff.ComponentDifferenceGroup in project nifi-registry by apache.

the class TestRegistryService method testGetDiffReturnsChangesInChronologicalOrder.

@Test
public void testGetDiffReturnsChangesInChronologicalOrder() {
    when(flowPersistenceProvider.getFlowContent(anyString(), anyString(), anyInt())).thenReturn(new byte[10], new byte[10]);
    final VersionedProcessGroup pgA = createVersionedProcessGroupA();
    final VersionedProcessGroup pgB = createVersionedProcessGroupB();
    when(snapshotSerializer.deserialize(any())).thenReturn(pgA, pgB);
    // getFlowDiff orders the changes in ascending order of version number regardless of param order
    final VersionedFlowDifference diff = registryService.getFlowDiff("bucketIdentifier", "flowIdentifier", 2, 1);
    assertNotNull(diff);
    Optional<ComponentDifferenceGroup> nameChangedComponent = diff.getComponentDifferenceGroups().stream().filter(p -> p.getComponentId().equals("ProcessorFirstV1")).findFirst();
    assertTrue(nameChangedComponent.isPresent());
    ComponentDifference nameChangeDifference = nameChangedComponent.get().getDifferences().stream().filter(d -> d.getDifferenceType().equals("NAME_CHANGED")).findFirst().get();
    assertEquals("ProcessorFirstV1", nameChangeDifference.getValueA());
    assertEquals("ProcessorFirstV2", nameChangeDifference.getValueB());
}
Also used : Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Validator(javax.validation.Validator) Set(java.util.Set) Serializer(org.apache.nifi.registry.serialization.Serializer) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) List(java.util.List) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Bucket(org.apache.nifi.registry.bucket.Bucket) ValidatorFactory(javax.validation.ValidatorFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) ComponentDifference(org.apache.nifi.registry.diff.ComponentDifference) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) Validation(javax.validation.Validation) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Before(org.junit.Before) OutputStream(java.io.OutputStream) VersionedProcessor(org.apache.nifi.registry.flow.VersionedProcessor) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup) VersionedProcessGroupSerializer(org.apache.nifi.registry.serialization.VersionedProcessGroupSerializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) FlowPersistenceProvider(org.apache.nifi.registry.flow.FlowPersistenceProvider) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) Mockito.verify(org.mockito.Mockito.verify) ConstraintViolationException(javax.validation.ConstraintViolationException) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentDifference(org.apache.nifi.registry.diff.ComponentDifference) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup) Test(org.junit.Test)

Example 3 with ComponentDifferenceGroup

use of org.apache.nifi.registry.diff.ComponentDifferenceGroup in project nifi-registry by apache.

the class DataModelMapper method map.

public static ComponentDifferenceGroup map(VersionedComponent versionedComponent) {
    ComponentDifferenceGroup grouping = new ComponentDifferenceGroup();
    grouping.setComponentId(versionedComponent.getIdentifier());
    grouping.setComponentName(versionedComponent.getName());
    grouping.setProcessGroupId(versionedComponent.getGroupIdentifier());
    grouping.setComponentType(versionedComponent.getComponentType().getTypeName());
    return grouping;
}
Also used : ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup)

Example 4 with ComponentDifferenceGroup

use of org.apache.nifi.registry.diff.ComponentDifferenceGroup in project nifi-registry by apache.

the class RegistryService method getFlowDiff.

/**
 * Returns the differences between two specified versions of a flow.
 *
 * @param bucketIdentifier the id of the bucket the flow exists in
 * @param flowIdentifier the flow to be examined
 * @param versionA the first version of the comparison
 * @param versionB the second version of the comparison
 * @return The differences between two specified versions, grouped by component.
 */
public VersionedFlowDifference getFlowDiff(final String bucketIdentifier, final String flowIdentifier, final Integer versionA, final Integer versionB) {
    if (StringUtils.isBlank(bucketIdentifier)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
    }
    if (StringUtils.isBlank(flowIdentifier)) {
        throw new IllegalArgumentException("Flow identifier cannot be null or blank");
    }
    if (versionA == null || versionB == null) {
        throw new IllegalArgumentException("Version cannot be null or blank");
    }
    // older version is always the lower, regardless of the order supplied
    final Integer older = Math.min(versionA, versionB);
    final Integer newer = Math.max(versionA, versionB);
    readLock.lock();
    try {
        // Get the content for both versions of the flow
        final byte[] serializedSnapshotA = flowPersistenceProvider.getFlowContent(bucketIdentifier, flowIdentifier, older);
        if (serializedSnapshotA == null || serializedSnapshotA.length == 0) {
            throw new IllegalStateException("No serialized content found for snapshot with flow identifier " + flowIdentifier + " and version " + older);
        }
        final byte[] serializedSnapshotB = flowPersistenceProvider.getFlowContent(bucketIdentifier, flowIdentifier, newer);
        if (serializedSnapshotB == null || serializedSnapshotB.length == 0) {
            throw new IllegalStateException("No serialized content found for snapshot with flow identifier " + flowIdentifier + " and version " + newer);
        }
        // deserialize the contents
        final InputStream inputA = new ByteArrayInputStream(serializedSnapshotA);
        final VersionedProcessGroup flowContentsA = processGroupSerializer.deserialize(inputA);
        final InputStream inputB = new ByteArrayInputStream(serializedSnapshotB);
        final VersionedProcessGroup flowContentsB = processGroupSerializer.deserialize(inputB);
        final ComparableDataFlow comparableFlowA = new StandardComparableDataFlow(String.format("Version %d", older), flowContentsA);
        final ComparableDataFlow comparableFlowB = new StandardComparableDataFlow(String.format("Version %d", newer), flowContentsB);
        // Compare the two versions of the flow
        final FlowComparator flowComparator = new StandardFlowComparator(comparableFlowA, comparableFlowB, null, new ConciseEvolvingDifferenceDescriptor());
        final FlowComparison flowComparison = flowComparator.compare();
        VersionedFlowDifference result = new VersionedFlowDifference();
        result.setBucketId(bucketIdentifier);
        result.setFlowId(flowIdentifier);
        result.setVersionA(older);
        result.setVersionB(newer);
        Set<ComponentDifferenceGroup> differenceGroups = getStringComponentDifferenceGroupMap(flowComparison.getDifferences());
        result.setComponentDifferenceGroups(differenceGroups);
        return result;
    } finally {
        readLock.unlock();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) FlowComparison(org.apache.nifi.registry.flow.diff.FlowComparison) ConciseEvolvingDifferenceDescriptor(org.apache.nifi.registry.flow.diff.ConciseEvolvingDifferenceDescriptor) StandardFlowComparator(org.apache.nifi.registry.flow.diff.StandardFlowComparator) StandardComparableDataFlow(org.apache.nifi.registry.flow.diff.StandardComparableDataFlow) ComparableDataFlow(org.apache.nifi.registry.flow.diff.ComparableDataFlow) ByteArrayInputStream(java.io.ByteArrayInputStream) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup) StandardComparableDataFlow(org.apache.nifi.registry.flow.diff.StandardComparableDataFlow) StandardFlowComparator(org.apache.nifi.registry.flow.diff.StandardFlowComparator) FlowComparator(org.apache.nifi.registry.flow.diff.FlowComparator)

Example 5 with ComponentDifferenceGroup

use of org.apache.nifi.registry.diff.ComponentDifferenceGroup in project nifi-registry by apache.

the class RegistryService method getStringComponentDifferenceGroupMap.

/**
 * Group the differences in the comparison by component
 * @param flowDifferences The differences to group together by component
 * @return A set of componentDifferenceGroups where each entry contains a set of differences specific to that group
 */
private Set<ComponentDifferenceGroup> getStringComponentDifferenceGroupMap(Set<FlowDifference> flowDifferences) {
    Map<String, ComponentDifferenceGroup> differenceGroups = new HashMap<>();
    for (FlowDifference diff : flowDifferences) {
        ComponentDifferenceGroup group;
        // A component may only exist on only one version for new/removed components
        VersionedComponent component = ObjectUtils.firstNonNull(diff.getComponentA(), diff.getComponentB());
        if (differenceGroups.containsKey(component.getIdentifier())) {
            group = differenceGroups.get(component.getIdentifier());
        } else {
            group = DataModelMapper.map(component);
            differenceGroups.put(component.getIdentifier(), group);
        }
        group.getDifferences().add(DataModelMapper.map(diff));
    }
    return differenceGroups.values().stream().collect(Collectors.toSet());
}
Also used : VersionedComponent(org.apache.nifi.registry.flow.VersionedComponent) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) FlowDifference(org.apache.nifi.registry.flow.diff.FlowDifference) HashMap(java.util.HashMap) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup)

Aggregations

ComponentDifferenceGroup (org.apache.nifi.registry.diff.ComponentDifferenceGroup)5 VersionedFlowDifference (org.apache.nifi.registry.diff.VersionedFlowDifference)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 Validation (javax.validation.Validation)2 Validator (javax.validation.Validator)2 ValidatorFactory (javax.validation.ValidatorFactory)2 Bucket (org.apache.nifi.registry.bucket.Bucket)2