Merge "[AAI] Fix doc config files"
[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 static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
24 import static org.hamcrest.Matchers.*;
25 import static org.junit.Assert.*;
26
27 import java.io.UnsupportedEncodingException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.Arrays;
31 import java.util.Collections;
32
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.janusgraph.core.JanusGraphFactory;
36 import org.junit.*;
37 import org.junit.rules.ExpectedException;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.db.props.AAIProperties;
40 import org.onap.aai.edges.EdgeIngestor;
41 import org.onap.aai.exceptions.AAIException;
42 import org.onap.aai.introspection.Introspector;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.ModelType;
45 import org.onap.aai.parsers.query.QueryParser;
46 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49 import org.onap.aai.setup.SchemaVersion;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.test.annotation.DirtiesContext;
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,
112                 AAIProperties.MINIMUM_DEPTH);
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         System.out.println(gvnf.marshal(true));
143         Vertex gvnfV = serializer.createNewVertex(gvnf);
144         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
145         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
146
147         assertTrue("generic-vnf-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).hasNext());
148         assertTrue("l-int created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintUri).hasNext());
149         assertTrue("l-int-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintAUri).hasNext());
150         assertTrue("sriov-vf created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, sriovVfUri).hasNext());
151         assertTrue("sriov-vf-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, sriovVfAUri).hasNext());
152
153         gvnf = loader.introspectorFromName("generic-vnf");
154         gvnf.setValue("vnf-id", "gvnf-b" + SOURCE_OF_TRUTH);
155         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
156
157         lint = loader.introspectorFromName("l-interface");
158         lint.setValue("interface-name", "lint-b");
159         Introspector relationship = loader.introspectorFromName("relationship");
160         relationship.setValue("related-link", sriovVfUri);
161         Introspector relationshipList = loader.introspectorFromName("relationship-list");
162         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
163         lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
164
165         lints = loader.introspectorFromName("l-interfaces");
166         lints.setValue("l-interface", Collections.singletonList(lint.getUnderlyingObject()));
167         gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
168
169         gvnfV = serializer.createNewVertex(gvnf);
170         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
171         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
172
173         engine.tx().traversal().V().forEachRemaining(v -> System.out.println(v.<String>value(AAIProperties.AAI_URI)));
174         assertTrue("generic-vnf-b created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).hasNext());
175         assertTrue("l-int-b created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).hasNext());
176         assertTrue("l-interface relationship sriov-vf created", engine.tx().traversal().V()
177                 .has(AAIProperties.AAI_URI, lIntBUri).both().has(AAIProperties.AAI_URI, sriovVfUri).hasNext());
178
179     }
180
181     @Test
182     public void verifyReadOfGenericVnfATest() throws AAIException, UnsupportedEncodingException {
183         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
184                 AAIProperties.MINIMUM_DEPTH);
185
186         String gvnfALatestView =
187                 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next())
188                         .marshal(false);
189
190         assertThat(gvnfALatestView, hasJsonPath("$.l-interfaces.l-interface[*]", hasSize(2)));
191         assertThat(gvnfALatestView, hasJsonPath("$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]", hasSize(2)));
192         assertThat(gvnfALatestView, hasJsonPath(
193                 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*].relationship-list.relationship[*].related-link",
194                 containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + lIntBUri)));
195     }
196
197     @Test
198     public void verifyReadOfGenericVnfBTest() throws AAIException, UnsupportedEncodingException {
199         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
200                 AAIProperties.MINIMUM_DEPTH);
201
202         String gvnfBLatestView =
203                 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).next())
204                         .marshal(false);
205
206         assertThat(gvnfBLatestView, hasJsonPath("$.l-interfaces.l-interface[*]", hasSize(1)));
207         assertThat(gvnfBLatestView, not(hasJsonPath("$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]")));
208         assertThat(gvnfBLatestView,
209                 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
210                         containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri)));
211     }
212
213     @Test
214     public void replaceRelationshipToSriovVfTest()
215             throws AAIException, UnsupportedEncodingException, URISyntaxException {
216         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
217                 AAIProperties.MINIMUM_DEPTH);
218
219         Introspector lint = serializer
220                 .getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next());
221         String lintView = lint.marshal(false);
222
223         assertThat(lintView, hasJsonPath("$.relationship-list.relationship[*].related-link",
224                 containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri)));
225
226         Introspector relationship = loader.introspectorFromName("relationship");
227         relationship.setValue("related-link", sriovVfAUri);
228         Introspector relationshipList = loader.introspectorFromName("relationship-list");
229         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
230         lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
231
232         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(lIntBUri));
233         serializer.serializeToDb(lint, engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next(),
234                 uriQuery, "generic-vnf", lint.marshal(false));
235     }
236
237     @Test
238     public void createRelationshipForNonExistentRuleTest()
239             throws AAIException, UnsupportedEncodingException, URISyntaxException {
240         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
241                 AAIProperties.MINIMUM_DEPTH);
242
243         Vertex gvnfAV = engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next();
244         Introspector gvnfA = serializer.getLatestVersionView(gvnfAV);
245         Introspector relationship = loader.introspectorFromName("relationship");
246         relationship.setValue("related-link", gvnfBUri);
247         Introspector relationshipList = loader.introspectorFromName("relationship-list");
248         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
249         gvnfA.setValue("relationship-list", relationshipList.getUnderlyingObject());
250         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
251         try {
252             serializer.serializeToDb(gvnfA, gvnfAV, uriQuery, "generic-vnf", gvnfA.marshal(false));
253         } catch (AAIException e) {
254             assertEquals("AAI_6120", e.getCode());
255             assertThat(e.getMessage(), containsString("generic-vnf"));
256         }
257     }
258
259 }