Skip to content

Commit

Permalink
test: Increase [integration-tx-api] module test case coverage (#6533)
Browse files Browse the repository at this point in the history
  • Loading branch information
azatyamanaev authored Jul 29, 2024
1 parent 20cd962 commit 7d3cf99
Show file tree
Hide file tree
Showing 17 changed files with 781 additions and 2 deletions.
4 changes: 3 additions & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6702](https://github.com/apache/incubator-seata/pull/6702)] fix REC security vulnerability during Raft deserialization

### test:
- [[#6533](https://github.com/apache/incubator-seata/pull/6533)] increase integration-tx-api module unit test coverage
- [[#6608](https://github.com/apache/incubator-seata/pull/6608)] add unit test for sql-parser-core
- [[#6647](https://github.com/apache/incubator-seata/pull/6647)] improve the test case coverage of saga module to 70%


Thanks to these contributors for their code commits. Please report an unintended omission.

<!-- Please make sure your Github ID is in the list below -->
Expand All @@ -57,11 +57,13 @@ Thanks to these contributors for their code commits. Please report an unintended
- [traitsisgiorgos](https://github.com/traitsisgiorgos)
- [wanghongzhou](https://github.com/wanghongzhou)
- [ggbocoder](https://github.com/ggbocoder)
- [azatyamanaev](https://github.com/azatyamanaev)
- [xjlgod](https://github.com/xjlgod)
- [xingfudeshi](https://github.com/xingfudeshi)
- [wuwen5](https://github.com/wuwen5)
- [iAmClever](https://github.com/iAmClever)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [liuqiufeng](https://github.com/liuqiufeng)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
5 changes: 5 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- [[#6702](https://github.com/apache/incubator-seata/pull/6702)] 修复Raft反序列化时存在RCE安全漏洞

### test:
- [[#6533](https://github.com/apache/incubator-seata/pull/6533)] 增加 Integration-TX-API 模块单元测试覆盖范围
- [[#6608](https://github.com/apache/incubator-seata/pull/6608)] 添加sql-parser-core模块测试用例
- [[#6647](https://github.com/apache/incubator-seata/pull/6647)] 增加saga模块的测试用例覆盖率

Expand All @@ -60,11 +61,15 @@
- [traitsisgiorgos](https://github.com/traitsisgiorgos)
- [wanghongzhou](https://github.com/wanghongzhou)
- [ggbocoder](https://github.com/ggbocoder)
- [azatyamanaev](https://github.com/azatyamanaev)
- [xjlgod](https://github.com/xjlgod)
- [xingfudeshi](https://github.com/xingfudeshi)
- [wuwen5](https://github.com/wuwen5)
- [iAmClever](https://github.com/iAmClever)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [liuqiufeng](https://github.com/liuqiufeng)



同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String toString() {
sb.append("[");
sb.append("isSuccess:").append(isSuccess);
if (StringUtils.isNotBlank(message)) {
sb.append(", msg").append(message);
sb.append(", msg:").append(message);
}
sb.append("]");
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 org.apache.seata.integration.tx.api.interceptor;

import java.util.HashMap;
import java.util.Map;

import org.apache.seata.common.Constants;
import org.apache.seata.core.model.BranchType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TwoPhaseBusinessActionParamTest {

private TwoPhaseBusinessActionParam actionParam;

@BeforeEach
public void setUp() {
actionParam = new TwoPhaseBusinessActionParam();
}

@Test
public void testGetActionName() {
actionParam.setActionName("business_action");
assertEquals("business_action", actionParam.getActionName());
}

@Test
public void testIsReportDelayed() {
actionParam.setDelayReport(true);
assertTrue(actionParam.getDelayReport());
}

@Test
public void testIsCommonFenceUsed() {
actionParam.setUseCommonFence(true);
assertTrue(actionParam.getUseCommonFence());
}

@Test
public void testFillBusinessActionContext() {
Map<String, Object> businessActionContextMap = new HashMap<>(2);
businessActionContextMap.put(Constants.COMMIT_METHOD, "commit");
businessActionContextMap.put(Constants.USE_COMMON_FENCE, false);

actionParam.setBusinessActionContext(businessActionContextMap);

assertEquals("commit", actionParam.getBusinessActionContext().get(Constants.COMMIT_METHOD));
assertFalse((Boolean) actionParam.getBusinessActionContext().get(Constants.USE_COMMON_FENCE));
}

@Test
public void testGetBranchType() {
actionParam.setBranchType(BranchType.TCC);
assertEquals(BranchType.TCC, actionParam.getBranchType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 org.apache.seata.integration.tx.api.interceptor.parser;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class IfNeedEnhanceBeanTest {

private IfNeedEnhanceBean enhanceBean;


@BeforeEach
public void setUp() {
enhanceBean = new IfNeedEnhanceBean();
}

@Test
public void testIsEnhanceNeeded() {
enhanceBean.setIfNeed(true);
assertTrue(enhanceBean.isIfNeed());
}

@Test
public void testGetNeedEnhanceEnum() {
enhanceBean.setNeedEnhanceEnum(NeedEnhanceEnum.SERVICE_BEAN);
assertEquals(NeedEnhanceEnum.SERVICE_BEAN, enhanceBean.getNeedEnhanceEnum());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 org.apache.seata.integration.tx.api.json;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonParserImpl implements JsonParser {

private final ObjectMapper mapper = new ObjectMapper();

@Override
public String toJSONString(Object object) throws IOException {
return mapper.writeValueAsString(object);
}

@Override
public <T> T parseObject(String text, Class<T> clazz) throws IOException {
return mapper.readValue(text, clazz);
}

@Override
public String getName() {
return "customParser";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 org.apache.seata.integration.tx.api.json;

import org.apache.seata.common.exception.JsonParseException;
import org.apache.seata.core.model.BranchType;
import org.apache.seata.integration.tx.api.interceptor.TwoPhaseBusinessActionParam;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JsonParserWrapTest {

private JsonParserWrap parserWrap;
private final String jsonString = "{\"actionName\":\"business_action\",\"useCommonFence\":null,\"businessActionContext\":null," +
"\"branchType\":\"TCC\",\"delayReport\":null}";


@BeforeEach
public void setUp() {
parserWrap = new JsonParserWrap(new JsonParserImpl());
}

@Test
public void testToJSONString() {
TwoPhaseBusinessActionParam actionParam = new TwoPhaseBusinessActionParam();
actionParam.setActionName("business_action");
actionParam.setBranchType(BranchType.TCC);

String resultString = parserWrap.toJSONString(actionParam);

assertEquals(jsonString, resultString);
}

@Test
public void testToJSONStringThrowsException() {
Object mockItem = mock(Object.class);
when(mockItem.toString()).thenReturn(mockItem.getClass().getName());
assertThrows(JsonParseException.class, () -> parserWrap.toJSONString(mockItem));
}

@Test
public void testParseObject() {
TwoPhaseBusinessActionParam actionParam = parserWrap.parseObject(jsonString, TwoPhaseBusinessActionParam.class);

assertEquals("business_action", actionParam.getActionName());
assertEquals(BranchType.TCC, actionParam.getBranchType());
}

@Test
public void testParseObjectThrowsException() {
assertThrows(JsonParseException.class, () -> parserWrap.parseObject(jsonString, Integer.class));
}

@Test
public void testGetName() {
assertEquals("customParser", parserWrap.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 org.apache.seata.integration.tx.api.remoting;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TwoPhaseResultTest {

private TwoPhaseResult result;

@BeforeEach
public void setUp() {
result = new TwoPhaseResult(false, "");
}

@Test
public void testGetMessage() {
result.setMessage("message");
assertEquals("message", result.getMessage());
}

@Test
public void testIsSuccess() {
result.setSuccess(true);
assertTrue(result.isSuccess());
}

@Test
public void testToStringEmptyMessage() {
assertEquals("[isSuccess:false]", result.toString());
}

@Test
public void testToStringNotEmptyMessage() {
result.setMessage("test");
assertEquals("[isSuccess:false, msg:test]", result.toString());
}
}
Loading

0 comments on commit 7d3cf99

Please sign in to comment.