diff --git a/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/pojo/CreateRefResponse.kt b/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/pojo/CreateRefResponse.kt index 021971fde7..e0421029f5 100644 --- a/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/pojo/CreateRefResponse.kt +++ b/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/pojo/CreateRefResponse.kt @@ -27,6 +27,19 @@ package com.tencent.bkrepo.ddc.pojo +import com.tencent.bkrepo.ddc.serialization.CbFieldType +import com.tencent.bkrepo.ddc.serialization.CbObject +import com.tencent.bkrepo.ddc.utils.beginUniformArray +import com.tencent.bkrepo.ddc.utils.writeStringValue + data class CreateRefResponse( val needs: Set -) +) { + fun serialize(): CbObject { + return CbObject.build { writer -> + writer.beginUniformArray(CreateRefResponse::needs.name, CbFieldType.String) + needs.forEach { writer.writeStringValue(it) } + writer.endArray() + } + } +} diff --git a/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/utils/DdcUtils.kt b/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/utils/DdcUtils.kt index f6b7b2f0fc..ded1c3942c 100644 --- a/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/utils/DdcUtils.kt +++ b/src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/utils/DdcUtils.kt @@ -29,8 +29,12 @@ package com.tencent.bkrepo.ddc.utils import com.tencent.bkrepo.ddc.model.TDdcBlob import com.tencent.bkrepo.ddc.model.TDdcRef +import com.tencent.bkrepo.ddc.serialization.CbObject +import org.slf4j.LoggerFactory object DdcUtils { + private val logger = LoggerFactory.getLogger(DdcUtils::class.java) + const val DIR_BLOBS = "blobs" fun TDdcRef.fullPath() = "/$bucket/$key" @@ -38,4 +42,13 @@ object DdcUtils { fun TDdcBlob.fullPath() = "/blobs/$blobId" fun buildRef(bucket: String, key: String): String = "ref/$bucket/$key" + + fun toError(e: Exception, statusCode: Int): Pair { + logger.info("batch op failed:\n${e.stackTraceToString()}") + val obj = CbObject.build { + it.writeString("title", e.message) + it.writeInteger("status", statusCode) + } + return Pair(obj, statusCode) + } } diff --git a/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/BatchOpsResponseTest.kt b/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/BatchOpsResponseTest.kt deleted file mode 100644 index 58741ba969..0000000000 --- a/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/BatchOpsResponseTest.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.tencent.bkrepo.ddc.pojo - -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import com.tencent.bkrepo.ddc.serialization.CbFieldType -import com.tencent.bkrepo.ddc.serialization.CbObject -import com.tencent.bkrepo.ddc.utils.beginUniformArray -import com.tencent.bkrepo.ddc.utils.writeStringValue -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test - -class BatchOpsResponseTest { - @Test - fun test() { - val response = CbObject.build { responseWriter -> - responseWriter.beginUniformArray("needs", CbFieldType.String) - responseWriter.writeStringValue("o1") - responseWriter.writeStringValue("o2") - responseWriter.endArray() - } - - val opResponse1 = OpResponse(1, response, 0) - val opResponse2 = OpResponse(2, response, 0) - val opsResponse = BatchOpsResponse(listOf(opResponse1, opResponse2)) - val opsResCb = opsResponse.serialize() - val json = opsResCb.toJson(jacksonObjectMapper()) - val expectedJson = "{\"results\":[" + - "{\"opId\":1,\"response\":{\"needs\":[\"o1\",\"o2\"]},\"statusCode\":0}," + - "{\"opId\":2,\"response\":{\"needs\":[\"o1\",\"o2\"]},\"statusCode\":0}]" + - "}" - assertEquals(expectedJson, json) - } -} diff --git a/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/BatchOpsTest.kt b/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/BatchOpsTest.kt deleted file mode 100644 index 4d3647f364..0000000000 --- a/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/BatchOpsTest.kt +++ /dev/null @@ -1,55 +0,0 @@ -package com.tencent.bkrepo.ddc.pojo - -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import com.tencent.bkrepo.ddc.serialization.CbFieldType -import com.tencent.bkrepo.ddc.serialization.CbObject -import com.tencent.bkrepo.ddc.serialization.CbWriter -import com.tencent.bkrepo.ddc.utils.beginUniformArray -import com.tencent.bkrepo.ddc.utils.writeBool -import com.tencent.bkrepo.ddc.utils.writeInteger -import com.tencent.bkrepo.ddc.utils.writeString -import com.tencent.bkrepo.ddc.utils.writerObject -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test - -class BatchOpsTest { - @Test - fun test() { - val writer = CbWriter() - writer.beginObject() - writer.beginUniformArray(BatchOps::ops.name, CbFieldType.Object) - writer.beginObject() - writer.writeInteger(BatchOp::opId.name, 0) - writer.writeString(BatchOp::bucket.name, "bucket") - writer.writeString(BatchOp::key.name, "key") - writer.writeString(BatchOp::op.name, Operation.GET.name) - writer.writeBool(BatchOp::resolveAttachments.name, true) - writer.endObject() - - writer.beginObject() - writer.writeInteger(BatchOp::opId.name, 0) - writer.writeString(BatchOp::bucket.name, "bucket") - writer.writeString(BatchOp::key.name, "key") - writer.writeString(BatchOp::op.name, Operation.HEAD.name) - writer.endObject() - - val payload = CbObject.build { innerWriter -> innerWriter.writeString("test", "test value") } - writer.beginObject() - writer.writeInteger(BatchOp::opId.name, 0) - writer.writeString(BatchOp::bucket.name, "bucket") - writer.writeString(BatchOp::key.name, "key") - writer.writeString(BatchOp::op.name, Operation.PUT.name) - writer.writerObject(BatchOp::payload.name, payload) - writer.writeString(BatchOp::payloadHash.name, "test hash") - writer.endObject() - writer.endArray() - writer.endObject() - - val batchOps = BatchOps.deserialize(writer.toByteArray()) - println(batchOps.ops[2].payload!!.toJson(jacksonObjectMapper())) - assertEquals( - "{\"test\":\"test value\"}", - batchOps.ops[2].payload!!.toJson(jacksonObjectMapper()) - ) - } -} diff --git a/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/SerializationTest.kt b/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/SerializationTest.kt new file mode 100644 index 0000000000..e50299f39f --- /dev/null +++ b/src/backend/ddc/biz-ddc/src/test/kotlin/com/tencent/bkrepo/ddc/pojo/SerializationTest.kt @@ -0,0 +1,125 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.bkrepo.ddc.pojo + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.tencent.bkrepo.common.api.constant.HttpStatus +import com.tencent.bkrepo.common.api.exception.ErrorCodeException +import com.tencent.bkrepo.common.api.message.CommonMessageCode +import com.tencent.bkrepo.ddc.serialization.CbFieldType +import com.tencent.bkrepo.ddc.serialization.CbObject +import com.tencent.bkrepo.ddc.serialization.CbWriter +import com.tencent.bkrepo.ddc.utils.DdcUtils +import com.tencent.bkrepo.ddc.utils.beginUniformArray +import com.tencent.bkrepo.ddc.utils.writeBool +import com.tencent.bkrepo.ddc.utils.writeInteger +import com.tencent.bkrepo.ddc.utils.writeString +import com.tencent.bkrepo.ddc.utils.writeStringValue +import com.tencent.bkrepo.ddc.utils.writerObject +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class SerializationTest { + @Test + fun testSerializeBatchOpsResponse() { + val response = CbObject.build { responseWriter -> + responseWriter.beginUniformArray("needs", CbFieldType.String) + responseWriter.writeStringValue("o1") + responseWriter.writeStringValue("o2") + responseWriter.endArray() + } + + val opResponse1 = OpResponse(1, response, 0) + val opResponse2 = OpResponse(2, response, 0) + val opsResponse = BatchOpsResponse(listOf(opResponse1, opResponse2)) + val opsResCb = opsResponse.serialize() + val json = opsResCb.toJson(jacksonObjectMapper()) + val expectedJson = "{\"results\":[" + + "{\"opId\":1,\"response\":{\"needs\":[\"o1\",\"o2\"]},\"statusCode\":0}," + + "{\"opId\":2,\"response\":{\"needs\":[\"o1\",\"o2\"]},\"statusCode\":0}]" + + "}" + assertEquals(expectedJson, json) + } + + @Test + fun testDeserializeBatchOps() { + val writer = CbWriter() + writer.beginObject() + writer.beginUniformArray(BatchOps::ops.name, CbFieldType.Object) + writer.beginObject() + writer.writeInteger(BatchOp::opId.name, 0) + writer.writeString(BatchOp::bucket.name, "bucket") + writer.writeString(BatchOp::key.name, "key") + writer.writeString(BatchOp::op.name, Operation.GET.name) + writer.writeBool(BatchOp::resolveAttachments.name, true) + writer.endObject() + + writer.beginObject() + writer.writeInteger(BatchOp::opId.name, 0) + writer.writeString(BatchOp::bucket.name, "bucket") + writer.writeString(BatchOp::key.name, "key") + writer.writeString(BatchOp::op.name, Operation.HEAD.name) + writer.endObject() + + val payload = CbObject.build { innerWriter -> innerWriter.writeString("test", "test value") } + writer.beginObject() + writer.writeInteger(BatchOp::opId.name, 0) + writer.writeString(BatchOp::bucket.name, "bucket") + writer.writeString(BatchOp::key.name, "key") + writer.writeString(BatchOp::op.name, Operation.PUT.name) + writer.writerObject(BatchOp::payload.name, payload) + writer.writeString(BatchOp::payloadHash.name, "test hash") + writer.endObject() + writer.endArray() + writer.endObject() + + val batchOps = BatchOps.deserialize(writer.toByteArray()) + println(batchOps.ops[2].payload!!.toJson(jacksonObjectMapper())) + assertEquals( + "{\"test\":\"test value\"}", + batchOps.ops[2].payload!!.toJson(jacksonObjectMapper()) + ) + } + + @Test + fun testSerializeCreateRefResponse() { + val res = CreateRefResponse(needs = setOf("a", "b", "c")) + assertEquals("{\"needs\":[\"a\",\"b\",\"c\"]}", res.serialize().toJson(jacksonObjectMapper())) + } + + @Test + fun testSerializeError() { + val e = ErrorCodeException(CommonMessageCode.PARAMETER_INVALID, "test") + val (error, code) = DdcUtils.toError(e, HttpStatus.BAD_REQUEST.value) + assertEquals(HttpStatus.BAD_REQUEST.value, code) + assertEquals( + "{\"title\":\"[250105]system.parameter.invalid\",\"status\":400}", + error.toJson(jacksonObjectMapper()) + ) + } +}