@PostConstruct protected void init() { try { provisioningRootPath = fileSystem.getPath(PROVISIONING_BRANCH, PROVISIONING_PATH); logger.debug("provisioningRootPath: " + provisioningRootPath.toUri()); } catch (Exception e) { //uncommon error logger.error("An error was produced during VFS registries directory initialization.", e); } entryMarshaller = (VFSRegistryEntryMarshaller) marshallerRegistry.get(VFSRegistryEntry.class); }
@Before @Override public void setUp() { super.setUp(); List<Marshaller<?>> marshallers = new ArrayList<>(); when(marshallerInstance.iterator()).thenReturn(marshallers.iterator()); marshallerRegistry = new VFSMarshallerRegistry(marshallerInstance); ((VFSMarshallerRegistry) marshallerRegistry).init(); }
@PostConstruct public void init() { marshallerInstance.iterator().forEachRemaining(marshaller -> { logger.debug("Registering marshaller into VFSMarshallerRegistry, marshaller: " + marshaller); register(marshaller.getType(), marshaller); }); } }
@Test public void testInit() { //mock a set of marshallers List<Marshaller<?>> marshallers = new ArrayList<>(); Arrays.stream(marshallerClasses).forEach(clazz -> { Marshaller marshaller = mock(Marshaller.class); when(marshaller.getType()).thenReturn(clazz); marshallers.add(marshaller); }); when(marshallerInstance.iterator()).thenReturn(marshallers.iterator()); ((VFSMarshallerRegistry) marshallerRegistry).init(); for (int i = 0; i < marshallerClasses.length; i++) { Class clazz = marshallerClasses[i]; Marshaller marshaller = marshallerRegistry.get(clazz); assertNotNull(marshaller); assertEquals(clazz, marshaller.getType()); } } }
@Test public void testInit() { //mock a set of marshallers List<Marshaller<?>> marshallers = new ArrayList<>(); Arrays.stream(marshallerClasses).forEach(clazz -> { Marshaller marshaller = mock(Marshaller.class); when(marshaller.getType()).thenReturn(clazz); marshallers.add(marshaller); }); when(marshallerInstance.iterator()).thenReturn(marshallers.iterator()); ((VFSMarshallerRegistry) marshallerRegistry).init(); for (int i = 0; i < marshallerClasses.length; i++) { Class clazz = marshallerClasses[i]; Marshaller marshaller = marshallerRegistry.get(clazz); assertNotNull(marshaller); assertEquals(clazz, marshaller.getType()); } } }
/** * Stores an Object marshalled value as a VFSRegistryEntry in the target path. * @param path a path for storing the generated VFSRegistryEntry. * @param value an object value to marshall and store. * @throws Exception exceptions might be thrown in cases of filesystem or marshalling errors. */ public void storeEntry(final Path path, final Object value) throws Exception { final Marshaller marshaller = marshallerRegistry.get(value.getClass()); if (marshaller == null) { throw new Exception("No marshaller was found for class: " + value.getClass()); } @SuppressWarnings("unchecked") final String marshalledValue = marshaller.marshal(value); final VFSRegistryEntry entry = new VFSRegistryEntry(value.getClass().getName(), marshalledValue); final String content = entryMarshaller.marshal(entry); writeBatch(path, content); }
@Before @Override public void setUp() { super.setUp(); List<Marshaller<?>> marshallers = new ArrayList<>(); when(marshallerInstance.iterator()).thenReturn(marshallers.iterator()); marshallerRegistry = new VFSMarshallerRegistry(marshallerInstance); ((VFSMarshallerRegistry) marshallerRegistry).init(); }
@Test public void testStoreEntryWhenMarshallerNotExists() throws Exception { Object value = mock(Object.class); when(marshallerRegistry.get(value.getClass())).thenReturn(null); expectedException.expectMessage("No marshaller was found for class: " + value.getClass()); registryHelper.storeEntry(mock(Path.class), value); }
/** * Reads an Object previously marshalled and stored as a VFSRegistryEntry in a given path. * @param path the path where the VFSRegistryEntry is stored. * @return the unmarshalled object backed by the VFSRegistryEntry. * @throws Exception exceptions might be thrown in cases of filesystem or marshalling errors. */ public Object readEntry(final Path path) throws Exception { final String entryContent = ioService.readAllString(path); final VFSRegistryEntry entry = entryMarshaller.unmarshal(entryContent); final Marshaller marshaller = marshallerRegistry.get(Class.forName(entry.getContentType())); if (marshaller == null) { throw new Exception("No marshaller was found for class: " + entry.getContentType()); } return marshaller.unmarshal(entry.getContent()); }
@Test public void testStoreEntryWhenMarshallerNotExists() throws Exception { Object value = mock(Object.class); when(marshallerRegistry.get(value.getClass())).thenReturn(null); expectedException.expectMessage("No marshaller was found for class: " + value.getClass()); registryHelper.storeEntry(mock(Path.class), value); }
@Test public void testStoreEntryWhenMarshallerExists() throws Exception { when(marshallerRegistry.get(value.getClass())).thenReturn(marshaller); when(marshaller.marshal(value)).thenReturn(MARSHALLED_VALUE); VFSRegistryEntry expectedEntry = new VFSRegistryEntry(value.getClass().getName(), MARSHALLED_VALUE); when(entryMarshaller.marshal(expectedEntry)).thenReturn(MARSHALLED_ENTRY); registryHelper.storeEntry(path, value); verify(marshallerRegistry, times(1)).get(value.getClass()); verify(registryHelper, times(1)).writeBatch(path, MARSHALLED_ENTRY); }
@Test public void testStoreEntryWhenMarshallerExists() throws Exception { when(marshallerRegistry.get(value.getClass())).thenReturn(marshaller); when(marshaller.marshal(value)).thenReturn(MARSHALLED_VALUE); VFSRegistryEntry expectedEntry = new VFSRegistryEntry(value.getClass().getName(), MARSHALLED_VALUE); when(entryMarshaller.marshal(expectedEntry)).thenReturn(MARSHALLED_ENTRY); registryHelper.storeEntry(path, value); verify(marshallerRegistry, times(1)).get(value.getClass()); verify(registryHelper, times(1)).writeBatch(path, MARSHALLED_ENTRY); }
@SuppressWarnings("unchecked") @Before public void setUp() { when(fileSystem.getPath(PROVISIONING_BRANCH, PROVISIONING_PATH)).thenReturn(provisioningPath); when(marshallerRegistry.get(VFSRegistryEntry.class)).thenReturn(entryMarshaller); registryHelper = spy(new VFSRegistryHelper(marshallerRegistry, ioService, fileSystem)); registryHelper.init(); verify(fileSystem, times(1)).getPath(PROVISIONING_BRANCH, PROVISIONING_PATH); }
@Test public void testReadEntry() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); Object unmarshalledValue = mock(Object.class); when(marshaller.unmarshal(MARSHALLED_VALUE)).thenReturn(unmarshalledValue); Object result = registryHelper.readEntry(path); assertEquals(unmarshalledValue, result); }
@Test public void testReadEntryWhenMarshallerNotExists() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(marshallerRegistry.get(Object.class.getClass())).thenReturn(null); expectedException.expectMessage("No marshaller was found for class: " + entry.getContentType()); registryHelper.readEntry(path); }
@Test public void testReadEntryWhenMarshallerNotExists() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(marshallerRegistry.get(Object.class.getClass())).thenReturn(null); expectedException.expectMessage("No marshaller was found for class: " + entry.getContentType()); registryHelper.readEntry(path); }
@Test public void testReadEntry() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); Object unmarshalledValue = mock(Object.class); when(marshaller.unmarshal(MARSHALLED_VALUE)).thenReturn(unmarshalledValue); Object result = registryHelper.readEntry(path); assertEquals(unmarshalledValue, result); }
@SuppressWarnings("unchecked") @Before public void setUp() { when(fileSystem.getPath(PROVISIONING_BRANCH, PROVISIONING_PATH)).thenReturn(provisioningPath); when(marshallerRegistry.get(VFSRegistryEntry.class)).thenReturn(entryMarshaller); registryHelper = spy(new VFSRegistryHelper(marshallerRegistry, ioService, fileSystem)); registryHelper.init(); verify(fileSystem, times(1)).getPath(PROVISIONING_BRANCH, PROVISIONING_PATH); }
private void prepareReadEntries() throws Exception { entryPaths = mockList(Path.class, ENTRY_COUNT); Iterator<Path> pathIterator = entryPaths.iterator(); expectedObjects = mockList(Object.class, ENTRY_COUNT); entries = mockList(VFSRegistryEntry.class, ENTRY_COUNT); DirectoryStream<Path> directoryStream = mock(DirectoryStream.class); when(directoryStream.iterator()).thenReturn(pathIterator); when(ioService.newDirectoryStream(rootPath, filter)).thenReturn(directoryStream); for (int i = 0; i < ENTRY_COUNT; i++) { VFSRegistryEntry entry = entries.get(i); when(ioService.readAllString(entryPaths.get(i))).thenReturn(MARSHALLED_ENTRY + i); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY + i)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE + i); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); when(marshaller.unmarshal(MARSHALLED_VALUE + i)).thenReturn(expectedObjects.get(i)); } } }
private void prepareReadEntries() throws Exception { entryPaths = mockList(Path.class, ENTRY_COUNT); Iterator<Path> pathIterator = entryPaths.iterator(); expectedObjects = mockList(Object.class, ENTRY_COUNT); entries = mockList(VFSRegistryEntry.class, ENTRY_COUNT); DirectoryStream<Path> directoryStream = mock(DirectoryStream.class); when(directoryStream.iterator()).thenReturn(pathIterator); when(ioService.newDirectoryStream(rootPath, filter)).thenReturn(directoryStream); for (int i = 0; i < ENTRY_COUNT; i++) { VFSRegistryEntry entry = entries.get(i); when(ioService.readAllString(entryPaths.get(i))).thenReturn(MARSHALLED_ENTRY + i); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY + i)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE + i); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); when(marshaller.unmarshal(MARSHALLED_VALUE + i)).thenReturn(expectedObjects.get(i)); } } }