2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.dbmap;
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.*;
28 import java.io.FileNotFoundException;
29 import java.util.HashSet;
31 import java.util.Optional;
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;
51 public class AAIGraphTest extends AAISetup {
54 AAIGraph.getInstance();
58 public void getRealtimeInstanceConnectionName() throws Exception {
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\\)$"));
71 public void getCachedInstanceConnectionName() throws Exception {
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\\)$"));
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\\)$"));
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\\)$"));
109 @Ignore("Need to create schema specific to the 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));
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);
141 return aliasedIndexedProps;