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 com.google.gson.JsonObject;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.T;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.dbmap.DBConnectionType;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.serialization.db.DBSerializer;
40 import org.onap.aai.serialization.db.EdgeSerializer;
41 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
42 import org.onap.aai.serialization.engines.QueryStyle;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
45 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
46 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
47 import org.onap.aai.setup.SchemaVersion;
48 import org.springframework.beans.factory.annotation.Autowired;
50 import java.util.Arrays;
51 import java.util.List;
53 import static org.junit.Assert.assertEquals;
54 import static org.mockito.Mockito.spy;
55 import static org.mockito.Mockito.when;
57 public class CountQuerySupportTest extends AAISetup {
60 private EdgeSerializer edgeSer;
63 private TransactionalGraphEngine dbEngine;
64 private Loader loader;
65 private final ModelType factoryType = ModelType.MOXY;
67 private SchemaVersion version;
72 private DBSerializer serializer;
74 private FormatFactory ff;
75 private Formatter formatter;
80 public void setUp() throws Exception {
82 version = schemaVersions.getDefaultVersion();
83 MockitoAnnotations.initMocks(this);
85 graph = TinkerGraph.open();
87 pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
89 complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
90 "physical-location-id", "physical-location-id-1", "country", "US");
92 complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex",
93 "physical-location-id", "physical-location-id-2", "country", "US");
95 GraphTraversalSource g = graph.traversal();
96 edgeSer.addEdge(g, pserver1, complex1);
98 createLoaderEngineSetup();
103 public void tearDown() throws Exception {
108 public void verifyComplexVertexCountTest1() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
109 List<Object> complexList = Arrays.asList(this.complex1, this.complex2 );
110 JsonObject jo = this.formatter.output(complexList);
111 assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
115 public void verifyPserverVertexCountTest1() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
116 List<Object> pserverList = Arrays.asList(this.pserver1 );
117 JsonObject jo = this.formatter.output(pserverList);
118 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
122 public void verifyComplexVertexCountTest2() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
123 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2 );
124 JsonObject jo = this.formatter.output(list);
125 assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
129 public void verifyPserverVertexCountTest2() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
130 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2 );
131 JsonObject jo = this.formatter.output(list);
132 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
136 public void verifyLongTest() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
137 List<Object> complexList = Arrays.asList(Long.valueOf(22L) );
138 JsonObject jo = this.formatter.output(complexList);
139 assertEquals(22, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("count").getAsInt());
143 public void createLoaderEngineSetup() throws AAIException {
145 if (loader == null) {
146 loader = loaderFactory.createLoaderForVersion(factoryType, version);
147 //loader = LoaderFactory.createLoaderForVersion(factoryType, version);
148 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
149 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
151 ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
152 formatter = ff.get(Format.count);
155 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
157 when(dbEngine.tx()).thenReturn(graph);
158 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
160 when(spyAdmin.getReadOnlyTraversalSource())
161 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
162 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());