Merge "[AAI] Fix doc config files"
[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 javax.ws.rs.core.MultivaluedHashMap;
33
34 import org.apache.tinkerpop.gremlin.process.traversal.Path;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
36 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
37 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
38 import org.apache.tinkerpop.gremlin.structure.Graph;
39 import org.apache.tinkerpop.gremlin.structure.T;
40 import org.apache.tinkerpop.gremlin.structure.Vertex;
41 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.aai.AAISetup;
47 import org.onap.aai.exceptions.AAIException;
48 import org.onap.aai.introspection.Loader;
49 import org.onap.aai.introspection.ModelType;
50 import org.onap.aai.serialization.db.DBSerializer;
51 import org.onap.aai.serialization.db.EdgeSerializer;
52 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
53 import org.onap.aai.serialization.engines.QueryStyle;
54 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
55 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
56 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
57 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
58 import org.onap.aai.setup.SchemaVersion;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.test.annotation.DirtiesContext;
61
62 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
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     @Autowired
74     private EdgeSerializer rules;
75     private Tree<?> resultTree;
76     private Path resultPath;
77     private SchemaVersion version;
78     private JsonObject expectedTreeIdFormat = new JsonParser().parse(
79             "{\"nodes\":[{\"resource-type\":\"generic-vnf\",\"nodes\":[{\"resource-type\":\"vserver\",\"nodes\":[{\"resource-type\":\"pserver\"}]},{\"resource-type\":\"pserver\",\"nodes\":[{\"resource-type\":\"complex\"}]}]}]}")
80             .getAsJsonObject();
81     private JsonObject expectedPathIdFormat = new JsonParser().parse(
82             "{\"path\":[{\"resource-type\":\"generic-vnf\"},{\"resource-type\":\"vserver\"},{\"resource-type\":\"pserver\"},{\"resource-type\":\"complex\"}]}")
83             .getAsJsonObject();
84     private JsonObject expectedAsTreeWithResourceFormat = new JsonParser().parse(
85             "{\"results\":[{\"generic-vnf\":{\"vnf-id\":\"vnf-id-1\",\"vnf-name\":\"vnf-name-1\",\"related-nodes\":[{\"vserver\":{\"vserver-id\":\"vserver-id-1\",\"vserver-name\":\"vserver-name-1\",\"related-nodes\":[{\"pserver\":{\"hostname\":\"hostname-1\"}}]}},{\"pserver\":{\"hostname\":\"hostname-2\",\"related-nodes\":[{\"complex\":{\"physical-location-id\":\"physical-location-id-2\",\"country\":\"US\"}}]}}]}}]}")
86             .getAsJsonObject();
87     private JsonObject expectedAsTreeWithSimpleFormat = new JsonParser().parse(
88             "{\"results\":[{\"id\":\"0\",\"node-type\":\"generic-vnf\",\"url\":null,\"properties\":{\"vnf-id\":\"vnf-id-1\",\"vnf-name\":\"vnf-name-1\"},\"related-to\":[{\"id\":\"1\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"vserver\",\"url\":null},{\"id\":\"5\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"pserver\",\"url\":null}],\"related-nodes\":[{\"id\":\"1\",\"node-type\":\"vserver\",\"url\":null,\"properties\":{\"vserver-id\":\"vserver-id-1\",\"vserver-name\":\"vserver-name-1\"},\"related-to\":[{\"id\":\"0\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"generic-vnf\",\"url\":null},{\"id\":\"2\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"pserver\",\"url\":null}],\"related-nodes\":[{\"id\":\"2\",\"node-type\":\"pserver\",\"url\":null,\"properties\":{\"hostname\":\"hostname-1\"},\"related-to\":[{\"id\":\"1\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"vserver\",\"url\":null},{\"id\":\"3\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"node-type\":\"complex\",\"url\":null}]}]},{\"id\":\"5\",\"node-type\":\"pserver\",\"url\":null,\"properties\":{\"hostname\":\"hostname-2\"},\"related-to\":[{\"id\":\"0\",\"relationship-label\":\"tosca.relationships.HostedOn\",\"node-type\":\"generic-vnf\",\"url\":null},{\"id\":\"6\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"node-type\":\"complex\",\"url\":null}],\"related-nodes\":[{\"id\":\"6\",\"node-type\":\"complex\",\"url\":null,\"properties\":{\"physical-location-id\":\"physical-location-id-2\",\"country\":\"US\"},\"related-to\":[{\"id\":\"5\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"node-type\":\"pserver\",\"url\":null}]}]}]}]}")
89             .getAsJsonObject();
90
91     @Before
92     public void setUp() throws Exception {
93
94         version = schemaVersions.getAppRootVersion();
95         MockitoAnnotations.initMocks(this);
96
97         graph = TinkerGraph.open();
98
99         Vertex gnvf1 = graph.addVertex(T.label, "generic-vnf", T.id, "0", "aai-node-type", "generic-vnf", "vnf-id",
100                 "vnf-id-1", "vnf-name", "vnf-name-1");
101         Vertex vserver1 = graph.addVertex(T.label, "vserver", T.id, "1", "aai-node-type", "vserver", "vserver-id",
102                 "vserver-id-1", "vserver-name", "vserver-name-1");
103         Vertex pserver1 =
104                 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
105         Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
106                 "physical-location-id", "physical-location-id-1", "country", "US");
107
108         Vertex pserver2 =
109                 graph.addVertex(T.label, "pserver", T.id, "5", "aai-node-type", "pserver", "hostname", "hostname-2");
110         Vertex complex2 = graph.addVertex(T.label, "complex", T.id, "6", "aai-node-type", "complex",
111                 "physical-location-id", "physical-location-id-2", "country", "US");
112
113         GraphTraversalSource g = graph.traversal();
114         rules.addEdge(g, gnvf1, vserver1);
115         rules.addEdge(g, vserver1, pserver1);
116         rules.addEdge(g, pserver1, complex1);
117         rules.addEdge(g, gnvf1, pserver2);
118         rules.addEdge(g, pserver2, complex2);
119
120         resultTree = graph.traversal().V("0").out().out().tree().next();
121         resultPath = graph.traversal().V("0").out().hasId("1").out().hasId("2").out().hasId("3").path().next();
122     }
123
124     @Test
125     public void testTreeResultQueryIdFormat()
126             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
127
128         createLoaderEngineSetup();
129         idFormat = new IdURL(loader, urlBuilder);
130
131         assertNotNull(dbEngine.tx());
132         assertNotNull(dbEngine.asAdmin());
133
134         JsonObject json = idFormat.formatObject(resultTree).get();
135
136         assertEquals(this.expectedTreeIdFormat, json);
137
138     }
139
140     @Test
141     public void testAsTreeParamAndSimpleFormat()
142             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
143
144         createLoaderEngineSetup();
145         DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
146         MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
147         params.add("as-tree", "true");
148         SimpleFormat sf = new SimpleFormat(new RawFormat.Builder(loader, serializer, urlBuilder).isTree(true));
149
150         assertNotNull(dbEngine.tx());
151         assertNotNull(dbEngine.asAdmin());
152
153         JsonObject json = sf.formatObject(resultTree).get();
154         assertEquals(this.expectedAsTreeWithSimpleFormat, json);
155
156     }
157
158     @Test
159     public void testAsTreeParamAndResourceFormat()
160             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
161
162         createLoaderEngineSetup();
163         DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
164         MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
165         params.add("as-tree", "true");
166         Resource sf = new Resource(new Resource.Builder(loader, serializer, urlBuilder, params).isTree(true));
167
168         assertNotNull(dbEngine.tx());
169         assertNotNull(dbEngine.asAdmin());
170
171         JsonObject json = sf.formatObject(resultTree).get();
172         assertEquals(this.expectedAsTreeWithResourceFormat, json);
173     }
174
175     @Test
176     public void testPathResultQueryIdFormat()
177             throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
178
179         createLoaderEngineSetup();
180         idFormat = new IdURL(loader, urlBuilder);
181
182         assertNotNull(dbEngine.tx());
183         assertNotNull(dbEngine.asAdmin());
184
185         JsonObject json = idFormat.formatObject(resultPath).get();
186
187         assertEquals(this.expectedPathIdFormat, json);
188
189     }
190
191     @Test(expected = AAIFormatQueryResultFormatNotSupported.class)
192     public void testThrowsExceptionIfObjectNotSupported() throws AAIFormatVertexException, AAIException,
193             UnsupportedEncodingException, AAIFormatQueryResultFormatNotSupported {
194
195         loader = mock(Loader.class);
196         idFormat = new IdURL(loader, urlBuilder);
197         idFormat.formatObject(new String());
198     }
199
200     public void createLoaderEngineSetup() {
201
202         if (loader == null) {
203             loader = loaderFactory.createLoaderForVersion(factoryType, version);
204             // loader = LoaderFactory.createLoaderForVersion(factoryType, version);
205             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
206
207             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
208
209             when(dbEngine.tx()).thenReturn(graph);
210             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
211
212             when(spyAdmin.getReadOnlyTraversalSource())
213                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
214             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
215         }
216     }
217 }