I am using hamcrest for first time, i have developed a framework to validate particular JSON field values need to satisfy some business rules, i am trying to check on of the field in JSON should not be " " or null. Please find below for the code,matcherOperator validate JSON field against a Array of values for example in ISINARRAY validate the JSON filed is present in the following list of array values [1,2,3,4] similarly NOTINARRAY validate the JSON filed should not be present given range of array values. I am trying to check whether given field is empty or null i defined array like this [null,""] and using NOTINARRAY to check JSON key value shouldn't be null or "" for null its working but the JSON value is "" its validating to true and i tried using Matchers.blankOrNullString() too but its not working and i saw two question of similar type but they are part of JUNIT testing which i tried to used but not working below are the question i referred
Hamcrest matcher with slashes is interpreted as a part of validation
Hamcrest check if value is null or empty array
public <T extends Comparable<T>> Matcher<String> mymatcher(final FieldOperator matcherOperator,
final List<String> value)
{
switch (matcherOperator) {
case ISINARRAY: {
return Matchers.in(value);
}
case NOTINARRAY: {
return Matchers.not(Matchers.in(value));
}
}
I am not sure where i am doing wrong please help me in solving the issue.
Related
I have an big query and my problem is setting NULL values using jooq.
For example, I have this piece of sql query:
IF(t.PANEL_ID IS NULL, t.ID, NULL) AS testId
If transform this into jooq implementation something like this will come out:
when(TEST.PANEL_ID.isNull(), TEST.ID).otherwise(null)).as("testId")
but this is ambiguous method call.
I made some research, and find this snippet:
DSL.val((String) null)
but it didn't work, because it cannot resolve method with jooq.Param<String>.
How should I proceed?
Your NULL expression must be of the same type as your TEST.ID column. I would imagine this is not a String column, but some numeric one. Irrespective of the actual data type, you can always create a bind value using the data type of another expression, e.g.
// Bind variable:
DSL.val(null, TEST.ID)
// Inline value / NULL literal
DSL.inline(null, TEST.ID)
If you're doing this a lot, you could also extract your own utility like this:
public static <T> util(Field<?> nullable, Field<T> other) {
return when(nullable.isNull(), other).otherwise(inline(null, other));
}
Notice, jOOQ has a built-in method NVL2 for this purpose:
nvl2(TEST.PANEL_ID, inline(null, TEST.ID), TEST.ID).as("testId")
Is there any way to convert the string below to a list?
This string is retrieved after scanning a QR code.
CashRequest{
orderid='0',
user_id='nvHt2U5RnqUwXB4ZK37Zn1DXPV82',
userName='username',
userEmail='whateveremailthisis#email.blabla',
fullName='full name',
phoneNumber=0,
totalCash='$304.00',
totalRV='$34.00',
foods=[
Order{
userID='nvHt2U5RnqUwXB4ZK37Zn1DXPV82',
ProductID='-LMDiT7klgoXU8bQEM-4',
ProductName='Coke',
Quantity='4',
Price='1',
RedemptionPrice='10',
RedemptionValue='1'},
Order{
userID='nvHt2U5RnqUwXB4ZK37Zn1DXPV82',
ProductID='1000',
ProductName='Kunau Ring Ring Pradu',
Quantity='3',
Price='100',
RedemptionPrice='10',
RedemptionValue='10'
}
]
}
The desired output is to store it in firebase realtime database as below :
Well you have a few options. Since it is newline between values, you could use simple newline reads and compare if it starts with "reserved word that you are looking for" then substring from there, but that can get messy and a lot of bloat code.
The simplest way would be to do the known replace first.
Make a method that replaces all bad json keys with quote surrounded json keys like:
val myJsonCorrected = yourStringAbove.replace("Order", "\"Order"\")
repeat for all known entities until you have made it into valid json. Single ticks are fine for the values, but the keys need quotes as well.
Then simply create an object that matches the json format.
class CashRequestModel{
#SerializableName("orderid")
var orderID: Int? = null
etc.....
#SerializableName("foods")
var myFoods: ArrayList<OrderModel>? = null
}
class OrderMode {
#SerializableName("userID")
var userID: String? = null
#SerializableName("ProductID")
var userID: String? = null
etc..
}
Then simply convert it to JSON
val cashRequest = getGson().fromJson(cleanedUpJson, classTypeForCashRequest);
and your done. Now just use the list. Of course it would be better if you could get valid JSON without having to clean it up first, but it looks like the keys are known and you can easily code string replaces to fix the bad json before casting it to object that matches the structure.
Hope that helps.
I want to compare input String from Database in tMap component in Talend Open Studio. If my String is equal to "{}", I want to put there "nodata" string. Otherwise I leave the original input as it is.
My code in expression/filter in tMap:
(row1.parameter).equals("{}")?"nodata":row1.parameter
Error I'm getting:
Detail Message: Type mismatch: cannot convert from String to boolean
Do you have any suggestions?
Try to assign the result to a String variable, like
String someVariable = row1.parameter.equals("{}") ? "nodata" : row1.parameter;
because your expression returns a String… Are you assigning it to a Boolean? If yes, that will cause / be a type mismatch.
Assuming you're keeping the "Parameter" field, it sounds like your Output field that you are assigning 'Parameter' to is not a 'String' field, and is set as 'Boolean':
So the first point of call would be to check/change the output to a type of 'String':
I have a native facet script that checks if a specific field (mapped to type long) in the document is empty, this is how I do it:
Object fieldValue = doc().get("fieldName");
return fieldValue == ScriptDocValues.EMPTY;
However, for some of the documents this returns false even when the field is empty (I've checked this with the exists filter). This behavior is inconsistent and it usually returns the correct result. Furthermore, the same document in a different host with the same mapping, same version and same code - returns the correct result.
Is there a better way to check if a field is empty?
I'm using ElasticSearch 0.90.5 with facet script 1.1.2 and java 1.7u17.
The correct way to check for an empty field is:
ScriptDocValues value = (ScriptDocValues) doc().get("field2");
return value.isEmpty();
I have a XML file containing metadata like a field's maximum length. I have to use drools to build rules to validate this metadata against a list of facts. I don't want to hardcode the name of each field that may or may not be specified in the XML.
I tried to do this :
when
$metadata: Metadata(maxLength != null);
$obj: Object(eval($metadata.getFieldName()).length > $metadata.maxLength);
then
// TODO
end
It does not work and I get the following error :
java.lang.IllegalStateException: Errors while building rules : Unable
to Analyse Expression $metadata.getFieldName() > $metadata.maxLength:
[Error: Comparison operation requires compatible types. Found class
java.lang.String and class java.lang.Integer] [Near : {...
$metadata.getFieldName() > $metadata.maxLength ....}]
Is it possible to dynamically get a field name and compare its maximum length? Will I have to create a java object to accomplish this?
Thank you
You talk of XML and metadata. Can you distinguish all entities? For example, if it is about orders, can you extract each order, and attributes of each order?
I solved a similar problem with using maps to store each attribute.
public class Order{
private int id;
private Map<String, Integer> num_attribute_map = new HashMap<String, Integer>();
public Map getNumAttributeMap(){
return this.num_attribute_map;
}
If an order has customer_satisfaction = 5,
order_obj.getNumAttributeMap().put("customer_satisfaction" , 5);
And thus you have created Orders with their attributes stored in the numAttributeMap.
For implementing a rule on an Order
$ord : Order(
getNumAttributeMap[$attribute] >= $value
)
where $attribute would be "customer_satisfaction", of course. The [] notation is used to access elements of a list, given index or values of a map, given the key.
Hope you "get" the concepts of maps. Also, do look up Drools language support for list and map access.
I have also implemented maps of lists of strings to perform an "is in" operation, in addition to maps of integers that do comparison operations. Please refer https://stackoverflow.com/a/9241089/604511 too
Finally, I have decided to generate my drools file dynamically from my XML using rule templates.