DBInstanceFactoryImpl implements DBInstanceFactory { @Override public void deleteInstance(String instanceId) throws JasDBStorageException { if(instances.containsKey(instanceId)) { try { storageServiceFactory.removeAllStorageService(instanceId); metadataStore.removeInstance(instanceId); instances.remove(instanceId); } catch(ConfigurationException e) { throw new JasDBStorageException("Unable to remove bags from instance: " + instanceId, e); } } else { throw new JasDBStorageException("Unable to delete instance: " + instanceId + " does not exist"); } } @Autowired  DBInstanceFactoryImpl(MetadataStore metadataStore, StorageServiceFactory storageServiceFactory); @Override void addInstance(String instanceId); @Override void deleteInstance(String instanceId); @Override DBInstance getInstance(); @Override DBInstance getInstance(String instanceId); @Override boolean hasInstance(String instanceId); @Override List<DBInstance> listInstances(); @Override void shutdown();  }
@Test public void testDeleteInstance() throws JasDBStorageException, IOException { DBInstanceFactoryImpl instanceFactory = new DBInstanceFactoryImpl(metadataStore, storageServiceFactory); Instance instanceMeta = mock(Instance.class); when(metadataStore.addInstance(INSTANCE_1)).thenReturn(instanceMeta); when(instanceMeta.getInstanceId()).thenReturn(INSTANCE_1); instanceFactory.addInstance(INSTANCE_1); assertThat(getInstanceIds(instanceFactory.listInstances()), hasItems(INSTANCE_1)); when(metadataStore.containsInstance(INSTANCE_1)).thenReturn(true); instanceFactory.deleteInstance(INSTANCE_1); verify(metadataStore, times(1)).removeInstance(INSTANCE_1); }
@Test(expected = JasDBStorageException.class) public void testDeleteNotExisting() throws JasDBStorageException { MetadataStore metadataStore = mock(MetadataStore.class); when(metadataStore.containsInstance("notexisting")).thenReturn(false); DBInstanceFactoryImpl instanceFactory = new DBInstanceFactoryImpl(metadataStore, storageServiceFactory); instanceFactory.deleteInstance("notexisting"); }