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