Last active
July 9, 2019 11:17
-
-
Save stephan-landers/4c1a2580a1a0437b111b5eec6206213b to your computer and use it in GitHub Desktop.
MockInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [...] | |
public class MockInterceptor implements MuleContextAware { | |
//[...] | |
public MessageProcessorBehavior aroundGetBetterMatchingBehavior(final ProceedingJoinPoint joinPoint) throws Throwable { | |
if (joinPoint.getTarget() instanceof MessageProcessorManager == false | |
|| joinPoint.getArgs()[0] instanceof MessageProcessorCall == false) { | |
throw new IllegalArgumentException( | |
"MockInterceptor needs signature MessageProcessorManager.getBetterMatchingBehavior(*)"); | |
// or better log and return joinPoint.proceed(); ? | |
} | |
final MessageProcessorBehavior result = (MessageProcessorBehavior) joinPoint.proceed(); | |
if (result == null) { | |
// no mocked content | |
return result; | |
} | |
return new MessageProcessorBehaviorWrapper(result, muleContext); | |
} | |
/** | |
* The MessageProcessorBehavior does not seem to be managed by Spring, so we start one step | |
* higher. The only thing changed is that we'll return our MessageTransformerWrapper. | |
* If anyone knows a better way to inject the Transformer below, let me know. | |
*/ | |
public static class MessageProcessorBehaviorWrapper extends MessageProcessorBehavior { | |
public MessageProcessorBehaviorWrapper(final MessageProcessorBehavior original, final MuleContext muleContext) { | |
super(original.getMessageProcessorCall(), new MessageTransformerWrapper(original.getMuleMessageTransformer(), muleContext)); | |
} | |
} | |
/** | |
* Since the initialization of mocked payload is outside of any test flow context, we'll add the | |
* ability to include MEL expressions in test data payload. These will be evaluated during the | |
* test flow, allowing us to return data fitting to repeated calls. | |
*/ | |
public static class MessageTransformerWrapper implements MuleMessageTransformer { | |
/** Start of a MEL expression. */ | |
private static final String MEL_START = "#["; | |
/** Keep the original transformer to delegate to. */ | |
private MuleMessageTransformer originalTransformer; | |
/** MuleContext to access MEL evaluation. */ | |
private MuleContext muleContext; | |
/** | |
* Constructor | |
* @param originalTransformer this will still do its transformation before ours. | |
*/ | |
public MessageTransformerWrapper(final MuleMessageTransformer originalTransformer, final MuleContext muleContext) { | |
super(); | |
this.originalTransformer = originalTransformer; | |
this.muleContext = muleContext; | |
} | |
@Override | |
public MuleMessage transform(final MuleMessage original) { | |
/** MuleContext to access MEL evaluation. */ | |
final MuleMessage result = this.originalTransformer.transform(original); | |
// Check if the payload contains MEL expressions | |
try { | |
final String payload = result.getPayloadAsString(); | |
if (payload.contains(MEL_START)) { // see full check below | |
final Object newPayload = muleContext.getExpressionManager().parse(payload, result, false); | |
// take care to copy the data type or you won't be able to use anything but Strings | |
result.setPayload(newPayload, result.getDataType()); | |
} | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment