Fix logic in Schema Generator to properly
[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 package org.onap.aai.dbmap;
21
22 import org.janusgraph.core.JanusGraph;
23 import org.janusgraph.core.JanusGraphFactory;
24 import org.janusgraph.core.schema.JanusGraphIndex;
25 import org.janusgraph.core.schema.JanusGraphManagement;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.config.SpringContextAware;
31 import org.onap.aai.introspection.Introspector;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.introspection.LoaderFactory;
34 import org.onap.aai.introspection.ModelType;
35 import org.onap.aai.schema.enums.PropertyMetadata;
36 import org.onap.aai.setup.SchemaVersions;
37 import org.onap.aai.util.AAIConstants;
38
39 import java.io.FileNotFoundException;
40 import java.util.HashSet;
41 import java.util.Map;
42 import java.util.Optional;
43 import java.util.Set;
44
45 import static org.hamcrest.CoreMatchers.containsString;
46 import static org.hamcrest.CoreMatchers.is;
47 import static org.hamcrest.Matchers.matchesPattern;
48 import static org.junit.Assert.*;
49
50 public class AAIGraphTest extends AAISetup{
51         @Before
52         public void setup() {
53                 AAIGraph.getInstance();
54         }
55
56         @Test
57         public void getRealtimeInstanceConnectionName() throws Exception {
58
59                 JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph().openManagement();
60                 String connectionInstanceName = graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
61                 assertThat(connectionInstanceName, containsString(SERVICE_NAME));
62                 assertThat(connectionInstanceName, containsString("realtime"));
63                 assertThat(connectionInstanceName, matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_realtime_\\d+\\(current\\)$"));
64                 graphMgt.rollback();
65         }
66
67         @Test
68         public void getCachedInstanceConnectionName() throws Exception {
69
70                 JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph(DBConnectionType.CACHED).openManagement();
71                 String connectionInstanceName = graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
72                 assertThat(connectionInstanceName, containsString(SERVICE_NAME));
73                 assertThat(connectionInstanceName, containsString("cached"));
74                 assertThat(connectionInstanceName, matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_cached_\\d+\\(current\\)$"));
75                 graphMgt.rollback();
76         }
77
78         @Test
79         public void JanusGraphOpenNameTest() throws Exception{
80                 JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(SERVICE_NAME).withGraphType("graphType").buildConfiguration());
81                 JanusGraphManagement graphMgt = graph.openManagement();
82                 String connectionInstanceName = graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
83                 assertThat(connectionInstanceName,matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_graphType_\\d+\\(current\\)$"));
84                 graphMgt.rollback();
85                 graph.close();
86         }
87
88         @Test (expected=FileNotFoundException.class)
89         public void JanusGraphOpenNameWithInvalidFilePathTest() throws Exception{
90                 JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder("invalid").forService(SERVICE_NAME).withGraphType("graphType").buildConfiguration());
91                 JanusGraphManagement graphMgt = graph.openManagement();
92                 String connectionInstanceName = graphMgt.getOpenInstances().stream().filter(c -> c.contains("current")).findFirst().get();
93                 assertThat(connectionInstanceName,matchesPattern("^\\d+_[\\w\\-\\d]+_" + SERVICE_NAME + "_graphType_\\d+\\(current\\)$"));
94                 graphMgt.rollback();
95                 graph.close();
96         }
97
98         @Ignore("Need to create schema specific to the test")
99     @Test
100     public void checkIndexOfAliasedIndexedProps() throws Exception {
101         Set<String> aliasedIndexedProps = getAliasedIndexedProps();
102         JanusGraphManagement graphMgt = AAIGraph.getInstance().getGraph().openManagement();
103         for (String aliasedIndexedProp : aliasedIndexedProps) {
104             JanusGraphIndex index = graphMgt.getGraphIndex(aliasedIndexedProp);
105             assertNotNull(aliasedIndexedProp + " index exists", index);
106             assertEquals(aliasedIndexedProp + " index has 1 property keys", index.getFieldKeys().length, 1);
107             assertThat(aliasedIndexedProp + " index indexes " + aliasedIndexedProp + " property key", 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 }