2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.aai.rest.history;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonParser;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.janusgraph.core.JanusGraphTransaction;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.onap.aai.PayloadUtil;
30 import org.onap.aai.db.props.AAIProperties;
31 import org.onap.aai.dbmap.AAIGraph;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
41 import static org.hamcrest.CoreMatchers.is;
42 import static org.junit.Assert.*;
44 @Ignore("The lifecycle format requires the history schema to be loaded. " +
45 "Because aaigraph is a singleton its very complicated to have 2 different schemas loaded for testing. " +
46 "This needs to be addressed.")
47 public class DslConsumerHistoryLifecycleSingleNodeTest extends AbstractSpringHistoryRestTest {
49 private static final Logger LOGGER = LoggerFactory.getLogger(DslConsumerHistoryLifecycleSingleNodeTest.class);
52 public void createTestGraph() {
53 JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
54 boolean success = true;
56 GraphTraversalSource g = transaction.traversal();
59 - created pserver @ time 100
60 - equip-type = first-ps-type
61 - update equip-type @ 500
62 - equip-type = first-ps-type-update
63 - deleted pserver @ time 1000
65 g.addV().property(AAIProperties.NODE_TYPE, "pserver",
66 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
67 AAIProperties.START_TX_ID, "JUNIT-C",
68 AAIProperties.END_SOT, "JUNIT-D",
69 AAIProperties.END_TX_ID, "JUNIT-D",
70 AAIProperties.START_TS, 100L,
71 AAIProperties.END_TS, 1000L
73 .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/test-pserver-dsl",
74 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
75 AAIProperties.START_TX_ID, "JUNIT-C",
76 AAIProperties.END_SOT, "JUNIT-D",
77 AAIProperties.END_TX_ID, "JUNIT-D",
78 AAIProperties.START_TS, 100L,
79 AAIProperties.END_TS, 1000L
81 .property(AAIProperties.RESOURCE_VERSION, "100",
82 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
83 AAIProperties.START_TX_ID, "JUNIT-C",
84 AAIProperties.END_SOT, "JUNIT-U",
85 AAIProperties.END_TX_ID, "JUNIT-U",
86 AAIProperties.START_TS, 100L,
87 AAIProperties.END_TS, 500L
89 .property(AAIProperties.RESOURCE_VERSION, "500",
90 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-U",
91 AAIProperties.START_TX_ID, "JUNIT-U",
92 AAIProperties.END_SOT, "JUNIT-D",
93 AAIProperties.END_TX_ID, "JUNIT-D",
94 AAIProperties.START_TS, 500L,
95 AAIProperties.END_TS, 1000L
97 .property("hostname", "test-pserver-dsl",
98 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
99 AAIProperties.START_TX_ID, "JUNIT-C",
100 AAIProperties.END_SOT, "JUNIT-D",
101 AAIProperties.END_TX_ID, "JUNIT-D",
102 AAIProperties.START_TS, 100L,
103 AAIProperties.END_TS, 1000L
105 .property("in-maint", false,
106 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
107 AAIProperties.START_TX_ID, "JUNIT-C",
108 AAIProperties.END_SOT, "JUNIT-D",
109 AAIProperties.END_TX_ID, "JUNIT-D",
110 AAIProperties.START_TS, 100L,
111 AAIProperties.END_TS, 1000L
113 .property("equip-type", "first-ps-type",
114 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
115 AAIProperties.START_TX_ID, "JUNIT-C",
116 AAIProperties.END_SOT, "JUNIT-U",
117 AAIProperties.END_TX_ID, "JUNIT-U",
118 AAIProperties.START_TS, 100L,
119 AAIProperties.END_TS, 500L
121 .property("equip-type", "first-ps-type-update",
122 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-U",
123 AAIProperties.START_TX_ID, "JUNIT-U",
124 AAIProperties.END_SOT, "JUNIT-D",
125 AAIProperties.END_TX_ID, "JUNIT-D",
126 AAIProperties.START_TS, 500L,
127 AAIProperties.END_TS, 1000L
129 .property(AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
130 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
131 AAIProperties.START_TX_ID, "JUNIT-C",
132 AAIProperties.END_SOT, "JUNIT-D",
133 AAIProperties.END_TX_ID, "JUNIT-D",
134 AAIProperties.START_TS, 100L,
135 AAIProperties.END_TS, 1000L
137 .property(AAIProperties.END_SOT, "JUNIT-D",
138 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-D",
139 AAIProperties.START_TX_ID, "JUNIT-D",
140 AAIProperties.END_SOT, "JUNIT-D",
141 AAIProperties.END_TX_ID, "JUNIT-D",
142 AAIProperties.START_TS, 100L,
143 AAIProperties.END_TS, 1000L
145 .property(AAIProperties.START_TS, 100L,
146 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
147 AAIProperties.START_TX_ID, "JUNIT-C",
148 AAIProperties.END_SOT, "JUNIT-D",
149 AAIProperties.END_TX_ID, "JUNIT-D",
150 AAIProperties.START_TS, 100L,
151 AAIProperties.END_TS, 1000L
153 .property(AAIProperties.END_TS, 1000L,
154 AAIProperties.SOURCE_OF_TRUTH, "JUNIT-D",
155 AAIProperties.START_TX_ID, "JUNIT-D",
156 AAIProperties.END_SOT, "JUNIT-D",
157 AAIProperties.END_TX_ID, "JUNIT-D",
158 AAIProperties.START_TS, 1000L,
159 AAIProperties.END_TS, 1000L
163 } catch (Exception ex) {
167 transaction.commit();
169 transaction.rollback();
170 fail("Unable to setup the graph");
175 private JsonArray getResultsForPserverLookupByHostname(String endpoint) throws Exception {
176 return getResultsForPserverLookupByHostnameAndAnother(endpoint, "");
179 private JsonArray getResultsForPserverLookupByHostnameAndAnother(String endpoint, String additionalFilterInParen) throws Exception {
180 Map<String, String> dslQueryMap = new HashMap<>();
181 dslQueryMap.put("dsl-query", "pserver*('hostname','test-pserver-dsl')" + additionalFilterInParen);
182 String payload = PayloadUtil.getTemplatePayload("dsl-query.json", dslQueryMap);
183 httpEntity = new HttpEntity(payload, headers);
184 ResponseEntity responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
185 JsonArray results = new JsonParser().parse(responseEntity.getBody().toString()).getAsJsonObject().getAsJsonArray("results");
186 LOGGER.debug("Response for PUT request with uri {} : {}", baseUrl + endpoint, responseEntity.getBody());
188 assertNotNull("Response from /aai/v14/dsl is not null", responseEntity);
189 assertEquals("Expected the response to be 200", HttpStatus.OK, responseEntity.getStatusCode());
194 private void verifyEquipTypeValues(JsonArray results, String... expectedEquipTypes) {
195 List<String> expected = Arrays.asList(expectedEquipTypes);
196 final List<String> actualEquipType = new ArrayList<>();
197 for (JsonElement result : results) {
198 for (JsonElement property : result.getAsJsonObject().get("properties").getAsJsonArray()) {
199 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
200 if (property.getAsJsonObject().has("value") && !property.getAsJsonObject().get("value").isJsonNull()) {
201 actualEquipType.add(property.getAsJsonObject().get("value").getAsString());
203 actualEquipType.add(null);
208 assertThat("Verify equip-type in lifecycle", actualEquipType, is(expected));
211 private void verifyEquipTypeSoTs(JsonArray results, String... expectedSTOs) {
212 List<String> expected = Arrays.asList(expectedSTOs);
213 final List<String> actualEquipType = new ArrayList<>();
214 for (JsonElement result : results) {
215 for (JsonElement property : result.getAsJsonObject().get("properties").getAsJsonArray()) {
216 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
217 if (property.getAsJsonObject().has("sot") && !property.getAsJsonObject().get("sot").isJsonNull()) {
218 actualEquipType.add(property.getAsJsonObject().get("sot").getAsString());
220 actualEquipType.add(null);
225 assertThat("Verify equip-type SoTs in lifecycle", actualEquipType, is(expected));
228 private void verifyEquipTypeTxId(JsonArray results, String... expectedSTOs) {
229 List<String> expected = Arrays.asList(expectedSTOs);
230 final List<String> actualEquipType = new ArrayList<>();
231 for (JsonElement result : results) {
232 for (JsonElement property : result.getAsJsonObject().get("properties").getAsJsonArray()) {
233 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
234 if (property.getAsJsonObject().has("tx-id") && !property.getAsJsonObject().get("tx-id").isJsonNull()) {
235 actualEquipType.add(property.getAsJsonObject().get("tx-id").getAsString());
237 actualEquipType.add(null);
242 assertThat("Verify equip-type tx-ids in lifecycle", actualEquipType, is(expected));
245 private void verifyEquipTypeTimestamps(JsonArray results, Long... expectedSTOs) {
246 List<Long> expected = Arrays.asList(expectedSTOs);
247 final List<Long> actualEquipType = new ArrayList<>();
248 for (JsonElement result : results) {
249 for (JsonElement property : result.getAsJsonObject().get("properties").getAsJsonArray()) {
250 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
251 if (property.getAsJsonObject().has("timestamp") && !property.getAsJsonObject().get("timestamp").isJsonNull()) {
252 actualEquipType.add(property.getAsJsonObject().get("timestamp").getAsLong());
254 actualEquipType.add(null);
259 assertThat("Verify equip-type timestamps in lifecycle", actualEquipType, is(expected));
262 private void verifyNodeTimestamps(JsonArray results, Long... nodeTimestamps) {
263 List<Long> expected = Arrays.asList(nodeTimestamps);
264 final List<Long> actualEquipType = new ArrayList<>();
265 for (JsonElement result : results) {
266 result.getAsJsonObject().get("node-actions").getAsJsonArray().forEach(jsonElement ->
267 actualEquipType.add(jsonElement.getAsJsonObject().get("timestamp").getAsLong()));
269 assertThat("Verify node-changes timestamps in lifecycle", actualEquipType, is(expected));
274 public void lifecycleQueryNoStartOrEndTsTest() throws Exception {
276 String endpoint = "/aai/v14/dsl?format=lifecycle";
277 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
278 assertEquals("Result size", 1, results.size());
280 verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
281 verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
282 verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
283 verifyEquipTypeTimestamps(results, 500L, 100L);
284 verifyNodeTimestamps(results, 1000L, 100L);
287 String equipType = "first-ps-type";
288 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
289 assertEquals("Filter equip-type on " + equipType, 1, results.size());
290 verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
291 verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
292 verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
293 verifyEquipTypeTimestamps(results, 500L, 100L);
294 verifyNodeTimestamps(results, 1000L, 100L);
296 equipType = "other-ps-type-recreated";
297 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
298 assertEquals("Filter equip-type on " + equipType, 0, results.size());
304 public void lifecycleQueryStartTsBeforeCreationTimeTest() throws Exception {
306 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=50";
307 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
308 assertEquals("Result size", 1, results.size());
310 verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
311 verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
312 verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
313 verifyEquipTypeTimestamps(results, 500L, 100L);
314 verifyNodeTimestamps(results, 1000L, 100L);
317 String equipType = "first-ps-type";
318 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
319 assertEquals("Filter equip-type on " + equipType, 1, results.size());
320 verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
321 verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
322 verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
323 verifyEquipTypeTimestamps(results, 500L, 100L);
324 verifyNodeTimestamps(results, 1000L, 100L);
326 equipType = "other-ps-type-recreated";
327 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
328 assertEquals("Filter equip-type on " + equipType, 0, results.size());
332 public void lifecycleQueryStartTsAtCreationTimeTest() throws Exception {
334 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=100";
335 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
336 assertEquals("Result size", 1, results.size());
338 verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
339 verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
340 verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
341 verifyEquipTypeTimestamps(results, 500L, 100L);
342 verifyNodeTimestamps(results, 1000L, 100L);
345 String equipType = "first-ps-type";
346 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
347 assertEquals("Filter equip-type on " + equipType, 1, results.size());
348 verifyEquipTypeValues(results, "first-ps-type-update", "first-ps-type");
349 verifyEquipTypeSoTs(results, "JUNIT-U", "JUNIT-C");
350 verifyEquipTypeTxId(results, "JUNIT-U", "JUNIT-C");
351 verifyEquipTypeTimestamps(results, 500L, 100L);
352 verifyNodeTimestamps(results, 1000L, 100L);
354 equipType = "other-ps-type-recreated";
355 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
356 assertEquals("Filter equip-type on " + equipType, 0, results.size());
360 public void lifecycleQueryStartTsAfterCreationTimeTest() throws Exception {
362 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=300";
363 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
364 assertEquals("Result size", 1, results.size());
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);
373 String equipType = "first-ps-type";
374 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('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);
382 equipType = "other-ps-type-recreated";
383 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
384 assertEquals("Filter equip-type on " + equipType, 0, results.size());
388 public void lifecycleQueryStartTsAtUpdateTimeTest() throws Exception {
390 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=500";
391 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
392 assertEquals("Result size", 1, results.size());
394 verifyEquipTypeValues(results, "first-ps-type-update");
395 verifyEquipTypeSoTs(results, "JUNIT-U");
396 verifyEquipTypeTxId(results, "JUNIT-U");
397 verifyEquipTypeTimestamps(results, 500L);
398 verifyNodeTimestamps(results, 1000L);
401 String equipType = "first-ps-type";
402 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
403 assertEquals("Filter equip-type on " + equipType, 1, results.size());
404 verifyEquipTypeValues(results, "first-ps-type-update");
405 verifyEquipTypeSoTs(results, "JUNIT-U");
406 verifyEquipTypeTxId(results, "JUNIT-U");
407 verifyEquipTypeTimestamps(results, 500L);
408 verifyNodeTimestamps(results, 1000L);
410 equipType = "other-ps-type-recreated";
411 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
412 assertEquals("Filter equip-type on " + equipType, 0, results.size());
416 public void lifecycleQueryStartTsAfterUpdateTimeTest() throws Exception {
418 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=700";
419 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
420 assertEquals("Result size", 1, results.size());
422 verifyEquipTypeValues(results);
423 verifyEquipTypeSoTs(results);
424 verifyEquipTypeTxId(results);
425 verifyEquipTypeTimestamps(results);
426 verifyNodeTimestamps(results, 1000L);
429 String equipType = "first-ps-type";
430 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
431 assertEquals("Filter equip-type on " + equipType, 0, results.size());
433 equipType = "other-ps-type-recreated";
434 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
435 assertEquals("Filter equip-type on " + equipType, 0, results.size());
439 public void lifecycleQueryStartTsAtDeleteTimeTest() throws Exception {
441 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=1000";
442 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
443 assertEquals("Result size", 1, results.size());
444 assertEquals("properties size should be 0", results.get(0).getAsJsonObject().get("properties").getAsJsonArray().size(), 0);
446 String equipType = "first-ps-type";
447 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
448 assertEquals("Filter equip-type on " + equipType, 0, results.size());
452 public void lifecycleQueryStartTsAfterDeleteTimeTest() throws Exception {
454 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=5000";
455 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
456 assertEquals("Result size", 0, results.size());
458 String equipType = "first-ps-type";
459 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
460 assertEquals("Filter equip-type on " + equipType, 0, results.size());
462 equipType = "other-ps-type-recreated";
463 results = getResultsForPserverLookupByHostnameAndAnother(endpoint, "('equip-type','" + equipType + "')");
464 assertEquals("Filter equip-type on " + equipType, 0, results.size());