Change openecomp to onap and update license
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats;
23
24 import com.google.gson.JsonObject;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
27 import org.apache.tinkerpop.gremlin.structure.Graph;
28 import org.apache.tinkerpop.gremlin.structure.T;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
31 import org.junit.Before;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aai.dbmap.DBConnectionType;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.LoaderFactory;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.introspection.Version;
42 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
43 import org.onap.aai.serialization.db.DBSerializer;
44 import org.onap.aai.serialization.engines.QueryStyle;
45 import org.onap.aai.serialization.engines.TitanDBEngine;
46 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
47 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
48 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
49
50 import java.io.UnsupportedEncodingException;
51
52 import static org.junit.Assert.*;
53 import static org.mockito.Matchers.anyBoolean;
54 import static org.mockito.Matchers.anyInt;
55 import static org.mockito.Matchers.anyObject;
56 import static org.mockito.Matchers.anyString;
57 import static org.mockito.Mockito.*;
58
59 public class SimpleFormatTest {
60
61         @Mock
62         private UrlBuilder urlBuilder;
63
64         private Graph graph;
65         private TransactionalGraphEngine dbEngine;
66         private Loader loader;
67         private DBSerializer serializer;
68         private RawFormat simpleFormat;
69         private Vertex vfModule;
70         private final ModelType factoryType = ModelType.MOXY;
71
72         @Before
73         public void setUp() throws Exception {
74
75                 MockitoAnnotations.initMocks(this);
76
77                 graph = TinkerGraph.open();
78
79                 vfModule = graph.addVertex(
80                                 T.label, "vf-module",
81                                 T.id, "5",
82                                 "aai-node-type", "vf-module",
83                                 "vf-module-id", "vf-module-id-val-68205",
84                                 "vf-module-name", "example-vf-module-name-val-68205",
85                                 "heat-stack-id", "example-heat-stack-id-val-68205",
86                                 "orchestration-status", "example-orchestration-status-val-68205",
87                                 "is-base-vf-module", "true",
88                                 "resource-version", "1498166571906",
89                                 "model-invariant-id", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
90                                 "model-invariant-id-local", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
91                                 "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
92                                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
93                                 "widget-model-id", "example-widget-model-id-val-68205",
94                                 "widget-model-version", "example-widget--model-version-val-68205",
95                                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205"
96                 );
97         }
98
99         @Test
100         public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
101
102             createLoaderEngineSetup();
103                 serializer = new DBSerializer(Version.v10, dbEngine, factoryType, "Junit");
104                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
105
106                 assertNotNull(dbEngine.tx());
107                 assertNotNull(dbEngine.asAdmin());
108
109                 JsonObject json = simpleFormat.createPropertiesObject(vfModule);
110
111                 assertTrue(json.has("model-invariant-id"));
112                 assertTrue(json.has("model-version-id"));
113
114                 assertFalse(json.has("model-invariant-id-local"));
115                 assertFalse(json.has("model-version-id-local"));
116
117         }
118
119         @Ignore
120         @Test(expected = AAIFormatVertexException.class)
121         public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
122
123                 serializer = mock(DBSerializer.class);
124                 loader = mock(Loader.class);
125
126                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
127
128                 when(serializer.dbToObject(anyObject(), anyObject(), anyInt(), anyBoolean(), anyString()))
129                         .thenThrow(new AAIException("Test Exception"));
130
131                 simpleFormat.createPropertiesObject(vfModule);
132         }
133
134         @Ignore
135         @Test(expected = AAIFormatVertexException.class)
136         public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
137
138                 loader = mock(Loader.class);
139                 serializer = mock(DBSerializer.class);
140
141                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
142
143                 when(loader.introspectorFromName(anyString()))
144                                 .thenThrow(new AAIUnknownObjectException("Test Exception"));
145
146                 simpleFormat.createPropertiesObject(vfModule);
147         }
148
149         public void createLoaderEngineSetup(){
150
151                 if(loader == null){
152                         loader = LoaderFactory.createLoaderForVersion(factoryType, Version.v10);
153                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
154
155                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
156
157                         when(dbEngine.tx()).thenReturn(graph);
158                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
159
160                         when(spyAdmin.getReadOnlyTraversalSource()).thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
161                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
162                 }
163         }
164 }