diff --git a/dsms-engine-application/src/test/java/com/dsms/common/prometheus/converter/result/QueryDataTypeTest.java b/dsms-engine-application/src/test/java/com/dsms/common/prometheus/converter/result/QueryDataTypeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2b3abb35ee1df700c00a4ab7c84dd9220def1b9b --- /dev/null +++ b/dsms-engine-application/src/test/java/com/dsms/common/prometheus/converter/result/QueryDataTypeTest.java @@ -0,0 +1,48 @@ +package com.dsms.common.prometheus.converter.result; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.dsms.common.util.PrometheusUtils; +import com.google.gson.JsonSyntaxException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class QueryDataTypeTest { + + @Test + void convert() { + String positiveVector = "{\"status\":\"success\",\"data\":{\"resultType\":\"vector\",\"result\":[{\"metric\":{},\"value\":[1742977682.864,\"+Inf\"]}]}}"; + String negativeVector = "{\"status\":\"success\",\"data\":{\"resultType\":\"vector\",\"result\":[{\"metric\":{},\"value\":[1742977682.864,\"-Inf\"]}]}}"; + String illegalVector = "{\"status\":\"success\",\"data\":{\"resultType\":\"vector\",\"result\":[{\"metric\":{},\"value\":[1742977682.864,\"test\"]}]}}"; + + DefaultQueryResult positiveVectorData = PrometheusUtils.convertQueryResultString(positiveVector); + DefaultQueryResult negativeVectorData = PrometheusUtils.convertQueryResultString(negativeVector); + DefaultQueryResult illegalVectorData = PrometheusUtils.convertQueryResultString(illegalVector); + + assertEquals(Double.POSITIVE_INFINITY, positiveVectorData.getResult().get(0).getDataValue().getValue()); + assertEquals(Double.NEGATIVE_INFINITY, negativeVectorData.getResult().get(0).getDataValue().getValue()); + assertEquals(Double.NaN, illegalVectorData.getResult().get(0).getDataValue().getValue()); + + String positiveMatrix = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{},\"values\":[[1742977682.864,\"+Inf\"]]}]}}"; + String negativeMatrix = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{},\"values\":[[1742977682.864,\"-Inf\"]]}]}}"; + String illegalMatrix = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{},\"values\":[[1742977682.864,\"test\"]]}]}}"; + + DefaultQueryResult positiveMatrixData = PrometheusUtils.convertQueryResultString(positiveMatrix); + DefaultQueryResult negativeMatrixData = PrometheusUtils.convertQueryResultString(negativeMatrix); + DefaultQueryResult illegalMatrixData = PrometheusUtils.convertQueryResultString(illegalMatrix); + double[][] positiveMatrixDataQueryResult = positiveMatrixData.getQueryResult(); + double[][] negativeMatrixDataQueryResult = negativeMatrixData.getQueryResult(); + double[][] illegalMatrixDataQueryResult = illegalMatrixData.getQueryResult(); + assertEquals(Double.POSITIVE_INFINITY, positiveMatrixDataQueryResult[0][1]); + assertEquals(Double.NEGATIVE_INFINITY, negativeMatrixDataQueryResult[0][1]); + assertEquals(Double.NaN, illegalMatrixDataQueryResult[0][1]); + + String noValuesMatrixData = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{},\"values1\":[[1742977682.864,\"+Inf\"]]}]}}"; + + Assertions.assertThrows(JsonSyntaxException.class, () -> PrometheusUtils.convertQueryResultString(noValuesMatrixData)); + + String noValues = "{\"status\":\"success\",\"data\":{\"resultType\":\"vector\",\"result\":[{\"metric\":{},\"values1\":[1742977682.864,\"+Inf\"]}]}}"; + Assertions.assertThrows(JsonSyntaxException.class, () -> PrometheusUtils.convertQueryResultString(noValues)); + + } +} \ No newline at end of file