c83b5b3995dd0e2470875822ea256f6d41cca946
[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 package org.onap.aai.serialization.queryformats;
21
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;
49
50 import java.util.Arrays;
51 import java.util.List;
52
53 import static org.junit.Assert.assertEquals;
54 import static org.mockito.Mockito.spy;
55 import static org.mockito.Mockito.when;
56
57 public class CountQuerySupportTest extends AAISetup {
58
59         @Autowired
60         private EdgeSerializer edgeSer;
61
62         private Graph graph;
63         private TransactionalGraphEngine dbEngine;
64         private Loader loader;
65         private final ModelType factoryType = ModelType.MOXY;
66         
67         private SchemaVersion version;
68         Vertex pserver1;
69         Vertex complex1;
70         Vertex complex2;
71
72         private DBSerializer serializer;
73         
74         private FormatFactory ff;
75         private Formatter formatter;
76
77         
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",
88                                 "hostname-1");
89                 complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
90                                 "physical-location-id", "physical-location-id-1", "country", "US");
91                 
92                 complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex",
93                                 "physical-location-id", "physical-location-id-2", "country", "US");
94
95                 GraphTraversalSource g = graph.traversal();
96                 edgeSer.addEdge(g, pserver1, complex1);
97                 
98                 createLoaderEngineSetup();
99
100         }
101         
102         @After
103         public void tearDown() throws Exception {
104                 graph.close();
105         }
106
107         @Test
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());
112         }
113         
114         @Test
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());
119         }
120         
121         @Test
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());
126         }
127         
128         @Test
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());
133         }
134         
135         @Test
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());
140         }
141
142
143         public void createLoaderEngineSetup() throws AAIException {
144
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");
150                         
151                         ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
152                         formatter = ff.get(Format.count);
153
154                         
155                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
156
157                         when(dbEngine.tx()).thenReturn(graph);
158                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
159
160                         when(spyAdmin.getReadOnlyTraversalSource())
161                                         .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
162                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
163                 }
164         }
165 }