Verify array with custom objects - java

I have a class:
public class MyCustomObject {
private String field1;
private String field2;
}
And I've created array of MyCustomObject:
MyCustomObject[] array = new MyCustomObject[]{new MyCustomObject()};
My goal is to verify elements of this array using hamcrest matchers. I've tried the following approach:
assertThat(array, allOf(hasItemInArray(hasProperty("field1", equalTo("value1")))), hasItemInArray(hasProperty("field2", equalTo("value2")))));
But unfortunatly it does not work.
In which way the array of custom objects can be verified?

I would change your array to an ArrayList just for testing purposes:
List<MyCustomObject> customObjects = Arrays.asList(array);
And then assert with the Hamcrest hasItems Matcher if the expected items are present in the list:
assertThat(customObjects, hasItems(myCustomObject1, myCustomObject2));

Have you tried adding getters to your class? That did the trick for me.
public class MyCustomObject {
private String field1;
private String field2;
public String getField1() {
return field1;
}
public String getField2() {
return field2;
}
}

Related

Sorting collections within objects using Comparator and functions

I have the below method, and would like to add another field to the comparison, however I am finding it difficult to include.
private static int daoComparator(EDao eDao1, EDao eDao2) {
return Comparator.comparing((EDao eDao) -> eDao.getObjectA().getStatus())
.thenComparing(EDao::getUpdatedDate)
.thenComparing(EDao::getCreatedDate)
.thenComparing((EDao eDao) -> eDao.getObjectA().getId())
.compare(eDao1, eDao2);
}
Within EDao class, there is ObjectA, and within ObjectA, there is a Collection<ObjectB>. Within ObjectB there is an Enum of type String which I need to retrieve and sort within the above daoComparator. How can I update my method to retrieve and sort this String Enum? It also needs to be the first comparison. Comparator is from java.util package.
public class EDao
{
private ObjectA objectA;
private Date updatedDate;
private Date createdDate;
}
public class ObjectA
{
private String id;
#Enumerated(EnumType.STRING)
private String status;
private Collection<ObjectB> objectB;
}
public class ObjectB
{
// field I want to retrieve for sorting
#Enumerated(EnumType.STRING)
String RegStatus status;
}
I managed to retrieve the status', I just need to handle the scenario where ObjectB could be null.
private static int daoComparator(EDao eDao1, EDao eDao2) {
return Comparator.comparing((EDao eDao) -> eDao.getObjectA().getObjectB().stream().findFirst().get().getStatus())
.thenComparing((eDao eDao) -> eDao.getObjectA().getStatus())
.thenComparing(EDao::getUpdatedDate)
.thenComparing(EDao::getCreatedDate)
.thenComparing((EDao eDao) -> eDao.getObjectA().getId())
.compare(eDao1, eDao2);

Convert list of jsonNode objects to another class, but add a new parent field

I am very new to java and I hope my question is not too stupid and has enough info for you guys to help me out.
I have a list of jsonNodes, each of them is in the following format:
{"field1":value1, "field2":value2, "field3":value3, "notneeded1":value4, "notneeded2":value5}
I am currently using a class like the following and converting it to list
#JsonIgnoreProperties(ignoreUnknown = true)
class customClass:
String field1;
String field2;
String field3;
Using TypeReference to convert the list of jsonNode to list of this class...
What I want to do is add a few of these fields within an another field
{"parentfield":{"field1":value1, "field2":value2}, "field3":value3}
How do I do this using this class?
AFIU you want this:
#JsonIgnoreProperties(ignoreUnknown = true)
class CustomClass {
public String field1;
public String field2;
public String field3;
public CustomClass2 parentfield;
}
#JsonIgnoreProperties(ignoreUnknown = true)
class CustomClass2 {
public String field1;
public String field2;
}
Then depends on your code, to set the values of the fields in an object of CustomClass2 with fields from an object of CustomClass.

Java 8 sort multiple objects

public class B {
private String name;
private String value;
//Setters and Get
}
public class C {
private String name;
private String value;
//Setters and Get Methods
}
public class D {
private String name;
private String value;
//Setters and Get
}
public class A {
private B b;
private C c;
private D d;
// Setters and Get
}
public class Example{
List<A> a = new Array List<A>();
//Lets assume a will contain objects of class B, C and D
a .sort( Comparator.comparing(A::getB().getName).thenComparing(A::getC().getName));
}
Sort field from one pojo , then sort field by next pojo.
Need to understand how to sort in this situation. Can we use
Comparator.comparing ()in this case?
You can't use method refences like that, but you could just use lambda expressions:
a.sort(Comparator.comparing((A x) -> x.getB().getName())
.thenComparing(x -> x.getC().getName()));

How to refactor this simple class fields

I have a very basic OOP question. I have two classes as follows
public class Class1 {
private String field1;
private String field2;
private List<String> field3;
// constructors
}
public class Class2 {
private String field1;
private String field2;
private List<Object1> field3;
// constructors
}
The only reason i had to create class2 was because the List in class1 can either be a List of Strings or a List of Object2. Is there a way to et rid of Class2, and have field3 behave as a List of String and also as a List of Object2.
I know i can do something like
public class Class1 {
private String field1;
private String field2;
private List<String> field3;
private List<Object1> field4;
// constructors which are initializing null to either field 3 or field 4
}
But the above doesnt seem clean, as an object instantiated with either list should not have any reference in it of the other List.
Use generics:
public class Class1<TField3> {
private String field1;
private String field2;
private List<TField3> field3;
// constructors
}
Class1<String> stringObj = new Class1<>();

jersey/jaxb unmarshalling a java object, but fields not populated

I have a Jersey server side code which takes a Java object as a body parameter. This Java object lets say Preferences is defined as below.
#XmlRootElement(name = "preferences", namespace = "http://arjun.test.com/tests/1.0")
public class Preferences {
String field1;
String field2;
public Preferences() {
}
#XmlElement(name = "field-1", namespace = "http://arjun.test.com/tests/1.0")
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
#XmlElement
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
}
My problem is: while sending the data from client I am populating both fields, but somehow in the server the first field value is always null, if I change the field1 XML annotation to the same as field2, then it works fine.
Can someone please let me know what mistake am I doing.
The Jersey server method is producing and consuming the JSON objects.

Categories