Search in sources :

Example 16 with BucketEntity

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

the class TestRegistryService method testCreateSnapshotFlowDoesNotExist.

@Test(expected = ResourceNotFoundException.class)
public void testCreateSnapshotFlowDoesNotExist() {
    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);
    when(metadataService.getFlowById(snapshot.getSnapshotMetadata().getFlowIdentifier())).thenReturn(null);
    registryService.createFlowSnapshot(snapshot);
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) Date(java.util.Date) Test(org.junit.Test)

Example 17 with BucketEntity

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

the class TestRegistryService method testUpdateFlow.

@Test
public void testUpdateFlow() throws InterruptedException {
    final BucketEntity existingBucket = new BucketEntity();
    existingBucket.setId("b1");
    existingBucket.setName("My Bucket");
    existingBucket.setDescription("This is my bucket");
    existingBucket.setCreated(new Date());
    final FlowEntity flowToUpdate = new FlowEntity();
    flowToUpdate.setId("flow1");
    flowToUpdate.setName("My Flow");
    flowToUpdate.setDescription("This is my flow.");
    flowToUpdate.setCreated(new Date());
    flowToUpdate.setModified(new Date());
    flowToUpdate.setBucketId(existingBucket.getId());
    when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
    when(metadataService.getFlowByIdWithSnapshotCounts(flowToUpdate.getId())).thenReturn(flowToUpdate);
    when(metadataService.getFlowsByName(flowToUpdate.getName())).thenReturn(Collections.singletonList(flowToUpdate));
    doAnswer(updateFlowAnswer()).when(metadataService).updateFlow(any(FlowEntity.class));
    final VersionedFlow versionedFlow = new VersionedFlow();
    versionedFlow.setBucketIdentifier(flowToUpdate.getBucketId());
    versionedFlow.setIdentifier(flowToUpdate.getId());
    versionedFlow.setName("New Flow Name");
    versionedFlow.setDescription("This is a new description");
    Thread.sleep(10);
    final VersionedFlow updatedFlow = registryService.updateFlow(versionedFlow);
    assertNotNull(updatedFlow);
    assertEquals(versionedFlow.getIdentifier(), updatedFlow.getIdentifier());
    // name and description should be updated
    assertEquals(versionedFlow.getName(), updatedFlow.getName());
    assertEquals(versionedFlow.getDescription(), updatedFlow.getDescription());
    // other fields should not be updated
    assertEquals(flowToUpdate.getBucketId(), updatedFlow.getBucketIdentifier());
    assertEquals(flowToUpdate.getCreated().getTime(), updatedFlow.getCreatedTimestamp());
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) Test(org.junit.Test)

Example 18 with BucketEntity

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

the class RegistryService method deleteBucket.

public Bucket deleteBucket(final String bucketIdentifier) {
    if (bucketIdentifier == null) {
        throw new IllegalArgumentException("Bucket identifier cannot be null");
    }
    writeLock.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.");
        }
        // for each flow in the bucket, delete all snapshots from the flow persistence provider
        for (final FlowEntity flowEntity : metadataService.getFlowsByBucket(existingBucket.getId())) {
            flowPersistenceProvider.deleteAllFlowContent(bucketIdentifier, flowEntity.getId());
        }
        // now delete the bucket from the metadata provider, which deletes all flows referencing it
        metadataService.deleteBucket(existingBucket);
        return DataModelMapper.map(existingBucket);
    } finally {
        writeLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Example 19 with BucketEntity

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

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

the class RegistryService method getBucketItems.

// ---------------------- BucketItem methods ---------------------------------------------
public List<BucketItem> getBucketItems(final String bucketIdentifier) {
    if (bucketIdentifier == null) {
        throw new IllegalArgumentException("Bucket identifier cannot be null");
    }
    readLock.lock();
    try {
        final BucketEntity bucket = metadataService.getBucketById(bucketIdentifier);
        if (bucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        final List<BucketItem> bucketItems = new ArrayList<>();
        metadataService.getBucketItems(bucket.getId()).stream().forEach(b -> addBucketItem(bucketItems, b));
        return bucketItems;
    } finally {
        readLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ArrayList(java.util.ArrayList) BucketItem(org.apache.nifi.registry.bucket.BucketItem) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Aggregations

BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)49 Test (org.junit.Test)32 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)31 Date (java.util.Date)26 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)16 ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)14 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)11 Bucket (org.apache.nifi.registry.bucket.Bucket)10 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)9 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)8 ArrayList (java.util.ArrayList)5 InputStream (java.io.InputStream)3 TreeSet (java.util.TreeSet)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BucketItem (org.apache.nifi.registry.bucket.BucketItem)2 BucketItemEntity (org.apache.nifi.registry.db.entity.BucketItemEntity)2 FlowSnapshotContext (org.apache.nifi.registry.flow.FlowSnapshotContext)2 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)2 StandardFlowSnapshotContext (org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext)2