use of org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor in project mule by mulesoft.
the class IdempotentMessageValidatorTestCase method testIdCheckWithHash.
@Test
public void testIdCheckWithHash() throws Exception {
String dwHashExpression = "%dw 2.0\n" + "output text/plain\n" + "import dw::Crypto\n" + "---\n" + "Crypto::hashWith(payload,'SHA-256')";
String payload = "payload to be hashed";
final BaseEventContext context = mock(BaseEventContext.class);
when(context.getCorrelationId()).thenReturn("1");
Message message = of(payload);
CoreEvent event = CoreEvent.builder(context).message(message).build();
// Set DW expression to hash value
idempotent.setIdExpression(dwHashExpression);
// Evaluate DW expression outside MessageValidator
ExpressionLanguageAdaptor expressionLanguageAdaptor = new DataWeaveExpressionLanguageAdaptor(muleContext, mock(Registry.class), new WeaveDefaultExpressionLanguageFactoryService());
TypedValue hashedValue = expressionLanguageAdaptor.evaluate(dwHashExpression, event, NULL_BINDING_CONTEXT);
// This one will process the event on the target endpoint
CoreEvent processedEvent = idempotent.process(event);
assertNotNull(processedEvent);
assertEquals(idempotent.getObjectStore().retrieve(IOUtils.toString((ByteArrayBasedCursorStreamProvider) hashedValue.getValue())), "1");
// This will not process, because the message is a duplicate
message = of(payload);
event = CoreEvent.builder(context).message(message).build();
expected.expect(ValidationException.class);
processedEvent = idempotent.process(event);
}
use of org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor in project mule by mulesoft.
the class IdempotentMessageValidatorTestCase method differentIdsShouldBeStored.
@Test
public void differentIdsShouldBeStored() throws Exception {
String dwHashExpression = "%dw 2.0\n" + "output text/plain\n" + "import dw::Crypto\n" + "---\n" + "Crypto::SHA1(payload)";
String payload = "payload to be hashed";
String otherPayload = "this is another payload to be hashed";
final BaseEventContext context = mock(BaseEventContext.class);
when(context.getCorrelationId()).thenReturn("1");
Message message = of(payload);
CoreEvent event = CoreEvent.builder(context).message(message).build();
// Set DW expression to hash value
idempotent.setIdExpression(dwHashExpression);
// Evaluate DW expression outside MessageValidator
ExpressionLanguageAdaptor expressionLanguageAdaptor = new DataWeaveExpressionLanguageAdaptor(muleContext, mock(Registry.class), new WeaveDefaultExpressionLanguageFactoryService());
TypedValue hashedValue = expressionLanguageAdaptor.evaluate(dwHashExpression, event, NULL_BINDING_CONTEXT);
// This one will process the event on the target endpoint
CoreEvent processedEvent = idempotent.process(event);
assertNotNull(processedEvent);
assertEquals(idempotent.getObjectStore().retrieve(IOUtils.toString((ByteArrayBasedCursorStreamProvider) hashedValue.getValue())), "1");
// This will process, because the message is a new one
Message otherMessage = of(otherPayload);
event = CoreEvent.builder(context).message(otherMessage).build();
processedEvent = idempotent.process(event);
assertNotNull(processedEvent);
}
use of org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor in project mule by mulesoft.
the class DefaultExpressionManager method initialise.
@Override
public void initialise() throws InitialisationException {
if (!initialized.getAndSet(true)) {
final DataWeaveExpressionLanguageAdaptor dwExpressionLanguage = DataWeaveExpressionLanguageAdaptor.create(muleContext, registry);
MVELExpressionLanguage mvelExpressionLanguage = null;
if (registry.lookupByName(COMPATIBILITY_PLUGIN_INSTALLED).isPresent()) {
mvelExpressionLanguage = registry.<MVELExpressionLanguage>lookupByName(OBJECT_EXPRESSION_LANGUAGE).get();
}
this.expressionLanguage = new ExpressionLanguageAdaptorHandler(dwExpressionLanguage, mvelExpressionLanguage);
this.melDefault = expressionLanguage.isMelDefault();
Collection<GlobalBindingContextProvider> contextProviders = registry.lookupAllByType(GlobalBindingContextProvider.class);
contextProviders.stream().map(GlobalBindingContextProvider::getBindingContext).forEach(expressionLanguage::addGlobalBindings);
if (melDefault) {
LOGGER.warn("Using MEL as the default expression language.");
}
}
}
Aggregations