2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * Copyright (C) 2023 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dcae.common.validator;
24 import org.jetbrains.annotations.NotNull;
25 import org.json.JSONObject;
26 import org.junit.Assume;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.onap.dcae.ApplicationSettings;
33 import org.onap.dcae.FileReader;
34 import org.onap.dcae.common.model.VesEvent;
35 import org.onap.dcae.restapi.ApiException;
36 import org.onap.dcae.restapi.EventValidatorException;
38 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
39 import static org.junit.jupiter.api.Assertions.assertEquals;
40 import static org.mockito.Mockito.when;
42 import java.nio.file.Paths;
44 @ExtendWith(MockitoExtension.class)
45 public class StndDefinedDataValidatorTest {
48 private ApplicationSettings settings;
49 private StndDefinedDataValidator stndDefinedDataValidator;
51 private static final String MAPPING_FILE_LOCATION = "./src/test/resources/stndDefined/schema-map.json";
52 private static final String SCHEMA_FILES_LOCATION = "./src/test/resources/stndDefined";
53 private static final String STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data";
54 private static final String SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference";
58 mockStndDefinedValidationProps();
59 StndDefinedValidatorResolver stndDefinedValidatorResolver = new StndDefinedValidatorResolver(settings);
60 stndDefinedDataValidator = new StndDefinedDataValidator(stndDefinedValidatorResolver.resolve());
64 public void shouldReturnTrueWhenEventIsValid() throws EventValidatorException {
65 Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
67 VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_valid.json");
71 assertDoesNotThrow(() -> stndDefinedDataValidator.validate(event));
75 public void shouldReturnFalseWhenEventIsInvalid() throws EventValidatorException {
77 VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_invalid.json");
81 stndDefinedDataValidator.validate(event);
82 } catch (EventValidatorException e) {
84 assertEquals(ApiException.STND_DEFINED_VALIDATION_FAILED, e.getApiException());
89 void shouldReturnErrorWhenMissingLocalSchemaReferenceInMappingFile() {
90 Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
92 VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_missing_local_schema_reference.json");
95 stndDefinedDataValidator.validate(event);
96 } catch (EventValidatorException e) {
98 assertEquals(ApiException.NO_LOCAL_SCHEMA_REFERENCE, e.getApiException());
103 void shouldReturnErrorWhenIncorrectInternalFileReference() {
104 Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
106 VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_wrong_internal_file_reference.json");
109 stndDefinedDataValidator.validate(event);
110 } catch (EventValidatorException e) {
112 assertEquals(ApiException.INCORRECT_INTERNAL_FILE_REFERENCE, e.getApiException());
117 void shouldReturnErrorWhenStndDefinedFieldsDataIsEmpty() {
119 VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_with_empty_stndDefined_fields_data.json");
122 stndDefinedDataValidator.validate(event);
123 } catch (EventValidatorException e) {
125 assertEquals(ApiException.STND_DEFINED_VALIDATION_FAILED, e.getApiException());
130 void shouldNotReturnErrorWhenValidatingInvalidEventAndStndDefinedReferenceMissing() {
132 VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_without_schema_reference.json");
136 assertDoesNotThrow(() -> stndDefinedDataValidator.validate(event));
140 private VesEvent getVesEvent(String filename) {
141 JSONObject jsonObjectEvent = getJsonObjectEvent(filename);
142 return new VesEvent(jsonObjectEvent);
145 private JSONObject getJsonObjectEvent(String fileName) {
146 String eventContent = FileReader.readFileAsString(fileName);
147 return new JSONObject(eventContent);
150 private void mockStndDefinedValidationProps() {
151 when(settings.getExternalSchemaMappingFileLocation()).thenReturn(Paths.get(MAPPING_FILE_LOCATION).toString());
152 when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH);
153 when(settings.getExternalSchemaSchemasLocation()).thenReturn(Paths.get(SCHEMA_FILES_LOCATION).toString());
154 when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH);