Skip to content

Commit 43bc73f

Browse files
authored
add UT for request/reseponse model (opensearch-project#264)
Signed-off-by: Zhongnan Su <[email protected]>
1 parent b08c5b4 commit 43bc73f

33 files changed

+1847
-91
lines changed

reports-scheduler/build.gradle

+8-8
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ integTest {
226226
}
227227

228228
if (System.getProperty("tests.clustername") != null) {
229-
exclude 'org/opensearch/reportsscheduler/ReportsSchedulerPluginIT.class'
229+
exclude 'org/opensearch/integTest/ReportsSchedulerPluginIT.class'
230230
}
231231

232232
if (System.getProperty("tests.rest.bwcsuite") == null) {
233233
filter {
234-
excludeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
234+
excludeTestsMatching "org.opensearch.integTest.bwc.*IT"
235235
}
236236
}
237237
}
@@ -340,7 +340,7 @@ task prepareBwcTests {
340340
dependsOn 'prepareBwcTests'
341341
useCluster testClusters."${baseName}$i"
342342
filter {
343-
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
343+
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
344344
}
345345
systemProperty 'tests.rest.bwcsuite', 'old_cluster'
346346
systemProperty 'tests.rest.bwcsuite_round', 'old'
@@ -360,7 +360,7 @@ task "${baseName}#mixedClusterTask"(type: StandaloneRestIntegTestTask) {
360360
testClusters."${baseName}0".upgradeNodeAndPluginToNextVersion(plugins)
361361
}
362362
filter {
363-
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
363+
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
364364
}
365365
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
366366
systemProperty 'tests.rest.bwcsuite_round', 'first'
@@ -379,7 +379,7 @@ task "${baseName}#twoThirdsUpgradedClusterTask"(type: StandaloneRestIntegTestTas
379379
testClusters."${baseName}0".upgradeNodeAndPluginToNextVersion(plugins)
380380
}
381381
filter {
382-
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
382+
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
383383
}
384384
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
385385
systemProperty 'tests.rest.bwcsuite_round', 'second'
@@ -398,7 +398,7 @@ task "${baseName}#rollingUpgradeClusterTask"(type: StandaloneRestIntegTestTask)
398398
testClusters."${baseName}0".upgradeNodeAndPluginToNextVersion(plugins)
399399
}
400400
filter {
401-
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
401+
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
402402
}
403403
mustRunAfter "${baseName}#mixedClusterTask"
404404
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
@@ -417,7 +417,7 @@ task "${baseName}#fullRestartClusterTask"(type: StandaloneRestIntegTestTask) {
417417
testClusters."${baseName}1".upgradeAllNodesAndPluginsToNextVersion(plugins)
418418
}
419419
filter {
420-
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
420+
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
421421
}
422422
systemProperty 'tests.rest.bwcsuite', 'upgraded_cluster'
423423
systemProperty 'tests.plugin_bwc_version', bwcVersion
@@ -447,7 +447,7 @@ task integTestRemote(type: RestIntegTestTask) {
447447
// Only rest case can run with remote cluster
448448
if (System.getProperty("tests.rest.cluster") != null) {
449449
filter {
450-
includeTestsMatching "org.opensearch.reportsscheduler.rest.*IT"
450+
includeTestsMatching "org.opensearch.integTest.rest.*IT"
451451
}
452452
}
453453
}

reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/model/ReportDefinitionDetailsSearchResults.kt

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.opensearch.reportsscheduler.model
77

