2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.gab.yaml;
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;
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;
36 class GABYamlParserTest {
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";
62 void shouldNotParseAnyYamlAndGenerateEmptyMapOfKeys() throws Exception {
64 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
65 result = yamlParser.filter(TEST).collect();
67 assertResultIsEmpty(result);
71 void shouldParseNonexistentYamlAndGenerateEmptyMapOfKeys() throws Exception {
73 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
74 result = yamlParser.parseFile(NONEXISTENT_FILE).collect();
76 assertResultIsEmpty(result);
80 void shouldParseExistentFileYamlAndGenerateEmptyMapOfKeysIfNoKeywordsSearched() throws Exception {
82 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
83 result = yamlParser.parseFile(FAULT_REGISTRATION_YML).collect();
85 assertResultIsEmpty(result);
89 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnSingleFilter() throws Exception {
91 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
92 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
93 .filter(EVENT_PRESENCE).filter(EVENT_PRESENCE).collect();
96 assertRowsSize(result, 5);
97 assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
101 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoIndependentFilters() throws Exception {
103 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
104 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
105 .filter(DOMAIN_VALUE)
106 .filter(EVENT_PRESENCE)
110 assertRowsSize(result, 5);
111 assertThatEntryIsEqualTo(result, 0 , 0, DOMAIN_VALUE, FAULT);
112 assertThatEntryIsEqualTo(result, 4 , 0, DOMAIN_VALUE, SYSLOG);
116 void shouldParsePMDictionaryInJsonAndGenerateMapOfKeysOnTwoFilters() throws Exception {
118 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
119 result = yamlParser.parseFile(PM_DICT_YML_IN_JSON)
121 .filter(MEAS_DESCRIPTION)
124 assertRowsSize(result, 3);
125 assertThatEntryIsEqualTo(result, 0 , 0, MEAS_TYPE, MEAS_TYPE_VALUE);
126 assertThatEntryIsEqualTo(result, 0 , 1, MEAS_DESCRIPTION, MEAS_DESCRIPTION_VALUE);
130 void shouldParsePMDictionaryAndGenerateMapOfKeysOnTwoFilters() throws Exception {
132 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
133 result = yamlParser.parseFile(PM_DICT_YML)
135 .filter(MEAS_DESCRIPTION)
138 assertRowsSize(result, 3);
139 assertThatEntryIsEqualTo(result, 0 , 0, MEAS_TYPE, MEAS_TYPE_VALUE);
140 assertThatEntryIsEqualTo(result, 0 , 1, MEAS_DESCRIPTION, MEAS_DESCRIPTION_VALUE);
144 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoDependentFilters() throws Exception {
146 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
147 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
148 .filter(Sets.newHashSet(DOMAIN_VALUE, EVENT_PRESENCE))
152 assertRowsSize(result, 5);
153 assertThatEntryIsEqualTo(result, 0, 0, DOMAIN_VALUE, FAULT);
154 assertThatEntryIsEqualTo(result, 4, 0, DOMAIN_VALUE, SYSLOG);
158 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnThreeCombinedFilters() throws Exception {
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)
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);
181 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnFilterFromArray() throws Exception {
183 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
184 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
185 .filter(Sets.newHashSet(EVENT_HEARTBEAT_ACTION, EVENT_PRESENCE))
188 assertRowsSize(result, 5);
189 assertThatEntryIsEqualTo(result,0,0, EVENT_PRESENCE, REQUIRED);
190 assertThatEntryIsEqualTo(result,2,1, EVENT_HEARTBEAT_ACTION, VNF_DOWN);
194 void shouldParseFaultRegistrationAndGenerateMapOfKeysRemovingInvalidKey() throws Exception {
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))
201 assertRowsSize(result, 5);
202 assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
206 void shouldParseInvalidYamlAndGenerateIOException() {
207 assertThrows(IOException.class, () -> {
208 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
209 yamlParser.parseFile(INVALID_YML)
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)));
221 private void assertResultIsEmpty(GABResults result) {
222 assertThat(result.getRows(), is(equalTo(Collections.<GABResult>emptyList())));
225 private void assertRowsSize(GABResults results, int size){
226 assertThat(results.getRows().size(), is(equalTo(size)));