Skip to content

Instantly share code, notes, and snippets.

View stephan-landers's full-sized avatar

stephan-landers

View GitHub Profile
@stephan-landers
stephan-landers / JSONAssertion.java
Created July 8, 2019 12:58
Shortened JSONAssertion to show the concept
/**
* This custom assertion compares JSON with expected data using JSONAssert.
*/
public class JSONAssertion implements MunitAssertion {
private Object expected;
// [...]
public JSONAssertion(final Object expected) {
super();
public static final String REGEX_FIND_STRINGS = // also properly handles escaped strings
"([^"\\]*(\\.[^"\\]*)*)"|\'([^\'\\]*(\\.[^\'\\]*)*)\'";
public static final String REGEX_FIND_DATES = // matches most common date formats
"((?:\\d{4,4}[-/.]\\d\\d[-/.]\\d\\d|\\d\\d[-/.]\\d\\d[-/.]\\d{4,4})[T ]\\d\\d:\\d\\d:\\d\\d(?:\\.\\d{1,3})?(?:Z|[+-]\\d\\d:?\\d\\d)?)";
public static final String REGEX_FIND_JAVA_DATES = // matches default Date.toString() output
"((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d \\d\\d:\\d\\d:\\d\\d [^ ]+ \\d\\d\\d\\d)";
public static final String FIND_JAVA_IDS = // matches Java IDs (replace with "$100000000" to preserve class information)
"(?:(\\w+\\[?@)[0123456789abcdef]+)";
<munit:assert-true
condition="#[jsonAssert('sample_data/steps/cart-before-add.json').check(flowVars.cart)]"
doc:name="JSONassert cart-before-add.json"/>
<configuration doc:name="Configuration">
<!-- [...] -->
<expression-language>
<!-- [...] -->
<import class="com.ricston.munit.assertion.JSONAssertion"/>
<!-- [...] -->
def jsonAssert(file) {
return new JSONAssertion(getResource(file).asString());
}
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>json-android</artifactId>
</exclusion>
@stephan-landers
stephan-landers / MockInterceptor.java
Last active July 9, 2019 11:17
MockInterceptor.java
// [...]
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(); ?
<spring:beans>
<!-- [...] -->
<spring:bean
id="interceptMockAdvice"
class="com.ricston.munit.mock.MockInterceptor">
<property name="triggerExpression" value="#[true]"/>
</spring:bean>
<aop:config>
<aop:aspect
id="interceptMockAspect"
{
"id": #[flowVars.currentTestId],
"name": "item #[flowVars.currentTestId]",
"price": 6.9,
"_links": {
"self": {
"href": "http://localhost:8082/api/products/#[flowVars.currentTestId]"
},
"product": {
"href": "http://localhost:8082/api/products/#[flowVars.currentTestId]"
/*
* © 2019 Ricston Ltd is protected under international copyright law. The software in this package
* is published under the terms of the Ricston End User Licensing Agreement (EULA), a copy of which
* has been included with this distribution in the LICENSE.md file.
*/
package com.ricston.munit.mock;
import static org.mule.api.transport.PropertyScope.INBOUND;
def isHttpBroken() {
return (message.inboundProperties.'http.status' >= 400); // or better
}
def errorMessage(pmessage, pwhateveryouneed) {
if (isHttpBroken()) { // would need to add expected here as well
flowVars.errorContainer.message = pmessage;
flowVars.errorContainer.httpStatus = message.inboundProperties.'http.status';
flowVars.errorContainer.httpReason = message.inboundProperties.'http.reason';
flowVars.errorContainer.payload = message.payloadAs(java.lang.String);