Merge "[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 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.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 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,
110                 AAIProperties.MINIMUM_DEPTH);
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         Vertex gvnfV = serializer.createNewVertex(gvnf);
131         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
132         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
133
134         assertTrue("generic-vnf-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).hasNext());
135         assertTrue("lag-int-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lagIntAUri).hasNext());
136         assertTrue("l-int created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintUri).hasNext());
137
138         gvnf = loader.introspectorFromName("generic-vnf");
139         gvnf.setValue("vnf-id", "gvnf-b" + SOURCE_OF_TRUTH);
140         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
141
142         lagInt = loader.introspectorFromName("lag-interface");
143         lagInt.setValue("interface-name", "lagint-b");
144         Introspector relationship = loader.introspectorFromName("relationship");
145         relationship.setValue("related-link", lintUri);
146         Introspector relationshipList = loader.introspectorFromName("relationship-list");
147         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
148         lagInt.setValue("relationship-list", relationshipList.getUnderlyingObject());
149
150         lagints = loader.introspectorFromName("lag-interfaces");
151         lagints.setValue("lag-interface", Collections.singletonList(lagInt.getUnderlyingObject()));
152         gvnf.setValue("lag-interfaces", lagints.getUnderlyingObject());
153
154         gvnfV = serializer.createNewVertex(gvnf);
155         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
156         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
157
158         engine.tx().traversal().V().forEachRemaining(v -> System.out.println(v.<String>value(AAIProperties.AAI_URI)));
159         assertTrue("generic-vnf-b created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).hasNext());
160         assertTrue("lag-int-b created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lagIntBUri).hasNext());
161         assertTrue("lag-interface relationship l-interface created", engine.tx().traversal().V()
162                 .has(AAIProperties.AAI_URI, lagIntBUri).both().has(AAIProperties.AAI_URI, lintUri).hasNext());
163     }
164
165     @Test
166     public void verifyReadOfGenericVnfATest() throws AAIException, UnsupportedEncodingException {
167         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
168                 AAIProperties.MINIMUM_DEPTH);
169
170         String gvnfALatestView =
171                 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next())
172                         .marshal(false);
173
174         assertThat(gvnfALatestView, hasJsonPath("$.lag-interfaces.lag-interface[*]", hasSize(1)));
175         assertThat(gvnfALatestView,
176                 hasJsonPath("$.lag-interfaces.lag-interface[*].l-interfaces.l-interface[*]", hasSize(1)));
177         assertThat(gvnfALatestView, hasJsonPath(
178                 "$.lag-interfaces.lag-interface[*].l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
179                 containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + lagIntBUri)));
180     }
181
182     @Test
183     public void verifyReadOfGenericVnfBTest() throws AAIException, UnsupportedEncodingException {
184         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
185                 AAIProperties.MINIMUM_DEPTH);
186
187         String gvnfBLatestView =
188                 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).next())
189                         .marshal(false);
190
191         assertThat(gvnfBLatestView, hasJsonPath("$.lag-interfaces.lag-interface[*]", hasSize(1)));
192         assertThat(gvnfBLatestView, not(hasJsonPath("$.lag-interfaces.lag-interface[*].l-interfaces.l-interface[*]")));
193         assertThat(gvnfBLatestView,
194                 hasJsonPath("$.lag-interfaces.lag-interface[*].relationship-list.relationship[*].related-link",
195                         containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + lintUri)));
196     }
197
198 }