Passed
Push — master ( d91c1f...3f9a31 )
by Filip
03:06
created

validate(JsonSchema,JsonSchema)   C

Complexity

Conditions 9

Size

Total Lines 60
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 45
c 0
b 0
f 0
dl 0
loc 60
rs 6.4666

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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