Commit 076b1943fb974d4093275ff54fca4337b0ca88a6

Authored by Sergey Matvienko
1 parent adb9a8d1

sql dao test: init ODBC connection to Postgres DB with custom PostgreSqlInitiali…

…zer (class rule replacement, PostgreSQL dao tests able to run as standalone)
@@ -30,13 +30,10 @@ @@ -30,13 +30,10 @@
30 */ 30 */
31 package org.thingsboard.server.dao; 31 package org.thingsboard.server.dao;
32 32
33 -import org.junit.ClassRule;  
34 import org.junit.extensions.cpsuite.ClasspathSuite; 33 import org.junit.extensions.cpsuite.ClasspathSuite;
35 import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters; 34 import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters;
36 import org.junit.runner.RunWith; 35 import org.junit.runner.RunWith;
37 36
38 -import java.util.Arrays;  
39 -  
40 @RunWith(ClasspathSuite.class) 37 @RunWith(ClasspathSuite.class)
41 @ClassnameFilters({ 38 @ClassnameFilters({
42 "org.thingsboard.server.dao.service.psql.*SqlTest", 39 "org.thingsboard.server.dao.service.psql.*SqlTest",
@@ -45,32 +42,4 @@ import java.util.Arrays; @@ -45,32 +42,4 @@ import java.util.Arrays;
45 "org.thingsboard.server.dao.service.timeseries.psql.*SqlTest" 42 "org.thingsboard.server.dao.service.timeseries.psql.*SqlTest"
46 }) 43 })
47 public class PostgreSqlDaoServiceTestSuite { 44 public class PostgreSqlDaoServiceTestSuite {
48 -  
49 - @ClassRule  
50 - public static CustomSqlUnit sqlUnit = new CustomSqlUnit(  
51 - Arrays.asList("sql/schema-ts-psql.sql", "sql/schema-entities.sql", "sql/schema-entities-idx.sql"  
52 - , "sql/system-data.sql"  
53 - , "sql/system-test-psql.sql"  
54 - ),  
55 - "sql/psql/drop-all-tables.sql",  
56 - "psql-test.properties"  
57 - );  
58 -  
59 -// @ClassRule  
60 -// public static CustomSqlUnit sqlUnit = new CustomSqlUnit(  
61 -// Arrays.asList("sql/schema-ts-psql.sql"  
62 -// , "sql/schema-entities.sql", "sql/schema-entities-idx.sql"  
63 -// , "sql/system-data.sql", "sql/system-test.sql"  
64 -// ),  
65 -// "sql/psql/drop-all-tables.sql",  
66 -// "sql-test.properties"  
67 -// );  
68 -  
69 -// @ClassRule  
70 -// public static CustomSqlUnit sqlUnit = new CustomSqlUnit(  
71 -// Arrays.asList("sql/schema-timescale.sql", "sql/schema-entities.sql", "sql/schema-entities-idx.sql", "sql/system-data.sql", "sql/system-test.sql"),  
72 -// "sql/timescale/drop-all-tables.sql",  
73 -// "sql-test.properties"  
74 -// );  
75 -  
76 } 45 }
  1 +/**
  2 + * ThingsBoard, Inc. ("COMPANY") CONFIDENTIAL
  3 + *
  4 + * Copyright © 2016-2021 ThingsBoard, Inc. All Rights Reserved.
  5 + *
  6 + * NOTICE: All information contained herein is, and remains
  7 + * the property of ThingsBoard, Inc. and its suppliers,
  8 + * if any. The intellectual and technical concepts contained
  9 + * herein are proprietary to ThingsBoard, Inc.
  10 + * and its suppliers and may be covered by U.S. and Foreign Patents,
  11 + * patents in process, and are protected by trade secret or copyright law.
  12 + *
  13 + * Dissemination of this information or reproduction of this material is strictly forbidden
  14 + * unless prior written permission is obtained from COMPANY.
  15 + *
  16 + * Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees,
  17 + * managers or contractors who have executed Confidentiality and Non-disclosure agreements
  18 + * explicitly covering such access.
  19 + *
  20 + * The copyright notice above does not evidence any actual or intended publication
  21 + * or disclosure of this source code, which includes
  22 + * information that is confidential and/or proprietary, and is a trade secret, of COMPANY.
  23 + * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE,
  24 + * OR PUBLIC DISPLAY OF OR THROUGH USE OF THIS SOURCE CODE WITHOUT
  25 + * THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
  26 + * AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.
  27 + * THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION
  28 + * DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS,
  29 + * OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR IN PART.
  30 + */
  31 +package org.thingsboard.server.dao;
  32 +
  33 +import com.google.common.base.Charsets;
  34 +import com.google.common.io.Resources;
  35 +import lombok.extern.slf4j.Slf4j;
  36 +
  37 +import java.io.IOException;
  38 +import java.net.URL;
  39 +import java.sql.Connection;
  40 +import java.sql.SQLException;
  41 +import java.util.List;
  42 +
  43 +@Slf4j
  44 +public class PostgreSqlInitializer {
  45 +
  46 + private static final List<String> sqlFiles = List.of(
  47 + "sql/schema-ts-psql.sql",
  48 + "sql/schema-entities.sql",
  49 + "sql/schema-entities-idx.sql",
  50 + "sql/system-data.sql",
  51 + "sql/system-test-psql.sql");
  52 + private static final String dropAllTablesSqlFile = "sql/psql/drop-all-tables.sql";
  53 +
  54 + public static void initDb(Connection conn) {
  55 + cleanUpDb(conn);
  56 + log.info("initialize Postgres DB...");
  57 + try {
  58 + for (String sqlFile : sqlFiles) {
  59 + URL sqlFileUrl = Resources.getResource(sqlFile);
  60 + String sql = Resources.toString(sqlFileUrl, Charsets.UTF_8);
  61 + conn.createStatement().execute(sql);
  62 + }
  63 + } catch (IOException | SQLException e) {
  64 + throw new RuntimeException("Unable to init the Postgres database. Reason: " + e.getMessage(), e);
  65 + }
  66 + log.info("Postgres DB is initialized!");
  67 + }
  68 +
  69 + private static void cleanUpDb(Connection conn) {
  70 + log.info("clean up Postgres DB...");
  71 + try {
  72 + URL dropAllTableSqlFileUrl = Resources.getResource(dropAllTablesSqlFile);
  73 + String dropAllTablesSql = Resources.toString(dropAllTableSqlFileUrl, Charsets.UTF_8);
  74 + conn.createStatement().execute(dropAllTablesSql);
  75 + } catch (IOException | SQLException e) {
  76 + throw new RuntimeException("Unable to clean up the Postgres database. Reason: " + e.getMessage(), e);
  77 + }
  78 + }
  79 +}
