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.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonParser;
32 import java.io.UnsupportedEncodingException;
34 import javax.ws.rs.core.MultivaluedHashMap;
36 import org.apache.tinkerpop.gremlin.process.traversal.Path;
37 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
38 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
39 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
40 import org.apache.tinkerpop.gremlin.structure.Graph;
41 import org.apache.tinkerpop.gremlin.structure.T;
42 import org.apache.tinkerpop.gremlin.structure.Vertex;
43 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.onap.aai.AAISetup;
49 import org.onap.aai.exceptions.AAIException;
50 import org.onap.aai.introspection.Loader;
51 import org.onap.aai.introspection.ModelType;
52 import org.onap.aai.serialization.db.DBSerializer;
53 import org.onap.aai.serialization.db.EdgeSerializer;
54 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
55 import org.onap.aai.serialization.engines.QueryStyle;
56 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
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;
60 import org.onap.aai.setup.SchemaVersion;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.test.annotation.DirtiesContext;
64 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
65 public class MultiFormatTest extends AAISetup {
68 private UrlBuilder urlBuilder;
71 private TransactionalGraphEngine dbEngine;
72 private Loader loader;
73 private IdURL idFormat;
74 private final ModelType factoryType = ModelType.MOXY;
76 private EdgeSerializer rules;
77 private Tree<?> resultTree;
78 private Path resultPath;
79 private SchemaVersion version;
80 private JsonObject expectedTreeIdFormat = JsonParser.parseString(
81 "{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}")
83 private JsonObject expectedPathIdFormat = JsonParser.parseString(
84 "{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}")
86 private JsonObject expectedAsTreeWithResourceFormat = JsonParser.parseString(
87 "{\"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\"}}]}}]}}]}")
89 private JsonObject expectedAsTreeWithSimpleFormat = JsonParser.parseString(
90 "{\"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}]}]}]}]}")
94 public void setUp() throws Exception {
96 version = schemaVersions.getAppRootVersion();
97 MockitoAnnotations.initMocks(this);
99 graph = TinkerGraph.open();
101 Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
102 "vnf-id-1", "vnf-name", "vnf-name-1");
103 Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
104 "vserver-id-1", "vserver-name", "vserver-name-1");
106 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
107 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
108 "physical-location-id", "physical-location-id-1", "country", "US");
111 graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname", "hostname-2");
112 Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
113 "physical-location-id", "physical-location-id-2", "country", "US");
115 GraphTraversalSource g = graph.traversal();
116 rules.addEdge(g, gnvf1, vserver1);
117 rules.addEdge(g, vserver1, pserver1);
118 rules.addEdge(g, pserver1, complex1);
119 rules.addEdge(g, gnvf1, pserver2);
120 rules.addEdge(g, pserver2, complex2);
122 resultTree = graph.traversal().V("0").out().out().tree().next();
123 resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
127 public void testTreeResultQueryIdFormat()
128 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
130 createLoaderEngineSetup();
131 idFormat = new IdURL(loader, urlBuilder);
133 assertNotNull(dbEngine.tx());
134 assertNotNull(dbEngine.asAdmin());
136 JsonObject json = idFormat.formatObject(resultTree).get();
138 assertEquals(this.expectedTreeIdFormat, json);
143 public void testAsTreeParamAndSimpleFormat()
144 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
146 createLoaderEngineSetup();
147 DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
148 MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
149 params.add("as-tree", "true");
150 SimpleFormat sf = new SimpleFormat(new RawFormat.Builder(loader, serializer, urlBuilder).isTree(true));
152 assertNotNull(dbEngine.tx());
153 assertNotNull(dbEngine.asAdmin());
155 JsonObject json = sf.formatObject(resultTree).get();
156 assertEquals(this.expectedAsTreeWithSimpleFormat, json);
161 public void testAsTreeParamAndResourceFormat()
162 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
164 createLoaderEngineSetup();
165 DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
166 MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
167 params.add("as-tree", "true");
168 Resource sf = new Resource(new Resource.Builder(loader, serializer, urlBuilder, params).isTree(true));
170 assertNotNull(dbEngine.tx());
171 assertNotNull(dbEngine.asAdmin());
173 JsonObject json = sf.formatObject(resultTree).get();
174 assertEquals(this.expectedAsTreeWithResourceFormat, json);
178 public void testPathResultQueryIdFormat()
179 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
181 createLoaderEngineSetup();
182 idFormat = new IdURL(loader, urlBuilder);
184 assertNotNull(dbEngine.tx());
185 assertNotNull(dbEngine.asAdmin());
187 JsonObject json = idFormat.formatObject(resultPath).get();
189 assertEquals(this.expectedPathIdFormat, json);
193 @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
194 public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException, AAIException,
195 UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
197 loader = mock(Loader.class);
198 idFormat = new IdURL(loader, urlBuilder);
199 idFormat.formatObject(new String());
202 public void createLoaderEngineSetup() {
204 if (loader == null) {
205 loader = loaderFactory.createLoaderForVersion(factoryType, version);
206 // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
207 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
209 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
211 when(dbEngine.tx()).thenReturn(graph);
212 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
214 when(spyAdmin.getReadOnlyTraversalSource())
215 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
216 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());