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.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
28 import java.io.UnsupportedEncodingException;
30 import org.apache.tinkerpop.gremlin.process.traversal.Path;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
33 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
34 import org.apache.tinkerpop.gremlin.structure.Graph;
35 import org.apache.tinkerpop.gremlin.structure.T;
36 import org.apache.tinkerpop.gremlin.structure.Vertex;
37 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.aai.AAISetup;
43 import org.onap.aai.dbmap.DBConnectionType;
44 import org.onap.aai.exceptions.AAIException;
45 import org.onap.aai.introspection.Loader;
46 import org.onap.aai.introspection.LoaderFactory;
47 import org.onap.aai.introspection.ModelType;
48 import org.onap.aai.introspection.Version;
49 import org.onap.aai.serialization.db.DBSerializer;
50 import org.onap.aai.serialization.db.EdgeRules;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TitanDBEngine;
53 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
54 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
55 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
56 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
58 import com.google.gson.JsonObject;
59 import com.google.gson.JsonParser;
61 public class MultiFormatTest extends AAISetup {
64 private UrlBuilder urlBuilder;
67 private TransactionalGraphEngine dbEngine;
68 private Loader loader;
69 private IdURL idFormat;
70 private final ModelType factoryType = ModelType.MOXY;
71 private final EdgeRules rules = EdgeRules.getInstance();
72 private Tree<?> resultTree;
73 private Path resultPath;
74 private Version version = Version.v11;
75 private JsonObject expectedTreeIdFormat = new JsonParser()
76 .parse("{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}").getAsJsonObject();
77 private JsonObject expectedPathIdFormat = new JsonParser()
78 .parse("{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}").getAsJsonObject();
81 public void setUp() throws Exception {
83 MockitoAnnotations.initMocks(this);
85 graph = TinkerGraph.open();
87 Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
88 "vnf-id-1", "vnf-name", "vnf-name-1");
89 Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
90 "vserver-id-1", "vserver-name", "vserver-name-1");
91 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
93 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
94 "physical-location-id", "physical-location-id-1", "country", "US");
96 Vertex pserver2 = graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname",
98 Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
99 "physical-location-id", "physical-location-id-2", "country", "US");
101 GraphTraversalSource g = graph.traversal();
102 rules.addEdge(g, gnvf1, vserver1);
103 rules.addEdge(g, vserver1, pserver1);
104 rules.addEdge(g, pserver1, complex1);
105 rules.addEdge(g, gnvf1, pserver2);
106 rules.addEdge(g, pserver2, complex2);
108 resultTree = graph.traversal().V("0").out().out().tree().next();
109 resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
113 public void testTreeResultQueryIdFormat()
114 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
116 createLoaderEngineSetup();
117 idFormat = new IdURL(loader, urlBuilder);
119 assertNotNull(dbEngine.tx());
120 assertNotNull(dbEngine.asAdmin());
122 JsonObject json = idFormat.formatObject(resultTree);
124 assertEquals(this.expectedTreeIdFormat, json);
129 public void testPathResultQueryIdFormat()
130 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
132 createLoaderEngineSetup();
133 idFormat = new IdURL(loader, urlBuilder);
135 assertNotNull(dbEngine.tx());
136 assertNotNull(dbEngine.asAdmin());
138 JsonObject json = idFormat.formatObject(resultPath);
140 assertEquals(this.expectedPathIdFormat, json);
145 @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
146 public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException,
147 AAIException, UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
149 loader = mock(Loader.class);
150 idFormat = new IdURL(loader, urlBuilder);
151 idFormat.formatObject(new String());
154 public void createLoaderEngineSetup() {
156 if (loader == null) {
157 loader = LoaderFactory.createLoaderForVersion(factoryType, version);
158 dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
160 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
162 when(dbEngine.tx()).thenReturn(graph);
163 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
165 when(spyAdmin.getReadOnlyTraversalSource())
166 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
167 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());