@@ -12,10 +12,10 @@ spring.jpa.hibernate.ddl-auto=none @@ -12,10 +12,10 @@ spring.jpa.hibernate.ddl-auto=none
12 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 12 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
13 spring.datasource.username=postgres 13 spring.datasource.username=postgres
14 spring.datasource.password=postgres 14 spring.datasource.password=postgres
15 -spring.datasource.url=jdbc:tc:postgresql:12.8:///thingsboard?TC_DAEMON=true&TC_TMPFS=/testtmpfs:rw 15 +spring.datasource.url=jdbc:tc:postgresql:12.8:///thingsboard?TC_DAEMON=true&TC_TMPFS=/testtmpfs:rw&?TC_INITFUNCTION=org.thingsboard.server.dao.PostgreSqlInitializer::initDb
16 spring.datasource.driverClassName=org.testcontainers.jdbc.ContainerDatabaseDriver 16 spring.datasource.driverClassName=org.testcontainers.jdbc.ContainerDatabaseDriver
17 #org.postgresql.Driver 17 #org.postgresql.Driver
18 -spring.datasource.hikari.maximumPoolSize=20 18 +spring.datasource.hikari.maximumPoolSize=50
19 service.type=monolith 19 service.type=monolith
20 #database.ts.type=timescale 20 #database.ts.type=timescale
21 #database.ts.type=sql 21 #database.ts.type=sql