Search in sources :

Example 6 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class TestRegistryService method testGetFlowSnapshots.

@Test
public void testGetFlowSnapshots() {
    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);
    final FlowSnapshotEntity existingSnapshot1 = new FlowSnapshotEntity();
    existingSnapshot1.setVersion(1);
    existingSnapshot1.setFlowId(existingFlow.getId());
    existingSnapshot1.setCreatedBy("user1");
    existingSnapshot1.setCreated(new Date());
    existingSnapshot1.setComments("This is snapshot 1");
    final FlowSnapshotEntity existingSnapshot2 = new FlowSnapshotEntity();
    existingSnapshot2.setVersion(2);
    existingSnapshot2.setFlowId(existingFlow.getId());
    existingSnapshot2.setCreatedBy("user2");
    existingSnapshot2.setCreated(new Date());
    existingSnapshot2.setComments("This is snapshot 2");
    final List<FlowSnapshotEntity> snapshots = new ArrayList<>();
    snapshots.add(existingSnapshot1);
    snapshots.add(existingSnapshot2);
    when(metadataService.getSnapshots(existingFlow.getId())).thenReturn(snapshots);
    final SortedSet<VersionedFlowSnapshotMetadata> retrievedSnapshots = registryService.getFlowSnapshots(existingBucket.getId(), existingFlow.getId());
    assertNotNull(retrievedSnapshots);
    assertEquals(2, retrievedSnapshots.size());
    // check that sorted set order is reversed
    assertEquals(2, retrievedSnapshots.first().getVersion());
    assertEquals(1, retrievedSnapshots.last().getVersion());
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ArrayList(java.util.ArrayList) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test)

Example 7 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class TestRegistryService method testGetSnapshotExists.

@Test
public void testGetSnapshotExists() {
    final BucketEntity existingBucket = createBucketEntity("b1");
    final FlowEntity existingFlow = createFlowEntity(existingBucket.getId());
    final FlowSnapshotEntity existingSnapshot = createFlowSnapshotEntity(existingFlow.getId());
    existingFlow.setSnapshotCount(10);
    when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
    when(metadataService.getFlowByIdWithSnapshotCounts(existingFlow.getId())).thenReturn(existingFlow);
    when(metadataService.getFlowSnapshot(existingFlow.getId(), existingSnapshot.getVersion())).thenReturn(existingSnapshot);
    // return a non-null, non-zero-length array so something gets passed to the serializer
    when(flowPersistenceProvider.getFlowContent(existingBucket.getId(), existingSnapshot.getFlowId(), existingSnapshot.getVersion())).thenReturn(new byte[10]);
    final VersionedFlowSnapshot snapshotToDeserialize = createSnapshot();
    when(snapshotSerializer.deserialize(any(InputStream.class))).thenReturn(snapshotToDeserialize.getFlowContents());
    final VersionedFlowSnapshot returnedSnapshot = registryService.getFlowSnapshot(existingBucket.getId(), existingSnapshot.getFlowId(), existingSnapshot.getVersion());
    assertNotNull(returnedSnapshot);
    assertNotNull(returnedSnapshot.getSnapshotMetadata());
    final VersionedFlowSnapshotMetadata snapshotMetadata = returnedSnapshot.getSnapshotMetadata();
    assertEquals(existingSnapshot.getVersion().intValue(), snapshotMetadata.getVersion());
    assertEquals(existingBucket.getId(), snapshotMetadata.getBucketIdentifier());
    assertEquals(existingSnapshot.getFlowId(), snapshotMetadata.getFlowIdentifier());
    assertEquals(existingSnapshot.getCreated(), new Date(snapshotMetadata.getTimestamp()));
    assertEquals(existingSnapshot.getCreatedBy(), snapshotMetadata.getAuthor());
    assertEquals(existingSnapshot.getComments(), snapshotMetadata.getComments());
    final VersionedFlow versionedFlow = returnedSnapshot.getFlow();
    assertNotNull(versionedFlow);
    assertNotNull(versionedFlow.getVersionCount());
    assertTrue(versionedFlow.getVersionCount() > 0);
    final Bucket bucket = returnedSnapshot.getBucket();
    assertNotNull(bucket);
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Bucket(org.apache.nifi.registry.bucket.Bucket) InputStream(java.io.InputStream) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test)

