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 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
27 import org.apache.tinkerpop.gremlin.structure.T;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.ModelType;
38 import org.onap.aai.serialization.db.DBSerializer;
39 import org.onap.aai.serialization.db.EdgeSerializer;
40 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
43 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
44 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
45 import org.onap.aai.setup.SchemaVersion;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.test.annotation.DirtiesContext;
49 import java.util.Arrays;
50 import java.util.List;
52 import static org.junit.Assert.assertEquals;
53 import static org.mockito.Mockito.spy;
54 import static org.mockito.Mockito.when;
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
60 public class CountQuerySupportTest extends AAISetup {
63 private EdgeSerializer edgeSer;
66 private TransactionalGraphEngine dbEngine;
67 private Loader loader;
68 private final ModelType factoryType = ModelType.MOXY;
70 private SchemaVersion version;
75 private DBSerializer serializer;
77 private FormatFactory ff;
78 private Formatter formatter;
81 public void setUp() throws Exception {
83 version = schemaVersions.getDefaultVersion();
84 MockitoAnnotations.initMocks(this);
86 graph = TinkerGraph.open();
88 pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
89 complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex", "physical-location-id",
90 "physical-location-id-1", "country", "US");
92 complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex", "physical-location-id",
93 "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()
109 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
110 List<Object> complexList = Arrays.asList(this.complex1, this.complex2);
111 JsonObject jo = this.formatter.output(complexList);
112 assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
116 public void verifyPserverVertexCountTest1()
117 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
118 List<Object> pserverList = Arrays.asList(this.pserver1);
119 JsonObject jo = this.formatter.output(pserverList);
120 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
124 public void verifyComplexVertexCountTest2()
125 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
126 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2);
127 JsonObject jo = this.formatter.output(list);
128 assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
132 public void verifyPserverVertexCountTest2()
133 throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
134 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2);
135 JsonObject jo = this.formatter.output(list);
136 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
140 public void verifyLongTest() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
141 List<Object> complexList = Arrays.asList(Long.valueOf(22L));
142 JsonObject jo = this.formatter.output(complexList);
143 assertEquals(22, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("count").getAsInt());
146 public void createLoaderEngineSetup() throws AAIException {
148 if (loader == null) {
149 loader = loaderFactory.createLoaderForVersion(factoryType, version);
150 // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
151 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
152 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
154 ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
155 formatter = ff.get(Format.count);
157 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
159 when(dbEngine.tx()).thenReturn(graph);
160 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
162 when(spyAdmin.getReadOnlyTraversalSource())
163 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
164 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());