I am working on this test class and trying to change the response expected to a bean response as I have changed the requests to bean requests.
private void assertXmlResponse(Document xmlResponse, int Elements,
String Message, String Code, String name,
String Funds)
{
Node topLevelElement = xmlResponse.getFirstChild();
NodeList childElements = topLevelElement.getChildNodes();
assertEquals("result", topLevelElement.getNodeName());
assertEquals(Elements, childElements.getLength());
assertEquals("message", childElements.item(0).getNodeName());
assertEquals(Message, childElements.item(0).getTextContent());
assertEquals("code", childElements.item(1).getNodeName());
assertEquals(Code, childElements.item(1).getTextContent());
assertEquals("name", childElements.item(2).getNodeName());
assertEquals(name, childElements.item(2).getTextContent());
}
Please can someone point me in the right direction or even let me know if it's possible?
Thanks
You are about to make POJO(Plain Old Java Objects).
public Class A{
private int Elements;
private String Message;
private String Funds;
private String code;
private String name;
//getters and setters
}
Keep the reference of this class as Parameter in your method.
Use the getters for accessing the value in your method.
Related
We are consuming a third party API on server side which returns response as below class.
public class SourceParent {
private String ultimateParentId;
private String name;
private List<SourceChildren> children;
}
public class SourceChildren {
private String ultimateParentId;
private String immediateParentId;
private String childName;
private String level;
private List<SourceChildren> children
}
Children nesting can be up to any level.
We have to map above object to similar type of target (class structure) class i.e.
public class TargetParent {
private String ultimateParentId;
private String name;
private List<TargeChildren> children;
}
public class TargeChildren {
private String ultimateParentId;
private String immediateParentId;
private String childName;
private String level;
private List<TargeChildren> children
}
Mapping has to be done field by field. We can not return source object from our API to our consumers because if any field name or field changes in source object we don't want our consumers to change their code instead we would just change in the mapper.
Somebody please suggest how to do this mapping efficiently from source to target object in java (preferred java 8 and above);
I am trying to map as below, however got stuck as children can be up to any level.
public TargetParent doMapping(SourceParent source) {
TargetParent target = new Targetparent();
target.setUltimateParentId(source.getUltimateParentId);
.......
target.setChildren() // Don't to how to approach here
}
Appreciate you help!
In your doMapping do this
targetParent.setChildren(sourceParent.children.stream().map(sourceChild -> doMappingForChild(sourceChild)).collect(Collectors.toList()));
And add this function below doMapping, this is a recurcive approach
public TargetChild doMappingForChild(SourceChild source){
TargetChild target = new TargetChild();
...other fields
target .setChildren(sourceParent.children.stream().map(sourceChild -> doMappingForChild(sourceChild)).collect(Collectors.toList()));
}
The webservice return the following JSON string:
{"errorCode":0,"error":"","status":"OK","data":{"id":"1234A"}}
So to get a class that receives the response in a function like this that performs a post in Retrofit:
Call<UploadImageData> postData(#Header("Cookie") String sessionId, #Body UploadImageModal image);
I'd need to make a class like this;
public class UploadImageData {
private int errorCode;
private String error;
private String status;
}
But I'm lost in how I would have to declare the part that would take "data":{"id":"1234A"}, so it gets the data from there correctly.
How could I do this?
Since data is a nested object within the surrounding json object, you can include it as another class in your UploadImageData class.
public class UploadImageData {
private int errorCode;
private String error;
private String status;
private MyDataClass data;
}
public class MyDataClass {
private String id;
}
DonĀ“t forget setter methods oder make fields public.
I have below class.. Need to set mock values for "EmployeeInterfaceFactory.getAddressImpl()".. Tried using PowerMock, but as the declaration is outside the method, Mocked object is not getting reflected and NullPointerException is coming.. Any help/suggestion would be appreciated.
public Address address(){
private EmpAddress empAddress = EmployeeInterfaceFactory.getAddressImpl();
public String getDetails(){
String details = empAddress.getEmpDetails();
return details;
}
}
your code looks a little bit incorrect. I assume following code:
public Address{
private EmpAddress empAddress = EmployeeInterfaceFactory.getAddressImpl();
public String getDetails(){
String details = empAddress.getEmpDetails();
return details;
}
}
Then you could just inject the EmployeeInterfaceFactory in a constructor:
public Address{
private EmpAddress empAddress;
public Address(EmployeeInterfaceFactory employeeFactory){
empAddress = employeeFactory.getAddressImpl();
}
public String getDetails(){
String details = empAddress.getEmpDetails();
return details;
}
}
This way you can use normal mockito functions to mock objects.
EmployeeInterfaceFactory employeeFactory = mock(EmployeeInterfaceFactory.class);
AddressImpl employeeAddress = new AddressImpl();
//set details on employeeAddress
when(employeeFactory.getAddressImpl()).thenReturn(employeeAddress);
So in general this is pushing up a dependecy to the caller (see refactoring methods for more information).
I'm trying to convert below simple JSON to Java Object using com.fasterxml.jackson.core. I have problem with bonusAmount field setter method.
JSON:
{"amount":332.5, "bonusamount":3, "action":"Spend"}
Java class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class GameRequest {
#JsonProperty("amount")
private BigDecimal amount;
#JsonProperty("bonusamount")
private BigDecimal bonusAmount;
#JsonProperty("action")
private String action;
.....
public BigDecimal getBonusAmount() {
return bonusAmount;
}
public void setBonusAmount(BigDecimal bonusAmount) {
this.bonusAmount = bonusAmount;
}
Value of bonusAmount field is NULL when I try to use it but if I change name of setter method from setBonusAmount to setBonusamount then it works. Can someone tell me why??
That is because you have renamed your field using #JsonProperty("bonusamount") that means Jackson searches for a method named setBonusamount (first char toUpperCase, rest stays the same)
The Code Below allows me to write an Object into an XML file.
public class BathGuest{
private String name = "";
private DateMinutesHours wakeUpTime;
private int duration = 0;
private DateMinutesHours _plannedTime;
#XmlElement(name ="plannedTime")
public DateMinutesHours get_plannedTime() {
return _plannedTime;
}
#XmlElement(name = "ID")
public String getName() {
return _name;
}
...
}
The problem I have is, that birthday is an other Class to handle my timeoperations. So the result of my XML File, is not really what i expected.
What I get is:
<bathroomEntity>
<duration>3</duration>
<ID>Walter</ID>
<startTime>
<totalMinutes>481</totalMinutes>
</startTime>
<plannedTime>
<totalMinutes>485</totalMinutes>
</plannedTime>
</bathroomEntity>
And what I want is:
<bathroomEntity>
<duration>3</duration>
<ID>Walter</ID>
<startTime>08:10</startTime>
<plannedTime>08:50</plannedTime>
</bathroomEntity>
How can I reach the Second XMl-File?
If you annotate the totalMinutes property on the DateMinutesHours class with #XmlValue then you will get the behaviour you are lookiing for.