Issue with jbehave example table - java

I am working on automation in jbehave. I have scenario in which i have multiple steps. Now out of that 2 steps are having same input parameter name. For one i am passing value from examples table and for other i am passing value in statement itself even though it is taking value from example table only.
I know i can change the input parameter name in java/jbehave statement but that is not possible because both the statements are used at multiple times and for one scenario i can not break all existing scenarios.
Any clue on this or anyone who faced similar situation should also comment on this.
code snippet
story file
Scenario :
Given I pass value '1'
And I take value from temp file '<value>'
Examples
value |
2
java file :
#Given("I pass value '$value'")
public void fn(#Named("value") int value)
{
------
}
#Given("I take value from temp '$value'")
public void fn2(#Named("value") int value)
{
-----
}
Now the issue here is that for first statement even though i am passing value '1' from statement it is taking value from example table only which is '2'.

I tested a similar scenario on versions 3.9.5 and 4.0.4 of JBehave, and it worked fine on both versions.
This is my scenario:
Scenario: some scenario
Given I pass value '1'
Given I take value from temp file '<value>'
Examples:
|value|
|2|
|3|
and here is my java code:
public class MySteps {
#Given("I pass value '$value'")
public void fn(int value)
{
System.out.println("GIVEN : I pass value = " + value);
}
#Given("I take value from temp file '$value'")
public void fn2( #Named("value") int value)
{
System.out.println("GIVEN : I take value from temp = " + value);
}
}
I have created a project in Eclipse using maven jbehave-simple-archetype.
You can clone this project into Eclipse from this link: https://github.com/kordirko/jb_test
To change a version just edit it in pom.xml => <jbehave.core.version>3.9.5</jbehave.core.version>
========== EDIT ================
Consider setting useDelimiterNamedParameters(true) option in JBehve configuration
http://jbehave.org/reference/stable/parametrised-scenarios.html
They write in documentation, that starting from version 4.0.4 this option is enabled by default.
We can configure JBehave to interpret the name contained between the delimiters as the parameter name and look it up in the parameters provided by the examples table. The default behaviour of parameter lookup is overridden via the ParameterControls:
new MostUsefulConfiguration()
.useParameterControls(new ParameterControls().useDelimiterNamedParameters(true));
In this mode, the step method would look much simplified:
#Given("a stock of $symbol and a $threshold")
public void aStock(String symbol, double threshold) {
// ...
}
Starting from version 4.0, the use of delimiter named parameters is the default behaviour.
I tested this option and it seemed to work both in 3.9.5 and 4.0.4 versions.
This is a test scenario:
Scenario: some scenario
Given I pass value '1'
Given I pass value '<value>'
........
Examples:
|value|
|2|
|3|
and a java method without #Named annotation:
#Given("I pass value '$value'")
public void fn(int value)
{
System.out.println("GIVEN : I pass value = " + value);
}
The first step Given I pass value '1' is taking 1 as a parameter.
The second step Given I pass value '<value>' matches <value> by name with a column named <value> from the example table, and is taking values from this table.

Related

Set a Default Value for a dropdown in ADF

In a jsff file a component has been designed and the values are being fetched from a Static VO. There are only two values in the static Vo. I need the first option to be set as default. But there is a empty value that is being set. I have written a condition to disable it. And when it is disabled the value must be set to the first one.
I have tried the List UI Hints and tried to enable the Include No selection item: Labeled Item first of list, I tried creating a new vo which only has one value and rendered it in Jsff(But it will make the code more complex for the future development) I have tried creating the switcher. But none of them worked as they should.
Can anyone suggest me a alternative where the code complexity does not increase and by default there is a first value selected. And disable if there is condition for disable tag in jsff is true.
PS: Once the field is disabled the first value must be the default value to be set by default.
There don't seem to be any default way to do this with Oracle ADF without adding code to your view Object. Here's how to automatically select First value in your ADF LOV (detail from here https://cedricleruth.com/autoselect-first-value-in-adf-lov-inside-a-table/) :
Generate the RowImpl Java class of your View Object and Static VO
In this RowImpl.java add the following function to return the first value if no value is already selected
public String defaultNextStatus() {
String value = (String) getAttributeInternal(AttributesEnum.NextStatus.index());
if (value == null) {
XxcrWorkflowUvVORowImpl nextStatut = (XxcrWorkflowUvVORowImpl) getWfkVA().first();
//Wkfva is the VO of the LOV
if (nextStatut != null) {
return nextStatut.getTxtValeur();
}
}
return value;
}
In the Detail panel of the attribute add the following Default Value Expression: adf.object.defaultNextStatus()
In the Detail panel of the attribute set the refresh Expression Value to false to avoid picking the first value again in case of ppr/refresh

Hibernate search: Cannot do a wild card search on a dynamically mapped field text fields

I have implemented a custom bridge which maps all the dynamic fields with related types. Types can be of FieldType.STRING or FieldType.DOUBLE or FieldType.BOOLEAN based on the value.
When I looked on the mapping on my elastic search schema, all the string fields are mapped with type TEXT where I expect it to be a keyword so that I can do a wildcard serach.
Here is my problem I want to filter "AAA-VALUE" for dynamically mapped field 'attribute.dynamic-field-1'
I have an indexed value as "AAA-VALUE" for dynamically mapped field 'attribute.dynamic-field-1'
If I want to do a keyword search, I faced error like 'Field bridge is not found' then I resolved the error by ignoring the bridge using ignoreFieldBridge and the error is gone.
Then again I tried to do a search with value as "AAA-VALUE" and the result is empty (no data found). Here I created the query using a keyword() query.
Then again I tried to do a phrase query then it got worked but the problem is how I can do a wild card search like '-VALUE'.
Regarding code, I followed similar implementation as given here https://github.com/hibernate/hibernate-search/blob/master/legacy/engine/src/test/java/org/hibernate/search/test/bridge/MultiFieldMapBridge.java
Only the type differs in my implementation, where the type can be a string or boolean or double.
My hibernate search version - hibernate-search.version and hibernate-search-elasticsearch = 5.11.3.Final
It got to work after doing below changes.
This how I added the fields before
public class MultiFieldMapClassBridge implements MetadataProvidingFieldBridge {
;
;
;
luceneOptions.addFieldToDocument( fieldPrefix + "." + key, value, document );
}
But the fields should be added as below.
public class MultiFieldMapClassBridge implements MetadataProvidingFieldBridge {
;
;
org.apache.lucene.document.Field field = new org.apache.lucene.document.StringField(fieldPrefix + "." + key, value, luceneOptions.getStore());
document.add(field);
}
I written the wild card query as below
queryBuilder.keyword().wildcard().onField(prefixedPath).ignoreFieldBridge().matching(String.format("*%s*", matchingString.toLowerCase(Locale.getDefault()))).createQuery();
I realised this after reading this doc where the class bridges have to add the field as StringField.
https://docs.jboss.org/hibernate/search/5.5/reference/en-US/html_single/#example-class-bridge

Percentile with spring boot and micrometer

I'm writing an application with Spring boot 2. My method try to generate value until the value will be unique. Each unique generated value is added to the cache. Generally, it should generate value with the first try, but the more application run - more chance that generated value will have duplicates, and it will be required to generate it again.
I want to have the metric, shows the percentile of tryToGenerate values.
Let's say my code looks the following:
public void generateUniqueValue() {
String value;
int tryToGenerate = 0;
do {
tryToGenerate ++
value = generateRandom();
boolean unique = isUniqueValue(value);
} while (!unique);
addGeneratedValueToCache(value);
}
I'm using micrometer, but have no idea what should I start with. Becase in order to calculate percentile I need to store not the single value, but the array of the values
To determine the result of isUniqueValue(..), you're already storing the used values already. You can use that storage to create a proper metric.
Let's assume that you're storing each used value within a list called usedIds. Additionally, let's say you're generating a unique number between 0 and 1000.
In that case, you could write a method like this:
public float getPercentageUsed() {
return (float) usedIds.size() / 1000;
}
Now you can create a Gauge to add this value to the metrics. Let's say that the class that's used to generate unique values is called UniqueValueService, and it's a proper Spring bean. In that case, you could create a Gauge bean like this:
#Bean
public Gauge uniqueValueUsedGauge(MeterRegistry registry, UniqueIdService service) {
return Gauge
.builder("unique-values-used", service::getPercentageUsed)
.baseUnit("%")
.description("Percentage of possible unique values that have been used")
.register(registry);
}
Edit: It seems I've misunderstood your question. If you want to get a histogram of tryToGenerate, to see how many attempts succeed within the first, second or nth attempt, you can use a DistributionSummary. For example:
#Bean
public DistributionSummary summary(MeterRegistry registry) {
return DistributionSummary
.builder("unique-value-attempts")
.sla(1, 5, 10)
.publishPercentileHistogram()
.register(registry);
}
In this example, it will count how many calls succeeded within the 1st, 5th or 10th attempt.
In your service, you can now autowire the DistributionSummary and at the end of the loop, you can use it like this:
do {
tryToGenerate++
value = generateRandom();
boolean unique = isUniqueValue(value);
} while (!unique);
distributionSummary.record(tryToGenerate); // Add this
Now you can use /actuator/metrics/unique-value-attempts.histogram?tag=le:1 to see how many calls succeeded within the first try. The same can be done with the 5th and 10th try.

Two way assertion

I am doing web automation testing, for example lets say I have two very basic scenarios:
Test a) step 1: add record to the database step
2: check if website
displayed record properly
Test b) step 1: edit record on the website step
2: check if record
were properly saved in database
By record, let say it was simple text field with some "value"
So for first scenario, I would use Assert equal:
private void check1()
{
Assert.assertEquals(valueFromDB, valueOnWebsite)
//many more assertions for more values here
}
but, for second scenario, it would be:
private void check2()
{
Assert.assertEquals(valueOnWebsite, valueFromDB)
//many more assertions for more values here
}
So basically they both do the same, but are inverted, in order to throw correct error log if assertion was incorrect, now how to make it in single method, that could work for both cases, BUT would display correct assertion log if values were not equal?
Use the overload which receives a message
String message = String.format("valueFromDB value: %s, valueOnWebsite value: %s", valueFromDB, valueOnWebsite);
Assert.assertEquals(message, valueFromDB, valueOnWebsite);
If you want to override the built in message you need to do your own implementation
if (!valueFromDB.equals(valueOnWebsite)) {
throw new AssertionFailedError(String.format("valueFromDB value: %s, valueOnWebsite value: %s", valueFromDB, valueOnWebsite));
}

Hibernate multiple parameters setString generated Java

I'm using hibernate, and trying to do a LIKE on certain fields.
I'm splitting a string, and then generating the HQL, with
table.entry LIKE :argsearch_0 OR table.entry LIKE :argsearch_0 OR
table.entry LIKE :argsearch_1 OR table.entry LIKE :argsearch_1
(0 and 1 is in fact incremented with a counter).
But i get :
Not all named parameters have been set: [argsearch_0]
First question :
Can I used 2 named parameter and do only 1 setParameter (or setString) :
String nameParam = "argsearch_"+i;
q.setParameter(nameParam, "%"+args[i]+"%");
Second question :
Why my parameters are not working ?
Depends what you mean when you ask "Can I used 2 named parameter and do only 1 setParameter".
In your original query you have 2 named parameters ('argsearch_0' and 'argsearch_1') and each has 2 usages in the query. So you have to call set for both 'argsearch_0' and 'argsearch_1'. But you only call set once for each (actually you can call set multiple times for each parameter if you really want, but only the last once is used.
As for your second question, as someone already pointed out, its because you have a bug in your code. You are not setting the value for the 'argsearch_0' parameter.
You can Try this
**Step 1--:** Add How many parameter you need just add in hashmap
-------------------------------------------------------------------
HashMap param_List=new HashMap();
param_List.put("contactNo",22);
**Step 2--:** Just You pass your Query
-------------------------------------------------------------------
Query query1 = session.createQuery("select * from emailTemplate where c.contactNo =:contactNo");
**Step 3--:** What ever Data type is no matters but get an output.
-------------------------------------------------------------------
for(Object paramKey : param_List.keySet())
{
query1.setParameter(paramkey.toString(), param_List.get(paramKey);
}
**Step 4--:**
-------------------------------------------------------------------
String finalResult=query1.getSingleResult().toString();

Categories