Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
append ut
Browse files Browse the repository at this point in the history
  • Loading branch information
TomorJM committed Sep 5, 2023
1 parent 13add48 commit 62b592c
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,8 +34,10 @@
* @author mingmen
* @date 2023/6/8
*/
public class InstallBizHandler extends AbstractCommandHandler<Input, ClientResponse> implements
ArkBizOps {
public class InstallBizHandler extends
AbstractCommandHandler<InstallBizHandler.Input, ClientResponse>
implements
ArkBizOps {

@Override
public Output<ClientResponse> handle(Input input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ClientResponse> 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<ClientResponse> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 62b592c

Please sign in to comment.