Expression in responseValidator in http:request in mule - mule4

I use mule-http-connector version 1.6.0 in mule 4.4 community edition (MuleSoft 4 ce) and I get a response (aka GraphQL):
{
"success": true,
"data": ...
}
I want validate the response by cheking success field. I can made validation by use two…four components (http:response, choice, raise…), but I found in Anypoint Studio 7.11 in component properties «Reponse» → «Response validator» → «Expression or Bean reference». I don't want use «Bean reference» and write java code, but I want use «Expression». I wrote that:
%dw 2.0
output application/json
---
payload.success
But I got error:
"org.mule.weave.v2.exception.UnexpectedFunctionCallTypesException: You called the function 'Value Selector' with these arguments:
1: Binary ("" as Binary {base: "64"})
2: Name ("success")
If I write something:
%dw 2.0
output application/json
---
true
I got that error:
org.mule.weave.v2.exception.WriterExecutionException: Unable to convert 'true' with class 'java.lang.Boolean' to class 'ResponseValidator'
4| true
^^^^
Trace:
at anonymous::main (line: 4, column: 1), while writing Java at
4| true
^^^^.
4| true
^^^^
Trace:
at anonymous::main (line: 4, column: 1)
" evaluating expression: "%dw 2.0
output application/json
---
true".
How to write a dw expression in a response validation?

That's not how response validation should be used. By default -when using a string- it validates HTTP status codes against ranges of status codes in the string. If you want to implement your custom validation you have to extend Java class org.mule.extension.http.api.request.validator.RangeStatusCodeValidator.
Example:
public class MyValidator extends RangeStatusCodeValidator {
public MyValidator() {}
public MyValidator(String values) {
setValues(values);
}
#Override
public void validate(Result<InputStream, HttpResponseAttributes> result, HttpRequest request) {
int status = result.getAttributes().get().getStatusCode();
String method = request.getMethod();
if ((!belongs(status)) && (!method.equals("GET"))) {
throwValidationException(result, request, status);
}
}
}
Then instantiate it in you flow and reference it in the response validator:
<java:new doc:name="New" class="validator.MyValidator" constructor='MyValidator(String)' target="out">
<java:args ><![CDATA[#[{arg0:"500"}]]]></java:args>
</java:new>
<http:request method="POST" doc:name="Request" config-ref="HTTP_Request_configuration" responseValidator='#[vars.out]' path="/endpoint">
</http:request>
Alternatively you can instance the class from DataWeave:
<http:request method="POST" doc:name="Request" config-ref="HTTP_Request_configuration" responseValidator='#[%dw 2.0 import java!validator::MyValidator output application/java --- MyValidator::new("500")]' path="/endpoint">
</http:request>
Source: https://help.mulesoft.com/s/article/How-to-define-a-custom-HTTP-response-validator-in-Mule-Runtime-4-x

Related

How to set up hydra config to accept a custom enum?

How do I set up a my hydra config to accept a custom enum? Specifically I followed the Structured Config Schema tutorial.
I have a dataclass config:
#dataclass_validate
#dataclass
class CustomConfig:
custom_enum: CustomEnum
With the custom enum:
class CustomEnum(str, Enum):
ENUM1 = "enum1"
ENUM2 = "enum2"
Error from running python my_app.py
Error merging 'data/config' with schema
Invalid value 'enum1', expected one of [ENUM1, ENUM2]
full_key: custom_enum
object_type=CustomConfig
Where my_app.py is just:
cs = ConfigStore.instance()
cs.store(name="base_config", node=Config)
cs.store(group="data", name="config", node=CustomConfig)
#hydra.main(config_path=".", config_name="config")
def setup_config(cfg: Config) -> None:
print(OmegaConf.to_yaml(cfg))
And the config in data/config.yaml is just
custom_enum: enum1
Note the error message: Invalid value 'enum1', expected one of [ENUM1, ENUM2].
This is to say, in your data/config.yaml file, you should be using ENUM1 instead of enum1.

Robot Framework: Rest call gives "unicode object has no attribute copy" error

Below error occurring on my code
${resp}= Post Request sw3 https://sw3- test.test1.com/api/v1/test1/patients
\ ... Content-Type:application/json
\ ... Authorization:authkey02 Accept=application/json
Error as follows
KEYWORD ${resp} = RequestsLibrary . Post Request sw3, https://sw3-test.test1.com/api/v1/test1/patients, Content-Type:application/json, Authorization:authkey02, Accept=application/json
Documentation:
Send a POST request on the session object found using the
Start / End / Elapsed: 20170315 12:08:21.283 / 20170315 12:08:21.285 / 00:00:00.002
12:08:21.283 TRACE Arguments: [ 'sw3' | 'https://sw3-test.test1.com/api/v1/test1/patients' | 'Content-Type:application/json' | 'Authorization:authkey02' | 'Accept=application/json' ]
12:08:21.284 FAIL AttributeError: 'unicode' object has no attribute 'copy'
This works ok if i do not use the third line of code - what gives?
You can give it a try like passing those key:value pairs as a dictionary Header variable.
&{Header}= Content-Type:application/json Authorization:authkey02
Accept=application/json
${resp} = RequestsLibrary.Post Request sw3 https://sw3-test.test1.com/api/v1/test1/patients headers=${Header}

SpringMVC/MockMVC/ jsonpath compare list of Integers

I have searched online but can't find why or how to fix it.
Problem:
My JUnit test pulls a list of user id's from a REST-API.
However, I can't seem to assert the contents of the response...
API Response:
[
{"userId":1458548702},
{"userId":1458548703},
{"userId":1458548704},
{"userId":1458548706},
{"userId":1458548707}
]
Assertion Code:
List<Integer> expectedIds = ....
ResultActions result = ...
//JSONArray and expectedIds are the same length
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$.*",
Matchers.hasSize(expectedIds.size())
)
);
//Below Fails:
result.andExpect(
MockMvcResultMatchers.jsonPath(
"$[*].userId",
Matchers.containsInAnyOrder(expectedIds)
)
);
Error Message:
java.lang.AssertionError: JSON path "$[*].userId"
Expected: iterable over [<[1458548702, 1458548703, 1458548704, 1458548706, 1458548707]>] in any order
but: Not matched: <1458548702>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
...
Libraries I'm using:
com.jayway.jsonpath json-path 2.0.0
com.jayway.jsonpath json-path-assert 2.0.0
org.mockito mockito-core 1.10.19
org.hamcrest hamcrest-all 1.3

