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 INVALID_YML = "yaml/invalid.yml";
56 void shouldNotParseAnyYamlAndGenerateEmptyMapOfKeys() throws Exception {
58 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
59 result = yamlParser.filter(TEST).collect();
61 assertResultIsEmpty(result);
65 void shouldParseNonexistentYamlAndGenerateEmptyMapOfKeys() throws Exception {
67 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
68 result = yamlParser.parseFile(NONEXISTENT_FILE).collect();
70 assertResultIsEmpty(result);
74 void shouldParseExistentFileYamlAndGenerateEmptyMapOfKeysIfNoKeywordsSearched() throws Exception {
76 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
77 result = yamlParser.parseFile(FAULT_REGISTRATION_YML).collect();
79 assertResultIsEmpty(result);
83 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnSingleFilter() throws Exception {
85 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
86 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
87 .filter(EVENT_PRESENCE).filter(EVENT_PRESENCE).collect();
90 assertRowsSize(result, 5);
91 assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
95 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoIndependentFilters() throws Exception {
97 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
98 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
100 .filter(EVENT_PRESENCE)
104 assertRowsSize(result, 5);
105 assertThatEntryIsEqualTo(result, 0 , 0, DOMAIN_VALUE, FAULT);
106 assertThatEntryIsEqualTo(result, 4 , 0, DOMAIN_VALUE, SYSLOG);
110 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoDependentFilters() throws Exception {
112 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
113 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
114 .filter(Sets.newHashSet(DOMAIN_VALUE, EVENT_PRESENCE))
118 assertRowsSize(result, 5);
119 assertThatEntryIsEqualTo(result, 0, 0, DOMAIN_VALUE, FAULT);
120 assertThatEntryIsEqualTo(result, 4, 0, DOMAIN_VALUE, SYSLOG);
124 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnThreeCombinedFilters() throws Exception {
126 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
127 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
128 .filter(Sets.newHashSet(DOMAIN_VALUE, EVENT_PRESENCE))
129 .filter(EVENT_STRUCTURE_HEARTBEAT_FIELDS_PRESENCE)
133 assertRowsSize(result, 5);
134 assertThatEntryIsEqualTo(result, 0,0, DOMAIN_VALUE, FAULT);
135 assertThatEntryIsEqualTo(result, 0,1, EVENT_PRESENCE, REQUIRED);
136 assertThatEntryIsEqualTo(result, 1,0, DOMAIN_VALUE, FAULT);
137 assertThatEntryIsEqualTo(result, 1,1, EVENT_PRESENCE, REQUIRED);
138 assertThatEntryIsEqualTo(result, 2,0, DOMAIN_VALUE, HEARTBEAT);
139 assertThatEntryIsEqualTo(result, 2,1, EVENT_PRESENCE, REQUIRED);
140 assertThatEntryIsEqualTo(result, 3,0, DOMAIN_VALUE, MEASUREMENTS_FOR_VF_SCALING);
141 assertThatEntryIsEqualTo(result, 3,1, EVENT_PRESENCE, REQUIRED);
142 assertThatEntryIsEqualTo(result, 4,0, DOMAIN_VALUE, SYSLOG);
143 assertThatEntryIsEqualTo(result, 4,1, EVENT_PRESENCE, REQUIRED);
147 void shouldParseFaultRegistrationAndGenerateMapOfKeysOnFilterFromArray() throws Exception {
149 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
150 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
151 .filter(Sets.newHashSet(EVENT_HEARTBEAT_ACTION, EVENT_PRESENCE))
154 assertRowsSize(result, 5);
155 assertThatEntryIsEqualTo(result,0,0, EVENT_PRESENCE, REQUIRED);
156 assertThatEntryIsEqualTo(result,2,1, EVENT_HEARTBEAT_ACTION, VNF_DOWN);
160 void shouldParseFaultRegistrationAndGenerateMapOfKeysRemovingInvalidKey() throws Exception {
162 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
163 result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
164 .filter(Sets.newHashSet(INVALID_EVENT_HEARTBEAT_ACTION, EVENT_PRESENCE))
167 assertRowsSize(result, 5);
168 assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
172 void shouldParseInvalidYamlAndGenerateIOException() {
173 assertThrows(IOException.class, () -> {
174 try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
175 yamlParser.parseFile(INVALID_YML)
181 private void assertThatEntryIsEqualTo(GABResults result, int rowIndex, int entryIndex, String path, String data){
182 GABResultEntry entry = result.getRows().get(rowIndex).getEntries().get(entryIndex);
183 assertThat(entry.getData(), is(equalTo(data)));
184 assertThat(entry.getPath(), is(equalTo(path)));
187 private void assertResultIsEmpty(GABResults result) {
188 assertThat(result.getRows(), is(equalTo(Collections.<GABResult>emptyList())));
191 private void assertRowsSize(GABResults results, int size){
192 assertThat(results.getRows().size(), is(equalTo(size)));