DBInstanceFactoryImpl implements DBInstanceFactory { @Override public void addInstance(String instanceId) throws JasDBStorageException { if(!metadataStore.containsInstance(instanceId)) { Instance instanceData = metadataStore.addInstance(instanceId); this.instances.put(instanceId, new DBInstanceImpl(storageServiceFactory, metadataStore, instanceData)); } else { throw new JasDBStorageException("Instance with id: " + instanceId + " was already configured, can't override"); } } @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 testAddInstance() throws JasDBStorageException, IOException { DBInstanceFactoryImpl instanceFactory = new DBInstanceFactoryImpl(metadataStore, storageServiceFactory); Instance instanceMeta = mock(Instance.class); when(instanceMeta.getInstanceId()).thenReturn(INSTANCE_1); when(metadataStore.addInstance(INSTANCE_1)).thenReturn(instanceMeta); instanceFactory.addInstance(INSTANCE_1); verify(metadataStore, times(1)).addInstance(INSTANCE_1); assertThat(getInstanceIds(instanceFactory.listInstances()), hasItems(INSTANCE_1)); }
@Test(expected = JasDBStorageException.class) public void testAddInstanceAlreadyExisting() throws JasDBStorageException { when(metadataStore.containsInstance(INSTANCE_1)).thenReturn(false).thenReturn(true); DBInstanceFactoryImpl instanceFactory = new DBInstanceFactoryImpl(metadataStore, storageServiceFactory); instanceFactory.addInstance(INSTANCE_1); instanceFactory.addInstance(INSTANCE_1); }