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