fde1307eda5d0ee059d25bd741842e7e305d7d62
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / MultiFormatTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29
30 import java.io.UnsupportedEncodingException;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.Path;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
34 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
35 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
36 import org.apache.tinkerpop.gremlin.structure.Graph;
37 import org.apache.tinkerpop.gremlin.structure.T;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.aai.AAISetup;
45 import org.onap.aai.dbmap.DBConnectionType;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.introspection.LoaderFactory;
49 import org.onap.aai.introspection.ModelType;
50 import org.onap.aai.introspection.Version;
51 import org.onap.aai.serialization.db.DBSerializer;
52 import org.onap.aai.serialization.db.EdgeRules;
53 import org.onap.aai.serialization.engines.QueryStyle;
54 import org.onap.aai.serialization.engines.TitanDBEngine;
55 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
56 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
57 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
58 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
59
60 import com.google.gson.JsonObject;
61 import com.google.gson.JsonParser;
62
63 public class MultiFormatTest extends AAISetup {
64
65         @Mock
66         private UrlBuilder urlBuilder;
67
68         private Graph graph;
69         private TransactionalGraphEngine dbEngine;
70         private Loader loader;
71         private IdURL idFormat;
72         private final ModelType factoryType = ModelType.MOXY;
73         private final EdgeRules rules = EdgeRules.getInstance();
74         private Tree<?> resultTree;
75         private Path resultPath;
76         private Version version = Version.v11;
77         private JsonObject expectedTreeIdFormat = new JsonParser()
78                         .parse("{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}").getAsJsonObject();
79         private JsonObject expectedPathIdFormat = new JsonParser()
80                         .parse("{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}").getAsJsonObject();
81
82         @Before
83         public void setUp() throws Exception {
84
85                 MockitoAnnotations.initMocks(this);
86
87                 graph = TinkerGraph.open();
88
89                 Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
90                                 "vnf-id-1", "vnf-name", "vnf-name-1");
91                 Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
92                                 "vserver-id-1", "vserver-name", "vserver-name-1");
93                 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
94                                 "hostname-1");
95                 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
96                                 "physical-location-id", "physical-location-id-1", "country", "US");
97
98                 Vertex pserver2 = graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname",
99                                 "hostname-2");
100                 Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
101                                 "physical-location-id", "physical-location-id-2", "country", "US");
102
103                 GraphTraversalSource g = graph.traversal();
104                 rules.addEdge(g, gnvf1, vserver1);
105                 rules.addEdge(g, vserver1, pserver1);
106                 rules.addEdge(g, pserver1, complex1);
107                 rules.addEdge(g, gnvf1, pserver2);
108                 rules.addEdge(g, pserver2, complex2);
109
110                 resultTree = graph.traversal().V("0").out().out().tree().next();
111                 resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
112         }
113
114         @Test
115         public void testTreeResultQueryIdFormat()
116                         throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
117
118                 createLoaderEngineSetup();
119                 idFormat = new IdURL(loader, urlBuilder);
120
121                 assertNotNull(dbEngine.tx());
122                 assertNotNull(dbEngine.asAdmin());
123
124                 JsonObject json = idFormat.formatObject(resultTree);
125                 
126                 assertEquals(this.expectedTreeIdFormat, json);
127
128         }
129         
130         @Test
131         public void testPathResultQueryIdFormat()
132                         throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
133
134                 createLoaderEngineSetup();
135                 idFormat = new IdURL(loader, urlBuilder);
136
137                 assertNotNull(dbEngine.tx());
138                 assertNotNull(dbEngine.asAdmin());
139
140                 JsonObject json = idFormat.formatObject(resultPath);
141                 
142                 assertEquals(this.expectedPathIdFormat, json);
143
144         }
145
146         
147         @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
148         public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException,
149                         AAIException, UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
150
151                 loader = mock(Loader.class);
152                 idFormat = new IdURL(loader, urlBuilder);
153                 idFormat.formatObject(new String());
154         }
155
156         public void createLoaderEngineSetup() {
157
158                 if (loader == null) {
159                         loader = LoaderFactory.createLoaderForVersion(factoryType, version);
160                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
161
162                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
163
164                         when(dbEngine.tx()).thenReturn(graph);
165                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
166
167                         when(spyAdmin.getReadOnlyTraversalSource())
168                                         .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
169                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
170                 }
171         }
172 }