Skip to content

Commit

Permalink
add UT for request/reseponse model (#264)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongnan Su <szhongna@amazon.com>
  • Loading branch information
zhongnansu authored Dec 22, 2021
1 parent b08c5b4 commit 43bc73f
Show file tree
Hide file tree
Showing 33 changed files with 1,847 additions and 91 deletions.
16 changes: 8 additions & 8 deletions reports-scheduler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ integTest {
}

if (System.getProperty("tests.clustername") != null) {
exclude 'org/opensearch/reportsscheduler/ReportsSchedulerPluginIT.class'
exclude 'org/opensearch/integTest/ReportsSchedulerPluginIT.class'
}

if (System.getProperty("tests.rest.bwcsuite") == null) {
filter {
excludeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
excludeTestsMatching "org.opensearch.integTest.bwc.*IT"
}
}
}
Expand Down Expand Up @@ -340,7 +340,7 @@ task prepareBwcTests {
dependsOn 'prepareBwcTests'
useCluster testClusters."${baseName}$i"
filter {
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
}
systemProperty 'tests.rest.bwcsuite', 'old_cluster'
systemProperty 'tests.rest.bwcsuite_round', 'old'
Expand All @@ -360,7 +360,7 @@ task "${baseName}#mixedClusterTask"(type: StandaloneRestIntegTestTask) {
testClusters."${baseName}0".upgradeNodeAndPluginToNextVersion(plugins)
}
filter {
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
}
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
systemProperty 'tests.rest.bwcsuite_round', 'first'
Expand All @@ -379,7 +379,7 @@ task "${baseName}#twoThirdsUpgradedClusterTask"(type: StandaloneRestIntegTestTas
testClusters."${baseName}0".upgradeNodeAndPluginToNextVersion(plugins)
}
filter {
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
}
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
systemProperty 'tests.rest.bwcsuite_round', 'second'
Expand All @@ -398,7 +398,7 @@ task "${baseName}#rollingUpgradeClusterTask"(type: StandaloneRestIntegTestTask)
testClusters."${baseName}0".upgradeNodeAndPluginToNextVersion(plugins)
}
filter {
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
}
mustRunAfter "${baseName}#mixedClusterTask"
systemProperty 'tests.rest.bwcsuite', 'mixed_cluster'
Expand All @@ -417,7 +417,7 @@ task "${baseName}#fullRestartClusterTask"(type: StandaloneRestIntegTestTask) {
testClusters."${baseName}1".upgradeAllNodesAndPluginsToNextVersion(plugins)
}
filter {
includeTestsMatching "org.opensearch.reportsscheduler.bwc.*IT"
includeTestsMatching "org.opensearch.integTest.bwc.*IT"
}
systemProperty 'tests.rest.bwcsuite', 'upgraded_cluster'
systemProperty 'tests.plugin_bwc_version', bwcVersion
Expand Down Expand Up @@ -447,7 +447,7 @@ task integTestRemote(type: RestIntegTestTask) {
// Only rest case can run with remote cluster
if (System.getProperty("tests.rest.cluster") != null) {
filter {
includeTestsMatching "org.opensearch.reportsscheduler.rest.*IT"
includeTestsMatching "org.opensearch.integTest.rest.*IT"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.reportsscheduler.model

import org.apache.lucene.search.TotalHits
import org.opensearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_LIST_FIELD
import org.opensearch.action.search.SearchResponse
import org.opensearch.common.xcontent.XContentParser
Expand All @@ -13,6 +14,13 @@ import org.opensearch.common.xcontent.XContentParser
* ReportDefinitions search results
*/
internal class ReportDefinitionDetailsSearchResults : SearchResults<ReportDefinitionDetails> {
constructor(
startIndex: Long,
totalHits: Long,
totalHitRelation: TotalHits.Relation,
objectList: List<ReportDefinitionDetails>
) : super(startIndex, totalHits, totalHitRelation, objectList, REPORT_DEFINITION_LIST_FIELD)

constructor(parser: XContentParser) : super(parser, REPORT_DEFINITION_LIST_FIELD)

constructor(from: Long, response: SearchResponse) : super(from, response, REPORT_DEFINITION_LIST_FIELD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler.model

import org.opensearch.common.xcontent.ToXContent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.integTest

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.junit.Assert
import java.time.Instant
import kotlin.test.assertTrue

private const val DEFAULT_TIME_ACCURACY_SEC = 5L

fun constructReportDefinitionRequest(
trigger: String = """
"trigger":{
"triggerType":"OnDemand"
},
""".trimIndent(),
name: String = "report_definition",
delivery: String = ""
): String {
return """
{
"reportDefinition":{
"name":"$name",
"isEnabled":true,
"source":{
"description":"description",
"type":"Dashboard",
"origin":"localhost:5601",
"id":"id"
},
$trigger
$delivery
"format":{
"duration":"PT1H",
"fileFormat":"Pdf",
"limit":1000,
"header":"optional header",
"footer":"optional footer"
}
}
}
""".trimIndent()
}

fun jsonify(text: String): JsonObject {
return JsonParser.parseString(text).asJsonObject
}

fun validateTimeNearRefTime(time: Instant, refTime: Instant, accuracySeconds: Long) {
assertTrue(time.plusSeconds(accuracySeconds).isAfter(refTime), "$time + $accuracySeconds > $refTime")
assertTrue(time.minusSeconds(accuracySeconds).isBefore(refTime), "$time - $accuracySeconds < $refTime")
}

fun validateTimeRecency(time: Instant, accuracySeconds: Long = DEFAULT_TIME_ACCURACY_SEC) {
validateTimeNearRefTime(time, Instant.now(), accuracySeconds)
}

fun validateErrorResponse(response: JsonObject, statusCode: Int, errorType: String = "status_exception") {
Assert.assertNotNull("Error response content should be generated", response)
val status = response.get("status").asInt
val error = response.get("error").asJsonObject
val rootCause = error.get("root_cause").asJsonArray
val type = error.get("type").asString
val reason = error.get("reason").asString
Assert.assertEquals(statusCode, status)
Assert.assertEquals(errorType, type)
Assert.assertNotNull(reason)
Assert.assertNotNull(rootCause)
Assert.assertTrue(rootCause.size() > 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler
package org.opensearch.integTest

import com.google.gson.JsonObject
import org.apache.http.Header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler
package org.opensearch.integTest

import org.opensearch.test.OpenSearchTestCase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler
package org.opensearch.integTest

import org.opensearch.action.admin.cluster.health.ClusterHealthRequest
import org.opensearch.action.admin.cluster.node.info.NodesInfoRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler.bwc
package org.opensearch.integTest.bwc

import org.junit.Assert
import org.opensearch.common.settings.Settings
import org.opensearch.reportsscheduler.PluginRestTestCase
import org.opensearch.integTest.PluginRestTestCase
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
import org.opensearch.reportsscheduler.constructReportDefinitionRequest
import org.opensearch.integTest.constructReportDefinitionRequest
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import java.time.Instant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler.rest
package org.opensearch.integTest.rest

import org.opensearch.reportsscheduler.PluginRestTestCase
import org.opensearch.integTest.PluginRestTestCase
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
import org.opensearch.reportsscheduler.jsonify
import org.opensearch.reportsscheduler.validateErrorResponse
import org.opensearch.reportsscheduler.validateTimeRecency
import org.opensearch.integTest.jsonify
import org.opensearch.integTest.validateErrorResponse
import org.opensearch.integTest.validateTimeRecency
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import org.junit.Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler.rest
package org.opensearch.integTest.rest

import org.opensearch.reportsscheduler.PluginRestTestCase
import org.opensearch.integTest.PluginRestTestCase
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
import org.opensearch.reportsscheduler.jsonify
import org.opensearch.reportsscheduler.validateErrorResponse
import org.opensearch.reportsscheduler.validateTimeNearRefTime
import org.opensearch.reportsscheduler.validateTimeRecency
import org.opensearch.integTest.jsonify
import org.opensearch.integTest.validateErrorResponse
import org.opensearch.integTest.validateTimeNearRefTime
import org.opensearch.integTest.validateTimeRecency
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import org.junit.Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler.rest
package org.opensearch.integTest.rest

import org.opensearch.reportsscheduler.PluginRestTestCase
import org.opensearch.integTest.PluginRestTestCase
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
import org.opensearch.reportsscheduler.constructReportDefinitionRequest
import org.opensearch.reportsscheduler.validateErrorResponse
import org.opensearch.integTest.constructReportDefinitionRequest
import org.opensearch.integTest.validateErrorResponse
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import org.junit.Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.reportsscheduler.rest
package org.opensearch.integTest.rest

import org.opensearch.reportsscheduler.PluginRestTestCase
import org.opensearch.integTest.PluginRestTestCase
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LEGACY_BASE_REPORTS_URI
import org.opensearch.reportsscheduler.constructReportDefinitionRequest
import org.opensearch.reportsscheduler.validateErrorResponse
import org.opensearch.integTest.constructReportDefinitionRequest
import org.opensearch.integTest.validateErrorResponse
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import org.junit.Assert
Expand Down
Loading

0 comments on commit 43bc73f

Please sign in to comment.