-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mock-tasks: allow matching mocks by meta attributes or step name (#1024)
- Loading branch information
Showing
4 changed files
with
348 additions
and
5 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
43 changes: 43 additions & 0 deletions
43
.../tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinitionContext.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,43 @@ | ||
package com.walmartlabs.concord.plugins.mock; | ||
|
||
/*- | ||
* ***** | ||
* Concord | ||
* ----- | ||
* Copyright (C) 2017 - 2024 Walmart Inc. | ||
* ----- | ||
* Licensed 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. | ||
* ===== | ||
*/ | ||
|
||
import com.walmartlabs.concord.runtime.v2.model.Step; | ||
import com.walmartlabs.concord.runtime.v2.sdk.Variables; | ||
|
||
import java.util.Objects; | ||
|
||
public record MockDefinitionContext(Step currentStep, String taskName, Variables input, String method, Object[] params) { | ||
|
||
public static MockDefinitionContext task(Step currentStep, String taskName, Variables input) { | ||
Objects.requireNonNull(taskName); | ||
Objects.requireNonNull(input); | ||
|
||
return new MockDefinitionContext(currentStep, taskName, input, null, null); | ||
} | ||
|
||
public static MockDefinitionContext method(Step currentStep, String taskName, String methodName, Object[] params) { | ||
Objects.requireNonNull(taskName); | ||
Objects.requireNonNull(methodName); | ||
Objects.requireNonNull(params); | ||
return new MockDefinitionContext(currentStep, taskName, null, methodName, params); | ||
} | ||
} |
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
186 changes: 186 additions & 0 deletions
186
...ks/mock/src/test/java/com/walmartlabs/concord/plugins/mock/MockDefinitionMatcherTest.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,186 @@ | ||
package com.walmartlabs.concord.plugins.mock; | ||
|
||
/*- | ||
* ***** | ||
* Concord | ||
* ----- | ||
* Copyright (C) 2017 - 2024 Walmart Inc. | ||
* ----- | ||
* Licensed 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. | ||
* ===== | ||
*/ | ||
|
||
import com.walmartlabs.concord.runtime.v2.model.Location; | ||
import com.walmartlabs.concord.runtime.v2.model.Step; | ||
import com.walmartlabs.concord.runtime.v2.model.TaskCall; | ||
import com.walmartlabs.concord.runtime.v2.model.TaskCallOptions; | ||
import com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.walmartlabs.concord.plugins.mock.MockDefinitionProvider.*; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class MockDefinitionMatcherTest { | ||
|
||
private MockDefinitionMatcher mockDefinitionMatcher; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
mockDefinitionMatcher = new MockDefinitionMatcher(); | ||
} | ||
|
||
@Test | ||
public void testTaskMatch() { | ||
/* | ||
task: myTask | ||
in: | ||
param1: value1 | ||
param2: value2 | ||
*/ | ||
var context = MockDefinitionContext.task(mock(Step.class), "myTask", new MapBackedVariables(Map.of("param1", "value1", "param2", "value2"))); | ||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask", | ||
"in", Map.of("param1", "value1") | ||
)); | ||
|
||
assertTrue(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testMatchOnlyByMeta() { | ||
/* | ||
task: myTask | ||
in: | ||
param1: value1 | ||
param2: value2 | ||
*/ | ||
var currentStep = new TaskCall(Location.builder().build(), "myTask", TaskCallOptions.builder().meta(Map.of("taskId", "BOO")).build()); | ||
var context = MockDefinitionContext.task(currentStep, "myTask", new MapBackedVariables(Map.of("param1", "value1", "param2", "value2"))); | ||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask", | ||
"stepMeta", Map.of("taskId", "BO.*") | ||
)); | ||
|
||
assertTrue(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testTaskMatchByMeta() { | ||
/* | ||
task: myTask | ||
in: | ||
param1: value1 | ||
meta: | ||
taskId: "BOO" | ||
*/ | ||
var currentStep = new TaskCall(Location.builder().build(), "myTask", TaskCallOptions.builder().meta(Map.of("taskId", "BOO")).build()); | ||
var context = MockDefinitionContext.task(currentStep, "myTask", new MapBackedVariables(Map.of("param1", "value1", "param2", "value2"))); | ||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask", | ||
"in", Map.of("param1", "value1"), | ||
"stepMeta", Map.of("taskId", "BO.*") | ||
)); | ||
|
||
assertTrue(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testTaskMethodMatch() { | ||
// expr: ${myTask.myMethod(1, 2)} | ||
var context = MockDefinitionContext.method(mock(Step.class), "myTask", "myMethod", new Object[] {1, 2}); | ||
|
||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask", | ||
"method", "myMethod", | ||
"args", List.of(1, 2) | ||
)); | ||
|
||
assertTrue(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testTaskMethodMatchByMeta() { | ||
// expr: ${myTask.myMethod(1, 2)} | ||
// meta: | ||
// taskId: "BOO" | ||
var currentStep = new TaskCall(Location.builder().build(), "myTask", TaskCallOptions.builder().meta(Map.of("taskId", "BOO")).build()); | ||
var context = MockDefinitionContext.method(currentStep, "myTask", "myMethod", new Object[] {1, 2}); | ||
|
||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask", | ||
"method", "myMethod", | ||
"args", List.of(1, 2), | ||
"stepMeta", Map.of("taskId", "BO.*") | ||
)); | ||
|
||
assertTrue(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testNotMatch_taskName() { | ||
/* | ||
task: myTask | ||
in: | ||
param1: value1 | ||
param2: value2 | ||
*/ | ||
var context = MockDefinitionContext.task(mock(Step.class), "myTask", new MapBackedVariables(Map.of("param1", "value1", "param2", "value2"))); | ||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask2", | ||
"in", Map.of("param1", "value1") | ||
)); | ||
|
||
// real taskName=myTask, mocked: "myTask2" | ||
assertFalse(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testNotMatch_InputParams() { | ||
/* | ||
task: myTask | ||
in: | ||
param1: value1 | ||
param2: value2 | ||
*/ | ||
var context = MockDefinitionContext.task(mock(Step.class), "myTask", new MapBackedVariables(Map.of("param1", "value1", "param2", "value2"))); | ||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask2", | ||
"in", Map.of("param3", "value3") | ||
)); | ||
|
||
assertFalse(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
|
||
@Test | ||
public void testNotMatch_Meta() { | ||
/* | ||
task: myTask | ||
in: | ||
param1: value1 | ||
param2: value2 | ||
*/ | ||
var context = MockDefinitionContext.task(mock(Step.class), "myTask", new MapBackedVariables(Map.of("param1", "value1", "param2", "value2"))); | ||
var mock = new MockDefinition(Map.of( | ||
"task", "myTask2", | ||
"stepMeta", Map.of("taskId", "BO.*") | ||
)); | ||
|
||
assertFalse(mockDefinitionMatcher.matches(context, mock)); | ||
} | ||
} |