Example 8 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class TestRegistryService method createSnapshot.

// ---------------------- Test VersionedFlowSnapshot methods ---------------------------------------------
private VersionedFlowSnapshot createSnapshot() {
    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setFlowIdentifier("flow1");
    snapshotMetadata.setVersion(1);
    snapshotMetadata.setComments("This is the first snapshot");
    snapshotMetadata.setBucketIdentifier("b1");
    snapshotMetadata.setAuthor("user1");
    final VersionedProcessGroup processGroup = new VersionedProcessGroup();
    processGroup.setIdentifier("pg1");
    processGroup.setName("My Process Group");
    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlowContents(processGroup);
    return snapshot;
}
Also used : VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 9 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class RegistryService method getFlowSnapshots.

/**
 * Returns all versions of a flow, sorted newest to oldest.
 *
 * @param bucketIdentifier the id of the bucket to search for the flowIdentifier
 * @param flowIdentifier the id of the flow to retrieve from the specified bucket
 * @return all versions of the specified flow, sorted newest to oldest
 */
public SortedSet<VersionedFlowSnapshotMetadata> getFlowSnapshots(final String bucketIdentifier, final String flowIdentifier) {
    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");
    }
    readLock.lock();
    try {
        // ensure the bucket exists
        final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // ensure the flow exists
        final FlowEntity existingFlow = metadataService.getFlowById(flowIdentifier);
        if (existingFlow == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
            throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
        }
        if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // convert the set of FlowSnapshotEntity to set of VersionedFlowSnapshotMetadata, ordered by version descending
        final SortedSet<VersionedFlowSnapshotMetadata> sortedSnapshots = new TreeSet<>(Collections.reverseOrder());
        final List<FlowSnapshotEntity> existingFlowSnapshots = metadataService.getSnapshots(existingFlow.getId());
        if (existingFlowSnapshots != null) {
            existingFlowSnapshots.stream().forEach(s -> sortedSnapshots.add(DataModelMapper.map(existingBucket, s)));
        }
        return sortedSnapshots;
    } finally {
        readLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) TreeSet(java.util.TreeSet) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 10 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class FlowsIT method testCreateFlowVersionGetFlowVersion.

