How to pass datatable as part of scenario outline example - java

I am using cucumber 4 with java
I have a scenario :-
Scenario: user views the address
Given I login with user1
Then I see all my house addresses
|london|
Given I login with user2
Then I see all my house addresses
|london|
|spain|
|brazil|
I want to convert it to scenario outline . How can I pass the addresses datatable as part of the Examples of scenario outline.
Scenario Outline: user views the address
Given I login with <user>
Then I see all my house addresses
Examples:
|user |
|user1|
|user2|
The implementation of I see all my house addresses is (DataTable dataTable) and I want to keep it as datatable. Don't want to concatenate all addresses to a single string then I would miss the datatable.
Then("^I see all my house addresses$", (DataTable dataTable) -> {
List<String> addresses = dataTable.asList(String.class);
});

Cucumber allows you to pass data table arguments in scenario outlines. The syntax is exactly the same as a normal scenario, except the <foo> tokens used as placeholders for the values in each examples table row.
Scenario Outline: user views the address
Given I login with <user>
Then I see all my house addresses
| <address 1> | # <-- "address 1" column in examples table
| <address 2> | # <-- "address 2" column in examples table
| <address 3> | # <-- "address 3" column in examples table
Examples:
| user | address 1 | address 2 | address 3 |
| user1 | ... | ... | ... |
| user2 | ... | ... | ... |
There is a tradeoff though. You cannot specify a different number of addresses for each user. You must specify the same number of addresses for each user. If user1 shows London, Spain and Brazil, but user2 only shows London and Spain, then a data table will not work for this use case.

Related

How to get element from MySQL table using a singular attribute in Java?

So let's say I have the following table for a student:
+----+------------+-----------+---------------------+----------+------------+
| first_name | last_name | ID | Birthday | Grade | Periods |
+----+------------+-----------+---------------------+----------+------------+
| John | Doe | 123456 | 1/1/2004 | 11 | 7 |
+----+------------+-----------+---------------------+----------+------------+
How could I get the ResultSet for just that one student by just using their ID for example?
Could the query be SELECT ID(123456) FROM Students" or something along those lines? I found some answers online as to how I could loop through all the Students for example and print out that data or do something else with it, but not how to get the data for a singular element in the table based on one attribute.
In short, I just want to be able to get all the data on a student from just their ID number.

Write Spring Specification to filter unrelated tables

I am working on a project Spring and Java, generated using JHipster. I want to filter on table that doesn't have a direct relationship with another.
My purpose is almost asked in a previous similar question
Write Spring Specification with multiple inner join & other conditions
But in my case , I ve two unrelated entities :
Consultant (id : Long , FullName : string , profileRank : Enum of string )
Rank (id : Long , level : Enum of string , rate : Double )
Consultant | Rank
|
id | FullName | profileRank | id | level | rate
1 | aaaaa | 'ONE' | 1 | 'ONE' | 1
2 | bbbbbb | 'THREE' | 2 | 'TWO' | 2
3 | cccccc | 'FOUR' | 3 | 'THREE' | 3
4 | dddddd | 'THREE' | 4 | 'FOUR' | 4
I want to filter consultant list by rate using level
Example : get consultants with rate greater than 3
Expected result
id | FullName | profileRank
3 | cccccc | 'FOUR'
I ve searched in documentation and many articles without get it to work please how to achieve that .
You don't need to write a specification for your case.
Fetch all the ranks with levels and rates
Filter these values and keep only the ones greater than 3 (step 1 and 2 can be combined). The result will be a List<Rank> that contains only FOUR rank
list.stream(rank => rank.level).collect(toList())
The result of step 3 will be passed to a repository method like List<Consultant> findByProfileRankIn(List<String> levelNames)
Another alternative would be joins something like this Joining tables without relation using JPA criteria
If you still want a spec that's also possible. Spring Data Join with Specifications

Compare Tables In SQL server from android studio

I am developing an app in which the user first login,and if he doesn't have an account he will register after finishing that step he will be directed to a page,there he will add users to his profile like.. his own family members anything like that and when they add a user its going to be saved in a table on an external server(SQL) and then get that in the user list view but how can the user only see his own users and nobody else can see his users except him and so for all other users,like their own data?!
i didn't figure out how to do this yet,any one help me with this task?
any help would be appreciated
It feels like your question is about how to design your database rather than the technical details of how to call the database from you code and display results on your pages?
You need to split your data up into multiple tables. Each table should have a unique id (the id for each row should be different). Then you link rows in tables together by referencing the ids.
One way to achieve what you want would be like this.
A user table:
User
----
Id
Username
Password (hashed)
A family members table:
Family_Members
--------------
User
Member
You put pairs of relationships in the family_members table. So you would put the id of one user in the first column user and the id of one of their family members in the second column member. Then you have a row for every relationship. When you load the table, you find all the rows where user is equal to the user id you are loading.
For example:
User
+--+--------+--------+
|Id|Username|Password|
| 1|Bob |abc |
| 2|Dad |def |
| 3|Mum |ghi |
+--+--------+--------+
And..
Family_members
+----+------+
|User|Member|
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
+----+------+
Then when you load family members for Bob, who's id is 1, you load all the rows from the family_members table that have User==1. Then you go back to the User table and load the details you want for each family member, and get an object like:
User:
- username: Bob
- password: abc
- family:
- User:
- username: Dad
- User:
- username: Mum
Hope this answers your question.

