Update spring boot to 2.4
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / SimpleFormatTest.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.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyBoolean;
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.when;
33
34 import com.google.gson.JsonObject;
35
36 import java.io.UnsupportedEncodingException;
37 import java.util.Arrays;
38
39 import javax.ws.rs.core.MultivaluedHashMap;
40 import javax.ws.rs.core.MultivaluedMap;
41
42 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
43 import org.apache.tinkerpop.gremlin.structure.Graph;
44 import org.apache.tinkerpop.gremlin.structure.T;
45 import org.apache.tinkerpop.gremlin.structure.Vertex;
46 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
47 import org.junit.Before;
48 import org.junit.Ignore;
49 import org.junit.Test;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.onap.aai.AAISetup;
53 import org.onap.aai.exceptions.AAIException;
54 import org.onap.aai.introspection.Loader;
55 import org.onap.aai.introspection.ModelType;
56 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
57 import org.onap.aai.serialization.db.DBSerializer;
58 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
59 import org.onap.aai.serialization.engines.QueryStyle;
60 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
61 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
62 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
63 import org.springframework.test.annotation.DirtiesContext;
64
65 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
66 public class SimpleFormatTest extends AAISetup {
67
68     @Mock
69     private UrlBuilder urlBuilder;
70
71     private Graph graph;
72     private TransactionalGraphEngine dbEngine;
73     private Loader loader;
74     private DBSerializer serializer;
75     private RawFormat simpleFormat;
76     private Vertex vfModule;
77     private Vertex unknown;
78     private final ModelType factoryType = ModelType.MOXY;
79
80     @Before
81     public void setUp() throws Exception {
82
83         MockitoAnnotations.openMocks(this);
84
85         graph = TinkerGraph.open();
86
87         vfModule = graph.addVertex(T.label, "vf-module", T.id, "5", "aai-node-type", "vf-module", "vf-module-id",
88                 "vf-module-id-val-68205", "vf-module-name", "example-vf-module-name-val-68205", "heat-stack-id",
89                 "example-heat-stack-id-val-68205", "orchestration-status", "example-orchestration-status-val-68205",
90                 "is-base-vf-module", "true", "resource-version", "1498166571906", "model-invariant-id",
91                 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-invariant-id-local",
92                 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
93                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c", "widget-model-id",
94                 "example-widget-model-id-val-68205", "widget-model-version", "example-widget--model-version-val-68205",
95                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
96
97         unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
98                 "vserver-id-1", "vserver-name", "vserver-name-1");
99     }
100
101     @Test
102     public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
103
104         createLoaderEngineSetup();
105         serializer = new DBSerializer(schemaVersions.getRelatedLinkVersion(), dbEngine, factoryType, "Junit");
106         simpleFormat =
107                 new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
108
109         assertNotNull(dbEngine.tx());
110         assertNotNull(dbEngine.asAdmin());
111
112         JsonObject json = simpleFormat.createPropertiesObject(vfModule).get();
113
114         assertTrue(json.has("model-invariant-id"));
115         assertTrue(json.has("model-version-id"));
116
117         assertFalse(json.has("model-invariant-id-local"));
118         assertFalse(json.has("model-version-id-local"));
119
120     }
121
122     @Test
123     public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
124
125         createLoaderEngineSetup();
126         serializer = new DBSerializer(schemaVersions.getRelatedLinkVersion(), dbEngine, factoryType, "Junit");
127         simpleFormat =
128                 new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
129
130         assertNotNull(dbEngine.tx());
131         assertNotNull(dbEngine.asAdmin());
132
133         assertFalse(simpleFormat.getJsonFromVertex(unknown).isPresent());
134
135     }
136
137     @Test
138     public void testFormattingUnknownVertex() throws AAIException {
139
140         createLoaderEngineSetup();
141         serializer = new DBSerializer(schemaVersions.getRelatedLinkVersion(), dbEngine, factoryType, "Junit");
142
143         FormatFactory ff =
144                 new FormatFactory(loader, serializer, schemaVersions, basePath, "https://localhost:8447/aai/");
145         MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
146         mvm.add("depth", "0");
147         Formatter formatter = ff.get(Format.simple, mvm);
148
149         JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
150
151     }
152
153     @Ignore
154     @Test(expected = AAIFormatVertexException.class)
155     public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails()
156             throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
157
158         serializer = mock(DBSerializer.class);
159         loader = mock(Loader.class);
160
161         simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
162
163         when(serializer.dbToObject(any(), any(), anyInt(), anyBoolean(), anyString()))
164                 .thenThrow(new AAIException("Test Exception"));
165
166         simpleFormat.createPropertiesObject(vfModule);
167     }
168
169     @Ignore
170     @Test(expected = AAIFormatVertexException.class)
171     public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject()
172             throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
173
174         loader = mock(Loader.class);
175         serializer = mock(DBSerializer.class);
176
177         simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
178
179         when(loader.introspectorFromName(anyString())).thenThrow(new AAIUnknownObjectException("Test Exception"));
180
181         simpleFormat.createPropertiesObject(vfModule);
182     }
183
184     public void createLoaderEngineSetup() {
185
186         if (loader == null) {
187             loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getRelatedLinkVersion());
188             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
189
190             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
191
192             when(dbEngine.tx()).thenReturn(graph);
193             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
194
195             when(spyAdmin.getReadOnlyTraversalSource())
196                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
197             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
198         }
199     }
200 }