Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / util / H2DbServerUtil.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.aai.esr.db.util;
17
18 import org.apache.tools.ant.Project;
19 import org.apache.tools.ant.taskdefs.SQLExec;
20 import org.apache.tools.ant.types.EnumeratedAttribute;
21
22 import java.io.File;
23 import java.net.URISyntaxException;
24
25
26 public class H2DbServerUtil {
27   private static String resourcePath;
28
29   /**
30    * init db table.
31    */
32   public static void initTable() {
33     init();
34     SQLExec sqlExec = new SQLExec();
35     // set db connetc parameter
36     sqlExec.setDriver("org.h2.Driver");
37     sqlExec.setUrl("jdbc:h2:tcp://localhost:18209/" + resourcePath + "db/extsys");
38     sqlExec.setUserid("extsys");
39     sqlExec.setPassword("extsys");
40     // execute sql
41     sqlExec.setSrc(new File(resourcePath + "sql/extsys-resource-createObj-mysql.sql"));
42     sqlExec.setOnerror(
43         (SQLExec.OnError) (EnumeratedAttribute.getInstance(SQLExec.OnError.class, "abort")));
44     sqlExec.setPrint(true); // set print
45     sqlExec.setProject(new Project());
46     sqlExec.execute();
47   }
48
49   private static void init() {
50     try {
51       resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
52     } catch (URISyntaxException error) {
53       error.printStackTrace();
54     }
55   }
56
57   /**
58    * init db.
59    */
60   public static void main(String [] args) {
61     H2DbServer.startUp();
62     H2DbServerUtil.initTable();
63     H2DbServer.shutDown();
64   }
65
66 }