1
-
1
+ /**
2
+ * Copyright © 2016-2022 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
+ */
2
16
package org .thingsboard .server .dao .sql .query ;
3
17
4
- import com .datastax .oss .driver .api .core .uuid .Uuids ;
5
18
import org .junit .Before ;
6
19
import org .junit .Test ;
20
+ import org .junit .jupiter .api .extension .ExtendWith ;
7
21
import org .junit .runner .RunWith ;
8
- import org .springframework .beans .factory .annotation .Autowired ;
9
- import org .springframework .boot .test .context .SpringBootTest ;
22
+ import org .mockito .BDDMockito ;
23
+ import org .mockito .Mockito ;
24
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
25
+ import org .springframework .boot .test .mock .mockito .SpyBean ;
26
+ import org .springframework .test .context .ContextConfiguration ;
27
+ import org .springframework .test .context .TestPropertySource ;
28
+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
10
29
import org .springframework .test .context .junit4 .SpringRunner ;
11
30
import org .thingsboard .server .common .data .EntityType ;
12
31
import org .thingsboard .server .common .data .id .TenantId ;
13
- import java . util . ArrayList ;
32
+
14
33
import java .util .List ;
15
34
import java .util .UUID ;
16
35
17
36
import static org .junit .Assert .assertEquals ;
37
+ import static org .mockito .Mockito .times ;
18
38
19
39
@ RunWith (SpringRunner .class )
20
- @ SpringBootTest (classes = DefaultQueryLogComponent .class )
40
+ @ ExtendWith (SpringExtension .class )
41
+ @ ContextConfiguration (classes = DefaultQueryLogComponent .class )
42
+ @ EnableConfigurationProperties
43
+ @ TestPropertySource (properties = {
44
+ "sql.log_queries=true" ,
45
+ "sql.log_queries_threshold:2999"
46
+ })
47
+
21
48
public class DefaultQueryLogComponentTest {
22
49
23
50
private TenantId tenantId ;
24
51
private QueryContext ctx ;
25
52
26
- @ Autowired
53
+ @ SpyBean
27
54
private DefaultQueryLogComponent queryLog ;
28
55
29
56
@ Before
30
57
public void setUp () {
31
- tenantId = new TenantId (Uuids . timeBased ( ));
58
+ tenantId = new TenantId (UUID . fromString ( "97275c1c-9cf2-4d25-a68d-933031158f84" ));
32
59
ctx = new QueryContext (new QuerySecurityContext (tenantId , null , EntityType .ALARM ));
33
60
}
34
61
62
+ @ Test
63
+ public void logQuery () {
64
+
65
+ BDDMockito .willCallRealMethod ().given (queryLog ).logQuery (ctx , "" , 3000 );
66
+ BDDMockito .willReturn ("" ).given (queryLog ).substituteParametersInSqlString ("" , ctx );
67
+ queryLog .logQuery (ctx , "" , 3000 );
68
+
69
+ Mockito .verify (queryLog , times (1 )).substituteParametersInSqlString ("" , ctx );
70
+
71
+ }
72
+
35
73
@ Test
36
74
public void substituteParametersInSqlString_StringType () {
37
75
38
- String Name = "Mery's" ;
39
- String id = "ID_1" ;
40
76
String sql = "Select * from Table Where name = :name AND id = :id" ;
41
77
String sqlToUse = "Select * from Table Where name = 'Mery''s' AND id = 'ID_1'" ;
42
78
43
- ctx .addStringParameter ("name" , Name );
44
- ctx .addStringParameter ("id" , id );
79
+ ctx .addStringParameter ("name" , "Mery's" );
80
+ ctx .addStringParameter ("id" , "ID_1" );
45
81
46
82
String sqlToUseResult = queryLog .substituteParametersInSqlString (sql , ctx );
47
83
assertEquals (sqlToUse , sqlToUseResult );
@@ -65,11 +101,10 @@ public void substituteParametersInSqlString_DoubleLongType() {
65
101
@ Test
66
102
public void substituteParametersInSqlString_BooleanType () {
67
103
68
- boolean check = true ;
69
104
String sql = "Select * from Table Where check = :check AND mark = :mark" ;
70
105
String sqlToUse = "Select * from Table Where check = true AND mark = false" ;
71
106
72
- ctx .addBooleanParameter ("check" , check );
107
+ ctx .addBooleanParameter ("check" , true );
73
108
ctx .addBooleanParameter ("mark" , false );
74
109
75
110
String sqlToUseResult = queryLog .substituteParametersInSqlString (sql , ctx );
@@ -79,7 +114,7 @@ public void substituteParametersInSqlString_BooleanType() {
79
114
@ Test
80
115
public void substituteParametersInSqlString_UuidType () {
81
116
82
- UUID guid = Uuids . timeBased ();
117
+ UUID guid = UUID . randomUUID ();
83
118
String sql = "Select * from Table Where guid = :guid" ;
84
119
String sqlToUse = "Select * from Table Where guid = '" + guid + "'" ;
85
120
@@ -106,10 +141,7 @@ public void substituteParametersInSqlString_StringListType() {
106
141
@ Test
107
142
public void substituteParametersInSqlString_UuidListType () {
108
143
109
- List <UUID > guids = new ArrayList <>();
110
- guids .add (UUID .fromString ("634a8d03-6871-4e01-94d0-876bf3e67dff" ));
111
- guids .add (UUID .fromString ("3adbb5b8-4dc6-4faf-80dc-681a7b518b5e" ));
112
- guids .add (UUID .fromString ("63a50f0c-2058-4d1d-8f15-812eb7f84412" ));
144
+ List <UUID > guids = List .of (UUID .fromString ("634a8d03-6871-4e01-94d0-876bf3e67dff" ), UUID .fromString ("3adbb5b8-4dc6-4faf-80dc-681a7b518b5e" ), UUID .fromString ("63a50f0c-2058-4d1d-8f15-812eb7f84412" ));
113
145
114
146
String sql = "Select * from Table Where guid IN (:guids)" ;
115
147
String sqlToUse = "Select * from Table Where guid IN ('634a8d03-6871-4e01-94d0-876bf3e67dff', '3adbb5b8-4dc6-4faf-80dc-681a7b518b5e', '63a50f0c-2058-4d1d-8f15-812eb7f84412')" ;
0 commit comments