DataServiceReferenceSynchronizer { public void sync( TransMeta transMeta, Function<? super Exception, ?> exceptionHandler ) { sync( transMeta, exceptionHandler, false ); } DataServiceReferenceSynchronizer( Context context ); @VisibleForTesting  DataServiceReferenceSynchronizer(); void sync( TransMeta transMeta, Function<? super Exception, ?> exceptionHandler ); void sync( TransMeta transMeta, Function<? super Exception, ?> exceptionHandler, boolean overwrite );  }
@Test public void testSyncWithNothingPreviouslyPublished() throws MetaStoreException { List<DataServiceMeta> metas = mockDataServiceMetas( "foo", "bar" ); when( embeddedMetaStoreFactory.getElements() ).thenReturn( metas ); when( externalMetaStoreFactory.getElements() ).thenReturn( Collections.emptyList() ); synchronizer.sync( transMeta, exceptionHandler ); verify( externalMetaStoreFactory, times( 2 ) ).saveElement( captor.capture() ); assertThat( captor.getAllValues().stream() .map( svcTrans -> svcTrans.getName() ).collect( Collectors.toList() ), containsInAnyOrder( "foo", "bar" ) ); verify( externalMetaStoreFactory, times( 0 ) ).deleteElement( anyString() ); }
@Test public void testSyncWithOtherStuffPublished() throws MetaStoreException { when( transMeta.getPathAndName() ).thenReturn( "/transLocation" ); List<DataServiceMeta> metas = mockDataServiceMetas( "foo", "bar", "baz" ); List<ServiceTrans> serviceTrans = mockDataServiceTrans( "/location", "bop", "bip", "fip" ); setupMetaStoreFactoryMocks( metas, serviceTrans ); synchronizer.sync( transMeta, null, true ); verify( externalMetaStoreFactory, times( 3 ) ).saveElement( captor.capture() ); assertThat( captor.getAllValues().stream() .map( svcTrans -> svcTrans.getName() ).collect( Collectors.toList() ), containsInAnyOrder( "foo", "bar", "baz" ) ); verify( externalMetaStoreFactory, times( 0 ) ).deleteElement( anyString() ); }
@Test public void testSyncWithConflictsNoOverwrite() throws MetaStoreException { when( transMeta.getPathAndName() ).thenReturn( "/transLocation" ); List<DataServiceMeta> metas = mockDataServiceMetas( "foo", "bar", "baz" ); List<ServiceTrans> serviceTrans = mockDataServiceTrans( "/location", "bar", "bip", "fip" ); setupMetaStoreFactoryMocks( metas, serviceTrans ); synchronizer.sync( transMeta, exceptionHandler, false ); verify( externalMetaStoreFactory, times( 2 ) ).saveElement( captor.capture() ); assertThat( captor.getAllValues().stream() .map( svcTrans -> svcTrans.getName() ).collect( Collectors.toList() ), containsInAnyOrder( "foo", "baz" ) ); verify( externalMetaStoreFactory, times( 0 ) ).deleteElement( anyString() ); }
@Test public void testSyncWithConflictsWithOverwrite() throws MetaStoreException { when( transMeta.getPathAndName() ).thenReturn( "/transLocation" ); List<DataServiceMeta> metas = mockDataServiceMetas( "foo", "bar", "baz" ); List<ServiceTrans> serviceTrans = mockDataServiceTrans( "/location", "bar", "bip", "fip" ); setupMetaStoreFactoryMocks( metas, serviceTrans ); synchronizer.sync( transMeta, exceptionHandler, true ); verify( externalMetaStoreFactory, times( 3 ) ).saveElement( captor.capture() ); assertThat( captor.getAllValues().stream() .map( svcTrans -> svcTrans.getName() ).collect( Collectors.toList() ), containsInAnyOrder( "foo", "bar", "baz" ) ); verify( externalMetaStoreFactory, times( 0 ) ).deleteElement( anyString() ); }
@Test public void testCleanupUnusedReferences() throws MetaStoreException { when( transMeta.getPathAndName() ).thenReturn( "/location" ); List<DataServiceMeta> metas = mockDataServiceMetas( "foo", "bar", "baz" ); List<ServiceTrans> serviceTrans = mockDataServiceTrans( "/location", "bar", "bip", "fip" ); setupMetaStoreFactoryMocks( metas, serviceTrans ); synchronizer.sync( transMeta, exceptionHandler, true ); verify( externalMetaStoreFactory, times( 3 ) ).saveElement( captor.capture() ); assertThat( captor.getAllValues().stream() .map( svcTrans -> svcTrans.getName() ).collect( Collectors.toList() ), containsInAnyOrder( "foo", "bar", "baz" ) ); verify( externalMetaStoreFactory, times( 1 ) ).deleteElement( "bip" ); verify( externalMetaStoreFactory, times( 1 ) ).deleteElement( "fip" ); }
@Test public void testMetastoreExceptionDuringSvcLoad() throws MetaStoreException { when( embeddedMetaStoreFactory.getElements() ).thenThrow( exception ); synchronizer.sync( transMeta, exceptionHandler, true ); verify( exceptionHandler, times( 1 ) ).apply( exception ); }
@Test public void testMetastoreExceptionDuringSave() throws MetaStoreException { doThrow( exception ).when( externalMetaStoreFactory ).saveElement( anyObject() ); List<DataServiceMeta> metas = mockDataServiceMetas( "foo" ); List<ServiceTrans> serviceTrans = mockDataServiceTrans( "/location", "bar" ); setupMetaStoreFactoryMocks( metas, serviceTrans ); synchronizer.sync( transMeta, exceptionHandler, true ); verify( exceptionHandler, times( 1 ) ).apply( exception ); }
@Test public void testMetastoreExceptionDuringDelete() throws MetaStoreException { when( transMeta.getPathAndName() ).thenReturn( "/location" ); doThrow( exception ).when( externalMetaStoreFactory ).deleteElement( anyObject() ); List<DataServiceMeta> metas = mockDataServiceMetas( "foo" ); List<ServiceTrans> serviceTrans = mockDataServiceTrans( "/location", "bar" ); setupMetaStoreFactoryMocks( metas, serviceTrans ); synchronizer.sync( transMeta, exceptionHandler, true ); verify( exceptionHandler, times( 1 ) ).apply( exception ); }