Update the license for 2017-2018 license
[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 static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
30 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
31 import org.apache.tinkerpop.gremlin.structure.Graph;
32 import org.apache.tinkerpop.gremlin.structure.T;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.aai.AAISetup;
41 import org.onap.aai.dbmap.DBConnectionType;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.LoaderFactory;
45 import org.onap.aai.introspection.ModelType;
46 import org.onap.aai.introspection.Version;
47 import org.onap.aai.serialization.db.DBSerializer;
48 import org.onap.aai.serialization.db.EdgeRules;
49 import org.onap.aai.serialization.engines.QueryStyle;
50 import org.onap.aai.serialization.engines.TitanDBEngine;
51 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
52 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
53 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
54 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
55
56 import com.google.gson.JsonObject;
57
58 public class CountQuerySupportTest extends AAISetup {
59
60         @Mock
61         private UrlBuilder urlBuilder;
62
63         private Graph graph;
64         private TransactionalGraphEngine dbEngine;
65         private Loader loader;
66         private final ModelType factoryType = ModelType.MOXY;
67         private final EdgeRules rules = EdgeRules.getInstance();
68         private Version version = Version.getLatest();
69         Vertex pserver1;
70         Vertex complex1;
71         Vertex complex2;
72
73         private DBSerializer serializer;
74         
75         private FormatFactory ff;
76         private Formatter formatter;
77
78         @Before
79         public void setUp() throws Exception {
80
81                 MockitoAnnotations.initMocks(this);
82
83                 graph = TinkerGraph.open();
84
85                 pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
86                                 "hostname-1");
87                 complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
88                                 "physical-location-id", "physical-location-id-1", "country", "US");
89                 
90                 complex2 = graph.addVertex(T.label, "complex", T.id, "4", "aai-node-type", "complex",
91                                 "physical-location-id", "physical-location-id-2", "country", "US");
92
93                 GraphTraversalSource g = graph.traversal();
94                 rules.addEdge(g, pserver1, complex1);
95                 
96                 createLoaderEngineSetup();
97
98         }
99         
100         @After
101         public void tearDown() throws Exception {
102                 graph.close();
103         }
104
105         @Test
106         public void verifyComplexVertexCountTest1() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
107                 List<Object> complexList = Arrays.asList(this.complex1, this.complex2 );
108                 JsonObject jo = this.formatter.output(complexList);
109                 assertEquals(2, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("complex").getAsInt());
110         }
111         
112         @Test
113         public void verifyPserverVertexCountTest1() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
114                 List<Object> pserverList = Arrays.asList(this.pserver1 );
115                 JsonObject jo = this.formatter.output(pserverList);
116                 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
117         }
118         
119         @Test
120         public void verifyComplexVertexCountTest2() 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() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
128                 List<Object> list = Arrays.asList(this.complex1, this.pserver1, this.complex2 );
129                 JsonObject jo = this.formatter.output(list);
130                 assertEquals(1, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("pserver").getAsInt());
131         }
132         
133         @Test
134         public void verifyLongTest() throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
135                 List<Object> complexList = Arrays.asList(Long.valueOf(22L) );
136                 JsonObject jo = this.formatter.output(complexList);
137                 assertEquals(22, jo.get("results").getAsJsonArray().get(0).getAsJsonObject().get("count").getAsInt());
138         }
139
140
141         public void createLoaderEngineSetup() throws AAIException {
142
143                 if (loader == null) {
144                         loader = LoaderFactory.createLoaderForVersion(factoryType, version);
145                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
146                         serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
147                         
148                         ff = new FormatFactory(loader, serializer);
149                         formatter = ff.get(Format.count);
150
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(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
159                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
160                 }
161         }
162 }