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.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import com.google.gson.JsonObject;
29 import java.util.Arrays;
30 import java.util.List;
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
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.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.aai.AAISetup;
44 import org.onap.aai.dbmap.DBConnectionType;
45 import org.onap.aai.exceptions.AAIException;
46 import org.onap.aai.introspection.Loader;
47 import org.onap.aai.introspection.ModelType;
48 import org.onap.aai.serialization.db.DBSerializer;
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;
59 public class CountQuerySupportTest extends AAISetup {
62 private EdgeSerializer edgeSer;
65 private TransactionalGraphEngine dbEngine;
66 private Loader loader;
67 private final ModelType factoryType = ModelType.MOXY;
69 private SchemaVersion version;
74 private DBSerializer serializer;
76 private FormatFactory ff;
77 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", "hostname-1");
88 complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex", "physical-location-id",
89 "physical-location-id-1", "country", "US");
91 complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex", "physical-location-id",
92 "physical-location-id-2", "country", "US");
94 GraphTraversalSource g = graph.traversal();
95 edgeSer.addEdge(g, pserver1, complex1);
97 createLoaderEngineSetup();
102 public void tearDown() throws Exception {
107 public void verifyComplexVertexCountTest1()
108 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()
116 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
117 List<Object> pserverList = Arrays.asList(this.pserver1);
118 JsonObject jo = this.formatter.output(pserverList);
119 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
123 public void verifyComplexVertexCountTest2()
124 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
125 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2);
126 JsonObject jo = this.formatter.output(list);
127 assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
131 public void verifyPserverVertexCountTest2()
132 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
133 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2);
134 JsonObject jo = this.formatter.output(list);
135 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
139 public void verifyLongTest() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
140 List<Object> complexList = Arrays.asList(Long.valueOf(22L));
141 JsonObject jo = this.formatter.output(complexList);
142 assertEquals(22, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("count").getAsInt());
145 public void createLoaderEngineSetup() throws AAIException {
147 if (loader == null) {
148 loader = loaderFactory.createLoaderForVersion(factoryType, version);
149 // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
150 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
151 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
153 ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
154 formatter = ff.get(Format.count);
156 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
158 when(dbEngine.tx()).thenReturn(graph);
159 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
161 when(spyAdmin.getReadOnlyTraversalSource())
162 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
163 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());