From 62b592cbf7e640d3b08aa823ec93c4429e7a3569 Mon Sep 17 00:00:00 2001 From: mingmen Date: Tue, 5 Sep 2023 22:11:00 +0800 Subject: [PATCH] append ut --- .../builtin/handler/InstallBizHandler.java | 7 +- .../sofa/serverless/arklet/core/BaseTest.java | 42 ++++ .../arklet/core/command/CommandTests.java | 23 +-- .../core/command/InstallBizHandlerTests.java | 183 ++++++++++++++++++ .../command/{ => custom}/CustomCommand.java | 2 +- .../{ => custom}/CustomCommandHandler.java | 2 +- .../core/command/{ => custom}/Input.java | 2 +- 7 files changed, 238 insertions(+), 23 deletions(-) create mode 100644 arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/BaseTest.java create mode 100644 arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/InstallBizHandlerTests.java rename arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/{ => custom}/CustomCommand.java (95%) rename arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/{ => custom}/CustomCommandHandler.java (96%) rename arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/{ => custom}/Input.java (94%) diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java index 1c185e83b..79e30320f 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java @@ -20,7 +20,6 @@ import com.alipay.sofa.ark.api.ResponseCode; import com.alipay.sofa.ark.common.util.StringUtils; import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand; -import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler.Input; import com.alipay.sofa.serverless.arklet.core.command.meta.AbstractCommandHandler; import com.alipay.sofa.serverless.arklet.core.command.meta.Command; import com.alipay.sofa.serverless.arklet.core.command.meta.Output; @@ -35,8 +34,10 @@ * @author mingmen * @date 2023/6/8 */ -public class InstallBizHandler extends AbstractCommandHandler implements - ArkBizOps { +public class InstallBizHandler extends + AbstractCommandHandler + implements + ArkBizOps { @Override public Output handle(Input input) { diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/BaseTest.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/BaseTest.java new file mode 100644 index 000000000..7cdc1c070 --- /dev/null +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/BaseTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.serverless.arklet.core; + +import com.alipay.sofa.serverless.arklet.core.command.CommandService; +import com.alipay.sofa.serverless.arklet.core.ops.UnifiedOperationService; +import org.junit.Before; + +/** + * @author mingmen + * @date 2023/9/5 + */ +public class BaseTest { + public static ArkletComponentRegistry componentRegistry; + public static CommandService commandService; + public static UnifiedOperationService operationService; + + @Before + public void setup() { + if (componentRegistry == null) { + ArkletComponentRegistry registry = new ArkletComponentRegistry(); + registry.initComponents(); + componentRegistry = registry; + commandService = ArkletComponentRegistry.getCommandServiceInstance(); + operationService = ArkletComponentRegistry.getOperationServiceInstance(); + } + } +} diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CommandTests.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CommandTests.java index 6d007e96a..28f48e84f 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CommandTests.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CommandTests.java @@ -20,34 +20,23 @@ import java.util.Map; import com.alibaba.fastjson.JSONObject; -import com.alipay.sofa.serverless.arklet.core.ArkletComponentRegistry; + +import com.alipay.sofa.serverless.arklet.core.BaseTest; import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand; import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler; import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.QueryBizOpsHandler; +import com.alipay.sofa.serverless.arklet.core.command.custom.CustomCommand; +import com.alipay.sofa.serverless.arklet.core.command.custom.CustomCommandHandler; import com.alipay.sofa.serverless.arklet.core.command.meta.Output; import com.alipay.sofa.serverless.arklet.core.command.record.ProcessRecord; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; -import org.mockito.MockitoAnnotations; /** * @author mingmen * @date 2023/6/26 */ -public class CommandTests { - - private static CommandService commandService; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - if (commandService == null) { - ArkletComponentRegistry registry = new ArkletComponentRegistry(); - registry.initComponents(); - commandService = ArkletComponentRegistry.getCommandServiceInstance(); - } - } +public class CommandTests extends BaseTest { @Test public void registerCustomCommand() { @@ -58,7 +47,7 @@ public void registerCustomCommand() { } @Test - public void process() throws Exception { + public void commandProcess() throws Exception { Output output = commandService.process(BuiltinCommand.HELP.getId(), new HashMap()); Assert.assertNotNull(output); } diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/InstallBizHandlerTests.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/InstallBizHandlerTests.java new file mode 100644 index 000000000..6a7efe246 --- /dev/null +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/InstallBizHandlerTests.java @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.serverless.arklet.core.command; + +import com.alipay.sofa.ark.api.ClientResponse; +import com.alipay.sofa.ark.api.ResponseCode; +import com.alipay.sofa.serverless.arklet.core.BaseTest; +import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand; +import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler; +import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler.Input; +import com.alipay.sofa.serverless.arklet.core.command.meta.Output; +import com.alipay.sofa.serverless.arklet.core.common.exception.CommandValidationException; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +/** + * @author mingmen + * @date 2023/9/5 + */ +public class InstallBizHandlerTests extends BaseTest { + + ///** + // * 测试用例编号:InstallBizHandlerTests001 + // * 测试方法:handle + // * 测试目的:验证当获取到的ClientResponse的code为SUCCESS时,handle方法返回的Output是否为成功状态 + // * 测试数据:Input对象,其中bizUrl属性为任意非空字符串 + // * 预期结果:handle方法返回的Output对象为成功状态 + // */ + //@Test + //public void testHandle_Success() throws Throwable { + // + // InstallBizHandler handler = (InstallBizHandler)commandService.getHandler(BuiltinCommand.INSTALL_BIZ); + // + // // 准备测试数据 + // Input input = new Input(); + // input.setBizUrl("testUrl"); + // + // ClientResponse response = new ClientResponse(); + // response.setCode(ResponseCode.SUCCESS); + // + // // 设置Mock行为 + // when(operationService.install(anyString())).thenReturn(response); + // + // // 执行测试 + // Output result = handler.handle(input); + // + // // 验证结果 + // assertSame(response, result.getData()); + // assertTrue(result.success()); + //} + + ///** + // * 测试用例编号:InstallBizHandlerTests002 + // * 测试方法:handle + // * 测试目的:验证当获取到的ClientResponse的code不为SUCCESS时,handle方法返回的Output是否为失败状态 + // * 测试数据:Input对象,其中bizUrl属性为任意非空字符串 + // * 预期结果:handle方法返回的Output对象为失败状态 + // */ + //@Test + //public void testHandle_Failure() throws Throwable { + // + // InstallBizHandler handler = (InstallBizHandler)commandService.getHandler(BuiltinCommand.INSTALL_BIZ); + // + // + // // 准备测试数据 + // Input input = new Input(); + // input.setBizUrl("testUrl"); + // + // ClientResponse response = new ClientResponse(); + // response.setCode(ResponseCode.FAILED); + // + // // 设置Mock行为 + // when(operationService.install(anyString())).thenReturn(response); + // + // // 执行测试 + // Output result = handler.handle(input); + // + // // 验证结果 + // assertSame(response, result.getData()); + // assertFalse(result.failed()); + //} + + /** + * 测试用例编号:InstallBizHandlerTests003 + * 测试方法:validate + * 测试目的:验证当input的bizName属性为空字符串时,validate方法会抛出CommandValidationException异常 + * 测试数据:Input对象,其中bizName属性为空字符串 + * 预期结果:validate方法抛出CommandValidationException异常 + */ + @Test(expected = CommandValidationException.class) + public void testValidate_BlankBizName() throws CommandValidationException { + + InstallBizHandler handler = (InstallBizHandler) commandService + .getHandler(BuiltinCommand.INSTALL_BIZ); + // 准备测试数据 + Input input = new Input(); + input.setBizName(""); + + // 执行测试 + handler.validate(input); + } + + /** + * 测试用例编号:InstallBizHandlerTests004 + * 测试方法:validate + * 测试目的:验证当input的bizVersion属性为空字符串时,validate方法会抛出CommandValidationException异常 + * 测试数据:Input对象,其中bizVersion属性为空字符串 + * 预期结果:validate方法抛出CommandValidationException异常 + */ + @Test(expected = CommandValidationException.class) + public void testValidate_BlankBizVersion() throws CommandValidationException { + + InstallBizHandler handler = (InstallBizHandler) commandService + .getHandler(BuiltinCommand.INSTALL_BIZ); + // 准备测试数据 + Input input = new Input(); + input.setBizVersion(""); + + // 执行测试 + handler.validate(input); + } + + /** + * 测试用例编号:InstallBizHandlerTests005 + * 测试方法:validate + * 测试目的:验证当input的isAync属性为true且requestId为空字符串时,validate方法会抛出CommandValidationException异常 + * 测试数据:Input对象,其中isAync属性为true,requestId属性为空字符串 + * 预期结果:validate方法抛出CommandValidationException异常 + */ + @Test(expected = CommandValidationException.class) + public void testValidate_BlankRequestId() throws CommandValidationException { + + InstallBizHandler handler = (InstallBizHandler) commandService + .getHandler(BuiltinCommand.INSTALL_BIZ); + + // 准备测试数据 + Input input = new Input(); + input.setAync(true); + input.setRequestId(""); + + // 执行测试 + handler.validate(input); + } + + /** + * 测试用例编号:InstallBizHandlerTests006 + * 测试方法:validate + * 测试目的:验证当input的bizUrl属性为空字符串时,validate方法会抛出CommandValidationException异常 + * 测试数据:Input对象,其中bizUrl属性为空字符串 + * 预期结果:validate方法抛出CommandValidationException异常 + */ + @Test(expected = CommandValidationException.class) + public void testValidate_BlankBizUrl() throws CommandValidationException { + InstallBizHandler handler = (InstallBizHandler) commandService + .getHandler(BuiltinCommand.INSTALL_BIZ); + + // 准备测试数据 + Input input = new Input(); + input.setBizUrl(""); + + // 执行测试 + handler.validate(input); + } +} diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CustomCommand.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/CustomCommand.java similarity index 95% rename from arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CustomCommand.java rename to arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/CustomCommand.java index 0b492d74b..6bf58e25d 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CustomCommand.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/CustomCommand.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.serverless.arklet.core.command; +package com.alipay.sofa.serverless.arklet.core.command.custom; import com.alipay.sofa.serverless.arklet.core.command.meta.Command; diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CustomCommandHandler.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/CustomCommandHandler.java similarity index 96% rename from arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CustomCommandHandler.java rename to arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/CustomCommandHandler.java index 844c005b7..532e96236 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/CustomCommandHandler.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/CustomCommandHandler.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.serverless.arklet.core.command; +package com.alipay.sofa.serverless.arklet.core.command.custom; import com.alipay.sofa.ark.common.util.StringUtils; import com.alipay.sofa.serverless.arklet.core.command.meta.AbstractCommandHandler; diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/Input.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/Input.java similarity index 94% rename from arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/Input.java rename to arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/Input.java index b514db043..b850136b9 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/Input.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/custom/Input.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.serverless.arklet.core.command; +package com.alipay.sofa.serverless.arklet.core.command.custom; import com.alipay.sofa.serverless.arklet.core.command.meta.InputMeta;