How do I map multiple values in data table into a list? - cucumber-jvm

Here is my problem:
When I have step with data:
|Name | Description | data1 | data2|
| A | new A | abc | xyz |
The step definition
#When I_have_step_with_data(List<DataSet> dataSet){
////
}
I am trying to map the above data table into this domain object.
public class DataSet{
private String name; -> maps to Name
private String description; -> maps to Description
private List<Data> data; -> creates list of Data with data1,abc and data2,xyz set.
}
public class Data {
private string key;
private String value;
}
Any pointers on how this can be achieved would be great.

Let's say here is your scenario :
When I have step with data:
|Name | Description | data1 | data2 |
| A | new A | abc | xyz |
You will create POJO class (Getters and Setter)
You will create one class call "DataClass"
public class DataClass{
private String Name;
private String Description;
private String Data1;
private String Data2;
public DataClass(String Name, String Description, String Data1,String Data2) {
this.Name = Name;
this.Description= Description;
this.Data1= Data1;
this.Data2= Data2;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getData1() {
return Data1;
}
public void setData1(String data1) {
Data1 = data1;
}
public String getData2() {
return Data2;
}
public void setData2(String data2) {
Data2 = data2;
}
}
In Step Definition , Here is loop to get value
public class descriptionSteDefs{
private Map<String, DataClass> DataClassList;
#When("^When I have step with data$")
public void description_data(List<DataClass> DataClassList) throws Throwable {
DataClassList = new HashMap<String, DataClass>();
for (DataClass DataClassValue: DataClassList) {
String key = DataClass.getDescription;
DataClassList .put(key, DataClassValue);
}
}

Related

Resteasy - Multiple resource methods match request "POST /.../..."

I am doing a REST API with Java Resteasy framework (using Jackson as well).
I was trying to define 2 api endpoints almost equal:
#POST
#Path("/addbook")
#Produces(MediaType.APPLICATION_XML)
#Consumes(MediaType.APPLICATION_XML)
public BookAdvanced addBook (BookAdvanced book){...}
#POST
#Path("/addbook")
#Produces(MediaType.APPLICATION_XML)
#Consumes(MediaType.APPLICATION_XML)
public Book addBook (Book book){...}
Is this possible? What I want is, depending on the xml arriving execute one or the other method
Here book class:
package package1;
import javax.xml.bind.annotation.*;
import java.util.Date;
#XmlRootElement(name = "book")
public class Book {
private Long id;
private String name;
private String author;
#XmlAttribute
public void setId(Long id) {
this.id = id;
}
#XmlElement(name = "title")
public void setName(String name) {
this.name = name;
}
#XmlElement(name = "author")
public void setAuthor(String author) {
this.author = author;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
// constructor, getters and setters
}
Here BookAdvanced class:
package package1;
import javax.xml.bind.annotation.*;
import java.util.Date;
#XmlRootElement(name = "book")
public class BookAdvanced {
private Long id;
private String name;
private String author;
private int year;
#XmlAttribute
public void setId(Long id) {
this.id = id;
}
#XmlElement(name = "title")
public void setName(String name) {
this.name = name;
}
#XmlElement(name = "author")
public void setAuthor(String author) {
this.author = author;
}
#XmlElement(name = "year")
public void setYear(int year) {
this.year = year;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
// constructor, getters and setters
}
27-Jan-2023 12:33:18.238 WARN [http-nio-8080-exec-39] org.jboss.resteasy.core.registry.SegmentNode.match RESTEASY002142: Multiple resource methods match request "POST /hello/addbook". Selecting one. Matching methods: [public package1.BookAdvanced prova_gradle_war.HelloWorldResource.addBook(package1.BookAdvanced), public package1.Book prova_gradle_war.HelloWorldResource.addBook(package1.Book)]
Matching is based on the request URI and not the request body. There is no real way to match the path and decide the method to use based on the body.
You could do something manually where you inspect the data and determine which type to create.

I have sql server hierarchical data table with this sample structure and data:

My table structure is:
Id name size type parrent_Id
1 AAAA 2k t1 null
2 BB 2k t2 1
3 CC 1k t3 1
4 DDDD 2k t4 null
5 EE 2k t5 4
6 FF 1k t6 5
I need a SQL query that generates JSON structure from table to use it in primeng tree table component. It requires JSON structure like this. iam using asp.net core web api with sql server:
{
"data":
[
{
"data":{
"name":"Documents",
"size":"2k",
"type":"Folder"
},
"children":[
{
"data":{
"name":"Work",
"size":"5k",
"type":"Folder"
},
]
}
]
Suppose you're using EF Core and your Model looks like :
public class XModel {
public int Id {get;set;}
public string Name {get;set;}
public string Size {get;set;}
public string Type {get;set;}
public int? ParentId {get;set;}
public XModel Parent {get;set;}
public IList<XModel> Children {get;set;}
}
Since you expects a model with a data & children field. Let's create DTO models for them:
public class Data {
public int Id {get;set;}
public string Name {get;set;}
public string Size {get;set;}
public string Type {get;set;}
[JsonIgnore]
public int? ParentId {get;set;}
}
public class Dto {
public Data Data {get;set;}
public IList<Dto> Children{get;set;}
}
Let's built an extension method that builds the tree
public static class TreeLikeExtensions
{
public static IList<Dto> BuildTrees(this IQueryable<XModel> models)
{
var dtos = models.Select(m =>new Dto{
Data = new Data { Id = m.Id, Name = m.Name, Size =m.Size, Type = m.Type, ParentId = m.ParentId, },
Children = null,
}).ToList();
return BuildTrees(null, dtos);
}
// private helper function that builds tree recursively
private static IList<Dto> BuildTrees(int? pid, IList<Dto> candicates)
{
var children = candicates.Where(c => c.Data.ParentId == pid).ToList();
if (children==null || children.Count() == 0){
return null;
}
foreach (var i in children){
i.Children= BuildTrees(i.Data.Id, candicates);
}
return children;
}
}
To get the trees, just invoke BuildTrees():
var result = _context.XModel.BuildTrees();
To ignore the null children property when serializing, just add a settings as below:
// var settings= new JsonSerializerSettings{
// NullValueHandling = NullValueHandling.Ignore,
// ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
// }
Or configure MVC Serivces in Startup.cs:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(o =>{
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
A Working Demo

#Indexed annotation is ignored

I have a simple Product class as it follows
#SolrDocument(collection = "product")
public class Product {
#Id
#Indexed(name = "id", type = "string")
private String id;
#Field
#Indexed(name = "namex", type = "text_general", stored = false, searchable=true)
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
my problem is that the annotation #Indexed is completely ignored. The name of the field is simply name (instead of namex) and the field is stored. Any guess?
UPDATE 1 if I remove the type annotation name works, but stored has no effect still
I managed by modifying the bean that creates the SolrTemplate object like follows:
#Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
SolrTemplate st = new SolrTemplate(client);
st.setSchemaCreationFeatures(Collections.singletonList(Feature.CREATE_MISSING_FIELDS));
st.afterPropertiesSet();
return st;
}

Room Android : Entities and Pojos must have a usable public constructor

Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type)
Am integrating room into my existing project. While annotating a POJO, which implements Parcelable, with #Entity tag and making necessary changes, am getting this error. I already have an empty constructor in it. Any help would be appreciated.
#Entity(tableName = "Departments")
public class Department implements Parcelable {
#PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String departmentName;
private String logoUrl;
#Embedded
private ArrayList<Template> templateList;
public Department() {
}
protected Department(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.departmentName = in.readString();
this.logoUrl = in.readString();
this.id = (Integer) in.readSerializable();
this.templateList = in.createTypedArrayList(Template.CREATOR);
}
public static final Creator<Department> CREATOR = new Creator<Department>() {
#Override
public Department createFromParcel(Parcel in) {
return new Department(in);
}
#Override
public Department[] newArray(int size) {
return new Department[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(primaryId);
dest.writeString(departmentName);
dest.writeString(logoUrl);
dest.writeSerializable(id);
dest.writeTypedList(templateList);
}
public Integer getPrimaryId() {
return primaryId;
}
public void setPrimaryId(Integer primaryId) {
this.primaryId = primaryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public ArrayList<Template> getTemplateList() {
return templateList;
}
public void setTemplateList(ArrayList<Template> templateList) {
this.templateList = templateList;
}
}
#Entity(tableName = "Templates")
public class Template implements Parcelable {
#PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String code;
private String description;
private Integer departmentId;
#Embedded
private ArrayList<Issue> issueList;
public Template() {
}
private Template(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.code = in.readString();
this.description = in.readString();
this.id = (Integer) in.readSerializable();
this.departmentId = (Integer) in.readSerializable();
this.issueList = in.createTypedArrayList(Issue.CREATOR);
}
public static final Creator<Template> CREATOR = new Creator<Template>() {
#Override
public Template createFromParcel(Parcel in) {
return new Template(in);
}
#Override
public Template[] newArray(int size) {
return new Template[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(primaryId);
dest.writeString(code);
dest.writeString(description);
dest.writeSerializable(id);
dest.writeSerializable(departmentId);
dest.writeTypedList(issueList);
}
public Integer getPrimaryId() {
return primaryId;
}
public void setPrimaryId(Integer primaryId) {
this.primaryId = primaryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayList<Issue> getIssueList() {
return issueList;
}
public void setIssueList(ArrayList<Issue> issueList) {
this.issueList = issueList;
}
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
}
#Entity(tableName = "Issues")
public class Issue implements Parcelable {
#PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String code;
private String description;
private Integer parentIssue;
public Issue() {
}
protected Issue(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.code = in.readString();
this.description = in.readString();
this.id = (Integer) in.readSerializable();
this.parentIssue = (Integer) in.readSerializable();
}
public static final Creator<Issue> CREATOR = new Creator<Issue>() {
#Override
public Issue createFromParcel(Parcel in) {
return new Issue(in);
}
#Override
public Issue[] newArray(int size) {
return new Issue[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(primaryId);
dest.writeString(code);
dest.writeString(description);
dest.writeSerializable(id);
dest.writeSerializable(parentIssue);
}
public Integer getPrimaryId() {
return primaryId;
}
public void setPrimaryId(Integer primaryId) {
this.primaryId = primaryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getParentIssue() {
return parentIssue;
}
public void setParentIssue(Integer parentIssue) {
this.parentIssue = parentIssue;
}
}
Room assumes your entity class will be having only one constructor. But there is no such limitations, If you have multiple constructor then annotate one of them with
#Ignore
Room will ignore this constructor and compile without any error.
Example
#Entity(tableName = "Departments")
public class Department implements Parcelable {
#PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String departmentName;
private String logoUrl;
#Embedded
private ArrayList<Template> templateList;
/**Room will ignore this constructor
**/
#Ignore
public Department() {
}
protected Department(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.departmentName = in.readString();
this.logoUrl = in.readString();
this.id = (Integer) in.readSerializable();
this.templateList = in.createTypedArrayList(Template.CREATOR);
}
}
I'm not sure why you are getting your specific constructor error. That said your code will error from embedding the ArrayList. #Embedded is not meant to be used this way. #Embedded allows you to flatten your POJO structure when storing it. Nested POJO properties will appear as if they had been properties on the parent POJO. Using Embedded on a List is the same as asking it to flatten the properties of the ArrayList object and store them, not flatten the list items and store them.
The appropriate measure is to transition into a foreign key, primary key relationship. An alternative solution is to create a new POJO that contains your list of items (ie Templates, with an 's'). This would contain an ArrayList of Template objects. You would then define a converter that converts the POJO to a json/comma seperated list, and stores it in a single column that by default would be called "templates". Here is a link to this approach :
Android room persistent library - TypeConverter error of error: Cannot figure out how to save field to database"
Hope this helps.

Hibernate : #OneToMany : Always deleting and reinserting the child records

Please help me resolve this issue. I tried googling for a solution and couldn't find one for this.
Table structure
Table: Catalog
catalog_id (primary key)
name
Table: Catalog_Locale
catalog_id
locale_id
sequence
composite key(catalog_id,locale_id)
Class
public Class Catalog{
#Id
#Column(name = "CATALOG_ID", nullable = false)
private String catalogId;
#Column(name = "NAME")
private String name;
#OneToMany(targetEntity = CatalogLocale.class,fetch=FetchType.LAZY)
#JoinColumn(name = "CHILD_CATALOG_ID", nullable = false)
#Cascade(value = {})
protected List<CatalogLocale> locales = new ArrayList<CatalogLocale>(10);
public void setCatalogId( String catalogId ){
this.catalogId = catalogId;
}
public void setName( String name ){
this.name = name;
}
public void setLocales( List<CatalogLocale> locales ){
this.locales = locales;
}
public void getCatalogId(){
return catalogId;
}
public void getName(){
return name;
}
public void getLocales(){
return locales;
}
}
public class CatalogLocale{
#EmbeddedId
CatalogLocalePk catalogLocalePk;
#Column(name = "SEQUENCE")
private int sequence;
public void setCatalogLocalePk( CatalogLocalePk catalogLocalePk ){
this.catalogLocalePk = catalogLocalePk;
}
public void setSequence( int sequence ){
this.sequence = sequence;
}
public CatalogLocalePk getCatalogLocalePk(){
return catalogLocalePk;
}
public int getSequence(){
return sequence;
}
#Embeddable
public static class CatalogLocalePk{
#Column(name = "CATALOG_ID", nullable = false)
private String catalogId;
#Column(name = "LOCALE_ID", nullable = false)
private String localeId;
public CatalogLocalePk(){
}
public CatalogLocalePk( String catalogId, String localeId ){
this.catalogId = catalogId;
this.localeId = localeId;
}
public void setCatalogId( String catalogId ){
this.catalogId = catalogId;
}
public void setLocaleId( String localeId ){
this.localeId = localeId;
}
public String getCatalogId(){
return catalogId;
}
public String getLocaleId(){
return localeId;
}
}
}
The code works for fine for the insert operation, but for any update to the Catalog will trigger for delete and reinsert all entries of the child table.
Is there any solution for this?