PM_Dictionary Support in GAB
[sdc.git] / common / onap-generic-artifact-browser / onap-generic-artifact-browser-service / src / test / java / org / onap / sdc / gab / yaml / GABYamlParserTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * GAB
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.gab.yaml;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.equalTo;
25 import static org.hamcrest.Matchers.is;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27
28 import com.google.common.collect.Sets;
29 import java.io.IOException;
30 import java.util.Collections;
31 import org.junit.jupiter.api.Test;
32 import org.onap.sdc.gab.model.GABResult;
33 import org.onap.sdc.gab.model.GABResultEntry;
34 import org.onap.sdc.gab.model.GABResults;
35
36 class GABYamlParserTest {
37
38     private static final String EVENT_PRESENCE = "event.presence";
39     private static final String REQUIRED = "required";
40     private static final String INVALID_EVENT_HEARTBEAT_ACTION = "event.heartbeatAction.[1]";
41     private static final String EVENT_HEARTBEAT_ACTION = "event.heartbeatAction[1]";
42     private static final String FAULT = "fault";
43     private static final String DOMAIN_VALUE = "event.structure.commonEventHeader.structure.domain.value";
44     private static final String VNF_DOWN = "vnfDown";
45     private static final String SYSLOG = "syslog";
46     private static final String EVENT_STRUCTURE_HEARTBEAT_FIELDS_PRESENCE = "event.structure.heartbeatFields.presence";
47     private static final String HEARTBEAT = "heartbeat";
48     private static final String MEASUREMENTS_FOR_VF_SCALING = "measurementsForVfScaling";
49     private static final String EVENT = "event";
50     private static final String TEST = "test";
51     private static final String NONEXISTENT_FILE = "nonexistent.file";
52     private static final String FAULT_REGISTRATION_YML = "yaml/faultRegistration.yml";
53     private static final String PM_DICT_YML_IN_JSON = "yaml/pmDictionaryInJson.yml";
54     private static final String PM_DICT_YML = "yaml/pmDictionary.yml";
55     private static final String MEAS_TYPE = "pmMetaData.pmFields.measType";
56     private static final String MEAS_TYPE_VALUE = "VS.NINFC.IntraFrPscelChAttempt";
57     private static final String MEAS_DESCRIPTION = "pmMetaData.pmFields.measDescription";
58     private static final String MEAS_DESCRIPTION_VALUE = "This counter indicates the number of intra gNB intra frequency PSCell change attempts.";
59     private static final String INVALID_YML = "yaml/invalid.yml";
60
61     @Test
62     void shouldNotParseAnyYamlAndGenerateEmptyMapOfKeys() throws Exception {
63         GABResults result;
64         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
65             result = yamlParser.filter(TEST).collect();
66         }
67         assertResultIsEmpty(result);
68     }
69
70     @Test
71     void shouldParseNonexistentYamlAndGenerateEmptyMapOfKeys() throws Exception {
72         GABResults result;
73         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
74             result = yamlParser.parseFile(NONEXISTENT_FILE).collect();
75         }
76         assertResultIsEmpty(result);
77     }
78
79     @Test
80     void shouldParseExistentFileYamlAndGenerateEmptyMapOfKeysIfNoKeywordsSearched() throws Exception {
81         GABResults result;
82         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
83             result = yamlParser.parseFile(FAULT_REGISTRATION_YML).collect();
84         }
85         assertResultIsEmpty(result);
86     }
87
88     @Test
89     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnSingleFilter() throws Exception {
90         GABResults result;
91         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
92             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
93                 .filter(EVENT_PRESENCE).filter(EVENT_PRESENCE).collect();
94
95         }
96         assertRowsSize(result, 5);
97         assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
98     }
99
100     @Test
101     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoIndependentFilters() throws Exception {
102         GABResults result;
103         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
104             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
105                 .filter(DOMAIN_VALUE)
106                 .filter(EVENT_PRESENCE)
107                 .collect();
108
109         }
110         assertRowsSize(result, 5);
111         assertThatEntryIsEqualTo(result, 0 , 0, DOMAIN_VALUE, FAULT);
112         assertThatEntryIsEqualTo(result, 4 , 0, DOMAIN_VALUE, SYSLOG);
113     }
114
115     @Test
116     void shouldParsePMDictionaryInJsonAndGenerateMapOfKeysOnTwoFilters() throws Exception {
117         GABResults result;
118         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
119             result = yamlParser.parseFile(PM_DICT_YML_IN_JSON)
120                 .filter(MEAS_TYPE)
121                 .filter(MEAS_DESCRIPTION)
122                 .collect();
123         }
124         assertRowsSize(result, 3);
125         assertThatEntryIsEqualTo(result, 0 , 0, MEAS_TYPE, MEAS_TYPE_VALUE);
126         assertThatEntryIsEqualTo(result, 0 , 1, MEAS_DESCRIPTION, MEAS_DESCRIPTION_VALUE);
127     }
128
129     @Test
130     void shouldParsePMDictionaryAndGenerateMapOfKeysOnTwoFilters() throws Exception {
131         GABResults result;
132         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
133             result = yamlParser.parseFile(PM_DICT_YML)
134                 .filter(MEAS_TYPE)
135                 .filter(MEAS_DESCRIPTION)
136                 .collect();
137         }
138         assertRowsSize(result, 3);
139         assertThatEntryIsEqualTo(result, 0 , 0, MEAS_TYPE, MEAS_TYPE_VALUE);
140         assertThatEntryIsEqualTo(result, 0 , 1, MEAS_DESCRIPTION, MEAS_DESCRIPTION_VALUE);
141     }
142
143     @Test
144     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoDependentFilters() throws Exception {
145         GABResults result;
146         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
147             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
148                 .filter(Sets.newHashSet(DOMAIN_VALUE, EVENT_PRESENCE))
149                 .collect();
150
151         }
152         assertRowsSize(result, 5);
153         assertThatEntryIsEqualTo(result, 0, 0, DOMAIN_VALUE, FAULT);
154         assertThatEntryIsEqualTo(result, 4, 0, DOMAIN_VALUE, SYSLOG);
155     }
156
157     @Test
158     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnThreeCombinedFilters() throws Exception {
159         GABResults result;
160         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
161             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
162                 .filter(Sets.newHashSet(DOMAIN_VALUE, EVENT_PRESENCE))
163                 .filter(EVENT_STRUCTURE_HEARTBEAT_FIELDS_PRESENCE)
164                 .collect();
165
166         }
167         assertRowsSize(result, 5);
168         assertThatEntryIsEqualTo(result, 0,0, DOMAIN_VALUE, FAULT);
169         assertThatEntryIsEqualTo(result, 0,1, EVENT_PRESENCE, REQUIRED);
170         assertThatEntryIsEqualTo(result, 1,0, DOMAIN_VALUE, FAULT);
171         assertThatEntryIsEqualTo(result, 1,1, EVENT_PRESENCE, REQUIRED);
172         assertThatEntryIsEqualTo(result, 2,0, DOMAIN_VALUE, HEARTBEAT);
173         assertThatEntryIsEqualTo(result, 2,1, EVENT_PRESENCE, REQUIRED);
174         assertThatEntryIsEqualTo(result, 3,0, DOMAIN_VALUE, MEASUREMENTS_FOR_VF_SCALING);
175         assertThatEntryIsEqualTo(result, 3,1, EVENT_PRESENCE, REQUIRED);
176         assertThatEntryIsEqualTo(result, 4,0, DOMAIN_VALUE, SYSLOG);
177         assertThatEntryIsEqualTo(result, 4,1, EVENT_PRESENCE, REQUIRED);
178     }
179
180     @Test
181     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnFilterFromArray() throws Exception {
182         GABResults result;
183         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
184             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
185                 .filter(Sets.newHashSet(EVENT_HEARTBEAT_ACTION, EVENT_PRESENCE))
186                 .collect();
187         }
188         assertRowsSize(result, 5);
189         assertThatEntryIsEqualTo(result,0,0, EVENT_PRESENCE, REQUIRED);
190         assertThatEntryIsEqualTo(result,2,1, EVENT_HEARTBEAT_ACTION, VNF_DOWN);
191     }
192
193     @Test
194     void shouldParseFaultRegistrationAndGenerateMapOfKeysRemovingInvalidKey() throws Exception {
195         GABResults result;
196         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
197             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
198                 .filter(Sets.newHashSet(INVALID_EVENT_HEARTBEAT_ACTION, EVENT_PRESENCE))
199                 .collect();
200         }
201         assertRowsSize(result, 5);
202         assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
203     }
204
205     @Test
206     void shouldParseInvalidYamlAndGenerateIOException() {
207         assertThrows(IOException.class, () -> {
208         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
209             yamlParser.parseFile(INVALID_YML)
210                 .filter(EVENT)
211                 .collect();
212         }});
213     }
214
215     private void assertThatEntryIsEqualTo(GABResults result, int rowIndex, int entryIndex, String path, String data){
216         GABResultEntry entry = result.getRows().get(rowIndex).getEntries().get(entryIndex);
217         assertThat(entry.getData(), is(equalTo(data)));
218         assertThat(entry.getPath(), is(equalTo(path)));
219     }
220
221     private void assertResultIsEmpty(GABResults result) {
222         assertThat(result.getRows(), is(equalTo(Collections.<GABResult>emptyList())));
223     }
224
225     private void assertRowsSize(GABResults results, int size){
226         assertThat(results.getRows().size(), is(equalTo(size)));
227     }
228
229 }