You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by kd...@apache.org on 2019/04/10 18:49:58 UTC

[nifi-registry] branch master updated: NIFIREG-249 - Allow creating a flow snapshot as the latest version by sending a version of -1

This is an automated email from the ASF dual-hosted git repository.

kdoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-registry.git


The following commit(s) were added to refs/heads/master by this push:
     new c2e4055  NIFIREG-249 - Allow creating a flow snapshot as the latest version by sending a version of -1
c2e4055 is described below

commit c2e40559e6d2e23c0b454b839fce3833f5c6f599
Author: Bryan Bende <bb...@apache.org>
AuthorDate: Wed Apr 3 11:42:49 2019 -0400

    NIFIREG-249 - Allow creating a flow snapshot as the latest version by sending a version of -1
    
    This closes #168.
    
    Signed-off-by: Kevin Doran <kd...@apache.org>
---
 .../flow/VersionedFlowSnapshotMetadata.java        |   2 +-
 .../nifi/registry/service/RegistryService.java     |  22 +++--
 .../nifi/registry/service/TestRegistryService.java | 108 +++++++++++++++++++++
 3 files changed, 124 insertions(+), 8 deletions(-)

diff --git a/nifi-registry-core/nifi-registry-data-model/src/main/java/org/apache/nifi/registry/flow/VersionedFlowSnapshotMetadata.java b/nifi-registry-core/nifi-registry-data-model/src/main/java/org/apache/nifi/registry/flow/VersionedFlowSnapshotMetadata.java
index dc58cf4..dab27e3 100644
--- a/nifi-registry-core/nifi-registry-data-model/src/main/java/org/apache/nifi/registry/flow/VersionedFlowSnapshotMetadata.java
+++ b/nifi-registry-core/nifi-registry-data-model/src/main/java/org/apache/nifi/registry/flow/VersionedFlowSnapshotMetadata.java
@@ -37,7 +37,7 @@ public class VersionedFlowSnapshotMetadata extends LinkableEntity implements Com
     @NotBlank
     private String flowIdentifier;
 
-    @Min(1)
+    @Min(-1)
     private int version;
 
     @Min(1)
diff --git a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
index d4de627..1e074bf 100644
--- a/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
+++ b/nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
@@ -638,6 +638,10 @@ public class RegistryService {
                 throw new IllegalStateException("The requested flow is not located in the given bucket");
             }
 
+            if (snapshotMetadata.getVersion() == 0) {
+                throw new IllegalArgumentException("Version must be greater than zero, or use -1 to indicate latest version");
+            }
+
             // convert the set of FlowSnapshotEntity to set of VersionedFlowSnapshotMetadata
             final SortedSet<VersionedFlowSnapshotMetadata> sortedSnapshots = new TreeSet<>();
             final List<FlowSnapshotEntity> existingFlowSnapshots = metadataService.getSnapshots(existingFlow.getId());
@@ -646,18 +650,22 @@ public class RegistryService {
             }
 
             // if we already have snapshots we need to verify the new one has the correct version
