2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
30 import java.io.UnsupportedEncodingException;
32 import org.apache.tinkerpop.gremlin.process.traversal.Path;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
34 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
35 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
36 import org.apache.tinkerpop.gremlin.structure.Graph;
37 import org.apache.tinkerpop.gremlin.structure.T;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.aai.AAISetup;
45 import org.onap.aai.dbmap.DBConnectionType;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.introspection.LoaderFactory;
49 import org.onap.aai.introspection.ModelType;
50 import org.onap.aai.introspection.Version;
51 import org.onap.aai.serialization.db.DBSerializer;
52 import org.onap.aai.serialization.db.EdgeRules;
53 import org.onap.aai.serialization.engines.QueryStyle;
54 import org.onap.aai.serialization.engines.TitanDBEngine;
55 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
56 import org.onap.aai.serialization.queryformats.RawFormat.Builder;
57 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
58 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
59 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
61 import com.google.gson.JsonObject;
62 import com.google.gson.JsonParser;
64 public class RawFormatTest extends AAISetup {
67 private UrlBuilder urlBuilder;
70 private TransactionalGraphEngine dbEngine;
71 private Loader loader;
72 private RawFormat rawFormat;
73 private final ModelType factoryType = ModelType.MOXY;
74 private final EdgeRules rules = EdgeRules.getInstance();
75 private Version version = Version.getLatest();
76 private Vertex pserver;
77 private Vertex complex;
79 private DBSerializer serializer;
82 public void setUp() throws Exception {
84 MockitoAnnotations.initMocks(this);
86 graph = TinkerGraph.open();
88 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
90 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
91 "physical-location-id", "physical-location-id-1", "country", "US");
93 GraphTraversalSource g = graph.traversal();
94 rules.addEdge(g, pserver1, complex1);
99 createLoaderEngineSetup();
104 public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
105 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
109 public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
110 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
114 public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
115 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
119 public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
120 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
123 public void createLoaderEngineSetup() throws AAIException {
125 if (loader == null) {
126 loader = LoaderFactory.createLoaderForVersion(factoryType, version);
127 dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
128 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
129 rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
131 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
133 when(dbEngine.tx()).thenReturn(graph);
134 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
136 when(spyAdmin.getReadOnlyTraversalSource())
137 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
138 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());