Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(6509): improve the test case coverage of saga module to 70% #6519

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Add changes here for all PR submitted to the 2.x branch.


### test:
- [[#6519](https://github.com/apache/incubator-seata/pull/6519)] improve the test case coverage of saga module to 70%


Thanks to these contributors for their code commits. Please report an unintended omission.
Expand All @@ -44,5 +45,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [tanyaofei](https://github.com/tanyaofei)
- [wanghongzhou](https://github.com/wanghongzhou)
- [ggbocoder](https://github.com/ggbocoder)
- [xjlgod](https://github.com/xjlgod)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
3 changes: 3 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
### security:

### test:
- [[#6519](https://github.com/apache/incubator-seata/pull/6519)] 增加saga模块的测试用例覆盖率



非常感谢以下 contributors 的代码贡献。若有无意遗漏,请报告。
Expand All @@ -44,6 +46,7 @@
- [tanyaofei](https://github.com/tanyaofei)
- [wanghongzhou](https://github.com/wanghongzhou)
- [ggbocoder](https://github.com/ggbocoder)
- [xjlgod](https://github.com/xjlgod)


同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.seata.saga.engine.store.db;

import org.apache.seata.common.util.BeanUtils;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -25,12 +25,10 @@
import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import org.apache.seata.common.exception.StoreException;
import org.apache.seata.common.util.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract store
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Loop Task Util
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.saga.proctrl.handler;

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

import org.apache.seata.common.exception.FrameworkException;
import org.apache.seata.saga.proctrl.ProcessRouter;
import org.apache.seata.saga.proctrl.ProcessType;
import org.apache.seata.saga.proctrl.impl.ProcessContextImpl;
import org.apache.seata.saga.proctrl.mock.MockProcessRouter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* DefaultRouterHandlerTest
*/
public class DefaultRouterHandlerTest {
@Test
public void testRouteOfFrameworkException() {
ProcessContextImpl context = new ProcessContextImpl();
DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler();
Assertions.assertThrows(FrameworkException.class, () -> defaultRouterHandler.route(context));
}

@Test
public void testRouteOfException() {
ProcessContextImpl context = new ProcessContextImpl();
context.setVariable("exception", new Object());
DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler();
Map<String, ProcessRouter> processRouters = new HashMap<>();
ProcessRouter processRouter = new MockProcessRouter();
processRouters.put(ProcessType.STATE_LANG.getCode(), processRouter);
defaultRouterHandler.setProcessRouters(processRouters);
Assertions.assertThrows(RuntimeException.class, () -> defaultRouterHandler.route(context));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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.saga.proctrl.impl;

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

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

/**
* ProcessContextImplTest
*/
public class ProcessContextImplTest {

@Test
public void testGetVariableFromParent() {
ProcessContextImpl context = new ProcessContextImpl();
ProcessContextImpl parentContext = new ProcessContextImpl();
parentContext.setVariable("key", "value");
context.setParent(parentContext);
Assertions.assertEquals("value", context.getVariable("key"));
}

@Test
public void testSetVariable() {
ProcessContextImpl context = new ProcessContextImpl();
context.setVariable("key", "value");
context.setVariable("key", "value1");
Assertions.assertEquals("value1", context.getVariable("key"));
context.removeVariable("key");
ProcessContextImpl parentContext = new ProcessContextImpl();
parentContext.setVariable("key", "value");
context.setParent(parentContext);
Assertions.assertEquals("value", context.getVariable("key"));
}

@Test
public void testGetVariables() {
ProcessContextImpl context = new ProcessContextImpl();
ProcessContextImpl parentContext = new ProcessContextImpl();
parentContext.setVariable("key", "value");
context.setParent(parentContext);
Assertions.assertEquals(1, context.getVariables().size());
}

@Test
public void testSetVariables() {
ProcessContextImpl context = new ProcessContextImpl();
Map<String, Object> map = new HashMap<>();
map.put("key", "value");
context.setVariables(map);
Assertions.assertEquals(1, context.getVariables().size());
}

@Test
public void testGetVariableLocally() {
ProcessContextImpl context = new ProcessContextImpl();
context.setVariable("key", "value");
Assertions.assertEquals("value", context.getVariableLocally("key"));
}


@Test
public void testSetVariablesLocally() {
ProcessContextImpl context = new ProcessContextImpl();
Map<String, Object> map = new HashMap<>();
map.put("key", "value");
context.setVariablesLocally(map);
Assertions.assertEquals("value", context.getVariableLocally("key"));
}

@Test
public void testHasVariable() {
ProcessContextImpl context = new ProcessContextImpl();
Assertions.assertFalse(context.hasVariable("key"));
}

@Test
public void testRemoveVariable() {
ProcessContextImpl context = new ProcessContextImpl();
ProcessContextImpl parentContext = new ProcessContextImpl();
parentContext.setVariable("key", "value");
context.setParent(parentContext);
context.setVariable("key1", "value1");
context.removeVariable("key");
context.removeVariable("key1");
Assertions.assertEquals(0, context.getVariables().size());
}

@Test
public void testRemoveVariableLocally() {
ProcessContextImpl context = new ProcessContextImpl();
context.setVariable("key", "value");
context.removeVariableLocally("key");
Assertions.assertEquals(0, context.getVariables().size());
}

@Test
public void testClearLocally() {
ProcessContextImpl context = new ProcessContextImpl();
context.setVariable("key", "value");
context.clearLocally();
Assertions.assertEquals(0, context.getVariables().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public Instruction route(ProcessContext context) throws FrameworkException {
return null;//end process
}
}
if (context.hasVariable("exception")) {
throw new RuntimeException("exception");
}
return instruction;
}
}
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.saga.proctrl.process.impl;

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

import org.apache.seata.common.exception.FrameworkException;
import org.apache.seata.saga.proctrl.ProcessContext;
import org.apache.seata.saga.proctrl.ProcessType;
import org.apache.seata.saga.proctrl.handler.ProcessHandler;
import org.apache.seata.saga.proctrl.handler.RouterHandler;
import org.apache.seata.saga.proctrl.impl.ProcessContextImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* CustomizeBusinessProcessorTest
*/
public class CustomizeBusinessProcessorTest {

@Test
public void testProcessFail() {
CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor();
ProcessContext processContext = new ProcessContextImpl();
processContext.setVariable(ProcessContext.VAR_NAME_PROCESS_TYPE, ProcessType.STATE_LANG);
Map<String, ProcessHandler> processHandlerMap = new HashMap<>(1);
processHandlerMap.put(ProcessType.STATE_LANG.getCode(), null);
customizeBusinessProcessor.setProcessHandlers(processHandlerMap);
Assertions.assertThrows(FrameworkException.class, () -> customizeBusinessProcessor.process(processContext));
}

@Test
public void testRouteFail() {
CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor();
ProcessContext processContext = new ProcessContextImpl();
Map<String, RouterHandler> routerHandlerMap = new HashMap<>(1);
routerHandlerMap.put(ProcessType.STATE_LANG.getCode(), null);
customizeBusinessProcessor.setRouterHandlers(routerHandlerMap);
Assertions.assertDoesNotThrow(() -> customizeBusinessProcessor.route(processContext));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.saga.engine.config;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;


import static org.mockito.Mockito.when;

/**
* DbStateMachineConfigTest
*/
public class DbStateMachineConfigTest {
@Test
public void testGetDbTypeFromDataSource() throws SQLException {
Connection connection = Mockito.mock(Connection.class);
DatabaseMetaData databaseMetaData = Mockito.mock(DatabaseMetaData.class);
when(connection.getMetaData()).thenReturn(databaseMetaData);
when(databaseMetaData.getDatabaseProductName()).thenReturn("test");
MockDataSource mockDataSource = new MockDataSource();
mockDataSource.setConnection(connection);
Assertions.assertEquals(DbStateMachineConfig.getDbTypeFromDataSource(mockDataSource), "test");
}

@Test
public void testAfterPropertiesSet() throws Exception {
DbStateMachineConfig dbStateMachineConfig = new DbStateMachineConfig();
Connection connection = Mockito.mock(Connection.class);
DatabaseMetaData databaseMetaData = Mockito.mock(DatabaseMetaData.class);
when(connection.getMetaData()).thenReturn(databaseMetaData);
when(databaseMetaData.getDatabaseProductName()).thenReturn("test");
MockDataSource mockDataSource = new MockDataSource();
mockDataSource.setConnection(connection);
dbStateMachineConfig.setDataSource(mockDataSource);
dbStateMachineConfig.setApplicationId("test");
dbStateMachineConfig.setTxServiceGroup("test");

Assertions.assertDoesNotThrow(dbStateMachineConfig::afterPropertiesSet);
}
}
Loading
Loading