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 com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import org.apache.tinkerpop.gremlin.process.traversal.Path;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
28 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.T;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.aai.AAISetup;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.serialization.db.DBSerializer;
42 import org.onap.aai.serialization.db.EdgeSerializer;
43 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
44 import org.onap.aai.serialization.engines.QueryStyle;
45 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
46 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
47 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
48 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
49 import org.onap.aai.setup.SchemaVersion;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.test.annotation.DirtiesContext;
53 import javax.ws.rs.core.MultivaluedHashMap;
54 import java.io.UnsupportedEncodingException;
56 import static org.junit.Assert.assertEquals;
57 import static org.junit.Assert.assertNotNull;
58 import static org.mockito.Mockito.*;
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\"}]}")
82 private JsonObject expectedAsTreeWithResourceFormat = new JsonParser().parse(
83 "{\"results\":[{\"generic-vnf\":{\"vnf-id\":\"vnf-id-1\",\"vnf-name\":\"vnf-name-1\",\"related-nodes\":[{\"vserver\":{\"vserver-id\":\"vserver-id-1\",\"vserver-name\":\"vserver-name-1\",\"related-nodes\":[{\"pserver\":{\"hostname\":\"hostname-1\"}}]}},{\"pserver\":{\"hostname\":\"hostname-2\",\"related-nodes\":[{\"complex\":{\"physical-location-id\":\"physical-location-id-2\",\"country\":\"US\"}}]}}]}}]}")
85 private JsonObject expectedAsTreeWithSimpleFormat = new JsonParser().parse(
86 "{\"results\":[{\"id\":\"0\",\"node-type\":\"generic-vnf\",\"url\":null,\"properties\":{\"vnf-id\":\"vnf-id-1\",\"vnf-name\":\"vnf-name-1\"},\"related-to\":[{\"id\":\"1\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"vserver\",\"url\":null},{\"id\":\"5\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"pserver\",\"url\":null}],\"related-nodes\":[{\"id\":\"1\",\"node-type\":\"vserver\",\"url\":null,\"properties\":{\"vserver-id\":\"vserver-id-1\",\"vserver-name\":\"vserver-name-1\"},\"related-to\":[{\"id\":\"0\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"generic-vnf\",\"url\":null},{\"id\":\"2\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"pserver\",\"url\":null}],\"related-nodes\":[{\"id\":\"2\",\"node-type\":\"pserver\",\"url\":null,\"properties\":{\"hostname\":\"hostname-1\"},\"related-to\":[{\"id\":\"1\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"vserver\",\"url\":null},{\"id\":\"3\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"node-type\":\"complex\",\"url\":null}]}]},{\"id\":\"5\",\"node-type\":\"pserver\",\"url\":null,\"properties\":{\"hostname\":\"hostname-2\"},\"related-to\":[{\"id\":\"0\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"generic-vnf\",\"url\":null},{\"id\":\"6\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"node-type\":\"complex\",\"url\":null}],\"related-nodes\":[{\"id\":\"6\",\"node-type\":\"complex\",\"url\":null,\"properties\":{\"physical-location-id\":\"physical-location-id-2\",\"country\":\"US\"},\"related-to\":[{\"id\":\"5\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"node-type\":\"pserver\",\"url\":null}]}]}]}]}")
89 public void setUp() throws Exception {
91 version = schemaVersions.getAppRootVersion();
92 MockitoAnnotations.initMocks(this);
94 graph = TinkerGraph.open();
96 Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
97 "vnf-id-1", "vnf-name", "vnf-name-1");
98 Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
99 "vserver-id-1", "vserver-name", "vserver-name-1");
101 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
102 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
103 "physical-location-id", "physical-location-id-1", "country", "US");
106 graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname", "hostname-2");
107 Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
108 "physical-location-id", "physical-location-id-2", "country", "US");
110 GraphTraversalSource g = graph.traversal();
111 rules.addEdge(g, gnvf1, vserver1);
112 rules.addEdge(g, vserver1, pserver1);
113 rules.addEdge(g, pserver1, complex1);
114 rules.addEdge(g, gnvf1, pserver2);
115 rules.addEdge(g, pserver2, complex2);
117 resultTree = graph.traversal().V("0").out().out().tree().next();
118 resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
122 public void testTreeResultQueryIdFormat()
123 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
125 createLoaderEngineSetup();
126 idFormat = new IdURL(loader, urlBuilder);
128 assertNotNull(dbEngine.tx());
129 assertNotNull(dbEngine.asAdmin());
131 JsonObject json = idFormat.formatObject(resultTree).get();
133 assertEquals(this.expectedTreeIdFormat, json);
138 public void testAsTreeParamAndSimpleFormat()
139 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
141 createLoaderEngineSetup();
142 DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
143 MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
144 params.add("as-tree", "true");
145 SimpleFormat sf = new SimpleFormat(new RawFormat.Builder(loader, serializer, urlBuilder).isTree(true));
147 assertNotNull(dbEngine.tx());
148 assertNotNull(dbEngine.asAdmin());
150 JsonObject json = sf.formatObject(resultTree).get();
151 assertEquals(this.expectedAsTreeWithSimpleFormat, json);
156 public void testAsTreeParamAndResourceFormat()
157 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
159 createLoaderEngineSetup();
160 DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
161 MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
162 params.add("as-tree", "true");
163 Resource sf = new Resource(new Resource.Builder(loader, serializer, urlBuilder, params).isTree(true));
165 assertNotNull(dbEngine.tx());
166 assertNotNull(dbEngine.asAdmin());
168 JsonObject json = sf.formatObject(resultTree).get();
169 assertEquals(this.expectedAsTreeWithResourceFormat, json);
174 public void testPathResultQueryIdFormat()
175 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
177 createLoaderEngineSetup();
178 idFormat = new IdURL(loader, urlBuilder);
180 assertNotNull(dbEngine.tx());
181 assertNotNull(dbEngine.asAdmin());
183 JsonObject json = idFormat.formatObject(resultPath).get();
185 assertEquals(this.expectedPathIdFormat, json);
189 @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
190 public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException, AAIException,
191 UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
193 loader = mock(Loader.class);
194 idFormat = new IdURL(loader, urlBuilder);
195 idFormat.formatObject(new String());
198 public void createLoaderEngineSetup() {
200 if (loader == null) {
201 loader = loaderFactory.createLoaderForVersion(factoryType, version);
202 // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
203 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
205 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
207 when(dbEngine.tx()).thenReturn(graph);
208 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
210 when(spyAdmin.getReadOnlyTraversalSource())
211 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
212 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());