/* * 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) }