48d2583d0d5159ebc2ab6af3e247c0b5a3f84ed6
[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() throws Exception {
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 getCachedInstanceConnectionName() throws Exception {
72
73         JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph(DBConnectionType.CACHED).openManagement();
74         String connectionInstanceName =
75                 graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
76         assertThat(connectionInstanceName, containsString(SERVICE_NAME));
77         assertThat(connectionInstanceName, containsString("cached"));
78         assertThat(connectionInstanceName,
79                 matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_cached_\\d+\\(current\\)$"));
80         graphMgt.rollback();
81     }
82
83     @Test
84     public void JanusGraphOpenNameTest() throws Exception {
85         JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG)
86                 .forService(SERVICE_NAME).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     @Test(expected = FileNotFoundException.class)
97     public void JanusGraphOpenNameWithInvalidFilePathTest() throws Exception {
98         JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder("invalid").forService(SERVICE_NAME)
99                 .withGraphType("graphType").buildConfiguration());
100         JanusGraphManagement graphMgt = graph.openManagement();
101         String connectionInstanceName =
102                 graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
103         assertThat(connectionInstanceName,
104                 matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_graphType_\\d+\\(current\\)$"));
105         graphMgt.rollback();
106         graph.close();
107     }
108
109     @Ignore("Need to create schema specific to the test")
110     @Test
111     public void checkIndexOfAliasedIndexedProps() throws Exception {
112         Set<String> aliasedIndexedProps = getAliasedIndexedProps();
113         JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph().openManagement();
114         for (String aliasedIndexedProp : aliasedIndexedProps) {
115             JanusGraphIndex index = graphMgt.getGraphIndex(aliasedIndexedProp);
116             assertNotNull(aliasedIndexedProp + " index exists", index);
117             assertEquals(aliasedIndexedProp + " index has 1 property keys", index.getFieldKeys().length, 1);
118             assertThat(aliasedIndexedProp + " index indexes " + aliasedIndexedProp + " property key",
119                     index.getFieldKeys()[0].name(), is(aliasedIndexedProp));
120         }
121         graphMgt.rollback();
122     }
123
124     private Set<String> getAliasedIndexedProps() {
125         Set<String> aliasedIndexedProps = new HashSet<>();
126         LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class);
127         SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
128         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
129         Map<String, Introspector> objs = loader.getAllObjects();
130         for (Introspector obj : objs.values()) {
131             for (String propName : obj.getProperties()) {
132                 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
133                 if (alias.isPresent()) {
134                     String dbPropName = alias.get();
135                     if (obj.getIndexedProperties().contains(propName)) {
136                         aliasedIndexedProps.add(dbPropName);
137                     }
138                 }
139             }
140         }
141         return aliasedIndexedProps;
142     }
143
144 }