### Question:
FlowMetadataSynchronizer { @EventListener(ContextRefreshedEvent.class) public void synchronize() { LOGGER.info("**************************************************"); if (!(persistenceProvider instanceof MetadataAwareFlowPersistenceProvider)) { LOGGER.info("* FlowPersistenceProvider is not metadata-aware, nothing to synchronize"); LOGGER.info("**************************************************"); return; } else { LOGGER.info("* Found metadata-aware FlowPersistenceProvider..."); } if (!metadataService.getAllBuckets().isEmpty()) { LOGGER.info("* Found existing buckets, will not synchronize metadata"); LOGGER.info("**************************************************"); return; } final MetadataAwareFlowPersistenceProvider metadataAwareFlowPersistenceProvider = (MetadataAwareFlowPersistenceProvider) persistenceProvider; LOGGER.info("* Synchronizing metadata from FlowPersistenceProvider to metadata database..."); final List<BucketMetadata> metadata = metadataAwareFlowPersistenceProvider.getMetadata(); LOGGER.info("* Synchronizing {} bucket(s)", new Object[]{metadata.size()}); for (final BucketMetadata bucketMetadata : metadata) { final BucketEntity bucketEntity = new BucketEntity(); bucketEntity.setId(bucketMetadata.getIdentifier()); bucketEntity.setName(bucketMetadata.getName()); bucketEntity.setDescription(bucketMetadata.getDescription()); bucketEntity.setCreated(new Date()); metadataService.createBucket(bucketEntity); createFlows(bucketMetadata); } LOGGER.info("* Done synchronizing metadata!"); LOGGER.info("**************************************************"); } @Autowired  FlowMetadataSynchronizer(final MetadataService metadataService,
                                    final FlowPersistenceProvider persistenceProvider); @EventListener(ContextRefreshedEvent.class) void synchronize();  }### Answer:
@Test public void testWhenMetadataAwareAndHasDataShouldSynchronize() { when(metadataService.getAllBuckets()).thenReturn(Collections.emptyList()); synchronizer.synchronize(); verify(metadataService, times(1)).createBucket(any(BucketEntity.class)); verify(metadataService, times(1)).createFlow(any(FlowEntity.class)); verify(metadataService, times(2)).createFlowSnapshot(any(FlowSnapshotEntity.class)); }
@Test public void testWhenMetadataAwareAndDatabaseNotEmptyShouldNotSynchronize() { final BucketEntity bucketEntity = new BucketEntity(); bucketEntity.setId("1"); when(metadataService.getAllBuckets()).thenReturn(Collections.singletonList(bucketEntity)); synchronizer.synchronize(); verify(metadataService, times(0)).createBucket(any(BucketEntity.class)); verify(metadataService, times(0)).createFlow(any(FlowEntity.class)); verify(metadataService, times(0)).createFlowSnapshot(any(FlowSnapshotEntity.class)); }
@Test public void testWhenNotMetadataAwareShouldNotSynchronize() { when(metadataService.getAllBuckets()).thenReturn(Collections.emptyList()); synchronizer = new FlowMetadataSynchronizer(metadataService, standardFlowPersistenceProvider); synchronizer.synchronize(); verify(metadataService, times(0)).createBucket(any(BucketEntity.class)); verify(metadataService, times(0)).createFlow(any(FlowEntity.class)); verify(metadataService, times(0)).createFlowSnapshot(any(FlowSnapshotEntity.class)); }