diff --git a/tests/openapi/src/test/java/com/tencent/bk/job/api/util/ApiUtil.java b/tests/openapi/src/test/java/com/tencent/bk/job/api/util/ApiUtil.java index 5e77e1e06f..9466773053 100644 --- a/tests/openapi/src/test/java/com/tencent/bk/job/api/util/ApiUtil.java +++ b/tests/openapi/src/test/java/com/tencent/bk/job/api/util/ApiUtil.java @@ -75,17 +75,6 @@ public static ResponseSpecification successResponseSpec() { return builder.build(); } - /** - * 失败响应通用设置 - */ - public static ResponseSpecification failResponseSpec() { - ResponseSpecBuilder builder = new ResponseSpecBuilder(); - builder.expectStatusCode(200); - builder.expectBody("result", equalTo(false)); - builder.expectBody("code", equalTo(ErrorCode.BAD_REQUEST)); - return builder.build(); - } - /** * 失败响应通用设置 */ diff --git a/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/PublicScriptResourceAPITest.java b/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/PublicScriptResourceAPITest.java index b8cb9c2266..57a7eee1e6 100644 --- a/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/PublicScriptResourceAPITest.java +++ b/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/PublicScriptResourceAPITest.java @@ -53,11 +53,6 @@ static void tearDown() { EsbDeletePublicScriptV3Req req = new EsbDeletePublicScriptV3Req(); req.setScriptId(script.getScriptId()); Operations.deletePublicScript(req); - // 清理脚本版本 - EsbDeletePublicScriptVersionV3Req req1 = new EsbDeletePublicScriptVersionV3Req(); - req1.setScriptId(script.getScriptId()); - req1.setScriptVersionId(script.getId()); - Operations.deletePublicScriptVersion(req1); }); } } @@ -108,19 +103,20 @@ void testCreatePublicScript() { @Test @DisplayName("创建公共脚本异常场景测试-比如参数校验,业务逻辑等") void givenInvalidCreatePublicScriptParamThenFail() { + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbCreatePublicScriptV3Req req = new EsbCreatePublicScriptV3Req(); - req.setContent(SHELL_SCRIPT_CONTENT_BASE64); - req.setDescription(TestValueGenerator.generateUniqueStrValue("shell_script_desc", 50)); - req.setType(ScriptTypeEnum.SHELL.getValue()); - req.setVersion("v1"); - req.setVersionDesc("v1_desc"); + req.setContent(Base64Util.base64EncodeContentToStr(createdPublicScript.getContent())); + req.setDescription(createdPublicScript.getDescription()); + req.setType(createdPublicScript.getType()); + req.setVersion("v2"); + req.setVersionDesc("v2_desc"); // 脚本名称为空,创建失败 req.setName(null); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 脚本名称有特殊字符,创建失败 req.setName(TestValueGenerator.generateUniqueStrValue("*<>", 50)); @@ -128,7 +124,7 @@ void givenInvalidCreatePublicScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); req.setName(TestValueGenerator.generateUniqueStrValue("shell_script", 50)); // 脚本类型非法,创建失败 @@ -137,7 +133,7 @@ void givenInvalidCreatePublicScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); req.setType(ScriptTypeEnum.SHELL.getValue()); // 脚本内容为空,创建失败 @@ -146,14 +142,14 @@ void givenInvalidCreatePublicScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号为空, 创建失败 req.setVersion(null); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号有特殊字符, 创建失败 req.setVersion("|"); @@ -161,20 +157,19 @@ void givenInvalidCreatePublicScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); + createdPublicScriptList.add(createdPublicScript); } @Test @DisplayName("测试公共脚本版本创建") void testCreatePublicScriptVersion() { + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbCreatePublicScriptVersionV3Req req = new EsbCreatePublicScriptVersionV3Req(); - req.setContent(SHELL_SCRIPT_CONTENT_BASE64); + req.setContent(Base64Util.base64EncodeContentToStr(createdPublicScript.getContent())); req.setVersion("v2"); req.setVersionDesc("v2_desc"); - if (CollectionUtils.isEmpty(createdPublicScriptList)){ - testCreatePublicScript(); - } - req.setScriptId(createdPublicScriptList.get(0).getScriptId()); + req.setScriptId(createdPublicScript.getScriptId()); EsbScriptVersionDetailV3DTO createdScript = given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) @@ -184,7 +179,7 @@ void testCreatePublicScriptVersion() { .spec(ApiUtil.successResponseSpec()) .body("data", notNullValue()) .body("data.id", greaterThan(0)) - .body("data.script_id", notNullValue()) + .body("data.script_id", equalTo(req.getScriptId())) .body("data.content", equalTo(Base64Util.base64DecodeContentToStr(req.getContent()))) .body("data.creator", equalTo(TestProps.DEFAULT_TEST_USER)) .body("data.create_time", greaterThan(0L)) @@ -198,19 +193,17 @@ void testCreatePublicScriptVersion() { .as(new TypeRef>() { }) .getData(); - createdPublicScriptList.add(createdScript); + createdPublicScriptList.add(createdPublicScript); } @Test @DisplayName("创建公共脚本版本异常场景测试-比如参数校验,业务逻辑等") void givenInvalidCreatePublicScriptVersionParamThenFail() { + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbCreatePublicScriptVersionV3Req req = new EsbCreatePublicScriptVersionV3Req(); - req.setContent(SHELL_SCRIPT_CONTENT_BASE64); + req.setContent(Base64Util.base64EncodeContentToStr(createdPublicScript.getContent())); req.setVersion("v2"); req.setVersionDesc("v2_desc"); - if (CollectionUtils.isEmpty(createdPublicScriptList)){ - testCreatePublicScript(); - } // 脚本ID为空,创建失败 req.setScriptId(null); @@ -219,16 +212,16 @@ void givenInvalidCreatePublicScriptVersionParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 脚本内容为空,创建失败 - req.setScriptId(createdPublicScriptList.get(0).getScriptId()); + req.setScriptId(createdPublicScript.getScriptId()); req.setContent(null); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号为空,创建失败 req.setContent(SHELL_SCRIPT_CONTENT_BASE64); req.setVersion(null); @@ -237,7 +230,7 @@ void givenInvalidCreatePublicScriptVersionParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号有特殊字符,创建失败 req.setVersion("v2?"); given() @@ -245,15 +238,16 @@ void givenInvalidCreatePublicScriptVersionParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 同一脚本下,脚本版本号重复,创建失败 - req.setVersion(createdPublicScriptList.get(0).getVersion()); + req.setVersion(createdPublicScript.getVersion()); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_PUBLIC_SCRIPT_VERSION) .then() .spec(ApiUtil.failResponseSpec(ErrorCode.SCRIPT_VERSION_NAME_EXIST)); + createdPublicScriptList.add(createdPublicScript); } } @@ -262,14 +256,9 @@ class GetTest { @Test @DisplayName("测试获取公共脚本版本列表") void testGetPublicScriptVersionList() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdPublicScriptList)) { - scriptVersionDTO = createdPublicScriptList.get(0); - } else { - scriptVersionDTO = Operations.createPublicScript(); - } + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbGetPublicScriptVersionListV3Request req = new EsbGetPublicScriptVersionListV3Request(); - req.setScriptId(scriptVersionDTO.getScriptId()); + req.setScriptId(createdPublicScript.getScriptId()); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.GET_PUBLIC_SCRIPT_VERSION_LIST) @@ -277,6 +266,7 @@ void testGetPublicScriptVersionList() { .spec(ApiUtil.successResponseSpec()) .body("data", notNullValue()) .body("data.total", greaterThan(0)); + createdPublicScriptList.add(createdPublicScript); } } @@ -286,14 +276,9 @@ class UpdateTest { @Test @DisplayName("测试更新公共脚本基础信息") void testUpdatePublicScriptBasic() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdPublicScriptList)) { - scriptVersionDTO = createdPublicScriptList.get(0); - } else { - scriptVersionDTO = Operations.createPublicScript(); - } + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbUpdatePublicScriptBasicV3Req req = new EsbUpdatePublicScriptBasicV3Req(); - req.setScriptId(scriptVersionDTO.getScriptId()); + req.setScriptId(createdPublicScript.getScriptId()); req.setName(TestValueGenerator.generateUniqueStrValue("shell_script", 50)); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) @@ -308,21 +293,16 @@ void testUpdatePublicScriptBasic() { .body("data.create_time", greaterThan(0L)) .body("data.last_modify_user", equalTo(TestProps.DEFAULT_TEST_USER)) .body("data.last_modify_time", greaterThan(0L)); + createdPublicScriptList.add(createdPublicScript); } @Test @DisplayName("测试更新公共脚本版本信息") void testUpdatePublicScriptVersion() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdPublicScriptList)) { - scriptVersionDTO = createdPublicScriptList.get(0); - } else { - scriptVersionDTO = Operations.createPublicScript(); - } - + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbUpdatePublicScriptVersionV3Req req = new EsbUpdatePublicScriptVersionV3Req(); - req.setScriptId(scriptVersionDTO.getScriptId()); - req.setScriptVersionId(scriptVersionDTO.getId()); + req.setScriptId(createdPublicScript.getScriptId()); + req.setScriptVersionId(createdPublicScript.getId()); req.setContent(SHELL_SCRIPT_CONTENT_BASE64); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) @@ -341,6 +321,7 @@ void testUpdatePublicScriptVersion() { .body("data.last_modify_time", greaterThan(0L)) .body("data.version", notNullValue()) .body("data.status", notNullValue()); + createdPublicScriptList.add(createdPublicScript); } } @@ -350,15 +331,10 @@ class OperationTest { @Test @DisplayName("测试上线公共脚本版本") void testPublishPublicScriptVersion() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdPublicScriptList)) { - scriptVersionDTO = createdPublicScriptList.get(0); - } else { - scriptVersionDTO = Operations.createPublicScript(); - } + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbManagePublicScriptVersionV3Req req = new EsbManagePublicScriptVersionV3Req(); - req.setScriptId(scriptVersionDTO.getScriptId()); - req.setScriptVersionId(scriptVersionDTO.getId()); + req.setScriptId(createdPublicScript.getScriptId()); + req.setScriptVersionId(createdPublicScript.getId()); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.PUBLISH_PUBLIC_SCRIPT_VERSION) @@ -367,37 +343,49 @@ void testPublishPublicScriptVersion() { .body("data.status", equalTo(JobResourceStatusEnum.ONLINE.getValue())) .body("data.id", equalTo(req.getScriptVersionId().intValue())) .body("data.script_id", equalTo(req.getScriptId())); + createdPublicScriptList.add(createdPublicScript); } @Test @DisplayName("测试禁用公共脚本版本") void testDisablePublicScriptVersion() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdPublicScriptList)) { - scriptVersionDTO = createdPublicScriptList.get(0); - } else { - scriptVersionDTO = Operations.createPublicScript(); - } + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbManagePublicScriptVersionV3Req req = new EsbManagePublicScriptVersionV3Req(); - req.setScriptId(scriptVersionDTO.getScriptId()); - req.setScriptVersionId(scriptVersionDTO.getId()); - if (scriptVersionDTO.getStatus() == JobResourceStatusEnum.ONLINE.getValue() - || scriptVersionDTO.getStatus() == JobResourceStatusEnum.OFFLINE.getValue()) { - given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) - .body(JsonUtil.toJson(req)) - .post(APIV3Urls.DISABLE_PUBLIC_SCRIPT_VERSION) - .then() - .spec(ApiUtil.successResponseSpec()) - .body("data.status", equalTo(JobResourceStatusEnum.DISABLED.getValue())) - .body("data.script_version_id", equalTo(req.getScriptVersionId().intValue())) - .body("data.script_id", equalTo(req.getScriptId())); - } else { - given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) - .body(JsonUtil.toJson(req)) - .post(APIV3Urls.DISABLE_PUBLIC_SCRIPT_VERSION) - .then() - .spec(ApiUtil.failResponseSpec(ErrorCode.UNSUPPORTED_OPERATION)); - } + req.setScriptId(createdPublicScript.getScriptId()); + req.setScriptVersionId(createdPublicScript.getId()); + given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) + .body(JsonUtil.toJson(req)) + .post(APIV3Urls.PUBLISH_PUBLIC_SCRIPT_VERSION) + .then() + .spec(ApiUtil.successResponseSpec()) + .body("data.status", equalTo(JobResourceStatusEnum.ONLINE.getValue())) + .body("data.id", equalTo(req.getScriptVersionId().intValue())) + .body("data.script_id", equalTo(req.getScriptId())); + + given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) + .body(JsonUtil.toJson(req)) + .post(APIV3Urls.DISABLE_PUBLIC_SCRIPT_VERSION) + .then() + .spec(ApiUtil.successResponseSpec()) + .body("data.status", equalTo(JobResourceStatusEnum.DISABLED.getValue())) + .body("data.id", equalTo(req.getScriptVersionId().intValue())) + .body("data.script_id", equalTo(req.getScriptId())); + createdPublicScriptList.add(createdPublicScript); + } + + @Test + @DisplayName("测试禁用公共脚本版本-不支持操作") + void testDisablePublicScriptVersionNotSupport() { + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); + EsbManagePublicScriptVersionV3Req req = new EsbManagePublicScriptVersionV3Req(); + req.setScriptId(createdPublicScript.getScriptId()); + req.setScriptVersionId(createdPublicScript.getId()); + given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) + .body(JsonUtil.toJson(req)) + .post(APIV3Urls.DISABLE_PUBLIC_SCRIPT_VERSION) + .then() + .spec(ApiUtil.failResponseSpec(ErrorCode.UNSUPPORTED_OPERATION)); + createdPublicScriptList.add(createdPublicScript); } } @@ -406,9 +394,9 @@ class DeleteTest { @Test @DisplayName("测试公共脚本删除") void testDeletePublicScript() { - EsbScriptVersionDetailV3DTO createdScript = Operations.createPublicScript(); + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbDeletePublicScriptV3Req req = new EsbDeletePublicScriptV3Req(); - req.setScriptId(createdScript.getScriptId()); + req.setScriptId(createdPublicScript.getScriptId()); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) @@ -421,10 +409,10 @@ void testDeletePublicScript() { @Test @DisplayName("测试公共脚本版本删除") void testDeletePublicScriptVersion() { - EsbScriptVersionDetailV3DTO createdScript = Operations.createPublicScript(); + EsbScriptVersionDetailV3DTO createdPublicScript = Operations.createPublicScript(); EsbDeletePublicScriptVersionV3Req req = new EsbDeletePublicScriptVersionV3Req(); - req.setScriptId(createdScript.getScriptId()); - req.setScriptVersionId(createdScript.getId()); + req.setScriptId(createdPublicScript.getScriptId()); + req.setScriptVersionId(createdPublicScript.getId()); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) @@ -432,8 +420,7 @@ void testDeletePublicScriptVersion() { .post(APIV3Urls.DELETE_PUBLIC_SCRIPT_VERSION) .then() .spec(ApiUtil.successResponseSpec()); + createdPublicScriptList.add(createdPublicScript); } } - - } diff --git a/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/ScriptResourceAPITest.java b/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/ScriptResourceAPITest.java index 0c876002e3..31fae2503c 100644 --- a/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/ScriptResourceAPITest.java +++ b/tests/openapi/src/test/java/com/tencent/bk/job/api/v3/testcase/ScriptResourceAPITest.java @@ -54,13 +54,6 @@ static void tearDown() { req.setScopeType(script.getScopeType()); req.setScriptId(script.getScriptId()); Operations.deleteScript(req); - // 清理脚本版本 - EsbDeleteScriptVersionV3Req req1 = new EsbDeleteScriptVersionV3Req(); - req1.setScopeId(script.getScopeId()); - req1.setScopeType(script.getScopeType()); - req1.setScriptId(script.getScriptId()); - req1.setScriptVersionId(script.getId()); - Operations.deleteScriptVersion(req1); }); } } @@ -121,15 +114,15 @@ void givenInvalidCreateScriptParamThenFail() { req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue()); req.setDescription(TestValueGenerator.generateUniqueStrValue("shell_script_desc", 50)); req.setType(ScriptTypeEnum.SHELL.getValue()); - req.setVersion("v1"); - req.setVersionDesc("v1_desc"); + req.setVersion("v2"); + req.setVersionDesc("v2_desc"); // 脚本名称为空,创建失败 req.setName(null); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 脚本名称有特殊字符,创建失败 req.setName(TestValueGenerator.generateUniqueStrValue("*<>", 50)); @@ -137,7 +130,7 @@ void givenInvalidCreateScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); req.setName(TestValueGenerator.generateUniqueStrValue("shell_script", 50)); // 脚本类型非法,创建失败 @@ -146,7 +139,7 @@ void givenInvalidCreateScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); req.setType(ScriptTypeEnum.SHELL.getValue()); // 脚本内容为空,创建失败 @@ -155,14 +148,14 @@ void givenInvalidCreateScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号为空, 创建失败 req.setVersion(null); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号有特殊字符, 创建失败 req.setVersion("|"); @@ -170,23 +163,22 @@ void givenInvalidCreateScriptParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); } @Test @DisplayName("测试脚本版本创建") void testCreateScriptVersion() { + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); + EsbCreateScriptVersionV3Req req = new EsbCreateScriptVersionV3Req(); - req.setContent(SHELL_SCRIPT_CONTENT_BASE64); - req.setScopeId(String.valueOf(TestProps.DEFAULT_BIZ)); - req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue()); + req.setContent(Base64Util.base64EncodeContentToStr(createdScript.getContent())); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); req.setVersion("v2"); req.setVersionDesc("v2_desc"); - if (CollectionUtils.isEmpty(createdScriptList)){ - testCreateScript(); - } - req.setScriptId(createdScriptList.get(0).getScriptId()); - EsbScriptVersionDetailV3DTO createdScript = + req.setScriptId(createdScript.getScriptId()); + EsbScriptVersionDetailV3DTO createdScriptNew = given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) @@ -195,7 +187,7 @@ void testCreateScriptVersion() { .spec(ApiUtil.successResponseSpec()) .body("data", notNullValue()) .body("data.id", greaterThan(0)) - .body("data.script_id", notNullValue()) + .body("data.script_id", equalTo(req.getScriptId())) .body("data.bk_scope_type", equalTo(req.getScopeType())) .body("data.bk_scope_id", equalTo(req.getScopeId())) .body("data.content", equalTo(Base64Util.base64DecodeContentToStr(req.getContent()))) @@ -211,21 +203,19 @@ void testCreateScriptVersion() { .as(new TypeRef>() { }) .getData(); - createdScriptList.add(createdScript); + createdScriptList.add(createdScriptNew); } @Test @DisplayName("创建脚本版本异常场景测试-比如参数校验,业务逻辑等") void givenInvalidCreateScriptVersionParamThenFail() { + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); EsbCreateScriptVersionV3Req req = new EsbCreateScriptVersionV3Req(); - req.setContent(SHELL_SCRIPT_CONTENT_BASE64); - req.setScopeId(String.valueOf(TestProps.DEFAULT_BIZ)); - req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue()); + req.setContent(Base64Util.base64EncodeContentToStr(createdScript.getContent())); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); req.setVersion("v2"); req.setVersionDesc("v2_desc"); - if (CollectionUtils.isEmpty(createdScriptList)){ - testCreateScript(); - } // 脚本ID为空,创建失败 req.setScriptId(null); @@ -234,25 +224,25 @@ void givenInvalidCreateScriptVersionParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 脚本内容为空,创建失败 - req.setScriptId(createdScriptList.get(0).getScriptId()); + req.setScriptId(createdScript.getScriptId()); req.setContent(null); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号为空,创建失败 - req.setContent(SHELL_SCRIPT_CONTENT_BASE64); + req.setContent(Base64Util.base64EncodeContentToStr(createdScript.getContent())); req.setVersion(null); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 版本号有特殊字符,创建失败 req.setVersion("v2?"); given() @@ -260,15 +250,16 @@ void givenInvalidCreateScriptVersionParamThenFail() { .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT_VERSION) .then() - .spec(ApiUtil.failResponseSpec()); + .spec(ApiUtil.failResponseSpec(ErrorCode.BAD_REQUEST)); // 同一脚本下,脚本版本号重复,创建失败 - req.setVersion(createdScriptList.get(0).getVersion()); + req.setVersion(createdScript.getVersion()); given() .spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.CREATE_SCRIPT_VERSION) .then() .spec(ApiUtil.failResponseSpec(ErrorCode.SCRIPT_VERSION_NAME_EXIST)); + createdScriptList.add(createdScript); } } @@ -277,16 +268,11 @@ class GetTest { @Test @DisplayName("测试获取脚本版本列表") void testGetScriptVersionList() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdScriptList)) { - scriptVersionDTO = createdScriptList.get(0); - } else { - scriptVersionDTO = Operations.createScript(); - } + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); EsbGetScriptVersionListV3Req req = new EsbGetScriptVersionListV3Req(); - req.setScopeId(String.valueOf(TestProps.DEFAULT_BIZ)); - req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue()); - req.setScriptId(scriptVersionDTO.getScriptId()); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); + req.setScriptId(createdScript.getScriptId()); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.GET_SCRIPT_VERSION_LIST) @@ -294,6 +280,7 @@ void testGetScriptVersionList() { .spec(ApiUtil.successResponseSpec()) .body("data", notNullValue()) .body("data.total", greaterThan(0)); + createdScriptList.add(createdScript); } } @@ -303,16 +290,11 @@ class UpdateTest { @Test @DisplayName("测试更新脚本基础信息") void testUpdateScriptBasic() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdScriptList)) { - scriptVersionDTO = createdScriptList.get(0); - } else { - scriptVersionDTO = Operations.createScript(); - } + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); EsbUpdateScriptBasicV3Req req = new EsbUpdateScriptBasicV3Req(); - req.setScopeId(scriptVersionDTO.getScopeId()); - req.setScopeType(scriptVersionDTO.getScopeType()); - req.setScriptId(scriptVersionDTO.getScriptId()); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); + req.setScriptId(createdScript.getScriptId()); req.setName(TestValueGenerator.generateUniqueStrValue("shell_script", 50)); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) @@ -329,23 +311,18 @@ void testUpdateScriptBasic() { .body("data.create_time", greaterThan(0L)) .body("data.last_modify_user", equalTo(TestProps.DEFAULT_TEST_USER)) .body("data.last_modify_time", greaterThan(0L)); + createdScriptList.add(createdScript); } @Test @DisplayName("测试更新脚本版本信息") void testUpdateScriptVersion() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdScriptList)) { - scriptVersionDTO = createdScriptList.get(0); - } else { - scriptVersionDTO = Operations.createScript(); - } - + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); EsbUpdateScriptVersionV3Req req = new EsbUpdateScriptVersionV3Req(); - req.setScopeId(scriptVersionDTO.getScopeId()); - req.setScopeType(scriptVersionDTO.getScopeType()); - req.setScriptId(scriptVersionDTO.getScriptId()); - req.setScriptVersionId(scriptVersionDTO.getId()); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); + req.setScriptId(createdScript.getScriptId()); + req.setScriptVersionId(createdScript.getId()); req.setContent(SHELL_SCRIPT_CONTENT_BASE64); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) @@ -366,6 +343,7 @@ void testUpdateScriptVersion() { .body("data.last_modify_time", greaterThan(0L)) .body("data.version", notNullValue()) .body("data.status", notNullValue()); + createdScriptList.add(createdScript); } } @@ -375,17 +353,12 @@ class OperationTest { @Test @DisplayName("测试上线脚本版本") void testPublishScriptVersion() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdScriptList)) { - scriptVersionDTO = createdScriptList.get(0); - } else { - scriptVersionDTO = Operations.createScript(); - } + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); EsbManageScriptVersionV3Req req = new EsbManageScriptVersionV3Req(); - req.setScopeId(scriptVersionDTO.getScopeId()); - req.setScopeType(scriptVersionDTO.getScopeType()); - req.setScriptId(scriptVersionDTO.getScriptId()); - req.setScriptVersionId(scriptVersionDTO.getId()); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); + req.setScriptId(createdScript.getScriptId()); + req.setScriptVersionId(createdScript.getId()); given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) .body(JsonUtil.toJson(req)) .post(APIV3Urls.PUBLISH_SCRIPT_VERSION) @@ -394,39 +367,55 @@ void testPublishScriptVersion() { .body("data.status", equalTo(JobResourceStatusEnum.ONLINE.getValue())) .body("data.id", equalTo(req.getScriptVersionId().intValue())) .body("data.script_id", equalTo(req.getScriptId())); + createdScriptList.add(createdScript); } @Test @DisplayName("测试禁用脚本版本") void testDisableScriptVersion() { - EsbScriptVersionDetailV3DTO scriptVersionDTO; - if (CollectionUtils.isNotEmpty(createdScriptList)) { - scriptVersionDTO = createdScriptList.get(0); - } else { - scriptVersionDTO = Operations.createScript(); - } + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); EsbManageScriptVersionV3Req req = new EsbManageScriptVersionV3Req(); - req.setScopeId(scriptVersionDTO.getScopeId()); - req.setScopeType(scriptVersionDTO.getScopeType()); - req.setScriptId(scriptVersionDTO.getScriptId()); - req.setScriptVersionId(scriptVersionDTO.getId()); - if (scriptVersionDTO.getStatus() == JobResourceStatusEnum.ONLINE.getValue() - || scriptVersionDTO.getStatus() == JobResourceStatusEnum.OFFLINE.getValue()) { - given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) - .body(JsonUtil.toJson(req)) - .post(APIV3Urls.DISABLE_SCRIPT_VERSION) - .then() - .spec(ApiUtil.successResponseSpec()) - .body("data.status", equalTo(JobResourceStatusEnum.DISABLED.getValue())) - .body("data.script_version_id", equalTo(req.getScriptVersionId().intValue())) - .body("data.script_id", equalTo(req.getScriptId())); - } else { - given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) - .body(JsonUtil.toJson(req)) - .post(APIV3Urls.DISABLE_SCRIPT_VERSION) - .then() - .spec(ApiUtil.failResponseSpec(ErrorCode.UNSUPPORTED_OPERATION)); - } + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); + req.setScriptId(createdScript.getScriptId()); + req.setScriptVersionId(createdScript.getId()); + // 上线版本 + given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) + .body(JsonUtil.toJson(req)) + .post(APIV3Urls.PUBLISH_SCRIPT_VERSION) + .then() + .spec(ApiUtil.successResponseSpec()) + .body("data.status", equalTo(JobResourceStatusEnum.ONLINE.getValue())) + .body("data.id", equalTo(req.getScriptVersionId().intValue())) + .body("data.script_id", equalTo(req.getScriptId())); + // 禁用版本 + given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) + .body(JsonUtil.toJson(req)) + .post(APIV3Urls.DISABLE_SCRIPT_VERSION) + .then() + .spec(ApiUtil.successResponseSpec()) + .body("data.status", equalTo(JobResourceStatusEnum.DISABLED.getValue())) + .body("data.id", equalTo(req.getScriptVersionId().intValue())) + .body("data.script_id", equalTo(req.getScriptId())); + createdScriptList.add(createdScript); + } + + @Test + @DisplayName("测试禁用脚本版本-不支持操作") + void testDisableScriptVersionNotSupport() { + // 只能禁用已上线、已下线状态的版本,否则返回“不支持的操作” + EsbScriptVersionDetailV3DTO createdScript = Operations.createScript(); + EsbManageScriptVersionV3Req req = new EsbManageScriptVersionV3Req(); + req.setScopeId(createdScript.getScopeId()); + req.setScopeType(createdScript.getScopeType()); + req.setScriptId(createdScript.getScriptId()); + req.setScriptVersionId(createdScript.getId()); + given().spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER)) + .body(JsonUtil.toJson(req)) + .post(APIV3Urls.DISABLE_SCRIPT_VERSION) + .then() + .spec(ApiUtil.failResponseSpec(ErrorCode.UNSUPPORTED_OPERATION)); + createdScriptList.add(createdScript); } } @@ -465,8 +454,7 @@ void testDeleteScriptVersion() { .post(APIV3Urls.DELETE_SCRIPT_VERSION) .then() .spec(ApiUtil.successResponseSpec()); + createdScriptList.add(createdScript); } } - - }