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=========================================================
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.mockito.Mockito.*;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
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.ModelType;
49 import org.onap.aai.serialization.db.EdgeSerializer;
50 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
54 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
55 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
56 import org.onap.aai.setup.SchemaVersion;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.test.annotation.DirtiesContext;
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
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;
72 private EdgeSerializer rules;
73 private Tree<?> resultTree;
74 private Path resultPath;
75 private SchemaVersion version;
76 private JsonObject expectedTreeIdFormat = new JsonParser().parse(
77 "{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}")
79 private JsonObject expectedPathIdFormat = new JsonParser().parse(
80 "{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}")
84 public void setUp() throws Exception {
86 version = schemaVersions.getAppRootVersion();
87 MockitoAnnotations.initMocks(this);
89 graph = TinkerGraph.open();
91 Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
92 "vnf-id-1", "vnf-name", "vnf-name-1");
93 Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
94 "vserver-id-1", "vserver-name", "vserver-name-1");
96 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
97 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
98 "physical-location-id", "physical-location-id-1", "country", "US");
101 graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname", "hostname-2");
102 Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
103 "physical-location-id", "physical-location-id-2", "country", "US");
105 GraphTraversalSource g = graph.traversal();
106 rules.addEdge(g, gnvf1, vserver1);
107 rules.addEdge(g, vserver1, pserver1);
108 rules.addEdge(g, pserver1, complex1);
109 rules.addEdge(g, gnvf1, pserver2);
110 rules.addEdge(g, pserver2, complex2);
112 resultTree = graph.traversal().V("0").out().out().tree().next();
113 resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
117 public void testTreeResultQueryIdFormat()
118 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
120 createLoaderEngineSetup();
121 idFormat = new IdURL(loader, urlBuilder);
123 assertNotNull(dbEngine.tx());
124 assertNotNull(dbEngine.asAdmin());
126 JsonObject json = idFormat.formatObject(resultTree).get();
128 assertEquals(this.expectedTreeIdFormat, json);
133 public void testPathResultQueryIdFormat()
134 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
136 createLoaderEngineSetup();
137 idFormat = new IdURL(loader, urlBuilder);
139 assertNotNull(dbEngine.tx());
140 assertNotNull(dbEngine.asAdmin());
142 JsonObject json = idFormat.formatObject(resultPath).get();
144 assertEquals(this.expectedPathIdFormat, json);
148 @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
149 public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException, AAIException,
150 UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
152 loader = mock(Loader.class);
153 idFormat = new IdURL(loader, urlBuilder);
154 idFormat.formatObject(new String());
157 public void createLoaderEngineSetup() {
159 if (loader == null) {
160 loader = loaderFactory.createLoaderForVersion(factoryType, version);
161 // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
162 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
164 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
166 when(dbEngine.tx()).thenReturn(graph);
167 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
169 when(spyAdmin.getReadOnlyTraversalSource())
170 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
171 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());