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 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.Ignore;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.openecomp.aai.dbmap.DBConnectionType;
36 import org.openecomp.aai.exceptions.AAIException;
37 import org.openecomp.aai.introspection.Loader;
38 import org.openecomp.aai.introspection.LoaderFactory;
39 import org.openecomp.aai.introspection.ModelType;
40 import org.openecomp.aai.introspection.Version;
41 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
42 import org.openecomp.aai.serialization.db.DBSerializer;
43 import org.openecomp.aai.serialization.engines.QueryStyle;
44 import org.openecomp.aai.serialization.engines.TitanDBEngine;
45 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
46 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
47 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
49 import java.io.UnsupportedEncodingException;
51 import static org.junit.Assert.*;
52 import static org.mockito.Matchers.anyBoolean;
53 import static org.mockito.Matchers.anyInt;
54 import static org.mockito.Matchers.anyObject;
55 import static org.mockito.Matchers.anyString;
56 import static org.mockito.Mockito.*;
58 public class SimpleFormatTest {
61 private UrlBuilder urlBuilder;
64 private TransactionalGraphEngine dbEngine;
65 private Loader loader;
66 private DBSerializer serializer;
67 private RawFormat simpleFormat;
68 private Vertex vfModule;
69 private final ModelType factoryType = ModelType.MOXY;
72 public void setUp() throws Exception {
74 MockitoAnnotations.initMocks(this);
76 graph = TinkerGraph.open();
78 vfModule = graph.addVertex(
81 "aai-node-type", "vf-module",
82 "vf-module-id", "vf-module-id-val-68205",
83 "vf-module-name", "example-vf-module-name-val-68205",
84 "heat-stack-id", "example-heat-stack-id-val-68205",
85 "orchestration-status", "example-orchestration-status-val-68205",
86 "is-base-vf-module", "true",
87 "resource-version", "1498166571906",
88 "model-invariant-id", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
89 "model-invariant-id-local", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
90 "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
91 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
92 "widget-model-id", "example-widget-model-id-val-68205",
93 "widget-model-version", "example-widget--model-version-val-68205",
94 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205"
99 public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
101 createLoaderEngineSetup();
102 serializer = new DBSerializer(Version.v10, dbEngine, factoryType, "Junit");
103 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
105 assertNotNull(dbEngine.tx());
106 assertNotNull(dbEngine.asAdmin());
108 JsonObject json = simpleFormat.createPropertiesObject(vfModule);
110 assertTrue(json.has("model-invariant-id"));
111 assertTrue(json.has("model-version-id"));
113 assertFalse(json.has("model-invariant-id-local"));
114 assertFalse(json.has("model-version-id-local"));
119 @Test(expected = AAIFormatVertexException.class)
120 public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
122 serializer = mock(DBSerializer.class);
123 loader = mock(Loader.class);
125 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
127 when(serializer.dbToObject(anyObject(), anyObject(), anyInt(), anyBoolean(), anyString()))
128 .thenThrow(new AAIException("Test Exception"));
130 simpleFormat.createPropertiesObject(vfModule);
134 @Test(expected = AAIFormatVertexException.class)
135 public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
137 loader = mock(Loader.class);
138 serializer = mock(DBSerializer.class);
140 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
142 when(loader.introspectorFromName(anyString()))
143 .thenThrow(new AAIUnknownObjectException("Test Exception"));
145 simpleFormat.createPropertiesObject(vfModule);
148 public void createLoaderEngineSetup(){
151 loader = LoaderFactory.createLoaderForVersion(factoryType, Version.v10);
152 dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
154 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
156 when(dbEngine.tx()).thenReturn(graph);
157 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
159 when(spyAdmin.getReadOnlyTraversalSource()).thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
160 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());