forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve the test case coverage of saga module to 70% (apache#6647)
- Loading branch information
Showing
23 changed files
with
1,417 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
/** | ||
* Loop Task Util | ||
* | ||
|
52 changes: 52 additions & 0 deletions
52
...essctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
.../test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.