Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@
* @return variables data
*/
@Override
public Map<String, Object> viewVariables(long projectCode, Integer workflowInstanceId) {

Check failure on line 792 in dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowInstanceServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache-dolphinscheduler&issues=AZzmzWWoSKwI_Qkfqy7Q&open=AZzmzWWoSKwI_Qkfqy7Q&pullRequest=18068
Map<String, Object> result = new HashMap<>();

WorkflowInstance workflowInstance = workflowInstanceMapper.queryDetailById(workflowInstanceId);
Expand Down Expand Up @@ -819,27 +819,29 @@
Map<String, String> timeParams = BusinessTimeUtils
.getBusinessTime(workflowInstance.getCmdTypeIfComplement(),
workflowInstance.getScheduleTime(), timezone);
String userDefinedParams = workflowInstance.getGlobalParams();

// global params
List<Property> globalParams = new ArrayList<>();

// global param string
String globalParamStr =
ParameterUtils.convertParameterPlaceholders(JSONUtils.toJsonString(globalParams), timeParams);
globalParams = JSONUtils.toList(globalParamStr, Property.class);
for (Property property : globalParams) {
timeParams.put(property.getProp(), property.getValue());
}
String globalParamsJson = workflowInstance.getGlobalParams();
List<Property> finalGlobalParams = new ArrayList<>();

if (StringUtils.isNotEmpty(globalParamsJson)) {
String replacedJsonStr = ParameterUtils.convertParameterPlaceholders(globalParamsJson, timeParams);
finalGlobalParams = JSONUtils.toList(replacedJsonStr, Property.class);

if (userDefinedParams != null && userDefinedParams.length() > 0) {
globalParams = JSONUtils.toList(userDefinedParams, Property.class);
if (finalGlobalParams != null) {
for (Property property : finalGlobalParams) {
if (property.getProp() != null && property.getValue() != null) {
timeParams.put(property.getProp(), property.getValue());
}
}
}
}

Map<String, Map<String, Object>> localUserDefParams = getLocalParams(workflowInstance, timeParams);

Map<String, Object> resultMap = new HashMap<>();

resultMap.put(GLOBAL_PARAMS, globalParams);
resultMap.put(GLOBAL_PARAMS, finalGlobalParams);
resultMap.put(LOCAL_PARAMS, localUserDefParams);

result.put(DATA_LIST, resultMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -790,6 +791,89 @@
Assertions.assertEquals(Status.WORKFLOW_INSTANCE_NOT_EXIST, processNotExist.get(Constants.STATUS));
}

@Test
public void testViewVariables_WithTimePlaceholders() {

Check warning on line 795 in dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'public' modifier.

See more on https://sonarcloud.io/project/issues?id=apache-dolphinscheduler&issues=AZzmzWYoSKwI_Qkfqy7R&open=AZzmzWYoSKwI_Qkfqy7R&pullRequest=18068
String globalParamsJson = "[{\"prop\":\"biz_date\",\"value\":\"$[yyyyMMdd]\",\"type\":\"VARCHAR\"}," +
"{\"prop\":\"env\",\"value\":\"${ENV_TYPE}\",\"type\":\"VARCHAR\"}]";

WorkflowInstance workflowInstance = getProcessInstance();
workflowInstance.setId(1);
workflowInstance.setCommandType(CommandType.SCHEDULER);

Calendar calendar = Calendar.getInstance();
calendar.set(2026, Calendar.MARCH, 13, 10, 0, 0);
workflowInstance.setScheduleTime(calendar.getTime());
workflowInstance.setGlobalParams(globalParamsJson);
workflowInstance.setWorkflowDefinitionCode(100L);

when(workflowInstanceMapper.queryDetailById(1)).thenReturn(workflowInstance);

WorkflowDefinition workflowDefinition = new WorkflowDefinition();
workflowDefinition.setCode(100L);
workflowDefinition.setProjectCode(1L);
when(workflowDefinitionMapper.queryByCode(100L)).thenReturn(workflowDefinition);

Map<String, Object> result = workflowInstanceService.viewVariables(1L, 1);

Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));

Map<String, Object> dataList = (Map<String, Object>) result.get(Constants.DATA_LIST);
Assertions.assertNotNull(dataList);

List<Property> globalParams = (List<Property>) dataList.get(Constants.GLOBAL_PARAMS);
Assertions.assertNotNull(globalParams);
Assertions.assertEquals(2, globalParams.size());

Property dateParam = globalParams.stream()
.filter(p -> "biz_date".equals(p.getProp()))
.findFirst()
.orElse(null);
Assertions.assertNotNull(dateParam);
Assertions.assertEquals("20260313", dateParam.getValue(),
"Time placeholder $[yyyyMMdd] should be replaced with schedule time");

Property envParam = globalParams.stream()
.filter(p -> "env".equals(p.getProp()))
.findFirst()
.orElse(null);
Assertions.assertNotNull(envParam);
}

@Test
public void testViewVariables_InstanceNotFound() {

Check warning on line 843 in dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'public' modifier.

See more on https://sonarcloud.io/project/issues?id=apache-dolphinscheduler&issues=AZzmzWYoSKwI_Qkfqy7S&open=AZzmzWYoSKwI_Qkfqy7S&pullRequest=18068
when(workflowInstanceMapper.queryDetailById(999)).thenReturn(null);

Map<String, Object> result = workflowInstanceService.viewVariables(1L, 999);

Assertions.assertEquals(Status.WORKFLOW_INSTANCE_NOT_EXIST, result.get(Constants.STATUS));
Assertions.assertNull(result.get(Constants.DATA_LIST));
}

@Test
public void testViewVariables_EmptyGlobalParams() {

Check warning on line 853 in dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'public' modifier.

See more on https://sonarcloud.io/project/issues?id=apache-dolphinscheduler&issues=AZzmzWYoSKwI_Qkfqy7T&open=AZzmzWYoSKwI_Qkfqy7T&pullRequest=18068
WorkflowInstance workflowInstance = getProcessInstance();
workflowInstance.setId(2);
workflowInstance.setCommandType(CommandType.START_PROCESS);
workflowInstance.setScheduleTime(new Date());
workflowInstance.setGlobalParams("");
workflowInstance.setWorkflowDefinitionCode(101L);

when(workflowInstanceMapper.queryDetailById(2)).thenReturn(workflowInstance);

WorkflowDefinition workflowDefinition = new WorkflowDefinition();
workflowDefinition.setCode(101L);
workflowDefinition.setProjectCode(1L);
when(workflowDefinitionMapper.queryByCode(101L)).thenReturn(workflowDefinition);

Map<String, Object> result = workflowInstanceService.viewVariables(1L, 2);

Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));

Map<String, Object> dataList = (Map<String, Object>) result.get(Constants.DATA_LIST);
List<Property> globalParams = (List<Property>) dataList.get(Constants.GLOBAL_PARAMS);
Assertions.assertTrue(globalParams.isEmpty(), "Global params list should be empty when input is empty string");
}

@Test
public void testViewGantt() throws Exception {
WorkflowInstance workflowInstance = getProcessInstance();
Expand Down
Loading