isEnumValid(Set,Set)   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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