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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import com.google.gson.JsonObject;
29 import java.util.Arrays;
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
35 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
36 import org.apache.tinkerpop.gremlin.structure.Graph;
37 import org.apache.tinkerpop.gremlin.structure.T;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.aai.AAISetup;
45 import org.onap.aai.dbmap.DBConnectionType;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.introspection.ModelType;
49 import org.onap.aai.serialization.db.DBSerializer;
50 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
54 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
55 import org.springframework.test.annotation.DirtiesContext;
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class ResourceFormatTest extends AAISetup {
61 private UrlBuilder urlBuilder;
64 private TransactionalGraphEngine dbEngine;
65 private Loader loader;
66 private DBSerializer serializer;
67 private Resource resource;
68 private Vertex vfModule;
69 private Vertex unknown;
70 private final ModelType factoryType = ModelType.MOXY;
73 public void setUp() throws Exception {
75 MockitoAnnotations.initMocks(this);
77 graph = TinkerGraph.open();
79 vfModule = graph.addVertex(T.label, "vf-module", T.id, "5", "aai-node-type", "vf-module", "vf-module-id",
80 "vf-module-id-val-68205", "vf-module-name", "example-vf-module-name-val-68205", "heat-stack-id",
81 "example-heat-stack-id-val-68205", "orchestration-status", "example-orchestration-status-val-68205",
82 "is-base-vf-module", "true", "resource-version", "1498166571906", "model-invariant-id",
83 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-invariant-id-local",
84 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
85 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c", "widget-model-id",
86 "example-widget-model-id-val-68205", "widget-model-version", "example-widget--model-version-val-68205",
87 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
89 unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
90 "vserver-id-1", "vserver-name", "vserver-name-1");
94 public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
96 createLoaderEngineSetup();
97 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
98 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
100 assertNotNull(dbEngine.tx());
101 assertNotNull(dbEngine.asAdmin());
103 JsonObject json = resource.getJsonFromVertex(vfModule).get();
105 assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
106 assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
108 assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
109 assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
114 public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
116 createLoaderEngineSetup();
117 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
118 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
120 assertNotNull(dbEngine.tx());
121 assertNotNull(dbEngine.asAdmin());
123 assertFalse(resource.getJsonFromVertex(unknown).isPresent());
128 public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
130 createLoaderEngineSetup();
131 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
133 FormatFactory ff = new FormatFactory(loader, serializer, schemaVersions, basePath);
134 MultivaluedMap mvm = new MultivaluedHashMap();
135 mvm.add("depth", "0");
136 Formatter formatter = ff.get(Format.resource, mvm);
138 JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
139 System.out.println(json);
143 public void createLoaderEngineSetup() {
145 if (loader == null) {
146 loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
147 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
149 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
151 when(dbEngine.tx()).thenReturn(graph);
152 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
154 when(spyAdmin.getReadOnlyTraversalSource())
155 .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
156 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());