2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.queryformats;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
29 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
30 import org.apache.tinkerpop.gremlin.structure.Graph;
31 import org.apache.tinkerpop.gremlin.structure.T;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.dbmap.DBConnectionType;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.introspection.Loader;
42 import org.onap.aai.introspection.LoaderFactory;
43 import org.onap.aai.introspection.ModelType;
44 import org.onap.aai.introspection.Version;
45 import org.onap.aai.serialization.db.DBSerializer;
46 import org.onap.aai.serialization.db.EdgeRules;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.onap.aai.serialization.engines.TitanDBEngine;
49 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
50 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
51 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
52 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
54 public class RawFormatTest extends AAISetup {
57 private UrlBuilder urlBuilder;
60 private TransactionalGraphEngine dbEngine;
61 private Loader loader;
62 private RawFormat rawFormat;
63 private final ModelType factoryType = ModelType.MOXY;
64 private final EdgeRules rules = EdgeRules.getInstance();
65 private Version version = Version.getLatest();
66 private Vertex pserver;
67 private Vertex complex;
69 private DBSerializer serializer;
72 public void setUp() throws Exception {
74 MockitoAnnotations.initMocks(this);
76 graph = TinkerGraph.open();
78 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
80 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
81 "physical-location-id", "physical-location-id-1", "country", "US");
83 GraphTraversalSource g = graph.traversal();
84 rules.addEdge(g, pserver1, complex1);
89 createLoaderEngineSetup();
94 public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
95 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
99 public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
100 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
104 public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
105 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
109 public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
110 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
113 public void createLoaderEngineSetup() throws AAIException {
115 if (loader == null) {
116 loader = LoaderFactory.createLoaderForVersion(factoryType, version);
117 dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
118 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
119 rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
121 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
123 when(dbEngine.tx()).thenReturn(graph);
124 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
126 when(spyAdmin.getReadOnlyTraversalSource())
127 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
128 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());