[AAI] Fix doc config files
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / EdgerPairCanBeBothCousinAndParentChildTest.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.Collections;
47
48 import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
49 import static org.hamcrest.Matchers.*;
50 import static org.junit.Assert.assertThat;
51 import static org.junit.Assert.assertTrue;
52
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
54 public class EdgerPairCanBeBothCousinAndParentChildTest 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 lagIntAUri = gvnfAUri + "/lag-interfaces/lag-interface/lagint-a";
78     private static final String lintUri = lagIntAUri + "/l-interfaces/l-interface/lint";
79
80     private static final String gvnfBUri = "/network/generic-vnfs/generic-vnf/gvnf-b" + SOURCE_OF_TRUTH;
81     private static final String lagIntBUri = gvnfBUri + "/lag-interfaces/lag-interface/lagint-b";
82
83     @BeforeClass
84     public static void init() {
85         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
86
87     }
88
89     @Before
90     public void setup() throws UnsupportedEncodingException, AAIException, URISyntaxException {
91         version = schemaVersions.getDefaultVersion();
92         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
93         engine = new JanusGraphDBEngine(queryStyle, loader);
94         initData();
95     }
96
97     @After
98     public void cleanup() {
99         engine.rollback();
100     }
101
102     /**
103      * Using latest version (see schema-ingest.properties)
104      * Create generic-vnf with lag-interface that has an l-interface
105      * Create generic-vnf with lag-interface relationship the l-interface
106      */
107     private void initData() throws UnsupportedEncodingException, AAIException, URISyntaxException {
108         engine.startTransaction();
109         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
110
111
112         Introspector gvnf = loader.introspectorFromName("generic-vnf");
113         gvnf.setValue("vnf-id", "gvnf-a" + SOURCE_OF_TRUTH);
114         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
115
116         Introspector lagInt = loader.introspectorFromName("lag-interface");
117         lagInt.setValue("interface-name", "lagint-a");
118
119         Introspector lint = loader.introspectorFromName("l-interface");
120         lint.setValue("interface-name", "lint");
121
122         Introspector lagints = loader.introspectorFromName("lag-interfaces");
123         Introspector lints = loader.introspectorFromName("l-interfaces");
124
125         lints.setValue("l-interface", Collections.singletonList(lint.getUnderlyingObject()));
126         lagInt.setValue("l-interfaces", lints.getUnderlyingObject());
127         lagints.setValue("lag-interface", Collections.singletonList(lagInt.getUnderlyingObject()));
128         gvnf.setValue("lag-interfaces", lagints.getUnderlyingObject());
129
130
131
132         Vertex gvnfV = serializer.createNewVertex(gvnf);
133         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
134         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
135
136         assertTrue("generic-vnf-a created", engine.tx().traversal().V()
137             .has(AAIProperties.AAI_URI, gvnfAUri)
138             .hasNext());
139         assertTrue("lag-int-a created", engine.tx().traversal().V()
140             .has(AAIProperties.AAI_URI, lagIntAUri)
141             .hasNext());
142         assertTrue("l-int created", engine.tx().traversal().V()
143             .has(AAIProperties.AAI_URI, lintUri)
144             .hasNext());
145
146
147
148         gvnf = loader.introspectorFromName("generic-vnf");
149         gvnf.setValue("vnf-id", "gvnf-b" + SOURCE_OF_TRUTH);
150         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
151
152         lagInt = loader.introspectorFromName("lag-interface");
153         lagInt.setValue("interface-name", "lagint-b");
154         Introspector relationship = loader.introspectorFromName("relationship");
155         relationship.setValue("related-link", lintUri);
156         Introspector relationshipList = loader.introspectorFromName("relationship-list");
157         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
158         lagInt.setValue("relationship-list", relationshipList.getUnderlyingObject());
159
160         lagints = loader.introspectorFromName("lag-interfaces");
161         lagints.setValue("lag-interface", Collections.singletonList(lagInt.getUnderlyingObject()));
162         gvnf.setValue("lag-interfaces", lagints.getUnderlyingObject());
163
164         gvnfV = serializer.createNewVertex(gvnf);
165         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
166         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
167
168         engine.tx().traversal().V().forEachRemaining(v -> System.out.println(v.<String>value(AAIProperties.AAI_URI)));
169         assertTrue("generic-vnf-b created", engine.tx().traversal().V()
170             .has(AAIProperties.AAI_URI, gvnfBUri)
171             .hasNext());
172         assertTrue("lag-int-b created", engine.tx().traversal().V()
173             .has(AAIProperties.AAI_URI, lagIntBUri)
174             .hasNext());
175         assertTrue("lag-interface relationship l-interface created", engine.tx().traversal().V()
176             .has(AAIProperties.AAI_URI, lagIntBUri)
177             .both()
178             .has(AAIProperties.AAI_URI, lintUri)
179             .hasNext());
180     }
181
182
183     @Test
184     public void verifyReadOfGenericVnfATest() throws AAIException, UnsupportedEncodingException {
185         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
186
187         String gvnfALatestView = serializer.getLatestVersionView(
188             engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next()).marshal(false);
189
190         assertThat(gvnfALatestView,
191             hasJsonPath(
192                 "$.lag-interfaces.lag-interface[*]",
193                 hasSize(1)
194             ));
195         assertThat(gvnfALatestView,
196             hasJsonPath(
197                 "$.lag-interfaces.lag-interface[*].l-interfaces.l-interface[*]",
198                 hasSize(1)
199             ));
200         assertThat(gvnfALatestView,
201             hasJsonPath(
202                 "$.lag-interfaces.lag-interface[*].l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
203                 containsInAnyOrder(
204                     "/aai/" + schemaVersions.getDefaultVersion() + lagIntBUri
205                 )
206             ));
207     }
208
209     @Test
210     public void verifyReadOfGenericVnfBTest() throws AAIException, UnsupportedEncodingException {
211         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
212
213         String gvnfBLatestView = serializer.getLatestVersionView(
214             engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).next()).marshal(false);
215
216         assertThat(gvnfBLatestView,
217             hasJsonPath(
218                 "$.lag-interfaces.lag-interface[*]",
219                 hasSize(1)
220             ));
221         assertThat(gvnfBLatestView,
222             not(hasJsonPath(
223                 "$.lag-interfaces.lag-interface[*].l-interfaces.l-interface[*]"
224             )));
225         assertThat(gvnfBLatestView,
226             hasJsonPath(
227                 "$.lag-interfaces.lag-interface[*].relationship-list.relationship[*].related-link",
228                 containsInAnyOrder(
229                     "/aai/" + schemaVersions.getDefaultVersion() + lintUri
230                 )
231             ));
232     }
233
234 }