Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / CountQuerySupportTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
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
48 import java.util.Arrays;
49 import java.util.List;
50
51 import static org.junit.Assert.assertEquals;
52 import static org.mockito.Mockito.spy;
53 import static org.mockito.Mockito.when;
54
55 public class CountQuerySupportTest extends AAISetup {
56
57     @Autowired
58     private EdgeSerializer edgeSer;
59
60     private Graph graph;
61     private TransactionalGraphEngine dbEngine;
62     private Loader loader;
63     private final ModelType factoryType = ModelType.MOXY;
64
65     private SchemaVersion version;
66     Vertex pserver1;
67     Vertex complex1;
68     Vertex complex2;
69
70     private DBSerializer serializer;
71
72     private FormatFactory ff;
73     private Formatter formatter;
74
75     @Before
76     public void setUp() throws Exception {
77
78         version = schemaVersions.getDefaultVersion();
79         MockitoAnnotations.initMocks(this);
80
81         graph = TinkerGraph.open();
82
83         pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
84         complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex", "physical-location-id",
85                 "physical-location-id-1", "country", "US");
86
87         complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex", "physical-location-id",
88                 "physical-location-id-2", "country", "US");
89
90         GraphTraversalSource g = graph.traversal();
91         edgeSer.addEdge(g, pserver1, complex1);
92
93         createLoaderEngineSetup();
94
95     }
96
97     @After
98     public void tearDown() throws Exception {
99         graph.close();
100     }
101
102     @Test
103     public void verifyComplexVertexCountTest1()
104             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
105         List<Object> complexList = Arrays.asList(this.complex1, this.complex2);
106         JsonObject jo = this.formatter.output(complexList);
107         assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
108     }
109
110     @Test
111     public void verifyPserverVertexCountTest1()
112             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
113         List<Object> pserverList = Arrays.asList(this.pserver1);
114         JsonObject jo = this.formatter.output(pserverList);
115         assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
116     }
117
118     @Test
119     public void verifyComplexVertexCountTest2()
120             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
121         List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2);
122         JsonObject jo = this.formatter.output(list);
123         assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
124     }
125
126     @Test
127     public void verifyPserverVertexCountTest2()
128             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
129         List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2);
130         JsonObject jo = this.formatter.output(list);
131         assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
132     }
133
134     @Test
135     public void verifyLongTest() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
136         List<Object> complexList = Arrays.asList(Long.valueOf(22L));
137         JsonObject jo = this.formatter.output(complexList);
138         assertEquals(22, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("count").getAsInt());
139     }
140
141     public void createLoaderEngineSetup() throws AAIException {
142
143         if (loader == null) {
144             loader = loaderFactory.createLoaderForVersion(factoryType, version);
145             // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
146             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
147             serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
148
149             ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
150             formatter = ff.get(Format.count);
151
152             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
153
154             when(dbEngine.tx()).thenReturn(graph);
155             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
156
157             when(spyAdmin.getReadOnlyTraversalSource())
158                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
159             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
160         }
161     }
162 }