|
1
|
+/**
|
|
2
|
+ * Copyright © 2016-2017 The Thingsboard Authors
|
|
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.thingsboard.server.dao;
|
|
17
|
+
|
|
18
|
+import com.google.common.base.Charsets;
|
|
19
|
+import com.google.common.io.Resources;
|
|
20
|
+import lombok.extern.slf4j.Slf4j;
|
|
21
|
+import org.junit.Before;
|
|
22
|
+import org.junit.rules.ExternalResource;
|
|
23
|
+import org.junit.rules.TestRule;
|
|
24
|
+import org.junit.runner.Description;
|
|
25
|
+import org.junit.runners.model.Statement;
|
|
26
|
+import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
|
|
27
|
+
|
|
28
|
+import java.io.IOException;
|
|
29
|
+import java.io.InputStream;
|
|
30
|
+import java.net.URL;
|
|
31
|
+import java.sql.Connection;
|
|
32
|
+import java.sql.DriverManager;
|
|
33
|
+import java.sql.SQLException;
|
|
34
|
+import java.util.List;
|
|
35
|
+import java.util.Properties;
|
|
36
|
+
|
|
37
|
+import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_6;
|
|
38
|
+
|
|
39
|
+/**
|
|
40
|
+ * Created by Valerii Sosliuk on 6/24/2017.
|
|
41
|
+ */
|
|
42
|
+@Slf4j
|
|
43
|
+public class CustomPostgresUnit extends ExternalResource {
|
|
44
|
+
|
|
45
|
+ private static final String HOST = "host";
|
|
46
|
+ private static final String PORT = "port";
|
|
47
|
+ private static final String DATABASE = "database";
|
|
48
|
+ private static final String USERNAME = "username";
|
|
49
|
+ private static final String PASSWORD = "password";
|
|
50
|
+
|
|
51
|
+ private List<String> sqlFiles;
|
|
52
|
+ private Properties properties;
|
|
53
|
+
|
|
54
|
+ private EmbeddedPostgres postgres;
|
|
55
|
+
|
|
56
|
+ public CustomPostgresUnit(List<String> sqlFiles, String configurationFileName) {
|
|
57
|
+ this.sqlFiles = sqlFiles;
|
|
58
|
+ this.properties = loadProperties(configurationFileName);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ @Override
|
|
62
|
+ public void before() {
|
|
63
|
+ postgres = new EmbeddedPostgres(V9_6);
|
|
64
|
+ load();
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ @Override
|
|
68
|
+ public void after() {
|
|
69
|
+ postgres.stop();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ private void load() {
|
|
73
|
+ Connection conn = null;
|
|
74
|
+ try {
|
|
75
|
+ String url = postgres.start(properties.getProperty(HOST),
|
|
76
|
+ Integer.parseInt(properties.getProperty(PORT)),
|
|
77
|
+ properties.getProperty(DATABASE),
|
|
78
|
+ properties.getProperty(USERNAME),
|
|
79
|
+ properties.getProperty(PASSWORD));
|
|
80
|
+
|
|
81
|
+ conn = DriverManager.getConnection(url);
|
|
82
|
+ for (String sqlFile : sqlFiles) {
|
|
83
|
+ URL sqlFileUrl = Resources.getResource(sqlFile);
|
|
84
|
+ String sql = Resources.toString(sqlFileUrl, Charsets.UTF_8);
|
|
85
|
+ conn.createStatement().execute(sql);
|
|
86
|
+ }
|
|
87
|
+ } catch (IOException | SQLException e) {
|
|
88
|
+ throw new RuntimeException("Unable to start embedded postgres. Reason: " + e.getMessage(), e);
|
|
89
|
+ } finally {
|
|
90
|
+ if (conn != null) {
|
|
91
|
+ try {
|
|
92
|
+ conn.close();
|
|
93
|
+ } catch (SQLException e) {
|
|
94
|
+ log.error(e.getMessage(), e);
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ private Properties loadProperties(String fileName) {
|
|
101
|
+ final Properties properties = new Properties();
|
|
102
|
+ try (final InputStream stream = this.getClass().getClassLoader().getResourceAsStream(fileName)) {
|
|
103
|
+ properties.load(stream);
|
|
104
|
+ return properties;
|
|
105
|
+ } catch (IOException e) {
|
|
106
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+} |
...
|
...
|
|