13eed2d5306c71c0cbb781c63e7baaccce1df902
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / test / CamundaDBSetup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 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 org.openecomp.mso.bpmn.test;
22
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.PreparedStatement;
26 import java.sql.SQLException;
27
28 /**
29  * Sets up the unit test (H2) database for Camunda.
30  */
31 public class CamundaDBSetup {
32         private static boolean isDBConfigured = false;
33
34         public static synchronized void configure() throws SQLException {
35                 if (isDBConfigured) {
36                         return;
37                 }
38
39                 System.out.println("Configuring the Camunda H2 database for MSO");
40
41                 Connection connection = null;
42                 PreparedStatement stmt = null;
43
44                 try {
45                         connection = DriverManager.getConnection(
46                                 "jdbc:h2:mem:camunda;DB_CLOSE_DELAY=-1", "sa", "");
47
48                         stmt = connection.prepareStatement("delete from ACT_HI_VARINST");
49                         stmt.executeUpdate();
50                         stmt.close();
51                         stmt = null;
52
53                         stmt = connection.prepareStatement("ALTER TABLE ACT_HI_VARINST alter column TEXT_ clob");
54                         stmt.executeUpdate();
55                         stmt.close();
56                         stmt = null;
57                         
58                         stmt = connection.prepareStatement("ALTER TABLE ACT_HI_VARINST alter column NAME_ clob");
59                         stmt.executeUpdate();
60                         stmt.close();
61                         stmt = null;
62
63                         stmt = connection.prepareStatement("delete from ACT_HI_DETAIL");
64                         stmt.executeUpdate();
65                         stmt.close();
66                         stmt = null;
67
68                         stmt = connection.prepareStatement("ALTER TABLE ACT_HI_DETAIL alter column TEXT_ clob");
69                         stmt.executeUpdate();
70                         stmt.close();
71                         stmt = null;
72
73                         stmt = connection.prepareStatement("ALTER TABLE ACT_HI_DETAIL alter column NAME_ clob");
74                         stmt.executeUpdate();
75                         stmt.close();
76                         stmt = null;
77
78                         stmt = connection.prepareStatement("ALTER TABLE ACT_RU_VARIABLE alter column TEXT_ clob");
79                         stmt.executeUpdate();
80                         stmt.close();
81                         stmt = null;
82
83                         connection.close();
84                         connection = null;
85
86                         isDBConfigured = true;
87                 } catch (SQLException e) {
88                         System.out.println("CamundaDBSetup caught " + e.getClass().getSimpleName());
89                         e.printStackTrace();
90                 } finally {
91                         if (stmt != null) {
92                                 try {
93                                         stmt.close();
94                                 } catch (Exception e) {
95                                         // Ignore
96                                 }
97                         }
98
99                         if (connection != null) {
100                                 try {
101                                         connection.close();
102                                 } catch (Exception e) {
103                                         // Ignore
104                                 }
105                         }
106                 }
107         }
108 }