Passed
Push — master ( 886a8d...e81f81 )
by Filip
02:36
created

jsonToString(JsonSchema)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package com.hltech.vaunt.validator;
2
3
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
4
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.List;
8
9
public class JsonSchemaValidator {
10
    static final String ERROR_FORMAT = "Schema with id %s has not matching %s - consumer: %s, provider: %s";
11
    static final String ERROR_FORMAT_SHORT = "Schema with id %s has not matching %s";
12
13
    public static List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) {
14
        List<String> errors = new ArrayList<>();
15
16
        if (!equals(consumerSchema.get$ref(), providerSchema.get$ref())) {
17
            errors.add(String.format(ERROR_FORMAT,
18
                    consumerSchema.getId(),
19
                    "$ref",
20
                    consumerSchema.get$ref(),
21
                    providerSchema.get$ref()));
22
        }
23
24
        if (!equals(consumerSchema.get$schema(), providerSchema.get$schema())) {
25
            errors.add(String.format(ERROR_FORMAT,
26
                    consumerSchema.getId(),
27
                    "$schema",
28
                    consumerSchema.get$schema(),
29
                    providerSchema.get$schema()));
30
        }
31
32
        if (!arraysEquals(consumerSchema.getDisallow(), providerSchema.getDisallow())) {
33
            errors.add(String.format(ERROR_FORMAT,
34
                    consumerSchema.getId(),
35
                    "disallow",
36
                    jsonArrayToString(consumerSchema.getDisallow()),
37
                    jsonArrayToString(providerSchema.getDisallow())));
38
        }
39
40
        if (!arraysEquals(consumerSchema.getExtends(), providerSchema.getExtends())) {
41
            errors.add(String.format(ERROR_FORMAT,
42
                    consumerSchema.getId(),
43
                    "extends",
44
                    jsonArrayToString(consumerSchema.getExtends()),
45
                    jsonArrayToString(providerSchema.getExtends())));
46
        }
47
48
        if (isRequired(consumerSchema) && !isRequired(providerSchema)) {
49
            errors.add(String.format(ERROR_FORMAT,
50
                    consumerSchema.getId(),
51
                    "required",
52
                    consumerSchema.getRequired(),
53
                    providerSchema.getRequired()));
54
        }
55
56
        if (!equals(consumerSchema.getReadonly(), providerSchema.getReadonly())) {
57
            errors.add(String.format(ERROR_FORMAT,
58
                    consumerSchema.getId(),
59
                    "readonly",
60
                    consumerSchema.getReadonly(),
61
                    providerSchema.getReadonly()));
62
        }
63
64
        if (!equals(consumerSchema.getDescription(), providerSchema.getDescription())) {
65
            errors.add(String.format(ERROR_FORMAT,
66
                    consumerSchema.getId(),
67
                    "description",
68
                    consumerSchema.getDescription(),
69
                    providerSchema.getDescription()));
70
        }
71
72
        return errors;
73
    }
74
75
    private static boolean isRequired(JsonSchema schema) {
76
        return schema.getRequired() != null && schema.getRequired();
77
    }
78
79
    public static boolean equals(Object object1, Object object2) {
80
        if (object1 == null) {
81
            return object2 == null;
82
        } else {
83
            return object1.equals(object2);
84
        }
85
    }
86
87
    private static <T> boolean arraysEquals(T[] array1, T[] array2) {
88
        if (array1 == null) {
89
            return array2 == null;
90
        }
91
        if (array2 == null) {
92
            return false;
93
        }
94
95
        return Arrays.equals(array1, array2);
96
    }
97
98
    private static String jsonArrayToString(JsonSchema[] array) {
99
        if (array == null) {
100
            return "null";
101
        }
102
103
        int inMax = array.length - 1;
104
        if (inMax == -1) {
105
            return "[]";
106
        }
107
108
        StringBuilder stringBuilder = new StringBuilder();
109
        stringBuilder.append('[');
110
        for (int i = 0; ; i++) {
111
            stringBuilder.append(jsonToString(array[i]));
112
            if (i == inMax) {
113
                return stringBuilder.append(']').toString();
114
            }
115
            stringBuilder.append(", ");
116
        }
117
    }
118
119
    private static String jsonToString(JsonSchema object) {
120
        return "JsonSchema(id=" + object.getId() + ")";
121
    }
122
}
123