2 * ============LICENSE_START=======================================================
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
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.openecomp.aai.serialization.queryformats;
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;
34 import java.io.UnsupportedEncodingException;
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;
62 import com.google.gson.JsonObject;
64 public class SimpleFormatTest {
67 private UrlBuilder urlBuilder;
70 private TransactionalGraphEngine dbEngine;
71 private Loader loader;
72 private DBSerializer serializer;
73 private RawFormat simpleFormat;
74 private Vertex vfModule;
78 private final ModelType factoryType = ModelType.MOXY;
81 public void setUp() throws Exception {
83 MockitoAnnotations.initMocks(this);
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/");
90 graph = TinkerGraph.open();
92 vfModule = graph.addVertex(
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"
113 public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
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();
119 assertNotNull(dbEngine.tx());
120 assertNotNull(dbEngine.asAdmin());
122 JsonObject json = simpleFormat.createPropertiesObject(vfModule);
124 assertTrue(json.has("model-invariant-id"));
125 assertTrue(json.has("model-version-id"));
127 assertFalse(json.has("model-invariant-id-local"));
128 assertFalse(json.has("model-version-id-local"));
133 @Test(expected = AAIFormatVertexException.class)
134 public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
136 serializer = mock(DBSerializer.class);
137 loader = mock(Loader.class);
139 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
141 when(serializer.dbToObject(anyObject(), anyObject(), anyInt(), anyBoolean(), anyString()))
142 .thenThrow(new AAIException("Test Exception"));
144 simpleFormat.createPropertiesObject(vfModule);
148 @Test(expected = AAIFormatVertexException.class)
149 public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
151 loader = mock(Loader.class);
152 serializer = mock(DBSerializer.class);
154 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
156 when(loader.introspectorFromName(anyString()))
157 .thenThrow(new AAIUnknownObjectException("Test Exception"));
159 simpleFormat.createPropertiesObject(vfModule);
162 public void createLoaderEngineSetup(){
165 loader = LoaderFactory.createLoaderForVersion(factoryType, Version.v10);
166 dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
168 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
170 when(dbEngine.tx()).thenReturn(graph);
171 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
173 when(spyAdmin.getReadOnlyTraversalSource()).thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
174 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());