1dcdccbf5852e5c2e87fcc368e39acccd13fe17d
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / VersionedScenariosTest.java
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
21 package org.onap.aai.serialization.db;
22
23 import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
24 import static org.hamcrest.Matchers.*;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.UnsupportedEncodingException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35 import org.apache.tinkerpop.gremlin.structure.Graph;
36 import org.apache.tinkerpop.gremlin.structure.Vertex;
37 import org.janusgraph.core.JanusGraphFactory;
38 import org.junit.*;
39 import org.junit.rules.ExpectedException;
40 import org.onap.aai.AAISetup;
41 import org.onap.aai.db.props.AAIProperties;
42 import org.onap.aai.edges.EdgeIngestor;
43 import org.onap.aai.exceptions.AAIException;
44 import org.onap.aai.introspection.Introspector;
45 import org.onap.aai.introspection.Loader;
46 import org.onap.aai.introspection.ModelType;
47 import org.onap.aai.parsers.query.QueryParser;
48 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
49 import org.onap.aai.serialization.engines.QueryStyle;
50 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
51 import org.onap.aai.setup.SchemaVersion;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.test.annotation.DirtiesContext;
54
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class VersionedScenariosTest extends AAISetup {
57
58     // to use, set thrown.expect to whatever your test needs
59     // this line establishes default of expecting no exception to be thrown
60     @Rule
61     public ExpectedException thrown = ExpectedException.none();
62
63     protected static Graph graph;
64
65     @Autowired
66     protected EdgeSerializer edgeSer;
67     @Autowired
68     protected EdgeIngestor ei;
69
70     private SchemaVersion version;
71     private final ModelType introspectorFactoryType = ModelType.MOXY;
72     private Loader loader;
73     private TransactionalGraphEngine engine;
74
75     public QueryStyle queryStyle = QueryStyle.TRAVERSAL_URI;
76
77     public static final String SOURCE_OF_TRUTH = "VersionedScenariosTest";
78     private static final String gvnfUri = "/network/generic-vnfs/generic-vnf/gvnf" + SOURCE_OF_TRUTH;
79     private static final String llDefaultUri = "/network/logical-links/logical-link/llDefault";
80     private static final String lintSourceUri = gvnfUri + "/l-interfaces/l-interface/source";
81     private static final String lintDestinationUri = gvnfUri + "/l-interfaces/l-interface/destination";
82     private static final String llLabeledUri = "/network/logical-links/logical-link/llLabeled";
83
84     @BeforeClass
85     public static void init() {
86         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
87
88     }
89
90     @Before
91     public void setup() throws UnsupportedEncodingException, AAIException, URISyntaxException {
92         version = schemaVersions.getDefaultVersion();
93         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
94         engine = new JanusGraphDBEngine(queryStyle, loader);
95         initData();
96     }
97
98     @After
99     public void cleanup() {
100         engine.rollback();
101     }
102
103     /**
104      * Using latest version (see schema-ingest.properties)
105      * Create generic-vnf with l-interfaces source, destination
106      * Create logical-link with relationship to interfaces source, destination (will use the default labels)
107      * Create logical-link with relationship to interfaces source, destination with specific labels
108      */
109     private void initData() throws UnsupportedEncodingException, AAIException, URISyntaxException {
110         engine.startTransaction();
111         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
112                 AAIProperties.MINIMUM_DEPTH);
113
114         Introspector gvnf = loader.introspectorFromName("generic-vnf");
115         gvnf.setValue("vnf-id", "gvnf" + SOURCE_OF_TRUTH);
116         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
117
118         Introspector lintSource = loader.introspectorFromName("l-interface");
119         lintSource.setValue("interface-name", "source");
120
121         Introspector lintDestination = loader.introspectorFromName("l-interface");
122         lintDestination.setValue("interface-name", "destination");
123
124         List<Object> lIntList = new ArrayList<>();
125         lIntList.add(lintSource.getUnderlyingObject());
126         lIntList.add(lintDestination.getUnderlyingObject());
127         Introspector lints = loader.introspectorFromName("l-interfaces");
128         lints.setValue("l-interface", lIntList);
129         gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
130
131         Vertex gvnfV = serializer.createNewVertex(gvnf);
132         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfUri));
133         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
134
135         assertTrue("generic-vnf created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).hasNext());
136         assertTrue("source created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintSourceUri).hasNext());
137         assertTrue("destination created",
138                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintDestinationUri).hasNext());
139
140         Introspector llDefault = loader.introspectorFromName("logical-link");
141         llDefault.setValue("link-name", "llDefault");
142         List<Object> relList = new ArrayList<>();
143         Introspector relationship = loader.introspectorFromName("relationship");
144         // relationship.setValue("related-to", "l-interface");
145         relationship.setValue("related-link", lintSourceUri);
146         relList.add(relationship.getUnderlyingObject());
147         relationship = loader.introspectorFromName("relationship");
148         // relationship.setValue("related-to", "l-interface");
149         relationship.setValue("related-link", lintDestinationUri);
150         relList.add(relationship.getUnderlyingObject());
151         Introspector relationshipList = loader.introspectorFromName("relationship-list");
152         relationshipList.setValue("relationship", relList);
153         llDefault.setValue("relationship-list", relationshipList.getUnderlyingObject());
154
155         Vertex llDefaultV = serializer.createNewVertex(llDefault);
156         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(llDefaultUri));
157         serializer.serializeToDb(llDefault, llDefaultV, uriQuery, "logical-link", llDefault.marshal(false));
158
159         assertTrue("logical-link created",
160                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).hasNext());
161         assertTrue("default source relationship created", engine.tx().traversal().V()
162                 .has(AAIProperties.AAI_URI, llDefaultUri).both().has(AAIProperties.AAI_URI, lintSourceUri).hasNext());
163         assertTrue("default destination relationship created",
164                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).both()
165                         .has(AAIProperties.AAI_URI, lintDestinationUri).hasNext());
166
167         Introspector llLabeled = loader.introspectorFromName("logical-link");
168         llLabeled.setValue("link-name", "llLabeled");
169         relList = new ArrayList<>();
170         relationship = loader.introspectorFromName("relationship");
171         relationship.setValue("related-to", "l-interface");
172         relationship.setValue("relationship-label", "org.onap.relationships.inventory.Source");
173         relationship.setValue("related-link", lintSourceUri);
174         relList.add(relationship.getUnderlyingObject());
175         relationship = loader.introspectorFromName("relationship");
176         relationship.setValue("related-to", "l-interface");
177         relationship.setValue("relationship-label", "org.onap.relationships.inventory.Destination");
178         relationship.setValue("related-link", lintDestinationUri);
179         relList.add(relationship.getUnderlyingObject());
180         relationshipList = loader.introspectorFromName("relationship-list");
181         relationshipList.setValue("relationship", relList);
182         llLabeled.setValue("relationship-list", relationshipList.getUnderlyingObject());
183
184         Vertex llLabeledV = serializer.createNewVertex(llLabeled);
185         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(llLabeledUri));
186         serializer.serializeToDb(llLabeled, llLabeledV, uriQuery, "logical-link", llLabeled.marshal(false));
187
188         assertTrue("logical-link created",
189                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri).hasNext());
190         assertTrue("labeled source relationship created",
191                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri)
192                         .both("org.onap.relationships.inventory.Source").has(AAIProperties.AAI_URI, lintSourceUri)
193                         .hasNext());
194         assertTrue("labeled destination relationship created",
195                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri)
196                         .both("org.onap.relationships.inventory.Destination")
197                         .has(AAIProperties.AAI_URI, lintDestinationUri).hasNext());
198     }
199
200     @Test
201     public void verifyRelsOfLatestViewOfGenericVnf() throws AAIException, UnsupportedEncodingException {
202         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
203                 AAIProperties.MINIMUM_DEPTH);
204
205         String gvnfLatestView =
206                 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next())
207                         .marshal(false);
208         assertThat(gvnfLatestView,
209                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*]", hasSize(4)));
210         assertThat(gvnfLatestView,
211                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
212                         containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + llDefaultUri,
213                                 "/aai/" + schemaVersions.getDefaultVersion() + llDefaultUri,
214                                 "/aai/" + schemaVersions.getDefaultVersion() + llLabeledUri,
215                                 "/aai/" + schemaVersions.getDefaultVersion() + llLabeledUri)));
216         assertThat(gvnfLatestView,
217                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].relationship-label",
218                         containsInAnyOrder("tosca.relationships.network.LinksTo", "tosca.relationships.network.LinksTo",
219                                 "org.onap.relationships.inventory.Source",
220                                 "org.onap.relationships.inventory.Destination")));
221     }
222
223     @Test
224     public void verifyRelsOfLatestViewOfLLDefault() throws AAIException, UnsupportedEncodingException {
225         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
226                 AAIProperties.MINIMUM_DEPTH);
227
228         String llDefaultLatestView = serializer
229                 .getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).next())
230                 .marshal(false);
231         assertThat(llDefaultLatestView, hasJsonPath("$.relationship-list.relationship[*]", hasSize(2)));
232         assertThat(llDefaultLatestView,
233                 hasJsonPath("$.relationship-list.relationship[*].related-link",
234                         containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + lintSourceUri,
235                                 "/aai/" + schemaVersions.getDefaultVersion() + lintDestinationUri)));
236         assertThat(llDefaultLatestView, hasJsonPath("$.relationship-list.relationship[*].relationship-label",
237                 containsInAnyOrder("tosca.relationships.network.LinksTo", "tosca.relationships.network.LinksTo")));
238         assertThat(llDefaultLatestView, hasJsonPath("$.relationship-list.relationship[*].relationship-label", not(
239                 contains("org.onap.relationships.inventory.Source", "org.onap.relationships.inventory.Destination"))));
240
241     }
242
243     @Test
244     public void verifyRelsOfLatestViewOfLLLabeled() throws AAIException, UnsupportedEncodingException {
245         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
246                 AAIProperties.MINIMUM_DEPTH);
247
248         String llLabeledLatestView = serializer
249                 .getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri).next())
250                 .marshal(false);
251         assertThat(llLabeledLatestView, hasJsonPath("$.relationship-list.relationship[*]", hasSize(2)));
252         assertThat(llLabeledLatestView,
253                 hasJsonPath("$.relationship-list.relationship[*].related-link",
254                         containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + lintSourceUri,
255                                 "/aai/" + schemaVersions.getDefaultVersion() + lintDestinationUri)));
256         assertThat(llLabeledLatestView, hasJsonPath("$.relationship-list.relationship[*].relationship-label",
257                 not(containsInAnyOrder("tosca.relationships.network.LinksTo", "tosca.relationships.network.LinksTo"))));
258         assertThat(llLabeledLatestView, hasJsonPath("$.relationship-list.relationship[*].relationship-label",
259                 contains("org.onap.relationships.inventory.Source", "org.onap.relationships.inventory.Destination")));
260
261     }
262
263     @Test
264     public void verifyRelsOfOldViewOfGenericVnf() throws AAIException, UnsupportedEncodingException {
265         SchemaVersion oldVersion = new SchemaVersion("v11");
266         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
267         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
268                 AAIProperties.MINIMUM_DEPTH);
269
270         String gvnfOldView = oldSerializer
271                 .dbToObject(
272                         Collections
273                                 .singletonList(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next()),
274                         oldLoader.introspectorFromName("generic-vnf"), AAIProperties.MAXIMUM_DEPTH, false, "false")
275                 .marshal(false);
276         assertThat(gvnfOldView,
277                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*]", hasSize(2)));
278         assertThat(gvnfOldView,
279                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].relationship-label",
280                         emptyCollectionOf(String.class)));
281         assertThat(gvnfOldView,
282                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
283                         containsInAnyOrder("/aai/" + oldVersion + llDefaultUri, "/aai/" + oldVersion + llDefaultUri)));
284     }
285
286     @Test
287     public void verifyRelsOfOldViewOfLLDefault() throws AAIException, UnsupportedEncodingException {
288         SchemaVersion oldVersion = new SchemaVersion("v11");
289         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
290         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
291                 AAIProperties.MINIMUM_DEPTH);
292
293         String llDefaultOldView = oldSerializer
294                 .dbToObject(
295                         Collections.singletonList(
296                                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).next()),
297                         oldLoader.introspectorFromName("logical-link"), AAIProperties.MAXIMUM_DEPTH, false, "false")
298                 .marshal(false);
299         assertThat(llDefaultOldView, hasJsonPath("$.relationship-list.relationship[*]", hasSize(2)));
300         assertThat(llDefaultOldView,
301                 hasJsonPath("$.relationship-list.relationship[*].relationship-label", emptyCollectionOf(String.class)));
302         assertThat(llDefaultOldView, hasJsonPath("$.relationship-list.relationship[*].related-link",
303                 containsInAnyOrder("/aai/" + oldVersion + lintSourceUri, "/aai/" + oldVersion + lintDestinationUri)));
304
305     }
306
307     @Test
308     public void verifyRelsOfOldViewOfLLLabeled() throws AAIException, UnsupportedEncodingException {
309         SchemaVersion oldVersion = new SchemaVersion("v11");
310         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
311         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
312                 AAIProperties.MINIMUM_DEPTH);
313
314         String llLabeledtOldView = oldSerializer
315                 .dbToObject(
316                         Collections.singletonList(
317                                 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri).next()),
318                         oldLoader.introspectorFromName("logical-link"), AAIProperties.MAXIMUM_DEPTH, false, "false")
319                 .marshal(false);
320         assertThat(llLabeledtOldView, not(hasJsonPath("$.relationship-list.relationship[*]")));
321     }
322
323     @Test
324     public void useOldVersionToUpdatedGenericVnfAndVerifyLatestVersionRels()
325             throws AAIException, UnsupportedEncodingException, URISyntaxException {
326         SchemaVersion oldVersion = new SchemaVersion("v11");
327         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
328         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
329                 AAIProperties.MINIMUM_DEPTH);
330
331         Vertex oldGvnfV = engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next();
332         Introspector oldGvnf = oldSerializer.dbToObject(Collections.singletonList(oldGvnfV),
333                 oldLoader.introspectorFromName("generic-vnf"), AAIProperties.MAXIMUM_DEPTH, false, "false");
334         assertThat(oldGvnf.marshal(false),
335                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
336                         containsInAnyOrder("/aai/" + oldVersion + llDefaultUri, "/aai/" + oldVersion + llDefaultUri)));
337         oldGvnf.setValue("in-maint", true);
338         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfUri));
339         oldSerializer.serializeToDb(oldGvnf, oldGvnfV, uriQuery, "generic-vnf", oldGvnf.marshal(false));
340
341         verifyRelsOfLatestViewOfGenericVnf();
342         verifyRelsOfOldViewOfGenericVnf();
343     }
344
345 }