8+
import org.apache.lucene.search.TotalHits
89
import org.opensearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_LIST_FIELD
910
import org.opensearch.action.search.SearchResponse
1011
import org.opensearch.common.xcontent.XContentParser
@@ -13,6 +14,13 @@ import org.opensearch.common.xcontent.XContentParser
1314
* ReportDefinitions search results
1415
*/
1516
internal class ReportDefinitionDetailsSearchResults : SearchResults<ReportDefinitionDetails> {
17+
constructor(
18+
startIndex: Long,
19+
totalHits: Long,
20+
totalHitRelation: TotalHits.Relation,
21+
objectList: List<ReportDefinitionDetails>
22+
) : super(startIndex, totalHits, totalHitRelation, objectList, REPORT_DEFINITION_LIST_FIELD)
23+
1624
constructor(parser: XContentParser) : super(parser, REPORT_DEFINITION_LIST_FIELD)
1725

1826
constructor(from: Long, response: SearchResponse) : super(from, response, REPORT_DEFINITION_LIST_FIELD)

reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/model/RestTag.kt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
56
package org.opensearch.reportsscheduler.model
67

78
import org.opensearch.common.xcontent.ToXContent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.integTest
7+
8+
import com.google.gson.JsonObject
9+
import com.google.gson.JsonParser
10+
import org.junit.Assert
11+
import java.time.Instant
12+
import kotlin.test.assertTrue
13+
14+
private const val DEFAULT_TIME_ACCURACY_SEC = 5L
15+
16+
fun constructReportDefinitionRequest(
17+
trigger: String = """
18+
"trigger":{
19+
"triggerType":"OnDemand"
20+
},
21+
""".trimIndent(),
22+
name: String = "report_definition",
23+
delivery: String = ""
24+
): String {
25+
return """
26+
{
27+
"reportDefinition":{
28+
"name":"$name",
29+
"isEnabled":true,
30+
"source":{
31+
"description":"description",
32+
"type":"Dashboard",
33+
"origin":"localhost:5601",
34+
"id":"id"
35+
},
36+
$trigger
37+
$delivery
38+
"format":{
39+
"duration":"PT1H",
40+
"fileFormat":"Pdf",
41+
"limit":1000,
42+
"header":"optional header",
43+
"footer":"optional footer"
44+
}
45+
}
46+
}
47+
""".trimIndent()
48+
}
49+
50+
fun jsonify(text: String): JsonObject {
51+
return JsonParser.parseString(text).asJsonObject
52+
}
53+
54+
fun validateTimeNearRefTime(time: Instant, refTime: Instant, accuracySeconds: Long) {
55+
assertTrue(time.plusSeconds(accuracySeconds).isAfter(refTime), "$time + $accuracySeconds > $refTime")
56+
assertTrue(time.minusSeconds(accuracySeconds).isBefore(refTime), "$time - $accuracySeconds < $refTime")
57+
}
58+
59+
fun validateTimeRecency(time: Instant, accuracySeconds: Long = DEFAULT_TIME_ACCURACY_SEC) {
60+
validateTimeNearRefTime(time, Instant.now(), accuracySeconds)
61+
}
62+
63+
fun validateErrorResponse(response: JsonObject, statusCode: Int, errorType: String = "status_exception") {
64+
Assert.assertNotNull("Error response content should be generated", response)
65+
val status = response.get("status").asInt
66+
val error = response.get("error").asJsonObject
67+
val rootCause = error.get("root_cause").asJsonArray
68+
val type = error.get("type").asString
69+
val reason = error.get("reason").asString
70+
Assert.assertEquals(statusCode, status)
71+
Assert.assertEquals(errorType, type)
72+
Assert.assertNotNull(reason)
73+
Assert.assertNotNull(rootCause)
74+
Assert.assertTrue(rootCause.size() > 0)
75+
}

reports-scheduler/src/test/kotlin/org/opensearch/reportsscheduler/PluginRestTestCase.kt renamed to reports-scheduler/src/test/kotlin/org/opensearch/integTest/PluginRestTestCase.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler
6+
package org.opensearch.integTest
77

88
import com.google.gson.JsonObject
99
import org.apache.http.Header
+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler
6+
package org.opensearch.integTest
77

88
import org.opensearch.test.OpenSearchTestCase
99

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler
6+
package org.opensearch.integTest
77

88
import org.opensearch.action.admin.cluster.health.ClusterHealthRequest
99
import org.opensearch.action.admin.cluster.node.info.NodesInfoRequest
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler.bwc
6+
package org.opensearch.integTest.bwc
77

88
import org.junit.Assert
99
import org.opensearch.common.settings.Settings
10-
import org.opensearch.reportsscheduler.PluginRestTestCase
10+
import org.opensearch.integTest.PluginRestTestCase
1111
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
1212
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
13-
import org.opensearch.reportsscheduler.constructReportDefinitionRequest
13+
import org.opensearch.integTest.constructReportDefinitionRequest
1414
import org.opensearch.rest.RestRequest
1515
import org.opensearch.rest.RestStatus
1616
import java.time.Instant
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler.rest
6+
package org.opensearch.integTest.rest
77

8-
import org.opensearch.reportsscheduler.PluginRestTestCase
8+
import org.opensearch.integTest.PluginRestTestCase
99
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
1010
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
11-
import org.opensearch.reportsscheduler.jsonify
12-
import org.opensearch.reportsscheduler.validateErrorResponse
13-
import org.opensearch.reportsscheduler.validateTimeRecency
11+
import org.opensearch.integTest.jsonify
12+
import org.opensearch.integTest.validateErrorResponse
13+
import org.opensearch.integTest.validateTimeRecency
1414
import org.opensearch.rest.RestRequest
1515
import org.opensearch.rest.RestStatus
1616
import org.junit.Assert
+6-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler.rest
6+
package org.opensearch.integTest.rest
77

8-
import org.opensearch.reportsscheduler.PluginRestTestCase
8+
import org.opensearch.integTest.PluginRestTestCase
99
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
1010
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
11-
import org.opensearch.reportsscheduler.jsonify
12-
import org.opensearch.reportsscheduler.validateErrorResponse
13-
import org.opensearch.reportsscheduler.validateTimeNearRefTime
14-
import org.opensearch.reportsscheduler.validateTimeRecency
11+
import org.opensearch.integTest.jsonify
12+
import org.opensearch.integTest.validateErrorResponse
13+
import org.opensearch.integTest.validateTimeNearRefTime
14+
import org.opensearch.integTest.validateTimeRecency
1515
import org.opensearch.rest.RestRequest
1616
import org.opensearch.rest.RestStatus
1717
import org.junit.Assert

reports-scheduler/src/test/kotlin/org/opensearch/reportsscheduler/rest/ReportDefinitionIT.kt renamed to reports-scheduler/src/test/kotlin/org/opensearch/integTest/rest/ReportDefinitionIT.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler.rest
6+
package org.opensearch.integTest.rest
77

8-
import org.opensearch.reportsscheduler.PluginRestTestCase
8+
import org.opensearch.integTest.PluginRestTestCase
99
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
1010
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
11-
import org.opensearch.reportsscheduler.constructReportDefinitionRequest
12-
import org.opensearch.reportsscheduler.validateErrorResponse
11+
import org.opensearch.integTest.constructReportDefinitionRequest
12+
import org.opensearch.integTest.validateErrorResponse
1313
import org.opensearch.rest.RestRequest
1414
import org.opensearch.rest.RestStatus
1515
import org.junit.Assert

reports-scheduler/src/test/kotlin/org/opensearch/reportsscheduler/rest/ReportInstanceIT.kt renamed to reports-scheduler/src/test/kotlin/org/opensearch/integTest/rest/ReportInstanceIT.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.reportsscheduler.rest
6+
package org.opensearch.integTest.rest
77

8-
import org.opensearch.reportsscheduler.PluginRestTestCase
8+
import org.opensearch.integTest.PluginRestTestCase
99
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
1010
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
11-
import org.opensearch.reportsscheduler.constructReportDefinitionRequest
12-
import org.opensearch.reportsscheduler.validateErrorResponse
11+
import org.opensearch.integTest.constructReportDefinitionRequest
12+
import org.opensearch.integTest.validateErrorResponse
1313
import org.opensearch.rest.RestRequest
1414
import org.opensearch.rest.RestStatus
1515
import org.junit.Assert

0 commit comments

Comments
 (0)