Update aai-common to 1.14.0 in graphadmin
[aai/graphadmin.git] / src / test / java / org / onap / aai / migration / v13 / MigrateVnfcModelInvariantIdTest.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 MigrateVnfcModelInvariantIdTest 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 MigrateVnfcModelInvariantId 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 vnfc1 = g.addV().property("aai-node-type", "vnfc").property("model-invariant-id", "vnfc-invariant-id-1")
66                 .property("vnfcName", "vnfc-name-1").property("nfcNamingCode", "naming-code-1")
67                 .property("nfcFunction", "function-1")
68                 .property("model-version-id", "vnfc-variant-id-1").next();
69
70         Vertex vnfc2 = g.addV().property("aai-node-type", "vnfc").property("model-invariant-id-local", "vnfc-invariant-id-2")
71                 .property("vnfcName", "vnfc-name-2").property("nfcNamingCode", "naming-code-2")
72                 .property("nfcFunction", "function-2")
73                 .property("model-version-id-local", "vnfc-version-id-2").next();
74
75                 TransactionalGraphEngine spy = spy(dbEngine);
76                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
77
78                 GraphTraversalSource traversal = g;
79                 GraphTraversalSource readOnly = graph.traversal().withStrategies(ReadOnlyStrategy.instance());
80                 when (spy.tx()).thenReturn(tx);
81                 when(spy.asAdmin()).thenReturn(adminSpy);
82                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
83                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
84
85                 migration = new MigrateVnfcModelInvariantId(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions);
86                 migration.run();
87         }
88
89         @After
90         public void cleanUp() {
91                 tx.tx().rollback();
92                 graph.close();
93         }
94
95         @Test
96     public void testIdsUpdated() throws Exception {
97         assertEquals(true,
98                 g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-1").has("model-invariant-id-local").next().property("model-invariant-id-local").isPresent());
99         assertEquals("model-invariant-id renamed to model-invariant-id-local for vnfc", "vnfc-invariant-id-1",
100                 g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-1").next().value("model-invariant-id-local").toString());
101     }
102
103     @Test
104     public void testIdsNotUpdated() throws Exception {
105             assertEquals("model-invariant-id-local should not be renamed for vnfc", "vnfc-invariant-id-2",
106                 g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-2").next().value("model-invariant-id-local").toString());
107     }
108 }