Update aai-common to 1.14.0 in graphadmin
[aai/graphadmin.git] / src / test / java / org / onap / aai / migration / v13 / MigrateInstanceGroupModelInvariantIdTest.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.v13;
21
22 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraph;
26 import org.janusgraph.core.JanusGraphFactory;
27 import org.janusgraph.core.JanusGraphTransaction;
28 import org.junit.*;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.dbmap.DBConnectionType;
31 import org.onap.aai.introspection.Loader;
32 import org.onap.aai.introspection.ModelType;
33 import org.onap.aai.setup.SchemaVersions;
34 import org.onap.aai.setup.SchemaVersion;
35 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
36 import org.onap.aai.serialization.engines.QueryStyle;
37 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Mockito.spy;
41 import static org.mockito.Mockito.when;
42
43 public class MigrateInstanceGroupModelInvariantIdTest extends AAISetup{
44
45         private final static ModelType introspectorFactoryType = ModelType.MOXY;
46         private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
47
48         private Loader loader;
49         private TransactionalGraphEngine dbEngine;
50         private JanusGraph graph;
51         private MigrateInstanceGroupModelInvariantId migration;
52         private JanusGraphTransaction tx;
53         private GraphTraversalSource g;
54
55         @Before
56         public void setUp() throws Exception {
57                 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
58                 tx = graph.newTransaction();
59                 g = tx.traversal();
60                 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
61                 dbEngine = new JanusGraphDBEngine(
62                                 queryStyle,
63                                 loader);
64
65                 Vertex instancegroup1 = g.addV().property("aai-node-type", "instance-group").property("id", "instance-id-1")
66                 .property("description","instance-description-1").property("instanceGroupType","instance-type-1")
67                 .property("model-invariant-id", "instance-invariant-id-1").next();
68
69         Vertex instancegroup2 = g.addV().property("aai-node-type", "instance-group").property("id", "instance-id-2")
70                 .property("description","instance-description-2").property("instanceGroupType","instance-type-1")
71                 .property("model-invariant-id-local", "instance-invariant-id-2").next();
72
73                 TransactionalGraphEngine spy = spy(dbEngine);
74                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
75
76                 GraphTraversalSource traversal = g;
77                 GraphTraversalSource readOnly = graph.traversal().withStrategies(ReadOnlyStrategy.instance());
78                 when (spy.tx()).thenReturn(tx);
79                 when(spy.asAdmin()).thenReturn(adminSpy);
80                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
81                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
82
83                 migration = new MigrateInstanceGroupModelInvariantId(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions);
84                 migration.run();
85         }
86
87         @After
88         public void cleanUp() {
89                 tx.tx().rollback();
90                 graph.close();
91         }
92
93         @Test
94     public void testIdsUpdated() throws Exception {
95         assertEquals(true,
96                 g.V().has("aai-node-type", "instance-group").has("id", "instance-id-1").has("model-invariant-id-local").next().property("model-invariant-id-local").isPresent());
97             assertEquals("model-invariant-id renamed to model-invariant-id-local for instance-group", "instance-invariant-id-1",
98                 g.V().has("aai-node-type", "instance-group").has("id", "instance-id-1").next().value("model-invariant-id-local").toString());
99     }
100
101     @Test
102     public void testIdsNotUpdated() throws Exception {
103         assertEquals("model-invariant-id-local remains the same for instance-group", "instance-invariant-id-2",
104                 g.V().has("aai-node-type", "instance-group").has("id", "instance-id-2").next().value("model-invariant-id-local").toString());
105     }
106 }