|
| 1 | +package io.qameta.allure.jsonunit; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import net.javacrumbs.jsonunit.core.listener.Difference; |
| 6 | +import net.javacrumbs.jsonunit.core.listener.DifferenceContext; |
| 7 | +import net.javacrumbs.jsonunit.core.listener.DifferenceListener; |
| 8 | +import org.apache.commons.lang3.StringUtils; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +/** |
| 16 | + * JsonUnit listener, that keeps difference and |
| 17 | + * return formatted json to represent deltas |
| 18 | + * (i.e. the output of jsondiffpatch.diff). |
| 19 | + */ |
| 20 | +public class JsonPatchListener implements DifferenceListener { |
| 21 | + |
| 22 | + private static final String UNKNOWN_TYPE_ERROR = "Difference has unknown type"; |
| 23 | + private final List<Difference> differences = new ArrayList<>(); |
| 24 | + |
| 25 | + private DifferenceContext context; |
| 26 | + |
| 27 | + @Override |
| 28 | + public void diff(final Difference difference, final DifferenceContext differenceContext) { |
| 29 | + this.context = differenceContext; |
| 30 | + differences.add(difference); |
| 31 | + } |
| 32 | + |
| 33 | + public List<Difference> getDifferences() { |
| 34 | + return differences; |
| 35 | + } |
| 36 | + |
| 37 | + public DifferenceContext getContext() { |
| 38 | + return context; |
| 39 | + } |
| 40 | + |
| 41 | + @SuppressWarnings("ReturnCount") |
| 42 | + private String getPath(final Difference difference) { |
| 43 | + switch (difference.getType()) { |
| 44 | + case DIFFERENT: |
| 45 | + return difference.getActualPath(); |
| 46 | + |
| 47 | + case MISSING: |
| 48 | + return difference.getExpectedPath(); |
| 49 | + |
| 50 | + case EXTRA: |
| 51 | + return difference.getActualPath(); |
| 52 | + |
| 53 | + default: |
| 54 | + throw new IllegalArgumentException(UNKNOWN_TYPE_ERROR); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @SuppressWarnings("ReturnCount") |
| 59 | + private List<Object> getPatch(final Difference difference) { |
| 60 | + final List<Object> result = new ArrayList<>(); |
| 61 | + |
| 62 | + switch (difference.getType()) { |
| 63 | + case DIFFERENT: |
| 64 | + result.add(difference.getExpected()); |
| 65 | + result.add(difference.getActual()); |
| 66 | + return result; |
| 67 | + |
| 68 | + case MISSING: |
| 69 | + result.add(difference.getExpected()); |
| 70 | + result.add(0); |
| 71 | + result.add(0); |
| 72 | + return result; |
| 73 | + |
| 74 | + case EXTRA: |
| 75 | + result.add(difference.getActual()); |
| 76 | + return result; |
| 77 | + |
| 78 | + default: |
| 79 | + throw new IllegalArgumentException(UNKNOWN_TYPE_ERROR); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @SuppressWarnings({"all", "unchecked"}) |
| 84 | + public String getJsonPatch() { |
| 85 | + final Map<String, Object> jsonDiffPatch = new HashMap<>(); |
| 86 | + // take care of corner case when two jsons absolutelly different |
| 87 | + if (getDifferences().size() == 1) { |
| 88 | + final Difference difference = getDifferences().get(0); |
| 89 | + final String field = getPath(difference); |
| 90 | + if (field.isEmpty()) { |
| 91 | + final ObjectMapper mapper = new ObjectMapper(); |
| 92 | + try { |
| 93 | + return mapper.writeValueAsString(getPatch(difference)); |
| 94 | + } catch (JsonProcessingException e) { |
| 95 | + throw new IllegalStateException("Could not process patch json", e); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + getDifferences().forEach(difference -> { |
| 100 | + |
| 101 | + final String field = getPath(difference); |
| 102 | + Map<String, Object> currentMap = jsonDiffPatch; |
| 103 | + |
| 104 | + final String fieldWithDots = StringUtils.replace(field, "[", "."); |
| 105 | + final int len = fieldWithDots.length(); |
| 106 | + int left = 0; |
| 107 | + int right = 0; |
| 108 | + while (left < len) { |
| 109 | + right = fieldWithDots.indexOf('.', left); |
| 110 | + if (right == -1) { |
| 111 | + right = len; |
| 112 | + } |
| 113 | + String fieldName = fieldWithDots.substring(left, right); |
| 114 | + fieldName = StringUtils.remove(fieldName, "]"); |
| 115 | + |
| 116 | + if (right != len) { |
| 117 | + if (!fieldName.isEmpty()) { |
| 118 | + if (!currentMap.containsKey(fieldName)) { |
| 119 | + currentMap.put(fieldName, new HashMap<>()); |
| 120 | + } |
| 121 | + currentMap = (Map<String, Object>) currentMap.get(fieldName); |
| 122 | + } |
| 123 | + |
| 124 | + if (field.charAt(right) == '[') { |
| 125 | + if (!currentMap.containsKey(fieldName)) { |
| 126 | + currentMap.put("_t", "a"); |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + final List<?> actualExpectedValue = getPatch(difference); |
| 131 | + currentMap.put(fieldName, actualExpectedValue); |
| 132 | + } |
| 133 | + left = right + 1; |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + final ObjectMapper mapper = new ObjectMapper(); |
| 138 | + try { |
| 139 | + return mapper.writeValueAsString(jsonDiffPatch); |
| 140 | + } catch (JsonProcessingException e) { |
| 141 | + throw new IllegalStateException("Could not process patch json", e); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments