51ccd363c8699f8ba5e450757d4f52b803ee7820
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / serialization / queryformats / SimpleFormatTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.aai.serialization.queryformats;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Matchers.anyBoolean;
27 import static org.mockito.Matchers.anyInt;
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.when;
33
34 import java.io.UnsupportedEncodingException;
35
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
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.Ignore;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.openecomp.aai.dbmap.DBConnectionType;
48 import org.openecomp.aai.exceptions.AAIException;
49 import org.openecomp.aai.introspection.Loader;
50 import org.openecomp.aai.introspection.LoaderFactory;
51 import org.openecomp.aai.introspection.ModelType;
52 import org.openecomp.aai.introspection.Version;
53 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
54 import org.openecomp.aai.serialization.db.DBSerializer;
55 import org.openecomp.aai.serialization.engines.QueryStyle;
56 import org.openecomp.aai.serialization.engines.TitanDBEngine;
57 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
58 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
59 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
60 import org.openecomp.aai.util.AAIConstants;
61
62 import com.google.gson.JsonObject;
63
64 public class SimpleFormatTest {
65
66         @Mock
67         private UrlBuilder urlBuilder;
68
69         private Graph graph;
70         private TransactionalGraphEngine dbEngine;
71         private Loader loader;
72         private DBSerializer serializer;
73         private RawFormat simpleFormat;
74         private Vertex vfModule;
75
76
77
78         private final ModelType factoryType = ModelType.MOXY;
79
80         @Before
81         public void setUp() throws Exception {
82
83                 MockitoAnnotations.initMocks(this);
84
85                 System.setProperty("AJSC_HOME", "src/test/resources");
86                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
87                 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"),
88                                 "src/test/resources/org/openecomp/aai/introspection/");
89
90                 graph = TinkerGraph.open();
91
92                 vfModule = graph.addVertex(
93                                 T.label, "vf-module",
94                                 T.id, "5",
95                                 "aai-node-type", "vf-module",
96                                 "vf-module-id", "vf-module-id-val-68205",
97                                 "vf-module-name", "example-vf-module-name-val-68205",
98                                 "heat-stack-id", "example-heat-stack-id-val-68205",
99                                 "orchestration-status", "example-orchestration-status-val-68205",
100                                 "is-base-vf-module", "true",
101                                 "resource-version", "1498166571906",
102                                 "model-invariant-id", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
103                                 "model-invariant-id-local", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
104                                 "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
105                                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
106                                 "widget-model-id", "example-widget-model-id-val-68205",
107                                 "widget-model-version", "example-widget--model-version-val-68205",
108                                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205"
109                 );
110         }
111
112         @Test
113         public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
114
115             createLoaderEngineSetup();
116                 serializer = new DBSerializer(Version.v10, dbEngine, factoryType, "Junit");
117                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
118
119                 assertNotNull(dbEngine.tx());
120                 assertNotNull(dbEngine.asAdmin());
121
122                 JsonObject json = simpleFormat.createPropertiesObject(vfModule);
123
124                 assertTrue(json.has("model-invariant-id"));
125                 assertTrue(json.has("model-version-id"));
126
127                 assertFalse(json.has("model-invariant-id-local"));
128                 assertFalse(json.has("model-version-id-local"));
129
130         }
131
132         @Ignore
133         @Test(expected = AAIFormatVertexException.class)
134         public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
135
136                 serializer = mock(DBSerializer.class);
137                 loader = mock(Loader.class);
138
139                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
140
141                 when(serializer.dbToObject(anyObject(), anyObject(), anyInt(), anyBoolean(), anyString()))
142                         .thenThrow(new AAIException("Test Exception"));
143
144                 simpleFormat.createPropertiesObject(vfModule);
145         }
146
147         @Ignore
148         @Test(expected = AAIFormatVertexException.class)
149         public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
150
151                 loader = mock(Loader.class);
152                 serializer = mock(DBSerializer.class);
153
154                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
155
156                 when(loader.introspectorFromName(anyString()))
157                                 .thenThrow(new AAIUnknownObjectException("Test Exception"));
158
159                 simpleFormat.createPropertiesObject(vfModule);
160         }
161
162         public void createLoaderEngineSetup(){
163
164                 if(loader == null){
165                         loader = LoaderFactory.createLoaderForVersion(factoryType, Version.v10);
166                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
167
168                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
169
170                         when(dbEngine.tx()).thenReturn(graph);
171                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
172
173                         when(spyAdmin.getReadOnlyTraversalSource()).thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
174                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
175                 }
176         }
177 }