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 static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.*;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
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;
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 {
50 private static final Logger LOGGER =
51 LoggerFactory.getLogger(DslConsumerHistoryLifecycleSingleNodeTest.class);
54 public void createTestGraph() {
55 JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
56 boolean success = true;
58 GraphTraversalSource g = transaction.traversal();
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
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)
119 } catch (Exception ex) {
123 transaction.commit();
125 transaction.rollback();
126 fail("Unable to setup the graph");
131 private JsonArray getResultsForPserverLookupByHostname(String endpoint) throws Exception {
132 return getResultsForPserverLookupByHostnameAndAnother(endpoint, "");
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());
149 assertNotNull("Response from /aai/v14/dsl is not null", responseEntity);
150 assertEquals("Expected the response to be 200", HttpStatus.OK,
151 responseEntity.getStatusCode());
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")
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());
166 actualEquipType.add(null);
171 assertThat("Verify equip-type in lifecycle", actualEquipType, is(expected));
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")
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());
185 actualEquipType.add(null);
190 assertThat("Verify equip-type SoTs in lifecycle", actualEquipType, is(expected));
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")
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());
204 actualEquipType.add(null);
209 assertThat("Verify equip-type tx-ids in lifecycle", actualEquipType, is(expected));
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")
218 if (property.getAsJsonObject().get("key").getAsString().equals("equip-type")) {
219 if (property.getAsJsonObject().has("timestamp")
220 && !property.getAsJsonObject().get("timestamp").isJsonNull()) {
222 .add(property.getAsJsonObject().get("timestamp").getAsLong());
224 actualEquipType.add(null);
229 assertThat("Verify equip-type timestamps in lifecycle", actualEquipType, is(expected));
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()));
240 assertThat("Verify node-changes timestamps in lifecycle", actualEquipType, is(expected));
244 public void lifecycleQueryNoStartOrEndTsTest() throws Exception {
246 String endpoint = "/aai/v14/dsl?format=lifecycle";
247 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
248 assertEquals("Result size", 1, results.size());
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);
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);
266 equipType = "other-ps-type-recreated";
267 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
268 "('equip-type','" + equipType + "')");
269 assertEquals("Filter equip-type on " + equipType, 0, results.size());
273 public void lifecycleQueryStartTsBeforeCreationTimeTest() throws Exception {
275 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=50";
276 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
277 assertEquals("Result size", 1, results.size());
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);
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);
295 equipType = "other-ps-type-recreated";
296 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
297 "('equip-type','" + equipType + "')");
298 assertEquals("Filter equip-type on " + equipType, 0, results.size());
302 public void lifecycleQueryStartTsAtCreationTimeTest() throws Exception {
304 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=100";
305 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
306 assertEquals("Result size", 1, results.size());
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);
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);
324 equipType = "other-ps-type-recreated";
325 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
326 "('equip-type','" + equipType + "')");
327 assertEquals("Filter equip-type on " + equipType, 0, results.size());
331 public void lifecycleQueryStartTsAfterCreationTimeTest() throws Exception {
333 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=300";
334 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
335 assertEquals("Result size", 1, results.size());
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);
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);
353 equipType = "other-ps-type-recreated";
354 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
355 "('equip-type','" + equipType + "')");
356 assertEquals("Filter equip-type on " + equipType, 0, results.size());
360 public void lifecycleQueryStartTsAtUpdateTimeTest() throws Exception {
362 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=500";
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);
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);
382 equipType = "other-ps-type-recreated";
383 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
384 "('equip-type','" + equipType + "')");
385 assertEquals("Filter equip-type on " + equipType, 0, results.size());
389 public void lifecycleQueryStartTsAfterUpdateTimeTest() throws Exception {
391 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=700";
392 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
393 assertEquals("Result size", 1, results.size());
395 verifyEquipTypeValues(results);
396 verifyEquipTypeSoTs(results);
397 verifyEquipTypeTxId(results);
398 verifyEquipTypeTimestamps(results);
399 verifyNodeTimestamps(results, 1000L);
401 String equipType = "first-ps-type";
402 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
403 "('equip-type','" + equipType + "')");
404 assertEquals("Filter equip-type on " + equipType, 0, results.size());
406 equipType = "other-ps-type-recreated";
407 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
408 "('equip-type','" + equipType + "')");
409 assertEquals("Filter equip-type on " + equipType, 0, results.size());
413 public void lifecycleQueryStartTsAtDeleteTimeTest() throws Exception {
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);
421 String equipType = "first-ps-type";
422 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
423 "('equip-type','" + equipType + "')");
424 assertEquals("Filter equip-type on " + equipType, 0, results.size());
428 public void lifecycleQueryStartTsAfterDeleteTimeTest() throws Exception {
430 String endpoint = "/aai/v14/dsl?format=lifecycle&startTs=5000";
431 JsonArray results = getResultsForPserverLookupByHostname(endpoint);
432 assertEquals("Result size", 0, results.size());
434 String equipType = "first-ps-type";
435 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
436 "('equip-type','" + equipType + "')");
437 assertEquals("Filter equip-type on " + equipType, 0, results.size());
439 equipType = "other-ps-type-recreated";
440 results = getResultsForPserverLookupByHostnameAndAnother(endpoint,
441 "('equip-type','" + equipType + "')");
442 assertEquals("Filter equip-type on " + equipType, 0, results.size());