Fix license headers
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / test / java / jtest / util / org / openecomp / sdnc / ra / TestTable.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package jtest.util.org.openecomp.sdnc.ra;
23
24 import org.springframework.jdbc.core.JdbcTemplate;
25 import org.springframework.jdbc.support.rowset.SqlRowSet;
26
27 public class TestTable {
28
29     private String tableName;
30     private String[] columnList;
31     private String idName;
32
33     private String insertSql;
34
35     private JdbcTemplate jdbcTemplate;
36
37     public TestTable(JdbcTemplate jdbcTemplate, String tableName, String idName, String... columnList) {
38         this.jdbcTemplate = jdbcTemplate;
39         this.tableName = tableName;
40         this.idName = idName;
41         this.columnList = columnList;
42         createInsertSql();
43     }
44
45     private void createInsertSql() {
46         StringBuilder ss = new StringBuilder();
47         ss.append("INSERT INTO ").append(tableName).append(" (");
48         for (String s : columnList)
49             ss.append(s).append(", ");
50         ss.setLength(ss.length() - 2);
51         ss.append(") VALUES (");
52         for (int i = 0; i < columnList.length; i++)
53             ss.append("?, ");
54         ss.setLength(ss.length() - 2);
55         ss.append(")");
56         insertSql = ss.toString();
57     }
58
59     public void add(Object... values) {
60         jdbcTemplate.update(insertSql, values);
61     }
62
63     public long getLastId() {
64         return jdbcTemplate.queryForObject("SELECT max(" + idName + ") FROM " + tableName, Long.class);
65     }
66
67     public Long getId(String where) {
68         String selectSql = "SELECT " + idName + " FROM " + tableName + " WHERE " + where;
69         SqlRowSet rs = jdbcTemplate.queryForRowSet(selectSql);
70         if (rs.first())
71             return rs.getLong(idName);
72         return null;
73     }
74
75     public boolean exists(String where) {
76         String selectSql = "SELECT * FROM " + tableName + " WHERE " + where;
77         SqlRowSet rs = jdbcTemplate.queryForRowSet(selectSql);
78         return rs.first();
79     }
80
81     public void delete(String where) {
82         jdbcTemplate.update("DELETE FROM " + tableName + " WHERE " + where);
83     }
84 }