9ac79ca8d9dba46f76c591dcc8e1adf5fdc605a2
[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
21 package org.onap.aai.serialization.queryformats;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.*;
26
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
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.ModelType;
49 import org.onap.aai.serialization.db.EdgeSerializer;
50 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
54 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
55 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
56 import org.onap.aai.setup.SchemaVersion;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.test.annotation.DirtiesContext;
59
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
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     @Autowired
72     private EdgeSerializer rules;
73     private Tree<?> resultTree;
74     private Path resultPath;
75     private SchemaVersion version;
76     private JsonObject expectedTreeIdFormat = new JsonParser().parse(
77             "{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}")
78             .getAsJsonObject();
79     private JsonObject expectedPathIdFormat = new JsonParser().parse(
80             "{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}")
81             .getAsJsonObject();
82
83     @Before
84     public void setUp() throws Exception {
85
86         version = schemaVersions.getAppRootVersion();
87         MockitoAnnotations.initMocks(this);
88
89         graph = TinkerGraph.open();
90
91         Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
92                 "vnf-id-1", "vnf-name", "vnf-name-1");
93         Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
94                 "vserver-id-1", "vserver-name", "vserver-name-1");
95         Vertex pserver1 =
96                 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
97         Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
98                 "physical-location-id", "physical-location-id-1", "country", "US");
99
100         Vertex pserver2 =
101                 graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname", "hostname-2");
102         Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
103                 "physical-location-id", "physical-location-id-2", "country", "US");
104
105         GraphTraversalSource g = graph.traversal();
106         rules.addEdge(g, gnvf1, vserver1);
107         rules.addEdge(g, vserver1, pserver1);
108         rules.addEdge(g, pserver1, complex1);
109         rules.addEdge(g, gnvf1, pserver2);
110         rules.addEdge(g, pserver2, complex2);
111
112         resultTree = graph.traversal().V("0").out().out().tree().next();
113         resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
114     }
115
116     @Test
117     public void testTreeResultQueryIdFormat()
118             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
119
120         createLoaderEngineSetup();
121         idFormat = new IdURL(loader, urlBuilder);
122
123         assertNotNull(dbEngine.tx());
124         assertNotNull(dbEngine.asAdmin());
125
126         JsonObject json = idFormat.formatObject(resultTree).get();
127
128         assertEquals(this.expectedTreeIdFormat, json);
129
130     }
131
132     @Test
133     public void testPathResultQueryIdFormat()
134             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
135
136         createLoaderEngineSetup();
137         idFormat = new IdURL(loader, urlBuilder);
138
139         assertNotNull(dbEngine.tx());
140         assertNotNull(dbEngine.asAdmin());
141
142         JsonObject json = idFormat.formatObject(resultPath).get();
143
144         assertEquals(this.expectedPathIdFormat, json);
145
146     }
147
148     @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
149     public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException, AAIException,
150             UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
151
152         loader = mock(Loader.class);
153         idFormat = new IdURL(loader, urlBuilder);
154         idFormat.formatObject(new String());
155     }
156
157     public void createLoaderEngineSetup() {
158
159         if (loader == null) {
160             loader = loaderFactory.createLoaderForVersion(factoryType, version);
161             // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
162             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
163
164             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
165
166             when(dbEngine.tx()).thenReturn(graph);
167             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
168
169             when(spyAdmin.getReadOnlyTraversalSource())
170                     .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
171             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
172         }
173     }
174 }