Introduced yaml parser as common lib
[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 INVALID_YML = "yaml/invalid.yml";
54
55     @Test
56     void shouldNotParseAnyYamlAndGenerateEmptyMapOfKeys() throws Exception {
57         GABResults result;
58         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
59             result = yamlParser.filter(TEST).collect();
60         }
61         assertResultIsEmpty(result);
62     }
63
64     @Test
65     void shouldParseNonexistentYamlAndGenerateEmptyMapOfKeys() throws Exception {
66         GABResults result;
67         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
68             result = yamlParser.parseFile(NONEXISTENT_FILE).collect();
69         }
70         assertResultIsEmpty(result);
71     }
72
73     @Test
74     void shouldParseExistentFileYamlAndGenerateEmptyMapOfKeysIfNoKeywordsSearched() throws Exception {
75         GABResults result;
76         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
77             result = yamlParser.parseFile(FAULT_REGISTRATION_YML).collect();
78         }
79         assertResultIsEmpty(result);
80     }
81
82     @Test
83     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnSingleFilter() throws Exception {
84         GABResults result;
85         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
86             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
87                 .filter(EVENT_PRESENCE).filter(EVENT_PRESENCE).collect();
88
89         }
90         assertRowsSize(result, 5);
91         assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
92     }
93
94     @Test
95     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoIndependentFilters() throws Exception {
96         GABResults result;
97         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
98             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
99                 .filter(DOMAIN_VALUE)
100                 .filter(EVENT_PRESENCE)
101                 .collect();
102
103         }
104         assertRowsSize(result, 5);
105         assertThatEntryIsEqualTo(result, 0 , 0, DOMAIN_VALUE, FAULT);
106         assertThatEntryIsEqualTo(result, 4 , 0, DOMAIN_VALUE, SYSLOG);
107     }
108
109     @Test
110     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnTwoDependentFilters() throws Exception {
111         GABResults result;
112         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
113             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
114                 .filter(Sets.newHashSet(DOMAIN_VALUE, EVENT_PRESENCE))
115                 .collect();
116
117         }
118         assertRowsSize(result, 5);
119         assertThatEntryIsEqualTo(result, 0, 0, DOMAIN_VALUE, FAULT);
120         assertThatEntryIsEqualTo(result, 4, 0, DOMAIN_VALUE, SYSLOG);
121     }
122
123     @Test
124     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnThreeCombinedFilters() throws Exception {
125         GABResults result;
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)
130                 .collect();
131
132         }
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);
144     }
145
146     @Test
147     void shouldParseFaultRegistrationAndGenerateMapOfKeysOnFilterFromArray() throws Exception {
148         GABResults result;
149         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
150             result = yamlParser.parseFile(FAULT_REGISTRATION_YML)
151                 .filter(Sets.newHashSet(EVENT_HEARTBEAT_ACTION, EVENT_PRESENCE))
152                 .collect();
153         }
154         assertRowsSize(result, 5);
155         assertThatEntryIsEqualTo(result,0,0, EVENT_PRESENCE, REQUIRED);
156         assertThatEntryIsEqualTo(result,2,1, EVENT_HEARTBEAT_ACTION, VNF_DOWN);
157     }
158
159     @Test
160     void shouldParseFaultRegistrationAndGenerateMapOfKeysRemovingInvalidKey() throws Exception {
161         GABResults result;
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))
165                 .collect();
166         }
167         assertRowsSize(result, 5);
168         assertThatEntryIsEqualTo(result, 0, 0, EVENT_PRESENCE, REQUIRED);
169     }
170
171     @Test
172     void shouldParseInvalidYamlAndGenerateIOException() {
173         assertThrows(IOException.class, () -> {
174         try (GABYamlParser yamlParser = new GABYamlParser(new YamlParser())){
175             yamlParser.parseFile(INVALID_YML)
176                 .filter(EVENT)
177                 .collect();
178         }});
179     }
180
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)));
185     }
186
187     private void assertResultIsEmpty(GABResults result) {
188         assertThat(result.getRows(), is(equalTo(Collections.<GABResult>emptyList())));
189     }
190
191     private void assertRowsSize(GABResults results, int size){
192         assertThat(results.getRows().size(), is(equalTo(size)));
193     }
194
195 }