added generic fabric support to SO
[so.git] / adapters / mso-openstack-adapters / src / test / java / db / migration / CloudConfigMigrationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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 db.migration;
22
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.so.adapters.vnf.BaseRestTestUtils;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Qualifier;
29
30 import javax.sql.DataSource;
31 import java.sql.Connection;
32 import java.sql.ResultSet;
33 import java.sql.Statement;
34
35 public class CloudConfigMigrationTest extends BaseRestTestUtils {
36
37     @Qualifier("dataSource")
38     @Autowired
39     DataSource dataSource;
40
41     R__CloudConfigMigration cloudConfigMigration;
42
43     @Before
44     public void setup() {
45         cloudConfigMigration = new R__CloudConfigMigration();
46     }
47
48     @Test
49     public void testMigrate() throws Exception {
50         System.setProperty("spring.profiles.active", "test");
51         cloudConfigMigration.migrate(dataSource.getConnection());
52         assertMigratedIdentityServiceData();
53         assertMigratedCloudSiteData();
54         assertMigratedCloudManagerData();
55     }
56
57     @Test
58     public void testMigrateNoData() throws Exception {
59         System.setProperty("spring.profiles.active", "nomigrate");
60         int identityCount = getDataCount("identity_services");
61         int cloudSiteCount = getDataCount("cloud_sites");
62         int cloudManagerCount = getDataCount("cloudify_managers");
63
64         cloudConfigMigration.migrate(dataSource.getConnection());
65
66         Assert.assertEquals(identityCount, getDataCount("identity_services"));
67         Assert.assertEquals(cloudSiteCount, getDataCount("cloud_sites"));
68         Assert.assertEquals(cloudManagerCount, getDataCount("cloudify_managers"));
69     }
70
71
72     private int getDataCount(String tableName) throws Exception {
73         try (Connection con = dataSource.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select count(1) from " + tableName)) {
74             while (rs.next()) {
75                 return rs.getInt(1);
76             }
77         }
78         return 0;
79     }
80
81     private void assertMigratedIdentityServiceData() throws Exception {
82         try (Connection con = dataSource.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from identity_services where id='MTKEYSTONE'")) {
83             boolean dataAvailable = false;
84             while (rs.next()) {
85                 dataAvailable = true;
86                 Assert.assertEquals("MTKEYSTONE", rs.getString("id"));
87                 Assert.assertEquals("http://localhost:5000/v2.0", rs.getString("identity_url"));
88                 Assert.assertEquals("john", rs.getString("mso_id"));
89                 Assert.assertEquals("313DECE408AF7759D442D7B06DD9A6AA", rs.getString("mso_pass"));
90                 Assert.assertEquals("admin", rs.getString("admin_tenant"));
91                 Assert.assertEquals("_member_", rs.getString("member_role"));
92                 Assert.assertEquals("KEYSTONE", rs.getString("identity_server_type"));
93                 Assert.assertEquals("USERNAME_PASSWORD", rs.getString("identity_authentication_type"));
94             }
95             Assert.assertTrue("Expected data in identity_services table post migration but didnt find any!!!", dataAvailable);
96         }
97     }
98
99     private void assertMigratedCloudSiteData() throws Exception {
100         try (Connection con = dataSource.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from cloud_sites where id='regionOne'")) {
101             boolean dataAvailable = false;
102             while (rs.next()) {
103                 dataAvailable = true;
104                 Assert.assertEquals("regionOne", rs.getString("id"));
105                 Assert.assertEquals("regionOne", rs.getString("region_id"));
106                 Assert.assertEquals("MT2", rs.getString("clli"));
107                 Assert.assertEquals("2.5", rs.getString("cloud_version"));
108                 Assert.assertEquals("MTKEYSTONE", rs.getString("identity_service_id"));
109             }
110             Assert.assertTrue("Expected data in identity_services table post migration but didnt find any!!!", dataAvailable);
111         }
112     }
113
114     private void assertMigratedCloudManagerData() throws Exception {
115         try (Connection con = dataSource.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from cloudify_managers where id='manager'")) {
116             boolean dataAvailable = false;
117             while (rs.next()) {
118                 dataAvailable = true;
119                 Assert.assertEquals("http://localhost:8080", rs.getString("cloudify_url"));
120                 Assert.assertEquals("user", rs.getString("username"));
121                 Assert.assertEquals("password", rs.getString("password"));
122                 Assert.assertEquals("2.0", rs.getString("version"));
123             }
124             Assert.assertTrue("Expected data in identity_services table post migration but didnt find any!!!", dataAvailable);
125         }
126     }
127 }