6d1a36c8a95a8e52cee7f3943045630fd5b31175
[aai/traversal.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T 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 package org.onap.aai.rest.history;
21
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.*;
24
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
28
29 import java.util.*;
30
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.janusgraph.core.JanusGraphTransaction;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.onap.aai.PayloadUtil;
36 import org.onap.aai.db.props.AAIProperties;
37 import org.onap.aai.dbmap.AAIGraph;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.http.HttpEntity;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44
45 @Ignore("The lifecycle format requires the history schema to be loaded.  "
46     + "Because aaigraph is a singleton its very complicated to have 2 different schemas loaded for testing.  "
47     + "This needs to be addressed.")
48 public class DslConsumerHistoryLifecycleSingleNodeTest extends AbstractSpringHistoryRestTest {
49
50     private static final Logger LOGGER =
51         LoggerFactory.getLogger(DslConsumerHistoryLifecycleSingleNodeTest.class);
52
53     @Override
54     public void createTestGraph() {
55         JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
56         boolean success = true;
57         try {
58             GraphTraversalSource g = transaction.traversal();
59             /*
60              * Pserver
61              * - created pserver @ time 100
62              * - equip-type = first-ps-type
63              * - update equip-type @ 500
64              * - equip-type = first-ps-type-update
65              * - deleted pserver @ time 1000
66              */
67             g.addV()
68                 .property(AAIProperties.NODE_TYPE, "pserver", AAIProperties.SOURCE_OF_TRUTH,
69                     "JUNIT-C", AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT,
70                     "JUNIT-D", AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 100L,
71                     AAIProperties.END_TS, 1000L)
72                 .property(AAIProperties.AAI_URI,
73                     "/cloud-infrastructure/pservers/pserver/test-pserver-dsl",
74                     AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C", AAIProperties.START_TX_ID, "JUNIT-C",
75                     AAIProperties.END_SOT, "JUNIT-D", AAIProperties.END_TX_ID, "JUNIT-D",
76                     AAIProperties.START_TS, 100L, AAIProperties.END_TS, 1000L)
77                 .property(AAIProperties.RESOURCE_VERSION, "100", AAIProperties.SOURCE_OF_TRUTH,
78                     "JUNIT-C", AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT,
79                     "JUNIT-U", AAIProperties.END_TX_ID, "JUNIT-U", AAIProperties.START_TS, 100L,
80                     AAIProperties.END_TS, 500L)
81                 .property(AAIProperties.RESOURCE_VERSION, "500", AAIProperties.SOURCE_OF_TRUTH,
82                     "JUNIT-U", AAIProperties.START_TX_ID, "JUNIT-U", AAIProperties.END_SOT,
83                     "JUNIT-D", AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 500L,
84                     AAIProperties.END_TS, 1000L)
85                 .property("hostname", "test-pserver-dsl", AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
86                     AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT, "JUNIT-D",
87                     AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 100L,
88                     AAIProperties.END_TS, 1000L)
89                 .property("in-maint", false, AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
90                     AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT, "JUNIT-D",
91                     AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 100L,
92                     AAIProperties.END_TS, 1000L)
93                 .property("equip-type", "first-ps-type", AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
94                     AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT, "JUNIT-U",
95                     AAIProperties.END_TX_ID, "JUNIT-U", AAIProperties.START_TS, 100L,
96                     AAIProperties.END_TS, 500L)
97                 .property("equip-type", "first-ps-type-update", AAIProperties.SOURCE_OF_TRUTH,
98                     "JUNIT-U", AAIProperties.START_TX_ID, "JUNIT-U", AAIProperties.END_SOT,
99                     "JUNIT-D", AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 500L,
100                     AAIProperties.END_TS, 1000L)
101                 .property(AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C", AAIProperties.SOURCE_OF_TRUTH,
102                     "JUNIT-C", AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT,
103                     "JUNIT-D", AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 100L,
104                     AAIProperties.END_TS, 1000L)
105                 .property(AAIProperties.END_SOT, "JUNIT-D", AAIProperties.SOURCE_OF_TRUTH,
106                     "JUNIT-D", AAIProperties.START_TX_ID, "JUNIT-D", AAIProperties.END_SOT,
107                     "JUNIT-D", AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 100L,
108                     AAIProperties.END_TS, 1000L)
109                 .property(AAIProperties.START_TS, 100L, AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
110                     AAIProperties.START_TX_ID, "JUNIT-C", AAIProperties.END_SOT, "JUNIT-D",
111                     AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 100L,
112                     AAIProperties.END_TS, 1000L)
113                 .property(AAIProperties.END_TS, 1000L, AAIProperties.SOURCE_OF_TRUTH, "JUNIT-D",
114                     AAIProperties.START_TX_ID, "JUNIT-D", AAIProperties.END_SOT, "JUNIT-D",
115                     AAIProperties.END_TX_ID, "JUNIT-D", AAIProperties.START_TS, 1000L,
116                     AAIProperties.END_TS, 1000L)
117                 .next();
118
119         } catch (Exception ex) {
120             success = false;
121         } finally {
122             if (success) {
123                 transaction.commit();
124             } else {
125                 transaction.rollback();
126                 fail("Unable to setup the graph");
127             }
128         }
129     }
130
131     private JsonArray getResultsForPserverLookupByHostname(String endpoint) throws Exception {
132         return getResultsForPserverLookupByHostnameAndAnother(endpoint, "");
133     }
134
135     private JsonArray getResultsForPserverLookupByHostnameAndAnother(String endpoint,
136         String additionalFilterInParen) throws Exception {
137         Map<String, String> dslQueryMap = new HashMap<>();
138         dslQueryMap.put("dsl-query",
139             "pserver*('hostname','test-pserver-dsl')" + additionalFilterInParen);
140         String payload = PayloadUtil.getTemplatePayload("dsl-query.json", dslQueryMap);
141         httpEntity = new HttpEntity(payload, headers);
142         ResponseEntity responseEntity =
143             restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
144         JsonArray results = new JsonParser().parse(responseEntity.getBody().toString())
145             .getAsJsonObject().getAsJsonArray("results");
146         LOGGER.debug("Response for PUT request with uri {} : {}", baseUrl + endpoint,
147             responseEntity.getBody());
148
149         assertNotNull("Response from /aai/v14/dsl is not null", responseEntity);
150         assertEquals("Expected the response to be 200", HttpStatus.OK,
151             responseEntity.getStatusCode());
152         return results;
153     }
154
155     private void verifyEquipTypeValues(JsonArray results, String... expectedEquipTypes) {
156         List<String> expected = Arrays.asList(expectedEquipTypes);
157         final List<String> actualEquipType = new ArrayList<>();
158         for (JsonElement result : results) {
159             for (JsonElement property : result.getAsJsonObject().get("properties")
160                 .getAsJsonArray()) {
161                 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
162                     if (property.getAsJsonObject().has("value")
163                         && !property.getAsJsonObject().get("value").isJsonNull()) {
164                         actualEquipType.add(property.getAsJsonObject().get("value").getAsString());
165                     } else {
166                         actualEquipType.add(null);
167                     }
168                 }
169             }
170         }
171         assertThat("Verify equip-type in lifecycle", actualEquipType, is(expected));
172     }
173
174     private void verifyEquipTypeSoTs(JsonArray results, String... expectedSTOs) {
175         List<String> expected = Arrays.asList(expectedSTOs);
176         final List<String> actualEquipType = new ArrayList<>();
177         for (JsonElement result : results) {
178             for (JsonElement property : result.getAsJsonObject().get("properties")
179                 .getAsJsonArray()) {
180                 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
181                     if (property.getAsJsonObject().has("sot")
182                         && !property.getAsJsonObject().get("sot").isJsonNull()) {
183                         actualEquipType.add(property.getAsJsonObject().get("sot").getAsString());
184                     } else {
185                         actualEquipType.add(null);
186                     }
187                 }
188             }
189         }
190         assertThat("Verify equip-type SoTs in lifecycle", actualEquipType, is(expected));
191     }
192
193     private void verifyEquipTypeTxId(JsonArray results, String... expectedSTOs) {
194         List<String> expected = Arrays.asList(expectedSTOs);
195         final List<String> actualEquipType = new ArrayList<>();
196         for (JsonElement result : results) {
197             for (JsonElement property : result.getAsJsonObject().get("properties")
198                 .getAsJsonArray()) {
199                 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
200                     if (property.getAsJsonObject().has("tx-id")
201                         && !property.getAsJsonObject().get("tx-id").isJsonNull()) {
202                         actualEquipType.add(property.getAsJsonObject().get("tx-id").getAsString());
203                     } else {
204                         actualEquipType.add(null);
205                     }
206                 }
207             }
208         }
209         assertThat("Verify equip-type tx-ids in lifecycle", actualEquipType, is(expected));
210     }
211
212     private void verifyEquipTypeTimestamps(JsonArray results, Long... expectedSTOs) {
213         List<Long> expected = Arrays.asList(expectedSTOs);
214         final List<Long> actualEquipType = new ArrayList<>();
215         for (JsonElement result : results) {
216             for (JsonElement property : result.getAsJsonObject().get("properties")
217                 .getAsJsonArray()) {
218                 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
219                     if (property.getAsJsonObject().has("timestamp")
220                         && !property.getAsJsonObject().get("timestamp").isJsonNull()) {
221                         actualEquipType
222                             .add(property.getAsJsonObject().get("timestamp").getAsLong());
223                     } else {
224                         actualEquipType.add(null);
225                     }
226                 }
227             }
228         }
229         assertThat("Verify equip-type timestamps in lifecycle", actualEquipType, is(expected));
230     }
231
232     private void verifyNodeTimestamps(JsonArray results, Long... nodeTimestamps) {
233         List<Long> expected = Arrays.asList(nodeTimestamps);
234         final List<Long> actualEquipType = new ArrayList<>();
235         for (JsonElement result : results) {
236             result.getAsJsonObject().get("node-actions").getAsJsonArray()
237                 .forEach(jsonElement -> actualEquipType
238                     .add(jsonElement.getAsJsonObject().get("timestamp").getAsLong()));
239         }
240         assertThat("Verify node-changes timestamps in lifecycle", actualEquipType, is(expected));
241     }
242
243     @Test
244     public void lifecycleQueryNoStartOrEndTsTest() throws Exception {
245
246         String endpoint = "/aai/v14/dsl?format=lifecycle";
247         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
248         assertEquals("Result size", 1, results.size());
249
250         verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
251         verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
252         verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
253         verifyEquipTypeTimestamps(results, 500L, 100L);
254         verifyNodeTimestamps(results, 1000L, 100L);
255
256         String equipType = "first-ps-type";
257         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
258             "('equip-type','" + equipType + "')");
259         assertEquals("Filter equip-type on " + equipType, 1, results.size());
260         verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
261         verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
262         verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
263         verifyEquipTypeTimestamps(results, 500L, 100L);
264         verifyNodeTimestamps(results, 1000L, 100L);
265
266         equipType = "other-ps-type-recreated";
267         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
268             "('equip-type','" + equipType + "')");
269         assertEquals("Filter equip-type on " + equipType, 0, results.size());
270     }
271
272     @Test
273     public void lifecycleQueryStartTsBeforeCreationTimeTest() throws Exception {
274
275         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=50";
276         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
277         assertEquals("Result size", 1, results.size());
278
279         verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
280         verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
281         verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
282         verifyEquipTypeTimestamps(results, 500L, 100L);
283         verifyNodeTimestamps(results, 1000L, 100L);
284
285         String equipType = "first-ps-type";
286         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
287             "('equip-type','" + equipType + "')");
288         assertEquals("Filter equip-type on " + equipType, 1, results.size());
289         verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
290         verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
291         verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
292         verifyEquipTypeTimestamps(results, 500L, 100L);
293         verifyNodeTimestamps(results, 1000L, 100L);
294
295         equipType = "other-ps-type-recreated";
296         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
297             "('equip-type','" + equipType + "')");
298         assertEquals("Filter equip-type on " + equipType, 0, results.size());
299     }
300
301     @Test
302     public void lifecycleQueryStartTsAtCreationTimeTest() throws Exception {
303
304         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=100";
305         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
306         assertEquals("Result size", 1, results.size());
307
308         verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
309         verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
310         verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
311         verifyEquipTypeTimestamps(results, 500L, 100L);
312         verifyNodeTimestamps(results, 1000L, 100L);
313
314         String equipType = "first-ps-type";
315         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
316             "('equip-type','" + equipType + "')");
317         assertEquals("Filter equip-type on " + equipType, 1, results.size());
318         verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
319         verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
320         verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
321         verifyEquipTypeTimestamps(results, 500L, 100L);
322         verifyNodeTimestamps(results, 1000L, 100L);
323
324         equipType = "other-ps-type-recreated";
325         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
326             "('equip-type','" + equipType + "')");
327         assertEquals("Filter equip-type on " + equipType, 0, results.size());
328     }
329
330     @Test
331     public void lifecycleQueryStartTsAfterCreationTimeTest() throws Exception {
332
333         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=300";
334         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
335         assertEquals("Result size", 1, results.size());
336
337         verifyEquipTypeValues(results, "first-ps-type-update");
338         verifyEquipTypeSoTs(results, "JUNIT-U");
339         verifyEquipTypeTxId(results, "JUNIT-U");
340         verifyEquipTypeTimestamps(results, 500L);
341         verifyNodeTimestamps(results, 1000L);
342
343         String equipType = "first-ps-type";
344         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
345             "('equip-type','" + equipType + "')");
346         assertEquals("Filter equip-type on " + equipType, 1, results.size());
347         verifyEquipTypeValues(results, "first-ps-type-update");
348         verifyEquipTypeSoTs(results, "JUNIT-U");
349         verifyEquipTypeTxId(results, "JUNIT-U");
350         verifyEquipTypeTimestamps(results, 500L);
351         verifyNodeTimestamps(results, 1000L);
352
353         equipType = "other-ps-type-recreated";
354         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
355             "('equip-type','" + equipType + "')");
356         assertEquals("Filter equip-type on " + equipType, 0, results.size());
357     }
358
359     @Test
360     public void lifecycleQueryStartTsAtUpdateTimeTest() throws Exception {
361
362         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=500";
363         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
364         assertEquals("Result size", 1, results.size());
365
366         verifyEquipTypeValues(results, "first-ps-type-update");
367         verifyEquipTypeSoTs(results, "JUNIT-U");
368         verifyEquipTypeTxId(results, "JUNIT-U");
369         verifyEquipTypeTimestamps(results, 500L);
370         verifyNodeTimestamps(results, 1000L);
371
372         String equipType = "first-ps-type";
373         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
374             "('equip-type','" + equipType + "')");
375         assertEquals("Filter equip-type on " + equipType, 1, results.size());
376         verifyEquipTypeValues(results, "first-ps-type-update");
377         verifyEquipTypeSoTs(results, "JUNIT-U");
378         verifyEquipTypeTxId(results, "JUNIT-U");
379         verifyEquipTypeTimestamps(results, 500L);
380         verifyNodeTimestamps(results, 1000L);
381
382         equipType = "other-ps-type-recreated";
383         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
384             "('equip-type','" + equipType + "')");
385         assertEquals("Filter equip-type on " + equipType, 0, results.size());
386     }
387
388     @Test
389     public void lifecycleQueryStartTsAfterUpdateTimeTest() throws Exception {
390
391         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=700";
392         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
393         assertEquals("Result size", 1, results.size());
394
395         verifyEquipTypeValues(results);
396         verifyEquipTypeSoTs(results);
397         verifyEquipTypeTxId(results);
398         verifyEquipTypeTimestamps(results);
399         verifyNodeTimestamps(results, 1000L);
400
401         String equipType = "first-ps-type";
402         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
403             "('equip-type','" + equipType + "')");
404         assertEquals("Filter equip-type on " + equipType, 0, results.size());
405
406         equipType = "other-ps-type-recreated";
407         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
408             "('equip-type','" + equipType + "')");
409         assertEquals("Filter equip-type on " + equipType, 0, results.size());
410     }
411
412     @Test
413     public void lifecycleQueryStartTsAtDeleteTimeTest() throws Exception {
414
415         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=1000";
416         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
417         assertEquals("Result size", 1, results.size());
418         assertEquals("properties size should be 0",
419             results.get(0).getAsJsonObject().get("properties").getAsJsonArray().size(), 0);
420
421         String equipType = "first-ps-type";
422         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
423             "('equip-type','" + equipType + "')");
424         assertEquals("Filter equip-type on " + equipType, 0, results.size());
425     }
426
427     @Test
428     public void lifecycleQueryStartTsAfterDeleteTimeTest() throws Exception {
429
430         String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=5000";
431         JsonArray results = getResultsForPserverLookupByHostname(endpoint);
432         assertEquals("Result size", 0, results.size());
433
434         String equipType = "first-ps-type";
435         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
436             "('equip-type','" + equipType + "')");
437         assertEquals("Filter equip-type on " + equipType, 0, results.size());
438
439         equipType = "other-ps-type-recreated";
440         results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
441             "('equip-type','" + equipType + "')");
442         assertEquals("Filter equip-type on " + equipType, 0, results.size());
443     }
444
445 }