Skip to content

Commit a738507

Browse files
committed
Updating unit tests
1 parent 8063a84 commit a738507

File tree

4 files changed

+75
-64
lines changed

4 files changed

+75
-64
lines changed

src/test/scala/com/github/mmolimar/ksql/jdbc/KsqlConnectionSpec.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class KsqlConnectionSpec extends WordSpec with Matchers with MockFactory {
2323

2424
"throw not supported exception if not supported" in {
2525
val methods = implementedMethods[KsqlConnection]
26-
reflectMethods[KsqlConnection](methods, false, ksqlConnection)
26+
reflectMethods[KsqlConnection](methods = methods, implemented = false, obj = ksqlConnection)
2727
.foreach(method => {
2828
assertThrows[SQLFeatureNotSupportedException] {
2929
method()
@@ -39,11 +39,11 @@ class KsqlConnectionSpec extends WordSpec with Matchers with MockFactory {
3939
ksqlConnection.getTransactionIsolation should be(Connection.TRANSACTION_NONE)
4040
ksqlConnection.setClientInfo(new Properties)
4141

42-
(ksqlRestClient.makeKsqlRequest _).expects(*)
42+
(ksqlRestClient.makeKsqlRequest(_: String)).expects(*)
4343
.returns(RestResponse.successful[KsqlEntityList](new KsqlEntityList))
4444
ksqlConnection.setClientInfo("", "")
4545
assertThrows[SQLException] {
46-
(ksqlRestClient.makeKsqlRequest _).expects(*)
46+
(ksqlRestClient.makeKsqlRequest(_: String)).expects(*)
4747
.returns(RestResponse.erroneous(new KsqlErrorMessage(-1, "", Collections.emptyList[String])))
4848
ksqlConnection.setClientInfo("", "")
4949
}
@@ -55,9 +55,9 @@ class KsqlConnectionSpec extends WordSpec with Matchers with MockFactory {
5555
(new CommandStatuses(Collections.emptyMap[CommandId, CommandStatus.Status])))
5656
ksqlConnection.isValid(0) should be(true)
5757

58-
Option(ksqlConnection.getMetaData) should not be (None)
58+
Option(ksqlConnection.getMetaData) should not be None
5959

60-
Option(ksqlConnection.createStatement) should not be (None)
60+
Option(ksqlConnection.createStatement) should not be None
6161
assertThrows[SQLFeatureNotSupportedException] {
6262
ksqlConnection.createStatement(-1, -1)
6363
}
@@ -70,9 +70,9 @@ class KsqlConnectionSpec extends WordSpec with Matchers with MockFactory {
7070
ksqlConnection.getCatalog should be(None.orNull)
7171

7272
(ksqlRestClient.close _).expects
73-
ksqlConnection.close
73+
ksqlConnection.close()
7474
ksqlConnection.isClosed should be(true)
75-
ksqlConnection.commit
75+
ksqlConnection.commit()
7676
}
7777
}
7878
}
@@ -84,7 +84,7 @@ class KsqlConnectionSpec extends WordSpec with Matchers with MockFactory {
8484
"throw not supported exception if not supported" in {
8585

8686
val resultSet = new ConnectionNotSupported
87-
reflectMethods[ConnectionNotSupported](Seq.empty, false, resultSet)
87+
reflectMethods[ConnectionNotSupported](methods = Seq.empty, implemented = false, obj = resultSet)
8888
.foreach(method => {
8989
assertThrows[SQLFeatureNotSupportedException] {
9090
method()

src/test/scala/com/github/mmolimar/ksql/jdbc/KsqlDatabaseMetaDataSpec.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
3131

3232
"throw not supported exception if not supported" in {
3333
(mockResponse.getEntity _).expects.returns(mock[InputStream]).once
34-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
34+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
3535
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
3636
.anyNumberOfTimes
3737

3838
val methods = implementedMethods[KsqlDatabaseMetaData]
39-
reflectMethods[KsqlDatabaseMetaData](methods, false, metadata)
39+
reflectMethods[KsqlDatabaseMetaData](methods = methods, implemented = false, obj = metadata)
4040
.foreach(method => {
4141
assertThrows[SQLFeatureNotSupportedException] {
4242
method()
@@ -50,7 +50,7 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
5050
val methods = implementedMethods[KsqlDatabaseMetaData]
5151
.filterNot(specialMethods.contains(_))
5252

53-
reflectMethods[KsqlDatabaseMetaData](methods, true, metadata)
53+
reflectMethods[KsqlDatabaseMetaData](methods = methods, implemented = true, obj = metadata)
5454
.foreach(method => {
5555
method()
5656
})
@@ -59,20 +59,20 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
5959
metadata.getTables("", "", "", Array[String]("test"))
6060
}
6161

62-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
62+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
6363
.returns(RestResponse.erroneous(new KsqlErrorMessage(-1, "error message", Collections.emptyList[String])))
6464
.once
6565
assertThrows[SQLException] {
6666
metadata.getTables("", "", "", Array[String](TableTypes.TABLE.name))
6767
}
6868

69-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
69+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
7070
.returns(RestResponse.successful[KsqlEntityList](new KsqlEntityList))
7171
.twice
7272
metadata.getTables("", "", "[a-z]*",
7373
Array[String](TableTypes.TABLE.name, TableTypes.STREAM.name)).next should be(false)
7474

75-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
75+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
7676
.returns(RestResponse.successful[KsqlEntityList](new KsqlEntityList))
7777
.twice
7878
metadata.getColumns("", "", "", "").next should be(false)
@@ -97,15 +97,15 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
9797
val descFn1 = new FunctionDescriptionList("DESCRIBE FUNCTION testfn;",
9898
"TESTFN", "Description", "Confluent", "version", "path",
9999
List(
100-
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description")).asJava, "BIGINT", "Description"),
101-
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description")).asJava, "STRING", "Description")
100+
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description", false)).asJava, "BIGINT", "Description"),
101+
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description", false)).asJava, "STRING", "Description")
102102
).asJava,
103103
FunctionType.scalar
104104
)
105105
val descFn2 = new FunctionDescriptionList("DESCRIBE FUNCTION testdatefn;",
106106
"TESTDATEFN", "Description", "Unknown", "version", "path",
107107
List(
108-
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description")).asJava, "BIGINT", "Description")
108+
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description", false)).asJava, "BIGINT", "Description")
109109
).asJava,
110110
FunctionType.scalar
111111
)
@@ -114,13 +114,13 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
114114
val entityDescribeFn2 = new KsqlEntityList
115115
entityDescribeFn2.add(descFn2)
116116

117-
(mockedKsqlRestClient.makeKsqlRequest _).expects("LIST FUNCTIONS;")
117+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects("LIST FUNCTIONS;")
118118
.returns(RestResponse.successful[KsqlEntityList](entityListFn))
119119
.repeat(4)
120-
(mockedKsqlRestClient.makeKsqlRequest _).expects("DESCRIBE FUNCTION TESTFN;")
120+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects("DESCRIBE FUNCTION TESTFN;")
121121
.returns(RestResponse.successful[KsqlEntityList](entityDescribeFn1))
122122
.repeat(4)
123-
(mockedKsqlRestClient.makeKsqlRequest _).expects("DESCRIBE FUNCTION TESTDATEFN;")
123+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects("DESCRIBE FUNCTION TESTDATEFN;")
124124
.returns(RestResponse.successful[KsqlEntityList](entityDescribeFn2))
125125
.repeat(4)
126126

@@ -129,7 +129,7 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
129129
metadata.getSystemFunctions should be("TESTFN")
130130
metadata.getTimeDateFunctions should be("TESTDATEFN")
131131

132-
Option(metadata.getConnection) should not be (None)
132+
Option(metadata.getConnection) should not be None
133133
metadata.getCatalogs.next should be(false)
134134
metadata.getCatalogTerm should be("TOPIC")
135135
metadata.getSchemaTerm should be("")
@@ -246,7 +246,7 @@ class KsqlDatabaseMetaDataSpec extends WordSpec with Matchers with MockFactory w
246246
"throw not supported exception if not supported" in {
247247

248248
val metadata = new DatabaseMetaDataNotSupported
249-
reflectMethods[DatabaseMetaDataNotSupported](Seq.empty, false, metadata)
249+
reflectMethods[DatabaseMetaDataNotSupported](methods = Seq.empty, implemented = false, obj = metadata)
250250
.foreach(method => {
251251
assertThrows[SQLFeatureNotSupportedException] {
252252
method()

src/test/scala/com/github/mmolimar/ksql/jdbc/KsqlStatementSpec.scala

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import java.io.{ByteArrayInputStream, InputStream}
44
import java.sql.{ResultSet, SQLException, SQLFeatureNotSupportedException}
55

66
import com.github.mmolimar.ksql.jdbc.utils.TestUtils._
7-
import io.confluent.ksql.metastore.{KsqlStream, KsqlTopic}
7+
import io.confluent.ksql.metastore.SerdeFactory
8+
import io.confluent.ksql.metastore.model.{KeyField, KsqlStream, KsqlTopic}
89
import io.confluent.ksql.rest.client.{KsqlRestClient, RestResponse}
910
import io.confluent.ksql.rest.entity.{ExecutionPlan, KafkaTopicsList, QueryDescriptionEntity, QueryDescriptionList, _}
10-
import io.confluent.ksql.serde.DataSource.DataSourceSerDe
11-
import io.confluent.ksql.serde.json.KsqlJsonTopicSerDe
11+
import io.confluent.ksql.rest.server.computation.CommandId
12+
import io.confluent.ksql.schema.ksql.KsqlSchema
13+
import io.confluent.ksql.serde.Format
14+
import io.confluent.ksql.serde.json.KsqlJsonSerdeFactory
1215
import io.confluent.ksql.util.timestamp.LongColumnTimestampExtractionPolicy
1316
import javax.ws.rs.core.Response
14-
import org.apache.kafka.connect.data.SchemaBuilder
17+
import org.apache.kafka.common.serialization.{Serde, Serdes}
18+
import org.apache.kafka.connect.data.{Schema, SchemaBuilder}
1519
import org.scalamock.scalatest.MockFactory
1620
import org.scalatest.{Matchers, OneInstancePerTest, WordSpec}
1721

@@ -31,12 +35,12 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
3135

3236
"throw not supported exception if not supported" in {
3337

34-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
38+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
3539
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
3640
.noMoreThanOnce
3741

3842
val methods = implementedMethods[KsqlStatement]
39-
reflectMethods[KsqlStatement](methods, false, statement)
43+
reflectMethods[KsqlStatement](methods = methods, implemented = false, obj = statement)
4044
.foreach(method => {
4145
assertThrows[SQLFeatureNotSupportedException] {
4246
method()
@@ -60,27 +64,27 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
6064
}
6165

6266
assertThrows[SQLException] {
63-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
67+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
6468
.returns(RestResponse.erroneous(new KsqlErrorMessage(-1, "error")))
6569
.once
6670
statement.execute("select * from test")
6771
}
6872

6973
assertThrows[SQLException] {
70-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
74+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
7175
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
7276
.once
73-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
77+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
7478
.returns(RestResponse.erroneous(new KsqlErrorMessage(-1, "error")))
7579
.once
7680
statement.execute("select * from test")
7781
}
7882

7983
assertThrows[SQLException] {
80-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
84+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
8185
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
8286
.once
83-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
87+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
8488
.returns(RestResponse.successful[KsqlEntityList](new KsqlEntityList))
8589
.once
8690
statement.execute("select * from test")
@@ -110,21 +114,21 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
110114
val entityList = new KsqlEntityList
111115
entityList.add(new QueryDescriptionEntity("select * from test;", queryDesc))
112116

113-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
117+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
114118
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
115119
.once
116-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
120+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
117121
.returns(RestResponse.successful[KsqlEntityList](entityList))
118122
.once
119123
statement.execute("select * from test") should be(true)
120124

121-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
125+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
122126
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
123127
.once
124-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
128+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
125129
.returns(RestResponse.successful[KsqlEntityList](entityList))
126130
.once
127-
Option(statement.executeQuery("select * from test;")) should not be (None)
131+
Option(statement.executeQuery("select * from test;")) should not be None
128132

129133
statement.getMaxRows should be(0)
130134
statement.getResultSet shouldNot be(None.orNull)
@@ -140,36 +144,36 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
140144
statement.getWarnings should be(None.orNull)
141145

142146
assertThrows[SQLException] {
143-
(mockedKsqlRestClient.makeQueryRequest _).expects(*)
147+
(mockedKsqlRestClient.makeQueryRequest _).expects(*, *)
144148
.returns(RestResponse.successful[KsqlRestClient.QueryStream](mockQueryStream(mockResponse)))
145149
.once
146150
val multipleResults = new KsqlEntityList
147151
multipleResults.add(new QueryDescriptionEntity("select * from test;", queryDesc))
148152
multipleResults.add(new QueryDescriptionEntity("select * from test;", queryDesc))
149-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
153+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
150154
.returns(RestResponse.successful[KsqlEntityList](multipleResults))
151155
.once
152156
statement.execute("select * from test")
153157
}
154158
assertThrows[SQLException] {
155159
statement.getResultSet
156160
}
157-
statement.cancel
161+
statement.cancel()
158162

159163
statement.isClosed should be(false)
160-
statement.close
161-
statement.close
164+
statement.close()
165+
statement.close()
162166
statement.isClosed should be(true)
163167
assertThrows[SQLException] {
164168
statement.executeQuery("select * from test;")
165169
}
166170
}
167171

168172
"work when printing topics" in {
169-
(mockedKsqlRestClient.makePrintTopicRequest _).expects(*)
173+
(mockedKsqlRestClient.makePrintTopicRequest _).expects(*, *)
170174
.returns(RestResponse.successful[InputStream](new ByteArrayInputStream("test".getBytes)))
171175
.once
172-
Option(statement.executeQuery("print 'test'")) should not be (None)
176+
Option(statement.executeQuery("print 'test'")) should not be None
173177
statement.getResultSet.next should be(true)
174178
statement.getResultSet.getString(1) should be("test")
175179
}
@@ -180,24 +184,24 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
180184
def validateCommand(entity: KsqlEntity, headers: List[HeaderField]): Unit = {
181185
val entityList = new KsqlEntityList
182186
entityList.add(entity)
183-
(mockedKsqlRestClient.makeKsqlRequest _).expects(*)
187+
(mockedKsqlRestClient.makeKsqlRequest(_: String)).expects(*)
184188
.returns(RestResponse.successful[KsqlEntityList](entityList))
185189
.once
186190
statement.execute(entity.getStatementText) should be(true)
187191
statement.getResultSet.getMetaData.getColumnCount should be(headers.size)
188-
headers.zipWithIndex.map { case (c, index) => {
192+
headers.zipWithIndex.map { case (c, index) =>
189193
statement.getResultSet.getMetaData.getColumnName(index + 1) should be(c.name)
190194
statement.getResultSet.getMetaData.getColumnLabel(index + 1).toUpperCase should be(c.name)
191195
}
192-
}
193196
}
194197

195-
val commandStatus = new CommandStatusEntity("REGISTER TOPIC TEST", "topic/1/create", "SUCCESS", "Success Message")
198+
val commandStatus = new CommandStatusEntity("REGISTER TOPIC TEST", CommandId.fromString("topic/1/create"),
199+
new CommandStatus(CommandStatus.Status.SUCCESS, "Success Message"), null)
196200
val executionPlan = new ExecutionPlan("DESCRIBE test")
197201
val functionDescriptionList = new FunctionDescriptionList("DESCRIBE FUNCTION test;",
198202
"TEST", "Description", "author", "version", "path",
199203
List(
200-
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description")).asJava, "BIGINT", "Description")
204+
new FunctionInfo(List(new ArgumentInfo("arg1", "INT", "Description", false)).asJava, "BIGINT", "Description")
201205
).asJava,
202206
FunctionType.scalar
203207
)
@@ -211,7 +215,7 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
211215
)
212216
val ksqlTopicsList = new KsqlTopicsList(
213217
"SHOW TOPICS;",
214-
List(new KsqlTopicInfo("ksqltopic", "kafkatopic", DataSourceSerDe.JSON)).asJava
218+
List(new KsqlTopicInfo("ksqltopic", "kafkatopic", Format.JSON)).asJava
215219
)
216220
val propertiesList = new PropertiesList(
217221
"list properties;",
@@ -250,15 +254,22 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
250254
"EXPLAIN select * from test;",
251255
List(queryDescription.getQueryDescription).asJava
252256
)
257+
val schema = SchemaBuilder
258+
.struct
259+
.field("key", Schema.OPTIONAL_INT64_SCHEMA)
260+
.build()
253261
val sourceDescEntity = new SourceDescriptionEntity(
254262
"DESCRIBE TEST;",
255263
new SourceDescription(
256264
new KsqlStream("sqlExpression",
257265
"datasource",
258-
SchemaBuilder.struct,
259-
SchemaBuilder.struct.field("key"),
266+
KsqlSchema.of(schema),
267+
KeyField.of("key", schema.field("key")),
260268
new LongColumnTimestampExtractionPolicy("timestamp"),
261-
new KsqlTopic("input", "input", new KsqlJsonTopicSerDe)),
269+
new KsqlTopic("input", "input", new KsqlJsonSerdeFactory, true),
270+
new SerdeFactory[String] {
271+
override def create(): Serde[String] = Serdes.String()
272+
}),
262273
true,
263274
"JSON",
264275
List.empty.asJava,
@@ -302,7 +313,7 @@ class KsqlStatementSpec extends WordSpec with Matchers with MockFactory with One
302313
"throw not supported exception if not supported" in {
303314

304315
val resultSet = new StatementNotSupported
305-
reflectMethods[StatementNotSupported](Seq.empty, false, resultSet)
316+
reflectMethods[StatementNotSupported](methods = Seq.empty, implemented = false, obj = resultSet)
306317
.foreach(method => {
307318
assertThrows[SQLFeatureNotSupportedException] {
308319
method()

0 commit comments

Comments
 (0)