0c90c7355a056ae88d5ab19e7824ac310138afa8
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / OneToOneEdgeUpdateTest.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 org.apache.tinkerpop.gremlin.structure.Graph;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraphFactory;
26 import org.junit.*;
27 import org.junit.rules.ExpectedException;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.edges.EdgeIngestor;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.introspection.ModelType;
35 import org.onap.aai.parsers.query.QueryParser;
36 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
37 import org.onap.aai.serialization.engines.QueryStyle;
38 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
39 import org.onap.aai.setup.SchemaVersion;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.test.annotation.DirtiesContext;
42
43 import java.io.UnsupportedEncodingException;
44 import java.net.URI;
45 import java.net.URISyntaxException;
46 import java.util.Arrays;
47 import java.util.Collections;
48
49 import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
50 import static org.hamcrest.Matchers.*;
51 import static org.junit.Assert.*;
52
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
54 public class OneToOneEdgeUpdateTest extends AAISetup {
55
56     // to use, set thrown.expect to whatever your test needs
57     // this line establishes default of expecting no exception to be thrown
58     @Rule
59     public ExpectedException thrown = ExpectedException.none();
60
61     protected static Graph graph;
62
63     @Autowired
64     protected EdgeSerializer edgeSer;
65     @Autowired
66     protected EdgeIngestor ei;
67
68     private SchemaVersion version;
69     private final ModelType introspectorFactoryType = ModelType.MOXY;
70     private Loader loader;
71     private TransactionalGraphEngine engine;
72
73     public QueryStyle queryStyle = QueryStyle.TRAVERSAL_URI;
74
75     public static final String SOURCE_OF_TRUTH = "EdgerPairCanBeBothCousinAndParentChildTest";
76     private static final String gvnfAUri = "/network/generic-vnfs/generic-vnf/gvnf-a" + SOURCE_OF_TRUTH;
77     private static final String lintUri = gvnfAUri + "/l-interfaces/l-interface/lint";
78     private static final String lintAUri = gvnfAUri + "/l-interfaces/l-interface/lint-a";
79     private static final String sriovVfUri = lintUri + "/sriov-vfs/sriov-vf/sriov-vf";
80     private static final String sriovVfAUri = lintAUri + "/sriov-vfs/sriov-vf/sriov-vf-a";
81
82     private static final String gvnfBUri = "/network/generic-vnfs/generic-vnf/gvnf-b" + SOURCE_OF_TRUTH;
83     private static final String lIntBUri = gvnfBUri + "/l-interfaces/l-interface/lint-b";
84
85     @BeforeClass
86     public static void init() {
87         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
88
89     }
90
91     @Before
92     public void setup() throws UnsupportedEncodingException, AAIException, URISyntaxException {
93         version = schemaVersions.getDefaultVersion();
94         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
95         engine = new JanusGraphDBEngine(queryStyle, loader);
96         initData();
97     }
98
99     @After
100     public void cleanup() {
101         engine.rollback();
102     }
103
104     /**
105      * Using latest version (see schema-ingest.properties) - sriov-vf to l-interface is one ot one
106      * Create generic-vnf with l-interface that has an sriov-vf
107      * Create generic-vnf with l-interface relationship the sriov-vf
108      */
109     private void initData() throws UnsupportedEncodingException, AAIException, URISyntaxException {
110         engine.startTransaction();
111         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
112
113
114         Introspector gvnf = loader.introspectorFromName("generic-vnf");
115         gvnf.setValue("vnf-id", "gvnf-a" + SOURCE_OF_TRUTH);
116         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
117
118         Introspector lint = loader.introspectorFromName("l-interface");
119         lint.setValue("interface-name", "lint");
120
121         Introspector sriovVf = loader.introspectorFromName("sriov-vf");
122         sriovVf.setValue("pci-id", "sriov-vf");
123
124         Introspector sriovVfs = loader.introspectorFromName("sriov-vfs");
125         sriovVfs.setValue("sriov-vf", Collections.singletonList(sriovVf.getUnderlyingObject()));
126         lint.setValue("sriov-vfs", sriovVfs.getUnderlyingObject());
127
128         Introspector lintA = loader.introspectorFromName("l-interface");
129         lintA.setValue("interface-name", "lint-a");
130
131         Introspector sriovVfA = loader.introspectorFromName("sriov-vf");
132         sriovVfA.setValue("pci-id", "sriov-vf-a");
133
134         sriovVfs = loader.introspectorFromName("sriov-vfs");
135         sriovVfs.setValue("sriov-vf", Collections.singletonList(sriovVfA.getUnderlyingObject()));
136         lintA.setValue("sriov-vfs", sriovVfs.getUnderlyingObject());
137
138         Introspector lints = loader.introspectorFromName("l-interfaces");
139         lints.setValue("l-interface", Arrays.asList(lint.getUnderlyingObject(), lintA.getUnderlyingObject()));
140         gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
141
142
143         System.out.println(gvnf.marshal(true));
144         Vertex gvnfV = serializer.createNewVertex(gvnf);
145         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
146         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
147
148         assertTrue("generic-vnf-a created", engine.tx().traversal().V()
149             .has(AAIProperties.AAI_URI, gvnfAUri)
150             .hasNext());
151         assertTrue("l-int created", engine.tx().traversal().V()
152             .has(AAIProperties.AAI_URI, lintUri)
153             .hasNext());
154         assertTrue("l-int-a created", engine.tx().traversal().V()
155             .has(AAIProperties.AAI_URI, lintAUri)
156             .hasNext());
157         assertTrue("sriov-vf created", engine.tx().traversal().V()
158             .has(AAIProperties.AAI_URI, sriovVfUri)
159             .hasNext());
160         assertTrue("sriov-vf-a created", engine.tx().traversal().V()
161             .has(AAIProperties.AAI_URI, sriovVfAUri)
162             .hasNext());
163
164
165         gvnf = loader.introspectorFromName("generic-vnf");
166         gvnf.setValue("vnf-id", "gvnf-b" + SOURCE_OF_TRUTH);
167         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
168
169         lint = loader.introspectorFromName("l-interface");
170         lint.setValue("interface-name", "lint-b");
171         Introspector relationship = loader.introspectorFromName("relationship");
172         relationship.setValue("related-link", sriovVfUri);
173         Introspector relationshipList = loader.introspectorFromName("relationship-list");
174         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
175         lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
176
177         lints = loader.introspectorFromName("l-interfaces");
178         lints.setValue("l-interface", Collections.singletonList(lint.getUnderlyingObject()));
179         gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
180
181         gvnfV = serializer.createNewVertex(gvnf);
182         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
183         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
184
185         engine.tx().traversal().V().forEachRemaining(v -> System.out.println(v.<String>value(AAIProperties.AAI_URI)));
186         assertTrue("generic-vnf-b created", engine.tx().traversal().V()
187             .has(AAIProperties.AAI_URI, gvnfBUri)
188             .hasNext());
189         assertTrue("l-int-b created", engine.tx().traversal().V()
190             .has(AAIProperties.AAI_URI, lIntBUri)
191             .hasNext());
192         assertTrue("l-interface relationship sriov-vf created", engine.tx().traversal().V()
193             .has(AAIProperties.AAI_URI, lIntBUri)
194             .both()
195             .has(AAIProperties.AAI_URI, sriovVfUri)
196             .hasNext());
197
198     }
199
200
201     @Test
202     public void verifyReadOfGenericVnfATest() throws AAIException, UnsupportedEncodingException {
203         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
204
205         String gvnfALatestView = serializer.getLatestVersionView(
206             engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next()).marshal(false);
207
208         assertThat(gvnfALatestView,
209             hasJsonPath(
210                 "$.l-interfaces.l-interface[*]",
211                 hasSize(2)
212             ));
213         assertThat(gvnfALatestView,
214             hasJsonPath(
215                 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]",
216                 hasSize(2)
217             ));
218         assertThat(gvnfALatestView,
219             hasJsonPath(
220                 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*].relationship-list.relationship[*].related-link",
221                 containsInAnyOrder(
222                     "/aai/" + schemaVersions.getDefaultVersion() + lIntBUri
223                 )
224             ));
225     }
226
227     @Test
228     public void verifyReadOfGenericVnfBTest() throws AAIException, UnsupportedEncodingException {
229         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
230
231         String gvnfBLatestView = serializer.getLatestVersionView(
232             engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).next()).marshal(false);
233
234         assertThat(gvnfBLatestView,
235             hasJsonPath(
236                 "$.l-interfaces.l-interface[*]",
237                 hasSize(1)
238             ));
239         assertThat(gvnfBLatestView,
240             not(hasJsonPath(
241                 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]"
242             )));
243         assertThat(gvnfBLatestView,
244             hasJsonPath(
245                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
246                 containsInAnyOrder(
247                     "/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri
248                 )
249             ));
250     }
251
252     @Test
253     public void replaceRelationshipToSriovVfTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
254         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
255
256         Introspector lint = serializer.getLatestVersionView(
257             engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next());
258         String lintView = lint.marshal(false);
259
260         assertThat(lintView,
261             hasJsonPath(
262                 "$.relationship-list.relationship[*].related-link",
263                 containsInAnyOrder(
264                     "/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri
265                 )
266             ));
267
268         Introspector relationship = loader.introspectorFromName("relationship");
269         relationship.setValue("related-link", sriovVfAUri);
270         Introspector relationshipList = loader.introspectorFromName("relationship-list");
271         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
272         lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
273
274         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(lIntBUri));
275         serializer.serializeToDb(lint, engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next(),
276             uriQuery, "generic-vnf", lint.marshal(false));
277     }
278
279
280     @Test
281     public void createRelationshipForNonExistentRuleTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
282         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
283
284         Vertex gvnfAV = engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next();
285         Introspector gvnfA = serializer.getLatestVersionView(gvnfAV);
286         Introspector relationship = loader.introspectorFromName("relationship");
287         relationship.setValue("related-link", gvnfBUri);
288         Introspector relationshipList = loader.introspectorFromName("relationship-list");
289         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
290         gvnfA.setValue("relationship-list", relationshipList.getUnderlyingObject());
291         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
292         try {
293             serializer.serializeToDb(gvnfA, gvnfAV, uriQuery, "generic-vnf", gvnfA.marshal(false));
294         } catch (AAIException e) {
295             assertEquals("AAI_6120", e.getCode());
296             assertThat(e.getMessage(), containsString("generic-vnf"));
297         }
298     }
299
300 }