Passed
Pull Request — master (#72)
by Filip
02:32
created

arraysEquals(T[],T[],BiPredicate)   B

Complexity

Conditions 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
dl 0
loc 17
rs 8.6666
1
package com.hltech.vaunt.validator.schema;
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
import java.util.Objects;
9
import java.util.Set;
10
import java.util.function.BiPredicate;
11
import java.util.stream.Collectors;
12
13
public abstract class JsonSchemaValidator {
14
    static final String ERROR_FORMAT = "Schema with id %s has not matching %s - consumer: %s, provider: %s";
15
    static final String ERROR_FORMAT_SHORT = "Schema with id %s has not matching %s";
16
17
    public abstract Class<?> supportsSchemaType();
18
19
    public List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) {
20
        List<String> errors = new ArrayList<>();
21
22
        if (!equals(consumerSchema.get$ref(), providerSchema.get$ref())) {
23
            errors.add(String.format(ERROR_FORMAT,
24
                    consumerSchema.getId(),
25
                    "$ref",
26
                    consumerSchema.get$ref(),
27
                    providerSchema.get$ref()));
28
        }
29
30
        if (!equals(consumerSchema.get$schema(), providerSchema.get$schema())) {
31
            errors.add(String.format(ERROR_FORMAT,
32
                    consumerSchema.getId(),
33
                    "$schema",
34
                    consumerSchema.get$schema(),
35
                    providerSchema.get$schema()));
36
        }
37
38
        if (!arraysEquals(consumerSchema.getDisallow(), providerSchema.getDisallow(), Object::equals)) {
39
            errors.add(String.format(ERROR_FORMAT,
40
                    consumerSchema.getId(),
41
                    "disallow",
42
                    jsonArrayToString(consumerSchema.getDisallow()),
43
                    jsonArrayToString(providerSchema.getDisallow())));
44
        }
45
46
        if (!arraysEquals(consumerSchema.getExtends(), providerSchema.getExtends(), Object::equals)) {
47
            errors.add(String.format(ERROR_FORMAT,
48
                    consumerSchema.getId(),
49
                    "extends",
50
                    jsonArrayToString(consumerSchema.getExtends()),
51
                    jsonArrayToString(providerSchema.getExtends())));
52
        }
53
54
        if (isRequired(consumerSchema) && !isRequired(providerSchema)) {
55
            errors.add(String.format(ERROR_FORMAT,
56
                    consumerSchema.getId(),
57
                    "required",
58
                    consumerSchema.getRequired(),
59
                    providerSchema.getRequired()));
60
        }
61
62
        if (!equals(consumerSchema.getReadonly(), providerSchema.getReadonly())) {
63
            errors.add(String.format(ERROR_FORMAT,
64
                    consumerSchema.getId(),
65
                    "readonly",
66
                    consumerSchema.getReadonly(),
67
                    providerSchema.getReadonly()));
68
        }
69
70
        if (!equals(consumerSchema.getDescription(), providerSchema.getDescription())) {
71
            errors.add(String.format(ERROR_FORMAT,
72
                    consumerSchema.getId(),
73
                    "description",
74
                    consumerSchema.getDescription(),
75
                    providerSchema.getDescription()));
76
        }
77
78
        return errors;
79
    }
80
81
    private boolean isRequired(JsonSchema schema) {
82
        return schema.getRequired() != null && schema.getRequired();
83
    }
84
85
    boolean equals(Object object1, Object object2) {
86
        return Objects.equals(object1, object2);
87
    }
88
89
    <T> boolean arraysEquals(T[] array1, T[] array2, BiPredicate<T, T> checker) {
90
        if (array1 == null) {
91
            return array2 == null;
92
        }
93
        if (array2 == null) {
94
            return false;
95
        }
96
        int len = array1.length;
97
        if (len != array2.length) {
98
            return false;
99
        }
100
        for (int i = 0; i < len; ++i) {
101
            if (!checker.test(array1[i], array2[i])) {
102
                return false;
103
            }
104
        }
105
        return true;
106
    }
107
108
    private String jsonArrayToString(JsonSchema[] array) {
109
        String content = Arrays.stream(array)
110
                .map(this::jsonToString)
111
                .collect(Collectors.joining(", "));
112
113
        return "[" + content + "]";
114
    }
115
116
    String jsonToString(JsonSchema object) {
117
        return "JsonSchema(id=" + object.getId() + ")";
118
    }
119
120
    boolean isEnumValid(Set<String> consumerEnums, Set<String> providerEnums) {
121
        if (representsString(consumerEnums) && representsEnum(providerEnums)) {
122
            return false;
123
        }
124
125
        if (representsEnum(consumerEnums) && representsEnum(providerEnums)) {
126
            return providerEnums.containsAll(consumerEnums);
127
        }
128
129
        return true;
130
    }
131
132
    private boolean representsEnum(Set<String> enums) {
133
        return enums.size() > 0;
134
    }
135
136
    private boolean representsString(Set<String> enums) {
137
        return enums.size() == 0;
138
    }
139
}
140