2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.aai.serialization.queryformats;
22 import com.google.gson.JsonObject;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.T;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.dbmap.DBConnectionType;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.ModelType;
38 import org.onap.aai.serialization.db.DBSerializer;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
43 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
44 import org.springframework.test.annotation.DirtiesContext;
46 import javax.ws.rs.core.MultivaluedHashMap;
47 import javax.ws.rs.core.MultivaluedMap;
48 import java.util.Arrays;
50 import static org.junit.Assert.*;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.when;
54 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
55 public class ResourceFormatTest extends AAISetup {
58 private UrlBuilder urlBuilder;
61 private TransactionalGraphEngine dbEngine;
62 private Loader loader;
63 private DBSerializer serializer;
64 private Resource resource;
65 private Vertex vfModule;
66 private Vertex unknown;
67 private final ModelType factoryType = ModelType.MOXY;
70 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
74 graph = TinkerGraph.open();
76 vfModule = graph.addVertex(
79 "aai-node-type", "vf-module",
80 "vf-module-id", "vf-module-id-val-68205",
81 "vf-module-name", "example-vf-module-name-val-68205",
82 "heat-stack-id", "example-heat-stack-id-val-68205",
83 "orchestration-status", "example-orchestration-status-val-68205",
84 "is-base-vf-module", "true",
85 "resource-version", "1498166571906",
86 "model-invariant-id", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
87 "model-invariant-id-local", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
88 "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
89 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
90 "widget-model-id", "example-widget-model-id-val-68205",
91 "widget-model-version", "example-widget--model-version-val-68205",
92 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205"
95 unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
96 "vserver-id-1", "vserver-name", "vserver-name-1");
100 public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
102 createLoaderEngineSetup();
103 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
104 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
106 assertNotNull(dbEngine.tx());
107 assertNotNull(dbEngine.asAdmin());
109 JsonObject json = resource.getJsonFromVertex(vfModule).get();
111 assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
112 assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
114 assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
115 assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
120 public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
122 createLoaderEngineSetup();
123 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
124 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
126 assertNotNull(dbEngine.tx());
127 assertNotNull(dbEngine.asAdmin());
129 assertFalse(resource.getJsonFromVertex(unknown).isPresent());
134 public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
136 createLoaderEngineSetup();
137 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
139 FormatFactory ff = new FormatFactory(loader, serializer,schemaVersions, basePath);
140 MultivaluedMap mvm = new MultivaluedHashMap();
141 mvm.add("depth","0");
142 Formatter formatter = ff.get(Format.resource, mvm);
144 JsonObject json = formatter.output(Arrays.asList(unknown,vfModule));
145 System.out.println(json);
149 public void createLoaderEngineSetup(){
152 loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
153 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
155 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
157 when(dbEngine.tx()).thenReturn(graph);
158 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
160 when(spyAdmin.getReadOnlyTraversalSource()).thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
161 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());