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=========================================================
21 package org.onap.aai.serialization.db;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.*;
26 import java.io.UnsupportedEncodingException;
28 import java.net.URISyntaxException;
30 import java.util.stream.Collectors;
32 import org.apache.tinkerpop.gremlin.structure.Graph;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.janusgraph.core.JanusGraphFactory;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
42 import org.onap.aai.AAISetup;
43 import org.onap.aai.db.props.AAIProperties;
44 import org.onap.aai.edges.EdgeIngestor;
45 import org.onap.aai.exceptions.AAIException;
46 import org.onap.aai.introspection.Introspector;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.introspection.ModelType;
49 import org.onap.aai.parsers.query.QueryParser;
50 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.setup.SchemaVersion;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.test.annotation.DirtiesContext;
57 @RunWith(value = Parameterized.class)
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class DbSerializerNotificationEventsTest extends AAISetup {
61 // to use, set thrown.expect to whatever your test needs
62 // this line establishes default of expecting no exception to be thrown
64 public ExpectedException thrown = ExpectedException.none();
66 protected static Graph graph;
69 protected EdgeSerializer edgeSer;
71 protected EdgeIngestor ei;
73 private SchemaVersion version;
74 private final ModelType introspectorFactoryType = ModelType.MOXY;
75 private Loader loader;
76 private TransactionalGraphEngine engine; // for tests that aren't mocking the engine
78 @Parameterized.Parameter(value = 0)
79 public QueryStyle queryStyle;
81 @Parameterized.Parameters(name = "QueryStyle.{0}")
82 public static Collection<Object[]> data() {
83 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
87 public static void init() throws Exception {
88 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
93 public void setup() throws Exception {
95 version = schemaVersions.getDefaultVersion();
96 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
97 engine = new JanusGraphDBEngine(queryStyle, loader);
102 * Create Pserver with pinterface and relationship to complex
103 * Update pserver removing relationship to complex
104 * Update pserver adding a second p-interface
105 * Add l-interface directly to the 2nd p-interface
109 public void createComplexPserverWithRelUpdatePserverToDeleteRelAddPinterfaceThenDeleteComplexCheckingUpdatedListTest()
110 throws AAIException, UnsupportedEncodingException, URISyntaxException {
111 engine.startTransaction();
113 System.out.println("Create Complex");
114 DBSerializer dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "create-complex",
115 AAIProperties.MINIMUM_DEPTH);
116 Introspector complex = loader.introspectorFromName("complex");
117 Vertex complexV = dbserLocal.createNewVertex(complex);
118 final String complexUri = "/cloud-infrastructure/complexes/complex/c-id-b";
119 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(complexUri));
121 complex.setValue("physical-location-id", "c-id-b");
122 complex.setValue("physical-location-type", "type");
123 complex.setValue("street1", "streetA");
124 complex.setValue("city", "cityA");
125 complex.setValue("postal-code", "11111");
126 complex.setValue("country", "abc");
127 complex.setValue("region", "ef");
128 dbserLocal.serializeToDb(complex, complexV, uriQuery, "complex", complex.marshal(false));
130 assertTrue("Complex created", engine.tx().traversal().V().has("aai-node-type", "complex")
131 .has("physical-location-id", "c-id-b").hasNext());
132 Map<Vertex, Boolean> updated = getUpdatedVertexes(dbserLocal);
133 assertEquals("Number of updated vertexes", 1, updated.size());
134 assertThat("Only modified vertexes are in the updated set",
135 updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
136 is(Collections.singleton(complexUri)));
137 List<String> didNotUpdateStandardVertexProps = updated.entrySet().stream().filter(e -> !e.getValue())
138 .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
139 assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps,
140 is(Collections.emptyList()));
142 System.out.println("Create Pserver with pinterface and relationship to complex ");
143 dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "create-pserver",
144 AAIProperties.MINIMUM_DEPTH);
145 Introspector pserver = loader.introspectorFromName("pserver");
146 Vertex pserverV = dbserLocal.createNewVertex(pserver);
147 final String pserverUri = "/cloud-infrastructure/pservers/pserver/ps-b";
148 uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(pserverUri));
150 Introspector relationship = loader.introspectorFromName("relationship");
151 relationship.setValue("related-to", "complex");
152 relationship.setValue("related-link", complexUri);
153 Introspector relationshipList = loader.introspectorFromName("relationship-list");
154 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
156 pserver.setValue("relationship-list", relationshipList.getUnderlyingObject());
157 pserver.setValue("hostname", "ps-b");
158 pserver.setValue("number-of-cpus", 20);
160 Introspector pint = loader.introspectorFromName("p-interface");
161 pint.setValue("interface-name", "pint-1");
162 final String pintUri = pserverUri + "/p-interfaces/p-interface/pint-1";
164 Introspector pints = loader.introspectorFromName("p-interfaces");
165 pints.setValue("p-interface", Collections.singletonList(pint.getUnderlyingObject()));
166 pserver.setValue("p-interfaces", pints.getUnderlyingObject());
167 dbserLocal.serializeToDb(pserver, pserverV, uriQuery, "pserver", pserver.marshal(false));
169 assertTrue("Pserver created",
170 engine.tx().traversal().V().has("aai-node-type", "pserver").has("hostname", "ps-b").hasNext());
171 assertTrue("Pserver has edge to complex", engine.tx().traversal().V().has("aai-node-type", "pserver")
172 .has("hostname", "ps-b").bothE().otherV().has("aai-node-type", "complex").hasNext());
173 assertTrue("Pserver has edge to pinterface", engine.tx().traversal().V().has("aai-node-type", "pserver")
174 .has("hostname", "ps-b").bothE().otherV().has("aai-node-type", "p-interface").hasNext());
175 updated = getUpdatedVertexes(dbserLocal);
176 assertEquals("Number of updated vertexes", 3, updated.size());
177 assertThat("Only modified vertexes are in the updated set",
178 updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
179 is(new HashSet<>(Arrays.asList(pserverUri, pintUri, complexUri))));
180 didNotUpdateStandardVertexProps = updated.entrySet().stream().filter(e -> !e.getValue())
181 .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
182 assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps,
183 is(Collections.emptyList()));
185 System.out.println("Update pserver removing relationship to complex");
186 dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "update-pserver",
187 AAIProperties.MINIMUM_DEPTH);
188 pserver = dbserLocal.getLatestVersionView(pserverV);
189 relationshipList = loader.introspectorFromName("relationship-list");
190 relationshipList.setValue("relationship", Collections.emptyList());
191 pserver.setValue("relationship-list", relationshipList.getUnderlyingObject());
192 pserver.setValue("equip-type", "server-a");
193 pserver.setValue("number-of-cpus", 99);
194 dbserLocal.serializeToDb(pserver, pserverV, uriQuery, "pserver", pserver.marshal(false));
196 assertFalse("Pserver no longer has edge to complex", engine.tx().traversal().V().has("aai-node-type", "pserver")
197 .has("hostname", "ps-b").bothE().otherV().has("aai-node-type", "complex").hasNext());
198 updated = getUpdatedVertexes(dbserLocal);
199 assertEquals("Number of updated vertexes", 2, updated.size());
200 assertThat("Only modified vertexes are in the updated set",
201 updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
202 is(new HashSet<>(Arrays.asList(pserverUri, complexUri))));
203 didNotUpdateStandardVertexProps = updated.entrySet().stream().filter(e -> !e.getValue())
204 .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
205 assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps,
206 is(Collections.emptyList()));
208 System.out.println("Update pserver adding a second p-interface");
209 dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "update-pserver",
210 AAIProperties.MINIMUM_DEPTH);
211 pserver = dbserLocal.getLatestVersionView(pserverV);
212 Introspector pint2 = loader.introspectorFromName("p-interface");
213 pint2.setValue("interface-name", "pint-2");
214 pints = pserver.getWrappedValue("p-interfaces");
215 List<Object> pintList = pserver.getWrappedValue("p-interfaces").getWrappedListValue("p-interface").stream()
216 .map(Introspector::getUnderlyingObject).collect(Collectors.toList());
217 pintList.add(pint2.getUnderlyingObject());
218 pints.setValue("p-interface", pintList);
219 pserver.setValue("p-interfaces", pints.getUnderlyingObject());
220 final String pint2Uri = pserverUri + "/p-interfaces/p-interface/pint-2";
221 dbserLocal.serializeToDb(pserver, pserverV, uriQuery, "pserver", pserver.marshal(false));
223 assertTrue("Pserver has edge to pinterface 2",
224 engine.tx().traversal().V().has("aai-node-type", "pserver").has("hostname", "ps-b").in()
225 .has("aai-node-type", "p-interface").has("interface-name", "pint-2").hasNext());
226 assertTrue("p-interface 2 created", engine.tx().traversal().V().has("aai-node-type", "p-interface")
227 .has("interface-name", "pint-2").has(AAIProperties.AAI_URI, pint2Uri).hasNext());
228 updated = getUpdatedVertexes(dbserLocal);
229 assertEquals("Number of updated vertexes", 1, updated.size());
230 assertThat("Only modified vertexes are in the updated set",
231 updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
232 is(Collections.singleton(pint2Uri)));
233 didNotUpdateStandardVertexProps = updated.entrySet().stream().filter(e -> !e.getValue())
234 .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
235 assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps,
236 is(Collections.emptyList()));
238 System.out.println("Add l-interface directly to the 2nd p-interface");
239 dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "create-pserver",
240 AAIProperties.MINIMUM_DEPTH);
241 Introspector lInt = loader.introspectorFromName("l-interface");
242 Vertex lIntV = dbserLocal.createNewVertex(lInt);
243 final String lIntUri = pint2Uri + "/l-interfaces/l-interface/lint-1";
244 uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(lIntUri));
245 lInt.setValue("interface-name", "lint-1");
246 dbserLocal.serializeToDb(lInt, lIntV, uriQuery, "l-interface", lInt.marshal(false));
248 assertTrue("l-interface created", engine.tx().traversal().V().has("aai-node-type", "l-interface")
249 .has("interface-name", "lint-1").hasNext());
251 assertTrue("Pserver has edge to pinterface to l-interface",
252 engine.tx().traversal().V().has("aai-node-type", "pserver").has("hostname", "ps-b").bothE().otherV()
253 .has("aai-node-type", "p-interface").bothE().otherV().has("aai-node-type", "l-interface")
255 updated = getUpdatedVertexes(dbserLocal);
256 assertEquals("Number of updated vertexes", 1, updated.size());
257 assertThat("Only modified vertexes are in the updated set",
258 updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
259 is(new HashSet<>(Collections.singletonList(lIntUri))));
260 didNotUpdateStandardVertexProps = updated.entrySet().stream().filter(e -> !e.getValue())
261 .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
262 assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps,
263 is(Collections.emptyList()));
265 System.out.println("Delete pserver");
266 dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "delete-pserver",
267 AAIProperties.MINIMUM_DEPTH);
268 pserver = dbserLocal.getLatestVersionView(pserverV);
269 String rv = pserver.getValue(AAIProperties.RESOURCE_VERSION);
270 dbserLocal.delete(engine.tx().traversal().V(pserverV).next(), rv, true);
272 assertFalse("pserver no longer exists", engine.tx().traversal().V().has("aai-node-type", "pserver").hasNext());
273 updated = getUpdatedVertexes(dbserLocal);
274 assertEquals("Number of updated vertexes", 0, updated.size());
275 didNotUpdateStandardVertexProps = updated.entrySet().stream().filter(e -> !e.getValue())
276 .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
277 assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps,
278 is(Collections.emptyList()));
282 private Map<Vertex, Boolean> getUpdatedVertexes(DBSerializer dbserLocal) {
283 Map<Vertex, Boolean> updated = new LinkedHashMap<>(dbserLocal.getUpdatedVertexes());
284 dbserLocal.touchStandardVertexPropertiesForEdges().forEach(v -> updated.putIfAbsent(v, true));