2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.T;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.aai.AAISetup;
38 import org.onap.aai.dbmap.DBConnectionType;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.Loader;
41 import org.onap.aai.introspection.ModelType;
42 import org.onap.aai.serialization.db.DBSerializer;
43 import org.onap.aai.serialization.db.EdgeSerializer;
44 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
45 import org.onap.aai.serialization.engines.QueryStyle;
46 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
47 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
48 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
49 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
50 import org.onap.aai.setup.SchemaVersion;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.test.annotation.DirtiesContext;
54 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
55 public class RawFormatTest extends AAISetup {
58 private UrlBuilder urlBuilder;
61 private TransactionalGraphEngine dbEngine;
62 private Loader loader;
63 private RawFormat rawFormat;
64 private final ModelType factoryType = ModelType.MOXY;
67 private EdgeSerializer rules;
69 private SchemaVersion version;
70 private Vertex pserver;
71 private Vertex complex;
73 private DBSerializer serializer;
76 public void setUp() throws Exception {
78 version = schemaVersions.getDefaultVersion();
80 MockitoAnnotations.initMocks(this);
82 graph = TinkerGraph.open();
85 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
86 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
87 "physical-location-id", "physical-location-id-1", "country", "US");
89 GraphTraversalSource g = graph.traversal();
90 rules.addEdge(g, pserver1, complex1);
95 System.setProperty("AJSC_HOME", ".");
96 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
98 createLoaderEngineSetup();
102 public void verifyPserverRelatedToHasEdgeLabel()
103 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
104 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label")
105 .getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
109 public void verifyPserverRelatedToComplexLabel()
110 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
111 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString()
116 public void verifyComplexRelatedToHasEdgeLabel()
117 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
118 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label")
119 .getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
123 public void verifyComplexRelatedToPserverLabel()
124 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
125 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString()
129 public void createLoaderEngineSetup() throws AAIException {
131 if (loader == null) {
132 loader = loaderFactory.createLoaderForVersion(factoryType, version);
133 // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
134 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
135 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
136 rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
138 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
140 when(dbEngine.tx()).thenReturn(graph);
141 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
143 when(spyAdmin.getReadOnlyTraversalSource())
144 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
145 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());