0df32bd3b2e2db0e22b25a2a87176d07c4822f20
[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 com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24 import org.apache.tinkerpop.gremlin.process.traversal.Path;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
27 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.T;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.dbmap.DBConnectionType;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.onap.aai.serialization.db.EdgeSerializer;
43 import org.onap.aai.serialization.engines.QueryStyle;
44 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
45 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
46 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
47 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
48 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.test.annotation.DirtiesContext;
51
52 import java.io.UnsupportedEncodingException;
53
54 import static org.junit.Assert.assertEquals;
55 import static org.junit.Assert.assertNotNull;
56 import static org.mockito.Mockito.*;
57
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class MultiFormatTest extends AAISetup {
60
61         @Mock
62         private UrlBuilder urlBuilder;
63
64         private Graph graph;
65         private TransactionalGraphEngine dbEngine;
66         private Loader loader;
67         private IdURL idFormat;
68         private final ModelType factoryType = ModelType.MOXY;
69         @Autowired
70         private EdgeSerializer rules;
71         private Tree<?> resultTree;
72         private Path resultPath;
73         private SchemaVersion version;
74         private JsonObject expectedTreeIdFormat = new JsonParser()
75                         .parse("{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}").getAsJsonObject();
76         private JsonObject expectedPathIdFormat = new JsonParser()
77                         .parse("{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}").getAsJsonObject();
78
79         
80         
81         @Before
82         public void setUp() throws Exception {
83
84             version = schemaVersions.getAppRootVersion();
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).get();
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).get();
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                         //loader = LoaderFactory.createLoaderForVersion(factoryType, version);
161                         dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
162
163                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
164
165                         when(dbEngine.tx()).thenReturn(graph);
166                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
167
168                         when(spyAdmin.getReadOnlyTraversalSource())
169                                         .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
170                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
171                 }
172         }
173 }