Search in sources :

Example 6 with FlowSnapshotEntity

use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.

the class TestRegistryService method testGetLatestSnapshotMetadataWhenNoVersionsExist.

@Test
public void testGetLatestSnapshotMetadataWhenNoVersionsExist() {
    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");
    when(metadataService.getLatestSnapshot(existingFlow.getId())).thenReturn(null);
    try {
        registryService.getLatestFlowSnapshotMetadata(existingBucket.getId(), existingFlow.getId());
        Assert.fail("Should have thrown exception");
    } catch (ResourceNotFoundException e) {
        assertEquals("The specified flow ID has no versions", e.getMessage());
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) Test(org.junit.Test)

Example 7 with FlowSnapshotEntity

use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity 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 8 with FlowSnapshotEntity

use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.

the class TestDatabaseMetadataService method testGetLatestSnapshot.

@Test
public void testGetLatestSnapshot() {
    final FlowSnapshotEntity latest = metadataService.getLatestSnapshot("1");
    assertNotNull(latest);
    assertEquals("1", latest.getFlowId());
    assertEquals(3, latest.getVersion().intValue());
}
Also used : FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Test(org.junit.Test)

Example 9 with FlowSnapshotEntity

use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.

the class TestDatabaseMetadataService method testGetFlowSnapshotDoesNotExist.

@Test
public void testGetFlowSnapshotDoesNotExist() {
    final FlowSnapshotEntity entity = metadataService.getFlowSnapshot("DOES-NOT-EXIST", 1);
    assertNull(entity);
}
Also used : FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Test(org.junit.Test)

Example 10 with FlowSnapshotEntity

use of org.apache.nifi.registry.db.entity.FlowSnapshotEntity in project nifi-registry by apache.

the class TestDatabaseMetadataService method testCreateFlowSnapshot.

@Test
public void testCreateFlowSnapshot() {
    final FlowSnapshotEntity flowSnapshot = new FlowSnapshotEntity();
    flowSnapshot.setFlowId("1");
    flowSnapshot.setVersion(4);
    flowSnapshot.setCreated(new Date());
    flowSnapshot.setCreatedBy("test-user");
    flowSnapshot.setComments("Comments");
    metadataService.createFlowSnapshot(flowSnapshot);
    final FlowSnapshotEntity createdFlowSnapshot = metadataService.getFlowSnapshot(flowSnapshot.getFlowId(), flowSnapshot.getVersion());
    assertNotNull(createdFlowSnapshot);
    assertEquals(flowSnapshot.getFlowId(), createdFlowSnapshot.getFlowId());
    assertEquals(flowSnapshot.getVersion(), createdFlowSnapshot.getVersion());
    assertEquals(flowSnapshot.getComments(), createdFlowSnapshot.getComments());
    assertEquals(flowSnapshot.getCreated(), createdFlowSnapshot.getCreated());
    assertEquals(flowSnapshot.getCreatedBy(), createdFlowSnapshot.getCreatedBy());
}
Also used : FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Date(java.util.Date) Test(org.junit.Test)

Aggregations

FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)23 Test (org.junit.Test)15 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)14 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)14 Date (java.util.Date)10 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)8 ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)6 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)4 Bucket (org.apache.nifi.registry.bucket.Bucket)3 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)3 InputStream (java.io.InputStream)2 TreeSet (java.util.TreeSet)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 FlowSnapshotContext (org.apache.nifi.registry.flow.FlowSnapshotContext)1 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)1 StandardFlowSnapshotContext (org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext)1 Nullable (org.springframework.lang.Nullable)1