aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java
blob: e5e2117717e1f6ae5c333d52f38c9c53a3985f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dcaegen2.collectors.ves
 * ================================================================================
 * Copyright (C) 2020 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcae.common.validator;

import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.dcae.ApplicationSettings;
import org.onap.dcae.FileReader;
import org.onap.dcae.common.model.VesEvent;
import org.onap.dcae.restapi.ApiException;
import org.onap.dcae.restapi.EventValidatorException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class GeneralEventValidatorTest {
    private static final String DUMMY_SCHEMA_VERSION = "v5";
    private static final String DUMMY_TYPE = "type";
    private final String newSchemaV7 = FileReader.readFileAsString("etc/CommonEventFormat_30.2_ONAP.json");
    private JSONObject sentEvent;
    private static final String V7_VERSION = "v7";
    private static JSONObject jsonObject;
    private static final String EVENT_TYPE = "event";

    @Mock
    private ApplicationSettings settings;

    private SchemaValidator schemaValidator = Mockito.spy( new SchemaValidator());

    private GeneralEventValidator sut;


    @BeforeAll
    static void setupTests() {
        jsonObject = new JSONObject("{" + DUMMY_TYPE + ":dummy}");
    }

    @BeforeEach
    public void setUp(){
        this.sut = new GeneralEventValidator(settings, schemaValidator);
    }

    @Test
    void shouldNotValidateEventWhenJsonSchemaValidationDisabled() throws EventValidatorException {
        //given
        when(settings.eventSchemaValidationEnabled()).thenReturn(false);

        //when
        this.sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION);

        //then
        verify(schemaValidator, never()).conformsToSchema(any(), any());

    }

    @Test
    void shouldReturnInvalidJsonErrorOnWrongType() {
        //given
        when(settings.eventSchemaValidationEnabled()).thenReturn(true);

        //when
        try {
            sut.validate(new VesEvent(jsonObject), "wrongType", DUMMY_SCHEMA_VERSION);
        } catch (EventValidatorException e) {
            //then
            Assertions.assertEquals(ApiException.INVALID_JSON_INPUT, e.getApiException());
        }


    }

    @Test
    void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() {
        //given
        String schemaRejectingEverything = "{\"not\":{}}";
        mockJsonSchema(schemaRejectingEverything);
        when(settings.eventSchemaValidationEnabled()).thenReturn(true);

        //when
        try {
            sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION);
        } catch (EventValidatorException e) {
            //then
            assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, e.getApiException());
        }

    }

    @Test
    void shouldReturnEmptyOptionalOnValidJsonObjectSchema() {
        //given
        String schemaAcceptingEverything = "{}";
        mockJsonSchema(schemaAcceptingEverything);
        when(settings.eventSchemaValidationEnabled()).thenReturn(true);

        //when
        try {
            sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION);
        } catch (EventValidatorException e) {
            failWithError();
        }
    }

    @Test
    public void shouldReturnNoErrorsWhenValidating30_1_1ValidEvent() {
        //given
        sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_30_1_1_event.json"));

        mockJsonSchema(newSchemaV7);
        when(settings.eventSchemaValidationEnabled()).thenReturn(true);

        //when
        try {
            sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION);
        } catch (EventValidatorException e) {
            failWithError();
        }
    }

    @Test
    void shouldReturnNoErrorsWhenValidatingValidEventWithStndDefinedFields() {
        //given
        sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves_stdnDefined_valid.json"));

        mockJsonSchema(newSchemaV7);
        when(settings.eventSchemaValidationEnabled()).thenReturn(true);

        //when
        try {
            sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION);
        } catch (EventValidatorException e) {
            failWithError();
        }
    }

    @Test
    void shouldReturnSchemaValidationFailedWhenValidating30_1_1InvalidEvent() {
        //given
        sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_invalid_30_1_1_event.json"));

        mockJsonSchema(newSchemaV7);
        when(settings.eventSchemaValidationEnabled()).thenReturn(true);

        //when
        try {
            sut.validate(new VesEvent(this.sentEvent), EVENT_TYPE, V7_VERSION);
        } catch (EventValidatorException e) {
            //then
            assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, e.getApiException());
        }


    }

    private void failWithError() {
        fail("Validation should not report any error!");
    }

    private void mockJsonSchema(String jsonSchemaContent) {
        JsonSchemaFactory factory = JsonSchemaFactory.getInstance();

        JsonSchema schema = factory.getSchema(jsonSchemaContent);
        when(settings.jsonSchema(any())).thenReturn(schema);
    }
}