Spring Data with strict In-clause

See updated question below
I want to query for elements where every element is required to have all given attributes of e.g. a set to be returned, not only one.
Default Spring Data Jpa Query, where one attribute is enough:
findAllByAttributeIn(Set<Attribute> setAttr)
Example problem
For these elements (abstracted, no actual table)
id | attr
----------
1 | A
2 | A,B
3 | A,B,C
with this filter:
setAttr: [A,B]
the default query
findAllByAttributeIn(Set<Attribute> setAttr)
returns all elements (by ids):
[1,2,3]
Desired result is only the second element (with [A,B]).
Is that possible with the given Spring Data Jpa query keywords or is the only solution to create a custom query?
Updated question:
There are two entities, Media and Tag. They have a many-to-many relationship which is realised in the table Media_has_tags. Every media can have any number of tags (0..*). So in my Spring app, media has a set of tags as an attribute and vice versa. The classes:
Media = {
id: string,
Set<Tag> tags,
...
}
Tag = {
id: string,
Set<Media> medias,
...
}
The corresponding tables are:
Media
-------
id | ...
Tag
-------
id | ...
title | ...
Media_has_tags
-------
media_id | tag_id
And now I have a set of tags with which I want to get all medias which have every tag of that set. They can have more tags of course, but they need to have every tag of the set I am providing.
Concrete example:
Media
-------
id | ...
-------
1 |
2 |
Tag
-------
id | title | ...
1 | A |
2 | B |
Media_has_tags
-------
media_id | tag_id
1 | 1
2 | 1
2 | 2
Given a set of tags [A, B] I want only media with id 2 as a result since media with id 1 doesn't have tag 'B'.
Can I achieve that with Keywords and if, how or do I have to build my own query?
Sorry, but your question is incorrect initially.
findAllByAttributeIn(Set<Attribute> setAttr)
produces an SQL request like this:
select * from <table_name> t where t.attribute in (?,?,?...?)
So according to RDBMS-rules your result is correct and expectable.
What is a real structure for your abstract tables?
id | attr
----------
1 | A
2 | A,B
3 | A,B,C
In real life it would be somethink like this:
id | attr
----------
1 | A
2 | A
2 | B
3 | A
3 | B
3 | C
And again - in this case the result you have got is ok for any RDBMS.
Give us real examples from your project, please
Update because of question update:
OK, the problem has been clarified.
In this case there is no straight way to solve it using standard CrudRepository syntax. But you can try to write #Query request to manage to get the goal.
In a clear SQL this problem has to be solved by using group by together with having. It would be something like this:
select media_id
From media_has_tags
Where tag_id in(1,2 3)
Group by media_id
Having count(*)=3
In terms of SpringData it means that you have to create MediaHasTagsRepository interface and an appropriate query method to get looked ids. After that you can easily find medias you're looking for.
But this approach is not looking good IMHO. The best way I suppose is to find all the medias by the your initial query and than filter them by the given condition in Java.
E.g. you have a List<Media> where each element has an least a one tag that you are looking for. Then we can do a loop to find medias that contains all the looked tags:
List<Media> list; // here is a filled list of medias
Set<String> titles; // here is a set of title interseptions we a looking for
final List<Media> result = new ArrayList<>();
for (Media media: list) {
if (!list.getTags().isEmpty()){
Set<String> tagTitles = list.getTags().stream().map(item -> item.getTitle()).filter(title -> titles.contains(title)).distinct().collect(Collectors.toSet());
if (tagTitles.size() == titles.size()) {
result.add(media);
}
}
}

Copy value from one datatable to another in Jbehave

I need to copy data from one data table to another as mentioned below
When I enter company manager details of:
|NAME |VALUE |
|title |$randomList |
|firstName |$random |
|initial |$random |
|surname |$random |
When I enter company employee details of
|NAME |VALUE |
|title |$randomList |
|firstName | |
|initial | |
|surname | |
I am new to jbehave. In the above tables when i execute this scenario random characters will be generated and assigned to firstname,initial,surname in the 1st table I somehow need the same data when I execute the second table. I know we should parametrize by using examples table but in this case I don't want to use it. so how to copy the data from one table to another.

Categories