aboutsummaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/test/java/org/onap/aai/rest/history/CQAllChildernFromPnfStateTest.java
blob: 7cf8138babeb2da7bc325e360a3315ea7519148e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.rest.history;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraphTransaction;
import org.junit.Ignore;
import org.junit.Test;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.dbmap.AAIGraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

@Ignore("The state format requires the history schema to be loaded.  " +
		"Because aaigraph is a singleton its very complicated to have 2 different schemas loaded for testing.  " +
		"This needs to be addressed.")
public class CQAllChildernFromPnfStateTest extends AbstractSpringHistoryRestTest {

	private static final Logger LOGGER = LoggerFactory.getLogger(CQAllChildernFromPnfStateTest.class);

	@Override
	public void createTestGraph() {
		JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
		boolean success = true;
		try {
			GraphTraversalSource g = transaction.traversal();

			Vertex pnf1 = g.addV()
				.property(AAIProperties.NODE_TYPE, "pnf",
				AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
				AAIProperties.START_TX_ID, "JUNIT-C",
				AAIProperties.START_TS, 100L
				)
				.property(AAIProperties.AAI_URI, "/network/pnfs/pnf/pnf-1",
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.property(AAIProperties.RESOURCE_VERSION, "100",
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.property(AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.property(AAIProperties.START_TS, 100L,
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.property("pnf-name", "pnf-1",
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.property("in-maint", false,
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.property("pnf-id", "pnf-1-id",
						AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
						AAIProperties.START_TX_ID, "JUNIT-C",
						AAIProperties.START_TS, 100L
				)
				.next();

			Vertex pint1 = g.addV()
					.property(AAIProperties.NODE_TYPE, "p-interface",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.AAI_URI, "/network/pnfs/pnf/pnf-1/p-interfaces/p-interface/pint-1",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.RESOURCE_VERSION, "100",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.START_TS, 100L,
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property("interface-name", "pint-1",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.next();

			Vertex lint1 = g.addV()
					.property(AAIProperties.NODE_TYPE, "l-interface",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.AAI_URI, "/network/pnfs/pnf/pnf-1/p-interfaces/p-interface/pint-1/l-interfaces/l-interface/lint-1",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.RESOURCE_VERSION, "100",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property(AAIProperties.START_TS, 100L,
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.property("interface-name", "lint-1",
							AAIProperties.SOURCE_OF_TRUTH, "JUNIT-C",
							AAIProperties.START_TX_ID, "JUNIT-C",
							AAIProperties.START_TS, 100L
					)
					.next();

			pint1.addEdge("tosca.relationships.network.BindsTo", pnf1,
					"start-ts", 100L,
					"private", false,
					"aai-uuid", "e713ce2c-62d5-4555-a481-95619cb158aa",
					"prevent-delete", "NONE",
					"delete-other-v", "IN",
					"source-of-truth", "JUNIT-EDGE-C",
					"start-tx-id", "JUNIT-EDGE-C",
					"contains-other-v", "IN");

			lint1.addEdge("tosca.relationships.network.BindsTo", pint1,
					"start-ts", 100L,
					"private", false,
					"aai-uuid", "e713ce2c-62d5-4555-a481-95619cb158ab",
					"prevent-delete", "NONE",
					"delete-other-v", "IN",
					"source-of-truth", "JUNIT-EDGE-C",
					"start-tx-id", "JUNIT-EDGE-C",
					"contains-other-v", "IN");

		} catch (Exception ex) {
			success = false;
		} finally {
			if (success) {
				transaction.commit();
			} else {
				transaction.rollback();
				fail("Unable to setup the graph");
			}
		}
	}

	private JsonArray executeCustomQuery(String endpoint, String queryName, String... startUris) throws Exception {
		JsonObject payload = new JsonObject();
		JsonArray start = new JsonArray();
		Arrays.stream(startUris).forEach(start::add);
		payload.add("start", start);
		payload.addProperty("query", "query/" + queryName);
		httpEntity = new HttpEntity(payload.toString(), headers);
		ResponseEntity responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
		JsonArray results = new JsonParser().parse(responseEntity.getBody().toString()).getAsJsonObject().getAsJsonArray("results");
		LOGGER.debug("Response for PUT request with uri {} : {}", baseUrl + endpoint, responseEntity.getBody());

		assertEquals("Expected the response to be 200", HttpStatus.OK, responseEntity.getStatusCode());
		return results;
	}

	private JsonArray executeGremlin(String endpoint, String query) throws Exception {
		JsonObject payload = new JsonObject();
		payload.addProperty("gremlin", query);
		httpEntity = new HttpEntity(payload.toString(), headers);
		ResponseEntity responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
		JsonArray results = new JsonParser().parse(responseEntity.getBody().toString()).getAsJsonObject().getAsJsonArray("results");
		LOGGER.debug("Response for PUT request with uri {} : {}", baseUrl + endpoint, responseEntity.getBody());

		assertEquals("Expected the response to be 200", HttpStatus.OK, responseEntity.getStatusCode());
		return results;
	}

	private void verifyResultUris(JsonArray results, String... uris) {
		Set<String> expected = new HashSet<>(Arrays.asList(uris));
		final Set<String> actualEquipType = new HashSet<>();
		for (JsonElement result : results) {
			actualEquipType.add(result.getAsJsonObject().get("uri").getAsString());
		}
		assertThat("Verify results uri's", actualEquipType, is(expected));
	}


	@Test
	public void pnfChildrenQueryTest() throws Exception {
		JsonArray results = executeCustomQuery("/aai/v14/query?format=state", "allchildren-fromPnf", "/network/pnfs/pnf/pnf-1");
		verifyResultUris(results, "/network/pnfs/pnf/pnf-1", "/network/pnfs/pnf/pnf-1/p-interfaces/p-interface/pint-1", "/network/pnfs/pnf/pnf-1/p-interfaces/p-interface/pint-1/l-interfaces/l-interface/lint-1");
	}


}