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