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 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
24 import org.apache.tinkerpop.gremlin.structure.Graph;
25 import org.apache.tinkerpop.gremlin.structure.T;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.aai.AAISetup;
33 import org.onap.aai.dbmap.DBConnectionType;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.serialization.db.DBSerializer;
38 import org.onap.aai.serialization.db.EdgeSerializer;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
43 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
44 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
45 import org.onap.aai.setup.SchemaVersion;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.test.annotation.DirtiesContext;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Mockito.spy;
51 import static org.mockito.Mockito.when;
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
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;
66 private EdgeSerializer rules;
68 private SchemaVersion version;
69 private Vertex pserver;
70 private Vertex complex;
72 private DBSerializer serializer;
75 public void setUp() throws Exception {
77 version = schemaVersions.getDefaultVersion();
79 MockitoAnnotations.initMocks(this);
81 graph = TinkerGraph.open();
83 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
85 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
86 "physical-location-id", "physical-location-id-1", "country", "US");
88 GraphTraversalSource g = graph.traversal();
89 rules.addEdge(g, pserver1, complex1);
94 System.setProperty("AJSC_HOME", ".");
95 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
97 createLoaderEngineSetup();
101 public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
102 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
106 public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
107 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
111 public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
112 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
116 public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
117 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
120 public void createLoaderEngineSetup() throws AAIException {
122 if (loader == null) {
123 loader = loaderFactory.createLoaderForVersion(factoryType, version);
124 //loader = LoaderFactory.createLoaderForVersion(factoryType, version);
125 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
126 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
127 rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
129 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
131 when(dbEngine.tx()).thenReturn(graph);
132 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
134 when(spyAdmin.getReadOnlyTraversalSource())
135 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
136 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());