aai-common support for v20
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / ResourceFormatTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonObject;
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.exceptions.AAIException;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.serialization.db.DBSerializer;
38 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
39 import org.onap.aai.serialization.engines.QueryStyle;
40 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
42 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
43 import org.springframework.test.annotation.DirtiesContext;
44
45 import javax.ws.rs.core.MultivaluedHashMap;
46 import javax.ws.rs.core.MultivaluedMap;
47 import java.util.Arrays;
48
49 import static org.junit.Assert.*;
50 import static org.mockito.Mockito.spy;
51 import static org.mockito.Mockito.when;
52
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
54 public class ResourceFormatTest extends AAISetup {
55
56     @Mock
57     private UrlBuilder urlBuilder;
58
59     private Graph graph;
60     private TransactionalGraphEngine dbEngine;
61     private Loader loader;
62     private DBSerializer serializer;
63     private Resource resource;
64     private Vertex vfModule;
65     private Vertex unknown;
66     private final ModelType factoryType = ModelType.MOXY;
67
68     @Before
69     public void setUp() throws Exception {
70
71         MockitoAnnotations.initMocks(this);
72
73         graph = TinkerGraph.open();
74
75         vfModule = graph.addVertex(T.label, "vf-module", T.id, "5", "aai-node-type", "vf-module", "vf-module-id",
76                 "vf-module-id-val-68205", "vf-module-name", "example-vf-module-name-val-68205", "heat-stack-id",
77                 "example-heat-stack-id-val-68205", "orchestration-status", "example-orchestration-status-val-68205",
78                 "is-base-vf-module", "true", "resource-version", "1498166571906", "model-invariant-id",
79                 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-invariant-id-local",
80                 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
81                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c", "widget-model-id",
82                 "example-widget-model-id-val-68205", "widget-model-version", "example-widget--model-version-val-68205",
83                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
84
85         unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
86                 "vserver-id-1", "vserver-name", "vserver-name-1");
87     }
88
89     @Test
90     public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
91
92         createLoaderEngineSetup();
93         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
94         resource = new Resource.Builder(loader, serializer, urlBuilder).build();
95
96         assertNotNull(dbEngine.tx());
97         assertNotNull(dbEngine.asAdmin());
98
99         JsonObject json = resource.getJsonFromVertex(vfModule).get();
100
101         assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
102         assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
103
104         assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
105         assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
106
107     }
108
109     @Test
110     public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
111
112         createLoaderEngineSetup();
113         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
114         resource = new Resource.Builder(loader, serializer, urlBuilder).build();
115
116         assertNotNull(dbEngine.tx());
117         assertNotNull(dbEngine.asAdmin());
118
119         assertFalse(resource.getJsonFromVertex(unknown).isPresent());
120
121     }
122
123     @Test
124     public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
125
126         createLoaderEngineSetup();
127         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
128
129         FormatFactory ff = new FormatFactory(loader, serializer, schemaVersions, basePath, "https://localhost:8447/aai/");
130         MultivaluedMap mvm = new MultivaluedHashMap();
131         mvm.add("depth", "0");
132         Formatter formatter = ff.get(Format.resource, mvm);
133
134         JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
135         System.out.println(json);
136
137     }
138
139     public void createLoaderEngineSetup() {
140
141         if (loader == null) {
142             loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
143             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
144
145             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
146
147             when(dbEngine.tx()).thenReturn(graph);
148             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
149
150             when(spyAdmin.getReadOnlyTraversalSource())
151                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
152             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
153         }
154     }
155 }