summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/validation/result/TestValidationResult.java
blob: d1833f0bfe9f9309722b1b84c48134c16f41b2c5 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2018-2019 European Software Marketing Ltd.
 * ================================================================================
 * 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.aai.validation.result;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.inject.Inject;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.aai.validation.exception.ValidationServiceException;
import org.onap.aai.validation.reader.EventReader;
import org.onap.aai.validation.reader.data.Entity;
import org.onap.aai.validation.result.Violation.Builder;
import org.onap.aai.validation.result.Violation.ViolationType;
import org.onap.aai.validation.test.util.TestUtil;
import org.onap.aai.validation.util.JsonUtil;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations = {"classpath:schema-ingest.properties"})
@ContextConfiguration(locations = {"classpath:validation-result/test-validation-service-beans.xml"})
public class TestValidationResult {

    static {
        System.setProperty("APP_HOME", ".");
    }

    private static final DateTimeFormatter formatter =
            DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssX").withZone(ZoneOffset.UTC);

    @Inject
    private EventReader eventReader;

    private static String vserverEvent;
    private static Entity entity;


    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        vserverEvent = TestUtil.getFileAsString(TestData.VSERVER.getFilename());
    }

    @Before
    public void setUp() throws Exception {
        entity = eventReader.getEntity(vserverEvent);
    }

    enum TestData {
        VSERVER("validation-result/vserver-create-event.json");

        private String filename;

        TestData(String filename) {
            this.filename = filename;
        }

        public String getFilename() {
            return this.filename;
        }
    }

    @Test
    public void testValidationResultWithViolationDetailsAsString() throws Exception {
        // Violation details
        Map<String, Object> violationDetails = new HashMap<>();
        violationDetails.put("attr1", "val1");
        violationDetails.put("attr2", "val2");

        ValidationResult validationResult = getValidationResult(violationDetails);
        ValidationResult transformedVr = toAndFromJson(validationResult);

        assertThatValidationResultIsValid(transformedVr);
        Violation violation = assertThatViolationIsValid(transformedVr, validationResult.getViolations().get(0));
        assertThat(violation.getViolationDetails(), is(violationDetails));
    }

    @Test
    public void testValidationResultWithViolationDetailsIncludingNull() throws Exception {
        // Violation details
        Map<String, Object> violationDetails = new HashMap<>();
        violationDetails.put("attr1", "val1");
        violationDetails.put("attr2", null);

        ValidationResult validationResult = getValidationResult(violationDetails);
        ValidationResult transformedVr = toAndFromJson(validationResult);

        // Check
        assertThatValidationResultIsValid(transformedVr);
        Violation violation = assertThatViolationIsValid(transformedVr, validationResult.getViolations().get(0));
        assertThat(violation.getViolationDetails(), is(violationDetails));
    }

    @Test
    public void testValidationResultWithViolationDetailsAsList() throws Exception {
        // Violation details
        Map<String, Object> violationDetails = new HashMap<>();
        violationDetails.put("attr1", Arrays.asList("val1", "val2"));
        violationDetails.put("attr2", Arrays.asList("val3", "val4"));

        ValidationResult validationResult = getValidationResult(violationDetails);
        ValidationResult transformedVr = toAndFromJson(validationResult);

        // Check
        assertThatValidationResultIsValid(transformedVr);
        Violation violation = assertThatViolationIsValid(transformedVr, validationResult.getViolations().get(0));
        assertThat(violation.getViolationDetails(), is(violationDetails));
    }

    @Test
    public void testValidationResultWithViolationDetailsAsInt() throws Exception {
        // Violation details
        Map<String, Object> violationDetails = new HashMap<>();
        violationDetails.put("attr1", 1);
        violationDetails.put("attr2", 2);

        ValidationResult validationResult = getValidationResult(violationDetails);
        ValidationResult transformedVr = toAndFromJson(validationResult);

        // Check
        assertThatValidationResultIsValid(transformedVr);
        Violation violation = assertThatViolationIsValid(transformedVr, validationResult.getViolations().get(0));
        assertThat(violation.getViolationDetails().get("attr1"), is(1.0));
        assertThat(violation.getViolationDetails().get("attr2"), is(2.0));
    }

    @Test
    public void testValidationResultWithViolationDetailsAsObject() throws Exception {
        // Violation details
        JsonArray jsonArray = new JsonArray();
        jsonArray.add("v1");
        JsonObject jsonObject = new JsonObject();
        jsonObject.add("p1", jsonArray);
        jsonObject.add("p2", jsonArray);
        Map<String, Object> violationDetails = new HashMap<>();
        violationDetails.put("attr1", jsonObject);

        ValidationResult validationResult = getValidationResult(violationDetails);
        ValidationResult transformedVr = toAndFromJson(validationResult);

        // Check
        assertThatValidationResultIsValid(transformedVr);
        Violation violation = assertThatViolationIsValid(transformedVr, validationResult.getViolations().get(0));
        String jsonDetails = violation.getViolationDetails().get("attr1").toString();
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement = jsonParser.parse(jsonDetails);
        assertThat(jsonObject, is(jsonElement));
    }

    @Test
    public void testCompareObjects() throws Exception {
        ValidationResultBuilder builder = new ValidationResultBuilder(eventReader, vserverEvent);
        ValidationResult validationResult = builder.build();
        assertThat(validationResult, is(not(equalTo(null))));

        validationResult.setEntityId(new JsonObject());
        assertThat(validationResult, is(not(equalTo(null))));

        ValidationResult other = builder.build();
        assertThat(validationResult, is(not(equalTo(other))));

        validationResult.setEntityType("type");
        assertThat(validationResult, is(not(equalTo(other))));

        Map<String, Object> violationDetails = new HashMap<>();

        //@formatter:off
        Violation violation = new Violation.Builder(entity)
                .category("category")
                .severity("severity")
                .violationType("violationType")
                .violationDetails(violationDetails)
                .errorMessage("errorMessage")
                .build();
        //@formatter:on

        validationResult.addViolation(violation);
        assertThat(validationResult, is(not(equalTo(other))));

        // Force call to hashCode()
        assertThat(validationResult.hashCode(), is(not(equalTo(other.hashCode()))));

    }

    /**
     * Tests for comparing two Violation objects. The generated Violation ID must be deterministic.
     *
     * @throws Exception
     */
    @Test
    public void testCompareViolationObjects() throws Exception {
        // Use the standard vserver event
        Builder builder = new Violation.Builder(entity);

        // Force call to toString() for coverage only
        assertThat(builder.toString(), is(equalTo(new Violation.Builder(entity).toString())));

        // Build a blank violation
        Violation violation = builder.build();

        // Identity tests
        assertThat(violation, is(not(equalTo(null))));
        assertThat(violation, is(not(equalTo(1))));
        assertThat(violation, is(equalTo(violation)));

        // Ensure that any violations we build are identical
        testViolationIdsForEquality(builder, builder, true);

        // Create a copy of the vserver event and vary the resourceVersion
        Entity entity2 = eventReader
                .getEntity(vserverEvent.replaceFirst("resource-version\": \"1464193654", "resource-version\": \"123"));

        // The violationId produced for identically built violations is the same for each builder (although the vserver
        // events differ).
        testViolationIdsForEquality(new Violation.Builder(entity), new Violation.Builder(entity2), true);

        // The violationId produced must differ whenever the violation values differ.
        testViolationIdsForInequality(new Violation.Builder(entity), new Violation.Builder(entity2));

        // Make a new variant of the vserver event using a different entity Id
        Entity entity3 = eventReader.getEntity(
                vserverEvent.replaceFirst("vserver-id\": \"example-vserver-id-val-34666", "vserver-id\": \"123"));

        // The violationId produced for identically built violations is now different for each builder (because the
        // entity Ids differ).
        testViolationIdsForEquality(new Violation.Builder(entity), new Violation.Builder(entity3), false);
    }

    /**
     * Generate various violations using the supplied builders and assert the expected equality of the generated
     * Violation IDs whenever the values supplied to the builders are the same.
     *
     * @param b1
     *            a builder
     * @param b2
     *            another builder
     * @param expectedResult
     *            whether or not the two builders should produce identical violations
     * @throws ValidationServiceException
     */
    private void testViolationIdsForEquality(Builder b1, Builder b2, Boolean expectedResult)
            throws ValidationServiceException {
        Violation v1 = b1.build();
        Violation v2 = b2.build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);

        // Use the same category
        String category = "INVALID OBJ";
        v1 = b1.category(category).build();
        v2 = b2.category(category).build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);

        // Use the same severity
        String severity = "CRITICAL";
        v1 = b1.severity(severity).build();
        v2 = b2.severity(severity).build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);

        // Use the same violation type
        v1 = b1.violationType(ViolationType.RULE).build();
        v2 = b2.violationType(ViolationType.RULE).build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);

        // Use the same validation rule
        String rule = "prov-status";
        v1 = b1.validationRule(rule).build();
        v2 = b2.validationRule(rule).build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);

        // Use the same error message
        String errorMessage = "Invalid prov-status value. Must have a value not equal to ACTIVE/active.";
        v1 = b1.errorMessage(errorMessage).build();
        v2 = b2.errorMessage(errorMessage).build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);

        // Use the same violation details
        Map<String, Object> details = new HashMap<>();
        details.put(rule, "ACTIVE");
        v1 = b1.violationDetails(details).build();
        v2 = b2.violationDetails(details).build();
        assertThatViolationsAreEqual(v1, v2, expectedResult);
    }

    /**
     * Generate violations using the supplied builders and assert that the generated Violation IDs differ whenever the
     * values supplied to the builders differ.
     *
     * @param builder
     * @param builder2
     * @throws ValidationServiceException
     */
    private void testViolationIdsForInequality(Builder builder, Builder builder2) throws ValidationServiceException {
        Violation violation;
        Violation other;

        // Vary the violation type
        violation = builder.violationType("").build();
        other = builder2.violationType(ViolationType.RULE).build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.violationType(ViolationType.NONE).build();
        other = builder2.violationType(ViolationType.RULE).build();
        assertThatViolationIdsDiffer(violation, other);

        // Vary the validation rule
        violation = builder.validationRule(null).build();
        other = builder2.validationRule("rule").build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.validationRule("rule1").build();
        other = builder2.validationRule(null).build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.validationRule("rule1").build();
        other = builder2.validationRule("rule2").build();
        assertThatViolationIdsDiffer(violation, other);

        // Vary the category
        violation = builder.category(null).build();
        other = builder2.category("category").build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.category("category").build();
        other = builder2.category(null).build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.category("category1").build();
        other = builder2.category("category2").build();
        assertThatViolationIdsDiffer(violation, other);

        // Vary the error message
        violation = builder.validationRule("rule").build();
        other = builder2.validationRule("rule").errorMessage("message2").build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.validationRule("rule").errorMessage("message1").build();
        other = builder2.validationRule("rule").errorMessage("message2").build();
        assertThatViolationIdsDiffer(violation, other);

        // Vary the severity
        violation = builder.errorMessage("msg").build();
        other = builder2.errorMessage("msg").severity("sev2").build();
        assertThatViolationIdsDiffer(violation, other);

        violation = builder.errorMessage("msg").severity("sev1").build();
        other = builder2.errorMessage("msg").severity("sev2").build();
        assertThatViolationIdsDiffer(violation, other);
    }

    private ValidationResult getValidationResult(Map<String, Object> violationDetails)
            throws ValidationServiceException {
        ValidationResult validationResult = new ValidationResultBuilder(eventReader, vserverEvent).build();

        //@formatter:off
        Violation violation = new Violation.Builder(entity)
                .category("category")
                .severity("severity")
                .violationType("violationType")
                .violationDetails(violationDetails)
                .errorMessage("errorMessage")
                .build();
        //@formatter:on

        validationResult.addViolation(violation);

        return validationResult;
    }

    private ValidationResult toAndFromJson(ValidationResult validationResult) {
        return JsonUtil.toAnnotatedClassfromJson(validationResult.toJson(), ValidationResultImpl.class);
    }

    private void assertThatValidationResultIsValid(ValidationResult validationResult) {
        assertTrue("Expected valid UUID", isValidEventId(validationResult.getValidationId()));
        assertIsValidTimestamp(validationResult.getValidationTimestamp());
        JsonObject expectedEntityId = new JsonObject();
        expectedEntityId.addProperty("vserver-id", "example-vserver-id-val-34666");
        assertThat(validationResult.getEntityId(), is(expectedEntityId));
        assertThat(validationResult.getEntityType(), is("vserver"));
        assertThat(validationResult.getResourceVersion(), is("1464193654"));
        assertThat(validationResult.getEntityLink(),
                is("cloud-infrastructure/cloud-regions/cloud-region/region1/"
                        + "AAIregion1/tenants/tenant/example-tenant-id-val-88551"
                        + "/vservers/vserver/example-vserver-id-val-34666"));
    }

    private Violation assertThatViolationIsValid(ValidationResult validationResult, Violation expectedViolation) {
        Violation violation = validationResult.getViolations().get(0);
        assertThat(violation.getViolationId(), is(expectedViolation.getViolationId()));
        assertThat(violation.getCategory(), is("category"));
        assertThat(violation.getSeverity(), is("severity"));
        assertThat(violation.getViolationType(), is("violationType"));
        assertThat(violation.getErrorMessage(), is("errorMessage"));
        return violation;
    }

    private void assertThatViolationsAreEqual(Violation v1, Violation v2, Boolean expectedResult) {
        assertThat("Violation equality in error:\n" + v1 + " equals " + v2, v1.equals(v2), is(expectedResult));
        assertThat("Violation ID equality in error:\n" + v1.getViolationId() + " equals " + v2.getViolationId(),
                v1.getViolationId().equals(v2.getViolationId()), is(expectedResult));
        // Force a call to toString() for code coverage only
        assertThat(v1.toString().equals(v2.toString()), is(equalTo(expectedResult)));
    }

    private void assertThatViolationIdsDiffer(Violation violation, Violation other) {
        assertThat(violation.getViolationId(), is(not(equalTo(other.getViolationId()))));
        assertThat(violation, is(not(equalTo(other))));
    }

    private boolean isValidEventId(String eventId) {
        try {
            UUID.fromString(eventId);
            return true;
        } catch (IllegalArgumentException exception) {
            return false;
        }
    }

    private void assertIsValidTimestamp(String date) {
        Instant.from(formatter.parse(date));
    }
}