Home > database >  Can a java POJO API response be converted to match a Map<String, String> data type?
Can a java POJO API response be converted to match a Map<String, String> data type?

Time:12-28

My assertion is failing because of data type conversion:

public void verify(Map<String, String> theNames) {
List <DemoResponse> DemoResponse = Arrays.asList(BaseService.getResponse().as(DemoResponse.class));

System.out.println(demoResponse);
System.out.println(theNames);

}

The argument Map<String, String> theNames is fetching data from a cucumber feature file and returns the expected output:

{name=Builder, description=Builder demo, id=ye208bq4-2485-8nd3-10ka-92f4cb91795x}

DemoResponse POJO class returns the output:

DemoResponse(id=ye208bq4-2485-8nd3-10ka-92f4cb91795x, name=Builder, description=Builder demo)

The two outputs obviously have the same properties, but different formats, so they are not equal

As a result, this assertion comparing inequality passes:

assertThat(demoResponse).isNotEqualTo(theNames);

But this assertion comparing equality fails:

assertThat(demoResponse).isEqualTo(theName);

Now, what I want to achieve is to make the two formats equal so that the failing assertion would pass.

This is the POJO class

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class DemoResponse{

  private UUID id;
  private String name;
  private String description;

}

In a bid to make the outputs match, I tried to convert the POJO response to a Map this way:

List demoResponse = Arrays.asList( BaseService.getResponse().as(DemoResponse.class));
ObjectMapper mapper = new ObjectMapper();
    TypeReference<List<DemoResponse>> typeReference = new TypeReference<List<DemoResponse>>() {/* */};
List<DemoResponse> res = mapper.convertValue(demoResponse,  typeReference);

System.out.println(res);

Output:

[DemoResponse(id=ye208bq4-2485-8nd3-10ka-92f4cb91795x, name=Builder, description=Builder demo)]

The output changed slightly from the previous approach but still did not match the format output from Map<String, String> theNames.

How do I get the format of the two outputs to match? I don't mind changing the data type of any of the two, as long as their outputs match, I would be ecstatic.

CodePudding user response:

In order to convert any class to a map and vice-versa, you can do as follows:

        ObjectMapper mapper = new ObjectMapper();
        DemoResponse foo = new DemoResponse(UUID.randomUUID(), "name", "description");

        // Convert POJO to Map
        Map<String, Object> map =
                mapper.convertValue(foo, new TypeReference<>() {
                });

        // Convert Map to POJO
        DemoResponse anotherFoo = mapper.convertValue(map, DemoResponse.class);

Just have in mind that, in the class DemoResponse, you must have a default constructor (no args) and possibly either getters/setters or the fields annotated with

@JsonProperty

After that, you can either convert your POJO to a map or convert the map of values to a POJO and perform the desired assertions.

CodePudding user response:

I have a basic response because you have only 3 fields and equals matching class, you need only to create an DemoResponse with the Map<String, String>, maybe with a constructor.

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class DemoResponse{

  private UUID id;
  private String name;
  private String description;

  public DemoResponse(Map<String, String> theNames){
     this.id = theNames.get("id");
     this.name = theNames.get("name");
     this.description = theName.get("description");
  }
}

And after you can assert like that

assertThat(demoResponse).isEqualTo(new DemoResponse(theName));

CodePudding user response:

you are comparing List and Map which are not the same object, thus they are not equal. you can try to convert the POJO into map first:

ObjectMapper mapper = new ObjectMapper(); 
Map<String, String> demoResponse = mapper.convertValue(BaseService.getResponse(), new TypeReference<Map<String,String>>() {});

then you can check using this:

assertThat(demoResponse).isEqualTo(theNames);

CodePudding user response:

Change your line

    TypeReference<List<DemoResponse>> typeReference = new TypeReference<List<DemoResponse>>() {/* */};
List<DemoResponse> res = mapper.convertValue(demoResponse,  typeReference);

to

    TypeReference<DemoResponse> typeReference = new TypeReference<DemoResponse>() {/* */};
DemoResponse res = mapper.convertValue(demoResponse,  typeReference);

And you will get what you want. You are parsing a single value as a List and because of that your value is surrounded by [] which is your difference

  •  Tags:  
  • java
  • Related