@Test
public void testCreateFlowVersionGetFlowVersion() throws Exception {
    // Given: an empty Bucket "3" (see FlowsIT.sql) with a newly created flow
    long testStartTime = System.currentTimeMillis();
    final String bucketId = "2";
    final VersionedFlow flow = new VersionedFlow();
    flow.setBucketIdentifier(bucketId);
    flow.setName("Test Flow for creating snapshots");
    flow.setDescription("This is a randomly named flow created by an integration test for the purpose of holding snapshots.");
    final VersionedFlow createdFlow = client.target(createURL("buckets/{bucketId}/flows")).resolveTemplate("bucketId", bucketId).request().post(Entity.entity(flow, MediaType.APPLICATION_JSON), VersionedFlow.class);
    final String flowId = createdFlow.getIdentifier();
    // When: an initial flow snapshot is created *without* a version
    final VersionedFlowSnapshotMetadata flowSnapshotMetadata = new VersionedFlowSnapshotMetadata();
    flowSnapshotMetadata.setBucketIdentifier("2");
    flowSnapshotMetadata.setFlowIdentifier(flowId);
    flowSnapshotMetadata.setComments("This is snapshot 1, created by an integration test.");
    final VersionedFlowSnapshot flowSnapshot = new VersionedFlowSnapshot();
    flowSnapshot.setSnapshotMetadata(flowSnapshotMetadata);
    // an empty root process group
    flowSnapshot.setFlowContents(new VersionedProcessGroup());
    WebTarget clientRequestTarget = client.target(createURL("buckets/{bucketId}/flows/{flowId}/versions")).resolveTemplate("bucketId", bucketId).resolveTemplate("flowId", flowId);
    final Response response = clientRequestTarget.request().post(Entity.entity(flowSnapshot, MediaType.APPLICATION_JSON), Response.class);
    // Then: an error is returned because version != 1
    assertEquals(400, response.getStatus());
    // But When: an initial flow snapshot is created with version == 1
    flowSnapshot.getSnapshotMetadata().setVersion(1);
    final VersionedFlowSnapshot createdFlowSnapshot = clientRequestTarget.request().post(Entity.entity(flowSnapshot, MediaType.APPLICATION_JSON), VersionedFlowSnapshot.class);
    // Then: the server returns the created flow snapshot, with server-set fields populated correctly :)
    assertFlowSnapshotsEqual(flowSnapshot, createdFlowSnapshot, false);
    // both server and client in same JVM, so there shouldn't be skew
    assertTrue(createdFlowSnapshot.getSnapshotMetadata().getTimestamp() - testStartTime > 0L);
    assertEquals("anonymous", createdFlowSnapshot.getSnapshotMetadata().getAuthor());
    assertNotNull(createdFlowSnapshot.getSnapshotMetadata().getLink());
    assertNotNull(createdFlowSnapshot.getSnapshotMetadata().getLink().getUri());
    assertNotNull(createdFlowSnapshot.getFlow());
    assertEquals(1, createdFlowSnapshot.getFlow().getVersionCount());
    assertNotNull(createdFlowSnapshot.getBucket());
    // And when .../flows/{id}/versions is queried, then the newly created flow snapshot is returned in the list
    final VersionedFlowSnapshotMetadata[] versionedFlowSnapshots = clientRequestTarget.request().get(VersionedFlowSnapshotMetadata[].class);
    assertNotNull(versionedFlowSnapshots);
    assertEquals(1, versionedFlowSnapshots.length);
    assertFlowSnapshotMetadataEqual(createdFlowSnapshot.getSnapshotMetadata(), versionedFlowSnapshots[0], true);
    // And when the link URI is queried, then the newly created flow snapshot is returned
    final VersionedFlowSnapshot flowSnapshotByLink = client.target(createURL(versionedFlowSnapshots[0].getLink().getUri().toString())).request().get(VersionedFlowSnapshot.class);
    assertFlowSnapshotsEqual(createdFlowSnapshot, flowSnapshotByLink, true);
    assertNotNull(flowSnapshotByLink.getFlow());
    assertNotNull(flowSnapshotByLink.getBucket());
    // And when the bucket is queried by .../versions/{v}, then the newly created flow snapshot is returned
    final VersionedFlowSnapshot flowSnapshotByVersionNumber = clientRequestTarget.path("/1").request().get(VersionedFlowSnapshot.class);
    assertFlowSnapshotsEqual(createdFlowSnapshot, flowSnapshotByVersionNumber, true);
    assertNotNull(flowSnapshotByVersionNumber.getFlow());
    assertNotNull(flowSnapshotByVersionNumber.getBucket());
    // And when the latest URI is queried, then the newly created flow snapshot is returned
    final VersionedFlowSnapshot flowSnapshotByLatest = clientRequestTarget.path("/latest").request().get(VersionedFlowSnapshot.class);
    assertFlowSnapshotsEqual(createdFlowSnapshot, flowSnapshotByLatest, true);
    assertNotNull(flowSnapshotByLatest.getFlow());
    assertNotNull(flowSnapshotByLatest.getBucket());
}
Also used : Response(javax.ws.rs.core.Response) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) WebTarget(javax.ws.rs.client.WebTarget) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test)

Aggregations

VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)30 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)14 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)9 Test (org.junit.Test)9 Bucket (org.apache.nifi.registry.bucket.Bucket)8 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)8 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)8 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)8 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)8 FlowSnapshotClient (org.apache.nifi.registry.client.FlowSnapshotClient)6 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)6 Date (java.util.Date)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 IOException (java.io.IOException)4 Consumes (javax.ws.rs.Consumes)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3