LocalStorageServiceFactoryImpl implements StorageServiceFactory { @Override public StorageService getOrCreateStorageService(String instanceId, String bagName) throws JasDBStorageException { String storageServiceKey = instanceId + "_" + bagName; if(storageServices.containsKey(storageServiceKey)) { return storageServices.get(storageServiceKey); } else { return createStorageService(instanceId, bagName, storageServiceKey); } }  @PostConstruct void initializeServices(); @Override StorageService getStorageService(String instanceId, String bagName); @Override StorageService getOrCreateStorageService(String instanceId, String bagName); @Override void removeStorageService(String instanceId, String bagName); @Override void removeAllStorageService(String instanceId); @Override void shutdownServiceFactory();  }
@Test public void testGetOrCreateStorageService() throws Exception { when(metadataStore.getInstance(TEST_INSTANCE)).thenReturn(new InstanceMeta(TEST_INSTANCE, jasdbHomeFolder)); when(metadataStore.containsBag(TEST_INSTANCE, TEST_BAG_NAME)).thenReturn(true); when(metadataStore.containsInstance(TEST_INSTANCE)).thenReturn(true); StorageService storageService = storageServiceFactory.getOrCreateStorageService(TEST_INSTANCE, TEST_BAG_NAME); assertNotNull(storageService); assertThat(storageService, is(storageService)); verify(storageService, times(1)).openService(eq(configuration)); StorageService newStorageservice = storageServiceFactory.getOrCreateStorageService(TEST_INSTANCE, NONEXISTING_BAG); assertNotNull(newStorageservice); assertThat(newStorageservice, is(storageService2)); verify(newStorageservice, times(1)).openService(eq(configuration)); ArgumentCaptor<BagMeta> bagMetaArgumentCaptor = ArgumentCaptor.forClass(BagMeta.class); verify(metadataStore, times(1)).addBag(bagMetaArgumentCaptor.capture()); assertThat(bagMetaArgumentCaptor.getValue().getName(), is(NONEXISTING_BAG)); }
@Test(expected = JasDBStorageException.class) public void testNonExistingInstanceCreateStorageService() throws Exception { storageServiceFactory.getOrCreateStorageService(NONEXISTING_INSTANCE, NONEXISTING_BAG); }