EdgeRules throws descriptive error on invalid rule
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / serialization / db / DbAliasTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.serialization.db;
22
23 import com.thinkaurelius.titan.core.TitanFactory;
24 import com.thinkaurelius.titan.core.TitanGraph;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.openecomp.aai.AAISetup;
32 import org.openecomp.aai.dbmap.DBConnectionType;
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.*;
35 import org.openecomp.aai.parsers.query.QueryParser;
36 import org.openecomp.aai.schema.enums.PropertyMetadata;
37 import org.openecomp.aai.serialization.engines.QueryStyle;
38 import org.openecomp.aai.serialization.engines.TitanDBEngine;
39 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
40
41 import java.io.UnsupportedEncodingException;
42 import java.lang.reflect.InvocationTargetException;
43 import java.net.MalformedURLException;
44 import java.net.URI;
45 import java.net.URISyntaxException;
46 import java.util.Collections;
47 import java.util.Map;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.mockito.Mockito.spy;
51 import static org.mockito.Mockito.when;
52
53 public class DbAliasTest extends AAISetup {
54
55         private TitanGraph graph;
56
57         private final Version version = Version.v9;
58         private final ModelType introspectorFactoryType = ModelType.MOXY;
59         private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
60         private final DBConnectionType type = DBConnectionType.REALTIME;
61         private Loader loader;
62         private TransactionalGraphEngine dbEngine;
63
64         @Before
65         public void setup() throws Exception {
66                 graph = TitanFactory.build().set("storage.backend","inmemory").open();
67                 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
68                 dbEngine = new TitanDBEngine(
69                                 queryStyle,
70                                 type,
71                                 loader);
72         }
73
74         @After
75         public void tearDown() {
76                 graph.tx().rollback();
77                 graph.close();
78         }
79
80         @Test
81         public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
82                 final String property = "persona-model-customization-id";
83                 String dbPropertyName = property;
84                 TransactionalGraphEngine spy = spy(this.dbEngine);
85                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
86                 Graph g = graph.newTransaction();
87                 GraphTraversalSource traversal = g.traversal();
88                 when(spy.asAdmin()).thenReturn(adminSpy);
89                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
90                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
91                 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
92                 Introspector obj = loader.introspectorFromName("generic-vnf");
93                 Vertex v = g.addVertex();
94                 Object id = v.id();
95                 obj.setValue("vnf-id", "key1");
96                 obj.setValue(property, "hello");
97                 serializer.serializeToDb(obj, v, uriQuery, "", "");
98                 g.tx().commit();
99                 v = graph.traversal().V(id).next();
100                 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
101                 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
102                         dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
103                 }
104
105                 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
106                 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
107                 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
108
109         }
110
111         @Test
112         public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
113                 final String property = "persona-model-customization-id";
114
115                 TransactionalGraphEngine spy = spy(dbEngine);
116                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
117                 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
118                 graph.tx().commit();
119                 Graph g = graph.newTransaction();
120                 GraphTraversalSource traversal = g.traversal();
121                 when(spy.asAdmin()).thenReturn(adminSpy);
122                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
123                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
124                 Introspector obj = loader.introspectorFromName("generic-vnf");
125                 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
126
127                 assertEquals("dbAlias property exists", "hello", obj.getValue(property));
128
129         }
130
131
132 }