Reduce the number of problems in aai-common by removing unused imports
[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 static org.junit.Assert.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import com.google.gson.JsonObject;
28
29 import java.util.*;
30
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;
55
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
57 public class AggregateFormatTest extends AAISetup {
58
59     @Mock
60     private UrlBuilder urlBuilder;
61
62     private Graph graph;
63     private TransactionalGraphEngine dbEngine;
64     private Loader loader;
65     private Aggregate aggregate;
66     private final ModelType factoryType = ModelType.MOXY;
67
68     @Autowired
69     private EdgeSerializer rules;
70
71     private SchemaVersion version;
72     private Vertex pserver;
73     private Vertex complex;
74
75     @Before
76     public void setUp() throws Exception {
77
78         version = schemaVersions.getDefaultVersion();
79
80         MockitoAnnotations.initMocks(this);
81
82         graph = TinkerGraph.open();
83
84         Vertex pserver1 =
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");
88
89         GraphTraversalSource g = graph.traversal();
90         rules.addEdge(g, pserver1, complex1);
91
92         pserver = pserver1;
93         complex = complex1;
94
95         System.setProperty("AJSC_HOME", ".");
96         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
97
98         createLoaderEngineSetup();
99     }
100
101     private void createLoaderEngineSetup() throws AAIException {
102
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();
108
109             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
110
111             when(dbEngine.tx()).thenReturn(graph);
112             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
113
114             when(spyAdmin.getReadOnlyTraversalSource())
115                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
116             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
117         }
118     }
119
120     @Test
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"));
136     }
137 }