Commit e922b7da528031e65cf5b52d02ba344820151bca
1 parent
9c4d0940
Fixed conversion test for H2 database
Showing
3 changed files
with
74 additions
and
0 deletions
... | ... | @@ -25,6 +25,7 @@ import org.junit.Assert; |
25 | 25 | import org.junit.Before; |
26 | 26 | import org.junit.Test; |
27 | 27 | import org.springframework.beans.factory.annotation.Autowired; |
28 | +import org.springframework.jdbc.core.JdbcTemplate; | |
28 | 29 | import org.thingsboard.server.common.data.DataConstants; |
29 | 30 | import org.thingsboard.server.common.data.Device; |
30 | 31 | import org.thingsboard.server.common.data.EntityType; |
... | ... | @@ -45,6 +46,8 @@ import org.thingsboard.server.dao.attributes.AttributesService; |
45 | 46 | import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; |
46 | 47 | import org.thingsboard.server.dao.rule.RuleChainService; |
47 | 48 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
49 | +import org.thingsboard.server.dao.util.DaoTestUtil; | |
50 | +import org.thingsboard.server.dao.util.SqlDbType; | |
48 | 51 | |
49 | 52 | import java.util.*; |
50 | 53 | import java.util.concurrent.ExecutionException; |
... | ... | @@ -60,6 +63,9 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { |
60 | 63 | |
61 | 64 | private TenantId tenantId; |
62 | 65 | |
66 | + @Autowired | |
67 | + private JdbcTemplate template; | |
68 | + | |
63 | 69 | @Before |
64 | 70 | public void before() { |
65 | 71 | Tenant tenant = new Tenant(); |
... | ... | @@ -821,6 +827,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { |
821 | 827 | .getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()); |
822 | 828 | } |
823 | 829 | List<String> deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); |
830 | + if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) { | |
831 | + // in H2 double values are stored with E0 in the end of the string | |
832 | + loadedTemperatures = loadedTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList()); | |
833 | + } | |
824 | 834 | Assert.assertEquals(deviceTemperatures, loadedTemperatures); |
825 | 835 | |
826 | 836 | pageLink = new EntityDataPageLink(10, 0, null, sortOrder); |
... | ... | @@ -848,6 +858,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { |
848 | 858 | entityData.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList()); |
849 | 859 | List<String> deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); |
850 | 860 | |
861 | + if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) { | |
862 | + // in H2 double values are stored with E0 in the end of the string | |
863 | + loadedHighTemperatures = loadedHighTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList()); | |
864 | + } | |
851 | 865 | Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures); |
852 | 866 | |
853 | 867 | deviceService.deleteDevicesByTenantId(tenantId); | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.util; | |
17 | + | |
18 | +import org.springframework.jdbc.core.JdbcTemplate; | |
19 | + | |
20 | +import java.sql.DriverManager; | |
21 | + | |
22 | +public class DaoTestUtil { | |
23 | + private static final String POSTGRES_DRIVER_CLASS = "org.postgresql.Driver"; | |
24 | + private static final String H2_DRIVER_CLASS = "org.hsqldb.jdbc.JDBCDriver"; | |
25 | + | |
26 | + | |
27 | + public static SqlDbType getSqlDbType(JdbcTemplate template){ | |
28 | + try { | |
29 | + String driverName = DriverManager.getDriver(template.getDataSource().getConnection().getMetaData().getURL()).getClass().getName(); | |
30 | + if (POSTGRES_DRIVER_CLASS.equals(driverName)) { | |
31 | + return SqlDbType.POSTGRES; | |
32 | + } else if (H2_DRIVER_CLASS.equals(driverName)) { | |
33 | + return SqlDbType.H2; | |
34 | + } | |
35 | + } catch (Exception e) { | |
36 | + e.printStackTrace(); | |
37 | + } | |
38 | + return null; | |
39 | + } | |
40 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.util; | |
17 | + | |
18 | +public enum SqlDbType { | |
19 | + POSTGRES, H2; | |
20 | +} | ... | ... |