2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import com.google.gson.JsonObject;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.T;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.ModelType;
45 import org.onap.aai.serialization.db.DBSerializer;
46 import org.onap.aai.serialization.db.EdgeSerializer;
47 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
48 import org.onap.aai.serialization.engines.QueryStyle;
49 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
50 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
51 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
52 import org.onap.aai.setup.SchemaVersion;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.test.annotation.DirtiesContext;
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
57 public class AggregateFormatTest extends AAISetup {
60 private UrlBuilder urlBuilder;
63 private TransactionalGraphEngine dbEngine;
64 private Loader loader;
65 private Aggregate aggregate;
66 private final ModelType factoryType = ModelType.MOXY;
69 private EdgeSerializer rules;
71 private SchemaVersion version;
72 private Vertex pserver;
73 private Vertex complex;
76 public void setUp() throws Exception {
78 version = schemaVersions.getDefaultVersion();
80 MockitoAnnotations.initMocks(this);
82 graph = TinkerGraph.open();
85 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "hostname-1");
86 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
87 "physical-location-id", "physical-location-id-1", "country", "US");
89 GraphTraversalSource g = graph.traversal();
90 rules.addEdge(g, pserver1, complex1);
95 System.setProperty("AJSC_HOME", ".");
96 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
98 createLoaderEngineSetup();
101 private void createLoaderEngineSetup() throws AAIException {
103 if (loader == null) {
104 loader = loaderFactory.createLoaderForVersion(factoryType, version);
105 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
106 DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
107 aggregate = new Aggregate.Builder(loader, serializer, urlBuilder).build();
109 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
111 when(dbEngine.tx()).thenReturn(graph);
112 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
114 when(spyAdmin.getReadOnlyTraversalSource())
115 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
116 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
121 public void run() throws AAIFormatVertexException {
122 assertNotNull(dbEngine.tx());
123 System.out.println(dbEngine.tx());
124 assertNotNull(graph.traversal());
125 JsonObject json = aggregate.createPropertiesObject(pserver).get();
126 json.entrySet().forEach((System.out::println));
127 assertTrue(json.has("hostname"));
128 assertFalse(json.has("node-type"));
129 Map<String, List<String>> propMap = new HashMap<>();
130 List<String> selectedProps = new ArrayList<String>(Arrays.asList("'physical-location-id'"));
131 propMap.put("complex", selectedProps);
132 JsonObject json1 = aggregate.createSelectedPropertiesObject(complex, propMap).get();
133 json1.entrySet().forEach((System.out::println));
134 assertFalse(json1.has("country"));
135 assertTrue(json1.has("physical-location-id"));