public static <T extends Exception> T testException(T exception, MuleContext muleContext) { ObjectStore<T> os = getObjectStore(muleContext); try { os.store(key, exception); return os.retrieve(key); } catch (Exception e) { throw new RuntimeException(e); } finally { try { os.clear(); } catch (ObjectStoreException e) { throw new RuntimeException(e); } } }
@Test public void testRetrieveWithNullKey() { try { getObjectStore().retrieve(null); fail("retrieve() called with null key must throw ObjectStoreException"); } catch (ObjectStoreException ose) { // this one was expected } }
private Serializable getUpdatedWatermark() { try { if (watermarkObjectStore.contains(UPDATED_WATERMARK_OS_KEY)) { return watermarkObjectStore.retrieve(UPDATED_WATERMARK_OS_KEY); } else { return null; } } catch (ObjectStoreException e) { throw new MuleRuntimeException( createStaticMessage(format("Failed to fetch watermark for Message source at location '%s'. %s", flowName, e.getMessage())), e); } }
private Serializable getCurrentWatermark() { try { if (watermarkObjectStore.contains(WATERMARK_OS_KEY)) { return watermarkObjectStore.retrieve(WATERMARK_OS_KEY); } else { return null; } } catch (ObjectStoreException e) { throw new MuleRuntimeException( createStaticMessage(format("Failed to fetch watermark for Message source at location '%s'. %s", flowName, e.getMessage())), e); } }
@Test public void retrieveCacheWhenExists() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(true); when(objectStore.retrieve(SOME_KEY)).thenReturn(mockCache); MetadataCache actual = cacheManager.getOrCreateCache(SOME_KEY); assertThat(actual, is(mockCache)); verify(objectStore).contains(SOME_KEY); verify(objectStore).retrieve(SOME_KEY); verify(objectStore, never()).store(anyString(), any(MetadataCache.class)); }
@Test public void testRetrieveUnboundKey() throws ObjectStoreException { try { // nothing was stored in the OS yet so using any key must trigger the // ObjectDoesNotExistException String key = createKey(); getObjectStore().retrieve(key); fail("retrieve() with unbound key must throw ObjectDoesNotExistException"); } catch (ObjectDoesNotExistException odne) { // this one was expected } }
@Before public void setUp() throws Exception { this.values = new HashMap<>(); this.values.put("fruit", "banana"); this.values.put("icecream", "chocolate"); this.values.put("drink", "coke"); when(this.objectStore.retrieve(Mockito.anyString())).thenAnswer(invocation -> { Serializable value = values.get(invocation.getArguments()[0]); if (value == null) { throw new ObjectDoesNotExistException(); } return value; }); when(this.objectStore.allKeys()).thenReturn(new ArrayList<>(this.values.keySet())); this.producer = new ObjectStoreProducer<>(this.objectStore); }
@Test public void createCacheWhenMissingKey() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(false); MetadataCache cache = cacheManager.getOrCreateCache(SOME_KEY); assertThat(cache, instanceOf(DefaultMetadataCache.class)); verify(objectStore).contains(SOME_KEY); verify(objectStore, never()).retrieve(SOME_KEY); verify(objectStore).store(SOME_KEY, cache); }
@Test public void disposeExactId() throws ObjectStoreException { when(objectStore.remove(SOME_KEY)).thenReturn(mockCache); cacheManager.dispose(SOME_KEY); verify(objectStore).remove(SOME_KEY); verify(objectStore, never()).contains(SOME_KEY); verify(objectStore, never()).retrieve(SOME_KEY); }
@Test public void storeUsingBigKey() throws Exception { ObjectStore os = objectStoreFactory.createObjectStore("myOs"); StringBuilder bigKey = new StringBuilder(); for (int i = 0; i < 50; i++) { bigKey.append("abcdefghijklmnopqrstuvwxyz"); } os.store(bigKey.toString(), 1); assertThat(os.retrieve(bigKey.toString()), Is.is(1)); }
private void storeObjects(ObjectStore<String> objectStore, int start, int stop) throws ObjectStoreException { for (int i = start; i < stop; i++) { objectStore.store("key" + i, "value" + i); assertEquals("value" + i, objectStore.retrieve("key" + i)); } }
@Test public void updateCache() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(true); when(objectStore.remove(SOME_KEY)).thenReturn(mockCache); DefaultMetadataCache cache = new DefaultMetadataCache(); cacheManager.updateCache(SOME_KEY, cache); verify(objectStore).contains(SOME_KEY); verify(objectStore).remove(SOME_KEY); verify(objectStore).store(SOME_KEY, cache); verify(objectStore, never()).retrieve(SOME_KEY); }
@Test public void expireWithoutMaxEntries() throws Exception { when(settings.getMaxEntries()).thenReturn(empty()); when(settings.getEntryTTL()).thenReturn(of(1L)); StoredObject value = new StoredObject("", 0L, KEY); when(objectStore.retrieve(KEY)).thenReturn(value); when(objectStore.allKeys()).thenReturn(asList(KEY)); when(objectStore.contains(KEY)).thenReturn(true); when(objectStore.remove(KEY)).thenReturn(value); wrapper = new MonitoredObjectStoreWrapper(objectStore, settings); wrapper.expire(); check(5000, 100, () -> { verify(objectStore).remove(KEY); return true; }); } }
@Test public void elementsAreStoredInCaches() throws Exception { // using config setLocation(OUTPUT_AND_METADATA_KEY_CACHE_RESOLVER); metadataService.getMetadataKeys(location); getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); // using alternative-config setLocation(CONTENT_AND_OUTPUT_CACHE_RESOLVER_WITH_ALTERNATIVE_CONFIG); getSuccessComponentDynamicMetadata(); MetadataCache configCache = getMetadataObjectStore().retrieve(OUTPUT_AND_METADATA_KEY_CACHE_ID); assertThat(configCache.get(AGE).get(), is(AGE_VALUE)); assertThat(configCache.get(NAME).get(), is(NAME_VALUE)); assertThat(configCache.get(BRAND).get(), is(BRAND_VALUE)); MetadataCache alternativeConfigCache = getMetadataObjectStore().retrieve(CONTENT_AND_OUTPUT_CACHE_ID); assertThat(alternativeConfigCache.get(BRAND).get(), is(BRAND_VALUE)); }
private void updateWatermark(Serializable value, Comparator comparator) { try { if (watermarkObjectStore.contains(WATERMARK_OS_KEY)) { Serializable currentValue = watermarkObjectStore.retrieve(WATERMARK_OS_KEY); if (compareWatermarks(currentValue, value, comparator) >= 0) { return; } watermarkObjectStore.remove(WATERMARK_OS_KEY); } updateRecentlyProcessedIds(); watermarkObjectStore.store(WATERMARK_OS_KEY, value); } catch (ObjectStoreException e) { throw new MuleRuntimeException( createStaticMessage(format("Failed to update watermark value for message source at location '%s'. %s", flowName, e.getMessage())), e); } }
@Test public void partitionObjectStoreDoesNotCollide() throws Exception { ObjectStore os = objectStoreFactory.createObjectStore("myOs"); ObjectStore os2 = objectStoreFactory.createObjectStore("myOs2"); os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1); os2.store(OBJECT_KEY, OBJECT_KEY_VALUE_2); assertThat(os.contains(OBJECT_KEY), is(true)); assertThat(os.retrieve(OBJECT_KEY), is(OBJECT_KEY_VALUE_1)); assertThat(os2.contains(OBJECT_KEY), is(true)); assertThat(os2.retrieve(OBJECT_KEY), is(OBJECT_KEY_VALUE_2)); assertThat(os.remove(OBJECT_KEY), is(OBJECT_KEY_VALUE_1)); assertThat(os2.remove(OBJECT_KEY), is(OBJECT_KEY_VALUE_2)); }
@Test public void testClearPartition() throws ObjectStoreException, InterruptedException, RegistrationException { getRegistry().registerObject(BASE_IN_MEMORY_OBJECT_STORE_KEY, new SimpleMemoryObjectStore<String>()); ObjectStoreManager manager = getRegistry().lookupObject(OBJECT_STORE_MANAGER); ObjectStore<String> partition1 = manager.createObjectStore("inMemoryPart1", unmanagedTransient()); ObjectStore<String> partition2 = manager.createObjectStore("inMemoryPart2", unmanagedTransient()); partition1.store("key1", "value1"); partition2.store("key2", "value2"); assertEquals("value1", partition1.retrieve("key1")); assertEquals("value2", partition2.retrieve("key2")); partition1.clear(); assertEquals("value2", partition2.retrieve("key2")); }
@Test public void serialization() throws Exception { final String key = "key"; ObjectStore<DateTime> os = muleContext.getObjectStoreManager().createObjectStore("DateTimeTestCase", unmanagedPersistent()); try { os.store(key, now); DateTime recovered = os.retrieve(key); assertEquals(now, recovered); } finally { os.clear(); } }
private void testObjectStoreExpiry(ObjectStoreManager manager, String storeName, ObjectStoreSettings settings) throws ObjectStoreException, InterruptedException { ObjectStore<String> objectStore = manager.createObjectStore(storeName, settings); try { objectStore.store("key1", "value1"); assertEquals("value1", objectStore.retrieve("key1")); new PollingProber(2000, 50).check(new JUnitLambdaProbe(() -> { try { assertFalse("Object with key1 still exists.", objectStore.contains("key1")); } catch (Exception e) { fail(e.getMessage()); } return true; })); } finally { manager.disposeStore(storeName); } }
@Test public void testIdCheckWithMEL() throws Exception { String melExpression = "#[payload]"; final BaseEventContext context = mock(BaseEventContext.class); when(context.getCorrelationId()).thenReturn("1"); Message okMessage = of("OK"); CoreEvent event = CoreEvent.builder(context).message(okMessage).build(); // Set MEL expression to hash value idempotent.setIdExpression(melExpression); // This one will process the event on the target endpoint CoreEvent processedEvent = idempotent.process(event); assertNotNull(processedEvent); assertEquals(idempotent.getObjectStore().retrieve("OK"), "1"); // This will not process, because the message is a duplicate okMessage = of("OK"); event = CoreEvent.builder(context).message(okMessage).build(); expected.expect(ValidationException.class); processedEvent = idempotent.process(event); }