Update aai-common to 1.14.0 in graphadmin
[aai/graphadmin.git] / src / test / java / org / onap / aai / migration / v12 / MigrateInvEvcInventoryTest.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.migration.v12;
21
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Optional;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.junit.*;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.dbmap.DBConnectionType;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.ModelType;
38 import org.onap.aai.setup.SchemaVersions;
39 import org.onap.aai.setup.SchemaVersion;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
42 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
43
44 import org.janusgraph.core.JanusGraphFactory;
45 import org.janusgraph.core.JanusGraph;
46 import org.janusgraph.core.JanusGraphTransaction;
47
48 public class MigrateInvEvcInventoryTest extends AAISetup {
49
50         private final static ModelType introspectorFactoryType = ModelType.MOXY;
51         private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
52
53         private static Loader loader;
54         private static TransactionalGraphEngine dbEngine;
55         private static JanusGraph graph;
56         private static MigrateINVEvcInventory migration;
57         private static JanusGraphTransaction tx;
58         private static GraphTraversalSource g;
59
60         @Before
61         public void setUp() throws Exception {
62                 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
63                 tx = graph.newTransaction();
64                 g = tx.traversal();
65                 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
66                 dbEngine = new JanusGraphDBEngine(
67                                 queryStyle,
68                                 loader);
69
70                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources");
71                 
72                 Vertex evc = g.addV()
73                                 .property("aai-node-type", "evc")
74                                 .property("evc-id", "evc-name-1")
75                                 .next();
76                 
77                 Vertex evc2 = g.addV()
78                                 .property("aai-node-type", "evc")
79                                 .property("evc-id", "evc-name-2")
80                                 .next();
81                 
82                 TransactionalGraphEngine spy = spy(dbEngine);
83                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
84
85                 GraphTraversalSource traversal = g;
86                 GraphTraversalSource readOnly = graph.traversal().withStrategies(ReadOnlyStrategy.instance());
87                 when (spy.tx()).thenReturn(tx);
88                 when(spy.asAdmin()).thenReturn(adminSpy);
89                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
90                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
91                 
92                 migration = new MigrateINVEvcInventory(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions);
93                 migration.run();
94         }
95         
96         @After
97         public void cleanUp() {
98                 tx.tx().rollback();
99                 graph.close();
100         }
101         
102         @Test
103         public void testRun_updateEvcNode() throws Exception {
104                 // check if graph nodes exist
105                 assertEquals("evc node exists", true, 
106                                 g.V().has("aai-node-type", "evc")
107                                          .has("evc-id", "evc-name-1")
108                                 .hasNext());
109                 
110                 // check if evc object is updated to set the value for inter-connect-type-ingress
111                 assertEquals("evc is updated", true, 
112                                 g.V().has("aai-node-type", "evc").has("evc-id", "evc-name-1")
113                                 .has("inter-connect-type-ingress", "SHARED")
114                                 .hasNext());
115         }
116         
117         @Test
118         public void testRun_evcNotCreated() throws Exception {
119                 
120                 assertEquals("evc node does not exist", false, 
121                                 g.V().has("aai-node-type", "evc").has("evc-id", "evc-name-3")
122                                 .hasNext());
123                 
124                 //inter-connect-type-ingress is not present on the evc
125                 assertEquals("evc node exists", true, 
126                                 g.V().has("aai-node-type", "evc").has("evc-id", "evc-name-2")
127                                 .hasNext());
128                 assertEquals("evc node not updated with inter-connect-type-ingress", false, 
129                                 g.V().has("aai-node-type", "evc").has("evc-id", "evc-name-2").has("inter-connect-type-ingress")
130                                 .hasNext());
131                 
132         }
133
134         @Test
135         public void testGetAffectedNodeTypes() {
136                 Optional<String[]> types = migration.getAffectedNodeTypes();
137                 Optional<String[]> expected = Optional.of(new String[]{"evc"});
138                 
139                 assertNotNull(types);
140                 assertArrayEquals(expected.get(), types.get());
141         }
142
143         @Test
144         public void testGetMigrationName() {
145                 String migrationName = migration.getMigrationName();
146
147                 assertNotNull(migrationName);
148                 assertEquals("MigrateINVEvcInventory", migrationName);
149         }
150 }