VESCollector Test optimization
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / validator / StndDefinedDataValidatorTest.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.dcae.common.validator;
23
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;
37
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;
41
42 import java.nio.file.Paths;
43
44 @ExtendWith(MockitoExtension.class)
45 public class StndDefinedDataValidatorTest {
46
47     @Mock
48     private ApplicationSettings settings;
49     private StndDefinedDataValidator stndDefinedDataValidator;
50
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";
55
56     @BeforeEach
57     public void setUp() {
58         mockStndDefinedValidationProps();
59         StndDefinedValidatorResolver stndDefinedValidatorResolver = new StndDefinedValidatorResolver(settings);
60         stndDefinedDataValidator = new StndDefinedDataValidator(stndDefinedValidatorResolver.resolve());
61     }
62
63     @Test
64     public void shouldReturnTrueWhenEventIsValid() throws EventValidatorException {
65         Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
66         //given
67         VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_valid.json");
68
69         //when
70         //then
71         assertDoesNotThrow(() -> stndDefinedDataValidator.validate(event));
72     }
73
74     @Test
75     public void shouldReturnFalseWhenEventIsInvalid() throws EventValidatorException {
76         //given
77         VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_invalid.json");
78
79         try {
80             //when
81             stndDefinedDataValidator.validate(event);
82         } catch (EventValidatorException e) {
83             //then
84             assertEquals(ApiException.STND_DEFINED_VALIDATION_FAILED, e.getApiException());
85         }
86     }
87
88     @Test
89     void shouldReturnErrorWhenMissingLocalSchemaReferenceInMappingFile() {
90         Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
91         //given
92         VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_missing_local_schema_reference.json");
93         try {
94             //when
95             stndDefinedDataValidator.validate(event);
96         } catch (EventValidatorException e) {
97             //then
98             assertEquals(ApiException.NO_LOCAL_SCHEMA_REFERENCE, e.getApiException());
99         }
100     }
101
102     @Test
103     void shouldReturnErrorWhenIncorrectInternalFileReference() {
104         Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
105         //given
106         VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_wrong_internal_file_reference.json");
107         try {
108             //when
109             stndDefinedDataValidator.validate(event);
110         } catch (EventValidatorException e) {
111             //then
112             assertEquals(ApiException.INCORRECT_INTERNAL_FILE_REFERENCE, e.getApiException());
113         }
114     }
115
116     @Test
117     void shouldReturnErrorWhenStndDefinedFieldsDataIsEmpty() {
118         //given
119         VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_with_empty_stndDefined_fields_data.json");
120         try {
121             //when
122             stndDefinedDataValidator.validate(event);
123         } catch (EventValidatorException e) {
124             //then
125             assertEquals(ApiException.STND_DEFINED_VALIDATION_FAILED, e.getApiException());
126         }
127     }
128
129     @Test
130     void shouldNotReturnErrorWhenValidatingInvalidEventAndStndDefinedReferenceMissing() {
131         //given
132         VesEvent event = getVesEvent("src/test/resources/ves_stdnDefined_without_schema_reference.json");
133         
134         //when
135         //then
136         assertDoesNotThrow(() -> stndDefinedDataValidator.validate(event));
137     }
138
139     @NotNull
140     private VesEvent getVesEvent(String filename) {
141             JSONObject jsonObjectEvent = getJsonObjectEvent(filename);
142             return new VesEvent(jsonObjectEvent);
143     }
144
145     private JSONObject getJsonObjectEvent(String fileName) {
146         String eventContent = FileReader.readFileAsString(fileName);
147         return new JSONObject(eventContent);
148     }
149
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);
155     }
156 }