@Override
    public Mono<Long> delete(final long id) {
        return Mono.zip(
                dataSourceRepository.existsByNamespace(id),
                collectorRepository.existsByNamespace(id),
                termRepository.existsByNamespace(id),
                dataEntityRepository.existsNonDeletedByNamespaceId(id)
            )
            .map(t -> BooleanUtils.toBoolean(t.getT1())
                || BooleanUtils.toBoolean(t.getT2())
                || BooleanUtils.toBoolean(t.getT3())
                || BooleanUtils.toBoolean(t.getT4()))
            .filter(exists -> !exists)
            .switchIfEmpty(Mono.error(new CascadeDeleteException(
                "Namespace cannot be deleted: there are still resources attached")))
            .flatMap(ign -> namespaceRepository.delete(id))
            .map(NamespacePojo::getId);
    }
@Test
    @DisplayName("Tries to delete a namespace which is tied with existing term and fails with an error")
    public void testDeleteTiedNamespaceWithTerm() {
        final long namespaceId = 1L;

        when(collectorRepository.existsByNamespace(eq(namespaceId))).thenReturn(Mono.just(false));
        when(dataSourceRepository.existsByNamespace(eq(namespaceId))).thenReturn(Mono.just(false));
        when(dataEntityRepository.existsNonDeletedByNamespaceId(eq(namespaceId))).thenReturn(Mono.just(false));
        when(termRepository.existsByNamespace(eq(namespaceId))).thenReturn(Mono.just(true));

        namespaceService.delete(namespaceId)
            .as(StepVerifier::create)
            .verifyError(CascadeDeleteException.class);

        verify(namespaceRepository, never()).delete(eq(namespaceId));
        verify(dataSourceRepository, only()).existsByNamespace(eq(namespaceId));
        verify(termRepository, only()).existsByNamespace(eq(namespaceId));
        verify(collectorRepository, only()).existsByNamespace(eq(namespaceId));
        verify(dataEntityRepository, only()).existsNonDeletedByNamespaceId(eq(namespaceId));
    }