daf7d47440c689dd66a9a4051d219308417a7488
[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 static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import com.google.gson.JsonObject;
28
29 import java.util.Arrays;
30 import java.util.List;
31
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.MockitoAnnotations;
42 import org.onap.aai.AAISetup;
43 import org.onap.aai.exceptions.AAIException;
44 import org.onap.aai.introspection.Loader;
45 import org.onap.aai.introspection.ModelType;
46 import org.onap.aai.serialization.db.DBSerializer;
47 import org.onap.aai.serialization.db.EdgeSerializer;
48 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
49 import org.onap.aai.serialization.engines.QueryStyle;
50 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
51 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
52 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
53 import org.onap.aai.setup.SchemaVersion;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.test.annotation.DirtiesContext;
56
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58
59 public class CountQuerySupportTest extends AAISetup {
60
61     @Autowired
62     private EdgeSerializer edgeSer;
63
64     private Graph graph;
65     private TransactionalGraphEngine dbEngine;
66     private Loader loader;
67     private final ModelType factoryType = ModelType.MOXY;
68
69     private SchemaVersion version;
70     Vertex pserver1;
71     Vertex complex1;
72     Vertex complex2;
73
74     private DBSerializer serializer;
75
76     private FormatFactory ff;
77     private Formatter formatter;
78
79     @Before
80     public void setUp() throws Exception {
81
82         version = schemaVersions.getDefaultVersion();
83         MockitoAnnotations.initMocks(this);
84
85         graph = TinkerGraph.open();
86
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");
90
91         complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex", "physical-location-id",
92                 "physical-location-id-2", "country", "US");
93
94         GraphTraversalSource g = graph.traversal();
95         edgeSer.addEdge(g, pserver1, complex1);
96
97         createLoaderEngineSetup();
98
99     }
100
101     @After
102     public void tearDown() throws Exception {
103         graph.close();
104     }
105
106     @Test
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());
112     }
113
114     @Test
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());
120     }
121
122     @Test
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());
128     }
129
130     @Test
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());
136     }
137
138     @Test
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());
143     }
144
145     public void createLoaderEngineSetup() throws AAIException {
146
147         if (loader == null) {
148             loader = loaderFactory.createLoaderForVersion(factoryType, version);
149             // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
150             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
151             serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
152
153             ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
154             formatter = ff.get(Format.count);
155
156             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
157
158             when(dbEngine.tx()).thenReturn(graph);
159             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
160
161             when(spyAdmin.getReadOnlyTraversalSource())
162                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
163             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
164         }
165     }
166 }