Django : NOT NULL constraint failed

I am trying to run a app which has a basic template which accepts the fields and saves it. My Model looks like this:
from django.db import models
# Create your models here.
class Book(models.Model):
"""docstring for MyApp"""
title = models.CharField(max_length=200)
abstract = models.TextField()
pub_date = models.DateTimeField('date published')
publisher = models.CharField(max_length=200)
def __unicode__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=200)
sci='Sci'
arts= 'Arts'
engg= 'Engg'
mgmt='mgmt'
none=' '
Choice_In_Intrest = (
(sci,'Science'),
(arts,'Arts'),
(engg,'Engineering'),
(mgmt,'Mangaement'),
)
intrest = models.CharField(max_length=4 ,choices= Choice_In_Intrest , default=none)
book = models.ForeignKey(Book, null=True)
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
#url(r'^admin/', include(admin.site.urls)),
url(r'^create/$', 'myBook.views.insertBook'),
url(r'^all/$', 'myBook.views.books'),
url(r'^get/(?P<book_id>\d+)/$', 'myBook.views.book'),
url(r'^addAuthor/(?P<book_id>\d+)/$', 'myBook.views.addAuthor'),
]
forms.py:
from django import forms
from models import Book, Author
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields ='__all__'
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
**insert_book.html:**
{% block content%}
<h2>Add Book</h2>
<form action='/create/' method="POST">{%csrf_token%}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" name="submit" value="Add Book">
</form>
{% endblock %}
When I runs the server the Html page displayed, but when I click the Add Book button, it shows the following error:
IntegrityError at /create/
NOT NULL constraint failed: myBook_book.author_id
Request Method: POST
Request URL: http://127.0.0.1:8080/create/
Django Version: 1.8.5
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: myBook_book.author_id
Exception Location: /home/rizwan/django-rizwan/local/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg/django/db/backends/sqlite3/base.py in execute, line 318
Python Executable: /home/rizwan/django-rizwan/bin/python
Python Version: 2.7.6
Python Path:
['/home/rizwan/projects/bookInfo',
'/home/rizwan/django-rizwan/local/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg',
'/home/rizwan/django-rizwan/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg',
'/home/rizwan/django-rizwan/lib/python2.7',
'/home/rizwan/django-rizwan/lib/python2.7/plat-x86_64-linux-gnu',
'/home/rizwan/django-rizwan/lib/python2.7/lib-tk',
'/home/rizwan/django-rizwan/lib/python2.7/lib-old',
'/home/rizwan/django-rizwan/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/rizwan/django-rizwan/local/lib/python2.7/site-packages',
'/home/rizwan/django-rizwan/lib/python2.7/site-packages']
Server time: Wed, 4 Nov 2015 11:12:00 +0000
I haven't define author_id any where, What caused it?
I am using Sqlite 3.
id fields are automatically generated by Models, and they are predictably of type serial NOT NULL PRIMARY KEY,. While using the Book model, they are required but have not been set yet. Database didn't get any author_id, so it attempted to set null on this field, but id field has the not null constraint. You can fix by either setting null=True on field definition in the model, or you can have a default, or add an author field to your form.

