Update spring boot to 2.4
[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 static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28
29 import com.google.gson.JsonObject;
30
31 import java.util.Arrays;
32
33 import javax.ws.rs.core.MultivaluedHashMap;
34 import javax.ws.rs.core.MultivaluedMap;
35
36 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
37 import org.apache.tinkerpop.gremlin.structure.Graph;
38 import org.apache.tinkerpop.gremlin.structure.T;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.aai.AAISetup;
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;
56
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class ResourceFormatTest extends AAISetup {
59
60     @Mock
61     private UrlBuilder urlBuilder;
62
63     private Graph graph;
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;
71
72     @Before
73     public void setUp() throws Exception {
74
75         MockitoAnnotations.openMocks(this);
76
77         graph = TinkerGraph.open();
78
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");
88
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");
91     }
92
93     @Test
94     public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
95
96         createLoaderEngineSetup();
97         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
98         resource = new Resource.Builder(loader, serializer, urlBuilder).build();
99
100         assertNotNull(dbEngine.tx());
101         assertNotNull(dbEngine.asAdmin());
102
103         JsonObject json = resource.getJsonFromVertex(vfModule).get();
104
105         assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
106         assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
107
108         assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
109         assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
110
111     }
112
113     @Test
114     public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
115
116         createLoaderEngineSetup();
117         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
118         resource = new Resource.Builder(loader, serializer, urlBuilder).build();
119
120         assertNotNull(dbEngine.tx());
121         assertNotNull(dbEngine.asAdmin());
122
123         assertFalse(resource.getJsonFromVertex(unknown).isPresent());
124
125     }
126
127     @Test
128     public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
129
130         createLoaderEngineSetup();
131         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
132
133         FormatFactory ff =
134                 new FormatFactory(loader, serializer, schemaVersions, basePath, "https://localhost:8447/aai/");
135         MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
136         mvm.add("depth", "0");
137         Formatter formatter = ff.get(Format.resource, mvm);
138
139         JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
140         System.out.println(json);
141
142     }
143
144     public void createLoaderEngineSetup() {
145
146         if (loader == null) {
147             loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
148             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
149
150             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
151
152             when(dbEngine.tx()).thenReturn(graph);
153             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
154
155             when(spyAdmin.getReadOnlyTraversalSource())
156                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
157             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
158         }
159     }
160 }