Commit 6048b45e397cd4897018f76333128efe8a5fafad

Authored by mp-loki
1 parent 5935de07

Embedded Postgresql test unit added

... ... @@ -174,6 +174,11 @@
174 174 <artifactId>h2</artifactId>
175 175 <scope>test</scope>
176 176 </dependency>
  177 + <dependency>
  178 + <groupId>ru.yandex.qatools.embed</groupId>
  179 + <artifactId>postgresql-embedded</artifactId>
  180 + <scope>test</scope>
  181 + </dependency>
177 182 </dependencies>
178 183 <build>
179 184 <plugins>
... ...
  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 +}
... ...
... ... @@ -20,9 +20,16 @@ import org.junit.ClassRule;
20 20 import org.junit.extensions.cpsuite.ClasspathSuite;
21 21 import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters;
22 22 import org.junit.runner.RunWith;
  23 +import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
23 24
  25 +import java.io.IOException;
  26 +import java.sql.Connection;
  27 +import java.sql.DriverManager;
  28 +import java.sql.SQLException;
24 29 import java.util.Arrays;
25 30
  31 +import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_6;
  32 +
26 33 @RunWith(ClasspathSuite.class)
27 34 @ClassnameFilters({
28 35 "org.thingsboard.server.dao.service.*Test",
... ... @@ -41,4 +48,10 @@ public class DaoTestSuite {
41 48 new ClassPathCQLDataSet("cassandra/system-data.cql", false, false),
42 49 new ClassPathCQLDataSet("system-test.cql", false, false)),
43 50 "cassandra-test.yaml", 30000l);
  51 +
  52 + @ClassRule
  53 + public static CustomPostgresUnit postgresUnit = new CustomPostgresUnit(
  54 + Arrays.asList("postgres/schema.sql", "postgres/system-data.sql"),
  55 + "postgres-embedded-test.properties");
  56 +
44 57 }
... ...
... ... @@ -5,7 +5,7 @@ sql.enabled=true
5 5 spring.jpa.show-sql=false
6 6 spring.jpa.hibernate.ddl-auto=validate
7 7
8   -spring.datasource.url=jdbc:postgresql://localhost:5432/thingsboard
  8 +spring.datasource.url=jdbc:postgresql://localhost:5433/thingsboard
9 9 spring.datasource.username=postgres
10 10 spring.datasource.password=postgres
11 11
... ...
  1 +host: localhost
  2 +port: 5433
  3 +database: thingsboard
  4 +username: postgres
  5 +password: postgres
\ No newline at end of file
... ...
... ... @@ -745,6 +745,13 @@
745 745 <groupId>com.h2database</groupId>
746 746 <artifactId>h2</artifactId>
747 747 <version>${h2.version}</version>
  748 + <scope>test</scope>
  749 + </dependency>
  750 + <dependency>
  751 + <groupId>ru.yandex.qatools.embed</groupId>
  752 + <artifactId>postgresql-embedded</artifactId>
  753 + <version>2.2</version>
  754 + <scope>test</scope>
748 755 </dependency>
749 756 <dependency>
750 757 <groupId>com.sun.winsw</groupId>
... ...