0

I have two JSON files, called "pickevent1" and "pickevent2". I have to compare if both files are matching; if they don't match, I need to know where they don't match.

pickevent1

 {
     "pickEventActivities": [{
         "orderId": "215",
         "lineNbr": 0,
         "pick": "EACH",
         "activations": [{
             "activationType": "Si",
             "activationValue": "31"
         }]
      }]
  }

pickevent2

{
    "pickEventActivities": [{
        "orderId": "115",
        "lineNbr": 1,
        "pick": "Hello",
        "activations": [{
            "activationType": "Bi",
            "activationValue": "3"
        }]
    }]
}

I created a pick event POJO class:

@JsonRootName(value = "pickEventActivities")
@Data
@JsonPropertyOrder({ "orderId", "lineNbr", "pick"})
class PickEvent {
    String orderId;
    String lineNbr;
    String pick;
    List<Activation> activations;
}

and a Activation POJO class:

@Data
@JsonPropertyOrder({ "activationType", "activationValue"})
public class Activation {
    String activationType;
    String activationValue;
}

To make sure it works, I created a test class:

public void compareJson() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    
    PickEvent result1 = objectMapper.readValue(new File("src/../pickevent1.json"), PickEvent.class);
    PickEvent result2 = objectMapper.readValue(new File("src/../pickevent2.json"), PickEvent.class);
    
    assertEquals(result1, result2);
}

But when I am doing assertSame(result1,result2) its giving me null for json values:

Exception in thread "main" java.lang.AssertionError: expected same:<PickEvent(orderId=null, lineNbr=null, pick=null, activations=null)> was not:<PickEvent(orderId=null, lineNbr=null, pick=null, activations=null)>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotSame(Assert.java:828)
    at org.junit.Assert.assertSame(Assert.java:771)
    at org.junit.Assert.assertSame(Assert.java:782)
    at JsonDiff.PickEventDiff.comparejson(PickEventDiff.java:26)
    at JsonDiff.PickEventDiff.main(PickEventDiff.java:32)

It should give me an assertion error, but the test succeeds.

Rumi
  • 61
  • 1
  • 9
  • Did you override `equals()` and `hashCode()` for your POJOs? – LHCHIN Dec 03 '21 at 07:30
  • @LHCHIN No I didnt – Rumi Dec 03 '21 at 07:32
  • You have to override `equals()` (generated by your IDE for example) so that you can compare two PickEvents based on their internal details. Please refer to [Compare two objects with .equals() and == operator](https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator). – LHCHIN Dec 03 '21 at 07:36
  • @LHCHIN – You are right that `equals()` is needed to compare based on the internal details. But when the method is not overwritten, `assertEquals()` for two distinct objects should always fail. Why is it not failing here?? – tquadrat Dec 03 '21 at 07:41
  • @Rumi – What happens if you replace `assertEquals()` by `assertSame()`? – tquadrat Dec 03 '21 at 07:44
  • 1
    I don't understand why your test is *not* failing, but be aware Lomboks `@Data` annotation already provides the `equals()` and `hashCode()` methods! – Jacob van Lingen Dec 03 '21 at 07:47
  • @tquadrat with assertSame its coming as AssertionFails but its retrieving null values for the json content. I am updating the code – Rumi Dec 03 '21 at 08:04
  • Remove ```objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);```. In fact, an exception occurred during the parsing process. – zysaaa Dec 03 '21 at 08:18
  • @zysaaa I removed that line and I can see the exception, none of the field is getting accepted, would you mind to paste a bit of your code, just want to check out your approach. – Rumi Dec 03 '21 at 08:28

3 Answers3

2

It should give me an assertion error, but the test succeeds.

Because you use objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);. In fact, an exception occurred during the parsing process.

Try:

    public void compareJson() throws Exception {
        final ObjectMapper objectMapper = new ObjectMapper();
        Wrapper wrapper = objectMapper.readValue(new File(""), Wrapper.class);
        Wrapper wrapper2 = objectMapper.readValue(new File(""), Wrapper.class);
        System.out.println(wrapper.equals(wrapper2));
    }

    @Data
    static class Wrapper {
        List<PickEvent> pickEventActivities;
    }
zysaaa
  • 1,777
  • 2
  • 8
  • 19
0

You are trying to read a PickEvent Object but you're actually sending a list there.

Please change your json to

{
    "pickEventActivities": {
        "orderId": "115",
        "lineNbr": 1,
        "pick": "Hello",
        "activations": [{
            "activationType": "Bi",
            "activationValue": "3"
        }]
    }
}

or try changing your code to

List<PickEvent> list1 = objectMapper.readValue(new File("src/../pickevent1.json"), new ParameterizedTypeReference<PickEvent>(){});
Utsav10
  • 400
  • 2
  • 11
0

Here is my github repo for Intellij Idea plugin. Basically JSON comparator implemented in java. It compares JSON by fields, values and objects in array

Volodymyr Kret
  • 1,511
  • 15
  • 16