DataServiceMetaStoreUtil { public DataServiceMeta checkConflict( DataServiceMeta dataServiceMeta, String ignored ) throws MetaStoreException, DataServiceAlreadyExistsException { TransMeta serviceTrans = dataServiceMeta.getServiceTrans(); DataServiceMeta stepConflict = getDataServiceByStepName( serviceTrans, dataServiceMeta.getStepname() ); if ( stepConflict != null && !stepConflict.getName().equals( ignored ) ) { throw new DataServiceAlreadyExistsException( dataServiceMeta, getString( PKG, "Messages.SaveError.StepConflict", stepConflict.getStepname(), stepConflict.getName() ) ); } String name = dataServiceMeta.getName(); if ( name.equals( ignored ) ) { return dataServiceMeta; } if ( getDataServiceNames( serviceTrans ).contains( name ) ) { throw new DataServiceAlreadyExistsException( dataServiceMeta ); } if ( getServiceTransMap( serviceTrans ).containsKey( name ) ) { throw new DataServiceAlreadyExistsException( dataServiceMeta ); } return dataServiceMeta; } protected  DataServiceMetaStoreUtil( DataServiceContext context, Cache<Integer, String> cache ); protected  DataServiceMetaStoreUtil( DataServiceMetaStoreUtil metaStoreUtil ); static DataServiceMetaStoreUtil create( DataServiceContext context ); DataServiceMeta getDataService( String serviceName, Repository repository, IMetaStore metaStore ); DataServiceMeta getDataService( String serviceName, TransMeta serviceTrans ); Iterable<DataServiceMeta> getDataServices( final Repository repository, IMetaStore metaStore,
                                                    final Function<Exception, Void> exceptionHandler ); Iterable<DataServiceMeta> getDataServices( TransMeta transMeta ); List<String> getDataServiceNames( TransMeta transMeta ); List<String> getDataServiceNames( IMetaStore metaStore ); DataServiceMeta getDataServiceByStepName( TransMeta transMeta, String stepName ); DataServiceMeta checkDefined( DataServiceMeta dataServiceMeta ); DataServiceMeta checkConflict( DataServiceMeta dataServiceMeta, String ignored ); void save( DataServiceMeta dataService ); void removeDataService( DataServiceMeta dataService ); void deleteDataServiceElementAndCleanCache( DataServiceMeta dataService, TransMeta transMeta ); @Deprecated( ) void sync( TransMeta transMeta, Function<? super Exception, ?> exceptionHandler ); ImmutableMap<String, ServiceTrans> getServiceTransMap( TransMeta transMeta ); void clearReferences( TransMeta transMeta ); LogChannelInterface getLogChannel(); List<PushDownFactory> getPushDownFactories(); Function<Exception, Void> logErrors( final String message ); DataServiceContext getContext(); Cache<Integer, String> getStepCache();  }
@Test public void testCheckConflict() throws Exception { final ServiceTrans published = mock( ServiceTrans.class ); when( published.getName() ).thenReturn( "PUBLISHED_SERVICE" ); metaStoreUtil = new DataServiceMetaStoreUtil( metaStoreUtil ) { @Override protected MetaStoreFactory<ServiceTrans> getServiceTransFactory( IMetaStore metaStore ) { return new MetaStoreFactory<ServiceTrans>( ServiceTrans.class, metaStore, NAMESPACE ) { @Override public List<ServiceTrans> getElements() throws MetaStoreException { return ImmutableList.of( published ); } }; } }; DataServiceMeta local = createDataService( "OTHER_SERVICE", transMeta ); local.setStepname( "OTHER_STEP" ); metaStoreUtil.getDataServiceFactory( transMeta ).saveElement( local ); metaStoreUtil.checkConflict( dataService, null ); metaStoreUtil.checkConflict( dataService, local.getName() ); try { dataService.setName( local.getName() ); metaStoreUtil.checkConflict( dataService, null ); fail( "Expected DataServiceAlreadyExistsException" ); } catch ( DataServiceAlreadyExistsException e ) { assertThat( e.getDataServiceMeta(), sameInstance( dataService ) ); } metaStoreUtil.checkConflict( dataService, local.getName() ); dataService = createDataService( DATA_SERVICE_NAME, transMeta ); metaStoreUtil.checkConflict( dataService, null ); try { dataService.setStepname( local.getStepname() ); metaStoreUtil.checkConflict( dataService, null ); fail( "Expected DataServiceAlreadyExistsException" ); } catch ( DataServiceAlreadyExistsException e ) { assertThat( e.getDataServiceMeta(), sameInstance( dataService ) ); } metaStoreUtil.checkConflict( dataService, local.getName() ); dataService = createDataService( DATA_SERVICE_NAME, transMeta ); metaStoreUtil.checkConflict( dataService, null ); dataService.setName( published.getName() ); when( published.getReferences() ).thenReturn( ImmutableList.<ServiceTrans.Reference>of() ); metaStoreUtil.checkConflict( dataService, null ); ServiceTrans.Reference reference = mock( ServiceTrans.Reference.class ); when( reference.exists( repository ) ).thenReturn( false, true ); when( published.getReferences() ).thenReturn( ImmutableList.of( reference ) ); metaStoreUtil.checkConflict( dataService, null ); try { metaStoreUtil.checkConflict( dataService, null ); fail( "Expected DataServiceAlreadyExistsException" ); } catch ( DataServiceAlreadyExistsException e ) { assertThat( e.getDataServiceMeta(), sameInstance( dataService ) ); } try { metaStoreUtil.checkConflict( dataService, local.getName() ); fail( "Expected DataServiceAlreadyExistsException" ); } catch ( DataServiceAlreadyExistsException e ) { assertThat( e.getDataServiceMeta(), sameInstance( dataService ) ); } }