-            if (sortedSnapshots != null && sortedSnapshots.size() > 0) {
+            if (sortedSnapshots.size() > 0) {
                 final VersionedFlowSnapshotMetadata lastSnapshot = sortedSnapshots.last();
 
-                if (snapshotMetadata.getVersion() <= lastSnapshot.getVersion()) {
+                // if we have existing versions and a client sends -1, then make this the latest version
+                if (snapshotMetadata.getVersion() == -1) {
+                    snapshotMetadata.setVersion(lastSnapshot.getVersion() + 1);
+                } else if (snapshotMetadata.getVersion() <= lastSnapshot.getVersion()) {
                     throw new IllegalStateException("A Versioned flow snapshot with the same version already exists: " + snapshotMetadata.getVersion());
+                } else if (snapshotMetadata.getVersion() > (lastSnapshot.getVersion() + 1)) {
+                    throw new IllegalStateException("Version must be a one-up number, last version was " + lastSnapshot.getVersion()
+                            + " and version for this snapshot was " + snapshotMetadata.getVersion());
                 }
 
-                if (snapshotMetadata.getVersion() > (lastSnapshot.getVersion() + 1)) {
-                    throw new IllegalStateException("Version must be a one-up number, last version was "
-                            + lastSnapshot.getVersion() + " and version for this snapshot was "
-                            + snapshotMetadata.getVersion());
-                }
+            } else if (snapshotMetadata.getVersion() == -1) {
+                // if we have no existing versions and a client sends -1, then this is the first version
+                snapshotMetadata.setVersion(1);
             } else if (snapshotMetadata.getVersion() != 1) {
                 throw new IllegalStateException("Version of first snapshot must be 1");
             }
diff --git a/nifi-registry-core/nifi-registry-framework/src/test/java/org/apache/nifi/registry/service/TestRegistryService.java b/nifi-registry-core/nifi-registry-framework/src/test/java/org/apache/nifi/registry/service/TestRegistryService.java
index 0af08c1..8f0de1b 100644
--- a/nifi-registry-core/nifi-registry-framework/src/test/java/org/apache/nifi/registry/service/TestRegistryService.java
+++ b/nifi-registry-core/nifi-registry-framework/src/test/java/org/apache/nifi/registry/service/TestRegistryService.java
@@ -826,6 +826,114 @@ public class TestRegistryService {
         registryService.createFlowSnapshot(snapshot);
     }
 
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateFirstSnapshotWithZeroVersion() {
+        final VersionedFlowSnapshot snapshot = createSnapshot();
+
+        final BucketEntity existingBucket = new BucketEntity();
+        existingBucket.setId("b1");
+        existingBucket.setName("My Bucket");
+        existingBucket.setDescription("This is my bucket");
+        existingBucket.setCreated(new Date());
+
+        when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
+
+        // return a flow with the existing snapshot when getFlowById is called
+        final FlowEntity existingFlow = new FlowEntity();
+        existingFlow.setId("flow1");
+        existingFlow.setName("My Flow");
+        existingFlow.setDescription("This is my flow.");
+        existingFlow.setCreated(new Date());
+        existingFlow.setModified(new Date());
+        existingFlow.setBucketId(existingBucket.getId());
+
+        when(metadataService.getFlowById(existingFlow.getId())).thenReturn(existingFlow);
+
+        // set the first version to something other than 1
+        snapshot.getSnapshotMetadata().setVersion(0);
+        registryService.createFlowSnapshot(snapshot);
+    }
+
+    @Test
+    public void testCreateFirstSnapshotWithLatestVersionWhenVersionExist() {
+        final VersionedFlowSnapshot snapshot = createSnapshot();
+
+        final BucketEntity existingBucket = new BucketEntity();
+        existingBucket.setId("b1");
+        existingBucket.setName("My Bucket");
+        existingBucket.setDescription("This is my bucket");
+        existingBucket.setCreated(new Date());
+
+        when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
+
+        // return a flow with the existing snapshot when getFlowById is called
+        final FlowEntity existingFlow = new FlowEntity();
+        existingFlow.setId("flow1");
+        existingFlow.setName("My Flow");
+        existingFlow.setDescription("This is my flow.");
+        existingFlow.setCreated(new Date());
+        existingFlow.setModified(new Date());
+        existingFlow.setBucketId(existingBucket.getId());
+
+        when(metadataService.getFlowById(existingFlow.getId())).thenReturn(existingFlow);
+        when(metadataService.getFlowByIdWithSnapshotCounts(existingFlow.getId())).thenReturn(existingFlow);
+
+        // make a snapshot that has the same version as the one being created
+        final FlowSnapshotEntity existingSnapshot = new FlowSnapshotEntity();
+        existingSnapshot.setFlowId(snapshot.getSnapshotMetadata().getFlowIdentifier());
+        existingSnapshot.setVersion(snapshot.getSnapshotMetadata().getVersion());
+        existingSnapshot.setComments("This is an existing snapshot");
+        existingSnapshot.setCreated(new Date());
+        existingSnapshot.setCreatedBy("test-user");
+
+        final List<FlowSnapshotEntity> existingSnapshots = Arrays.asList(existingSnapshot);
+        when(metadataService.getSnapshots(existingFlow.getId())).thenReturn(existingSnapshots);
+
+        // set the version to -1 to indicate that registry should make this the latest version
+        snapshot.getSnapshotMetadata().setVersion(-1);
+        registryService.createFlowSnapshot(snapshot);
+
+        final VersionedFlowSnapshot createdSnapshot = registryService.createFlowSnapshot(snapshot);
+        assertNotNull(createdSnapshot);
+        assertNotNull(createdSnapshot.getSnapshotMetadata());
+        assertEquals(2, createdSnapshot.getSnapshotMetadata().getVersion());
+    }
+
+    @Test
+    public void testCreateFirstSnapshotWithLatestVersionWhenNoVersionsExist() {
+        final VersionedFlowSnapshot snapshot = createSnapshot();
+
+        final BucketEntity existingBucket = new BucketEntity();
+        existingBucket.setId("b1");
+        existingBucket.setName("My Bucket");
+        existingBucket.setDescription("This is my bucket");
+        existingBucket.setCreated(new Date());
+
+        when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
+
+        // return a flow with the existing snapshot when getFlowById is called
+        final FlowEntity existingFlow = new FlowEntity();
+        existingFlow.setId("flow1");
+        existingFlow.setName("My Flow");
+        existingFlow.setDescription("This is my flow.");
+        existingFlow.setCreated(new Date());
+        existingFlow.setModified(new Date());
+        existingFlow.setBucketId(existingBucket.getId());
+
+        when(metadataService.getFlowById(existingFlow.getId())).thenReturn(existingFlow);
+        when(metadataService.getFlowByIdWithSnapshotCounts(existingFlow.getId())).thenReturn(existingFlow);
+        when(metadataService.getSnapshots(existingFlow.getId())).thenReturn(Collections.emptyList());
+
+        // set the version to -1 to indicate that registry should make this the latest version
+        snapshot.getSnapshotMetadata().setVersion(-1);
+        registryService.createFlowSnapshot(snapshot);
+
+        final VersionedFlowSnapshot createdSnapshot = registryService.createFlowSnapshot(snapshot);
+        assertNotNull(createdSnapshot);
+        assertNotNull(createdSnapshot.getSnapshotMetadata());
+        assertEquals(1, createdSnapshot.getSnapshotMetadata().getVersion());
+    }
+
     @Test
     public void testGetFlowSnapshots() {
         final BucketEntity existingBucket = new BucketEntity();