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=========================================================
20 package org.onap.aai.serialization.queryformats;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.T;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.dbmap.DBConnectionType;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.introspection.LoaderFactory;
41 import org.onap.aai.introspection.ModelType;
42 import org.onap.aai.introspection.Version;
43 import org.onap.aai.serialization.db.DBSerializer;
44 import org.onap.aai.serialization.db.EdgeRules;
45 import org.onap.aai.serialization.engines.QueryStyle;
46 import org.onap.aai.serialization.engines.TitanDBEngine;
47 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
48 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
49 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
50 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
52 public class RawFormatTest extends AAISetup {
55 private UrlBuilder urlBuilder;
58 private TransactionalGraphEngine dbEngine;
59 private Loader loader;
60 private RawFormat rawFormat;
61 private final ModelType factoryType = ModelType.MOXY;
62 private final EdgeRules rules = EdgeRules.getInstance();
63 private Version version = Version.getLatest();
64 private Vertex pserver;
65 private Vertex complex;
67 private DBSerializer serializer;
70 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
74 graph = TinkerGraph.open();
76 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
78 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
79 "physical-location-id", "physical-location-id-1", "country", "US");
81 GraphTraversalSource g = graph.traversal();
82 rules.addEdge(g, pserver1, complex1);
87 createLoaderEngineSetup();
92 public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
93 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
97 public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
98 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
102 public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
103 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
107 public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
108 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
111 public void createLoaderEngineSetup() throws AAIException {
113 if (loader == null) {
114 loader = LoaderFactory.createLoaderForVersion(factoryType, version);
115 dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
116 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
117 rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
119 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
121 when(dbEngine.tx()).thenReturn(graph);
122 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
124 when(spyAdmin.getReadOnlyTraversalSource())
125 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
126 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());