servlet filter in jython

Based on this java example, I made the following servlet filter in jython (exact code):
from javax.servlet import Filter
from javax.servlet.http import HttpServletRequest
class HttpServletRequestWrapper(HttpServletRequest):
def init(self, request):
self.originalURL = self.getRequestURL()
pathi = self.originalURL.find('/', 10) # find start of path
qsi = self.originalURL.find('?', pathi) # find start of qs if any
qs = self.originalURL[qsi:] if qsi > -1 else ''
self.newURL = self.originalURL[:pathi] + '/ccc/jope.py' + qs
def getRequestURL(self):
return self.newURL
class Route2Jope(Filter):
def init(self, config):
pass
def doFilter(self, request, response, chain):
wrapped = HttpServletRequestWrapper(request)
chain.doFilter(wrapped, response)
However, I am getting the error message:
Traceback (most recent call last):
File "c:\CCC\webapps\ccc\WEB-INF\pyfilter\Route2Jope.py", line 24, in doFilter
wrapped = HttpServletRequestWrapper(request)
TypeError: org.python.proxies.__main__$HttpServletRequestWrapper$2(): expected 0 args; got 1
org.python.core.Py.TypeError(Py.java:259)
org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:209)
org.python.core.PyReflectedFunction.throwArgCountError(PyReflectedFunction.java:262)
org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:319)
org.python.core.PyReflectedConstructor.__call__(PyReflectedConstructor.java:177)
org.python.core.PyObject.__call__(PyObject.java:419)
org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237)
org.python.core.PyMethod.__call__(PyMethod.java:228)
org.python.core.PyMethod.__call__(PyMethod.java:223)
org.python.core.Deriveds.dispatch__init__(Deriveds.java:19)
org.python.core.PyObjectDerived.dispatch__init__(PyObjectDerived.java:1112)
org.python.core.PyType.type___call__(PyType.java:1713)
org.python.core.PyType.__call__(PyType.java:1696)
org.python.core.PyObject.__call__(PyObject.java:461)
org.python.core.PyObject.__call__(PyObject.java:465)
org.python.pycode._pyx1.doFilter$6(c:\CCC\webapps\ccc\WEB-INF\pyfilter\Route2Jope.py:25)
org.python.pycode._pyx1.call_function(c:\CCC\webapps\ccc\WEB-INF\pyfilter\Route2Jope.py)
org.python.core.PyTableCode.call(PyTableCode.java:167)
org.python.core.PyBaseCode.call(PyBaseCode.java:307)
org.python.core.PyBaseCode.call(PyBaseCode.java:198)
org.python.core.PyFunction.__call__(PyFunction.java:482)
org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237)
org.python.core.PyMethod.__call__(PyMethod.java:228)
org.python.core.PyMethod.__call__(PyMethod.java:218)
org.python.core.PyMethod.__call__(PyMethod.java:213)
org.python.core.PyObject._jcallexc(PyObject.java:3626)
org.python.proxies.__main__$Route2Jope$3.doFilter(Unknown Source)
org.python.util.PyFilter.doFilter(PyFilter.java:80)
I think it's telling me I should not pass the parameter 'request', but it does not make sense to me. Maybe I am overlooking some mapping issue between python ad java classes? Suggestions?
The name of your constructor must be __init__, not init. :)

Resources