Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / search / SimpleFormatTest.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 package org.onap.aai.rest.search;
21
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.tinkerpop.gremlin.structure.Graph;
31 import org.apache.tinkerpop.gremlin.structure.T;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.aai.dbmap.DBConnectionType;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.introspection.LoaderFactory;
41 import org.onap.aai.introspection.ModelType;
42 import org.onap.aai.introspection.Version;
43 import org.onap.aai.serialization.db.DBSerializer;
44 import org.onap.aai.serialization.db.EdgeRules;
45 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
46 import org.onap.aai.serialization.engines.QueryStyle;
47 import org.onap.aai.serialization.engines.TitanDBEngine;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49 import org.onap.aai.serialization.queryformats.RawFormat;
50 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
51 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
52
53 import com.google.gson.JsonObject;
54
55 public class SimpleFormatTest {
56
57         protected Graph graph;
58         private TransactionalGraphEngine dbEngine;
59
60         protected final List<Vertex> expectedResult = new ArrayList<>();
61         protected final EdgeRules rules = EdgeRules.getInstance();
62         protected Loader loader;
63         private DBSerializer serializer;
64
65         @Mock
66         private UrlBuilder urlBuilder;
67         private RawFormat _simpleFormat;
68
69         Vertex vfmodule = null;
70
71         public SimpleFormatTest() throws AAIException, NoEdgeRuleFoundException {
72                 setUp();
73         }
74
75         public void setUp() throws AAIException, NoEdgeRuleFoundException {
76                 System.setProperty("AJSC_HOME", ".");
77                 System.setProperty("BUNDLECONFIG_DIR", "src/main/resources");
78                 MockitoAnnotations.initMocks(this);
79                 graph = TinkerGraph.open();
80                 loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
81                 vfmodule = graph.addVertex(T.label, "vf-module",
82                                 T.id, "5",
83                                 "aai-node-type", "vf-module",
84                                 "vf-module-id", "vf-module-id-val-68205",
85                                 "vf-module-name", "example-vf-module-name-val-68205",
86                                 "heat-stack-id", "example-heat-stack-id-val-68205",
87                                 "orchestration-status", "example-orchestration-status-val-68205",
88                                 "is-base-vf-module", "true",
89                                 "resource-version", "1498166571906",
90                                 "model-invariant-id", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
91                                 "model-invariant-id-local", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
92                                 "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
93                                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
94                                 "widget-model-id", "example-widget-model-id-val-68205",
95                                 "widget-model-version", "example-widget--model-version-val-68205",
96                                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
97
98                 final ModelType factoryType = ModelType.MOXY;
99                 Loader loader = LoaderFactory.createLoaderForVersion(factoryType, Version.v10);
100                 dbEngine = spy(new TitanDBEngine(
101                                 QueryStyle.TRAVERSAL,
102                                 DBConnectionType.CACHED,
103                                 loader));
104
105                 when(dbEngine.tx()).thenReturn(graph);
106                 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
107                 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
108                 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
109                 serializer = new DBSerializer(Version.v10, dbEngine, factoryType, "Junit");
110                 _simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).modelDriven().build();
111                 dbEngine.startTransaction();
112         }
113
114         @Test
115         public void run() throws AAIFormatVertexException {
116                 assertNotNull(dbEngine.tx());
117                 System.out.println(dbEngine.tx());
118                 assertNotNull(graph.traversal());
119                 JsonObject json = _simpleFormat.createPropertiesObject(vfmodule);
120                 json.entrySet().stream().forEach((System.out::println));
121                 assertTrue(json.has("model-invariant-id"));
122
123         }
124
125 }