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();
<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>
<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);
// Replicates the belongs() method used in Mule's SuccessStatusValidator
def belongs(values, value) {
var intValue = Integer.parseInt(value);
var valueParts = values.toString().split(','); //values.split sometimes fails. Strange...
var valuePart;
var limits;
var code;
var lower;
var upper;
var i;
public class HttpRequestExceptionInterceptor {
/** Flow variable name for transferring the outside event session to inside the enricher */
private static final String OUTSIDE_EVENT_SESSION =
"_HttpRequestExceptionInterceptor_outsideEventSession";
/** Session variable name for the MuleMessage on an exception */
private String lastErrorMessageVarName = "lastHttpError";
public void setLastErrorMessageVarName(final String name) {
this.lastErrorMessageVarName = name;
}