Cherry pick 105924 to frankfurt
[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 import org.springframework.test.annotation.DirtiesContext;
48
49 import java.util.Arrays;
50 import java.util.List;
51
52 import static org.junit.Assert.assertEquals;
53 import static org.mockito.Mockito.spy;
54 import static org.mockito.Mockito.when;
55
56
57
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59
60 public class CountQuerySupportTest extends AAISetup {
61
62     @Autowired
63     private EdgeSerializer edgeSer;
64
65     private Graph graph;
66     private TransactionalGraphEngine dbEngine;
67     private Loader loader;
68     private final ModelType factoryType = ModelType.MOXY;
69
70     private SchemaVersion version;
71     Vertex pserver1;
72     Vertex complex1;
73     Vertex complex2;
74
75     private DBSerializer serializer;
76
77     private FormatFactory ff;
78     private Formatter formatter;
79
80     @Before
81     public void setUp() throws Exception {
82
83         version = schemaVersions.getDefaultVersion();
84         MockitoAnnotations.initMocks(this);
85
86         graph = TinkerGraph.open();
87
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");
91
92         complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex", "physical-location-id",
93                 "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()
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());
113     }
114
115     @Test
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());
121     }
122
123     @Test
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());
129     }
130
131     @Test
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());
137     }
138
139     @Test
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());
144     }
145
146     public void createLoaderEngineSetup() throws AAIException {
147
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");
153
154             ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
155             formatter = ff.get(Format.count);
156
157             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
158
159             when(dbEngine.tx()).thenReturn(graph);
160             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
161
162             when(spyAdmin.getReadOnlyTraversalSource())
163                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
164             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
165         }
166     }
167 }