05cb34674d1550c685de96df3c73a8663f029f63
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / dbmap / AAIGraphTest.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.dbmap;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.Matchers.matchesPattern;
26 import static org.junit.Assert.*;
27
28 import java.io.FileNotFoundException;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33
34 import org.janusgraph.core.JanusGraph;
35 import org.janusgraph.core.JanusGraphFactory;
36 import org.janusgraph.core.schema.JanusGraphIndex;
37 import org.janusgraph.core.schema.JanusGraphManagement;
38 import org.junit.Before;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.config.SpringContextAware;
43 import org.onap.aai.introspection.Introspector;
44 import org.onap.aai.introspection.Loader;
45 import org.onap.aai.introspection.LoaderFactory;
46 import org.onap.aai.introspection.ModelType;
47 import org.onap.aai.schema.enums.PropertyMetadata;
48 import org.onap.aai.setup.SchemaVersions;
49 import org.onap.aai.util.AAIConstants;
50
51 public class AAIGraphTest extends AAISetup {
52     @Before
53     public void setup() {
54         AAIGraph.getInstance();
55     }
56
57     @Test
58     public void getRealtimeInstanceConnectionName() {
59
60         JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph().openManagement();
61         String connectionInstanceName =
62                 graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
63         assertThat(connectionInstanceName, containsString(SERVICE_NAME));
64         assertThat(connectionInstanceName, containsString("realtime"));
65         assertThat(connectionInstanceName,
66                 matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_realtime_\\d+\\(current\\)$"));
67         graphMgt.rollback();
68     }
69
70     @Test
71     public void janusGraphOpenNameTest() throws Exception {
72         JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG)
73                 .forService(SERVICE_NAME).withGraphType("graphType").buildConfiguration());
74         JanusGraphManagement graphMgt = graph.openManagement();
75         String connectionInstanceName =
76                 graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
77         assertThat(connectionInstanceName,
78                 matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_graphType_\\d+\\(current\\)$"));
79         graphMgt.rollback();
80         graph.close();
81     }
82
83     @Test(expected = FileNotFoundException.class)
84     public void janusGraphOpenNameWithInvalidFilePathTest() throws Exception {
85         JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder("invalid").forService(SERVICE_NAME)
86                 .withGraphType("graphType").buildConfiguration());
87         JanusGraphManagement graphMgt = graph.openManagement();
88         String connectionInstanceName =
89                 graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
90         assertThat(connectionInstanceName,
91                 matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_graphType_\\d+\\(current\\)$"));
92         graphMgt.rollback();
93         graph.close();
94     }
95
96     @Ignore("Need to create schema specific to the test")
97     @Test
98     public void checkIndexOfAliasedIndexedProps() {
99         Set<String> aliasedIndexedProps = getAliasedIndexedProps();
100         JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph().openManagement();
101         for (String aliasedIndexedProp : aliasedIndexedProps) {
102             JanusGraphIndex index = graphMgt.getGraphIndex(aliasedIndexedProp);
103             assertNotNull(aliasedIndexedProp + " index exists", index);
104             assertEquals(aliasedIndexedProp + " index has 1 property keys", index.getFieldKeys().length, 1);
105             assertThat(aliasedIndexedProp + " index indexes " + aliasedIndexedProp + " property key",
106                     index.getFieldKeys()[0].name(), is(aliasedIndexedProp));
107         }
108         graphMgt.rollback();
109     }
110
111     private Set<String> getAliasedIndexedProps() {
112         Set<String> aliasedIndexedProps = new HashSet<>();
113         LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class);
114         SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
115         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
116         Map<String, Introspector> objs = loader.getAllObjects();
117         for (Introspector obj : objs.values()) {
118             for (String propName : obj.getProperties()) {
119                 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
120                 if (alias.isPresent()) {
121                     String dbPropName = alias.get();
122                     if (obj.getIndexedProperties().contains(propName)) {
123                         aliasedIndexedProps.add(dbPropName);
124                     }
125                 }
126             }
127         }
128         return aliasedIndexedProps;
129     }
130
131 }