From b8fa4b2d79d180a1efcdbb56cf31862e0fa00c82 Mon Sep 17 00:00:00 2001 From: Yury Brigadirenko Date: Mon, 1 Apr 2024 11:15:02 -0400 Subject: [PATCH] plugins: add new mock-tasks plugin (#754) --- plugins/tasks/mock/pom.xml | 95 +++++++++++++++++++ .../concord/plugins/mock/MockDefinition.java | 58 +++++++++++ .../plugins/mock/MockDefinitionProvider.java | 53 +++++++++++ .../concord/plugins/mock/MockModule.java | 37 ++++++++ .../concord/plugins/mock/MockTask.java | 48 ++++++++++ .../plugins/mock/MockTaskProvider.java | 62 ++++++++++++ 6 files changed, 353 insertions(+) create mode 100644 plugins/tasks/mock/pom.xml create mode 100644 plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinition.java create mode 100644 plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinitionProvider.java create mode 100644 plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockModule.java create mode 100644 plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTask.java create mode 100644 plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTaskProvider.java diff --git a/plugins/tasks/mock/pom.xml b/plugins/tasks/mock/pom.xml new file mode 100644 index 0000000000..20fea546a6 --- /dev/null +++ b/plugins/tasks/mock/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + + com.walmartlabs.concord.plugins.basic + parent + 1.101.1-SNAPSHOT + ../../pom.xml + + + mock-tasks + takari-jar + + + + com.walmartlabs.concord + concord-common + provided + + + com.walmartlabs.concord + concord-sdk + provided + + + com.walmartlabs.concord.runtime.v2 + concord-runtime-sdk-v2 + provided + + + javax.inject + javax.inject + provided + + + com.google.inject + guice + provided + + + + + org.immutables + value + provided + + + org.immutables + builder + provided + + + com.google.code.findbugs + jsr305 + provided + + + com.google.errorprone + error_prone_annotations + provided + + + + org.eclipse.sisu + org.eclipse.sisu.inject + + + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-databind + + + + + + + org.eclipse.sisu + sisu-maven-plugin + + + + io.takari.maven.plugins + takari-lifecycle-plugin + + proc + + + + + diff --git a/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinition.java b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinition.java new file mode 100644 index 0000000000..0a29f4aa12 --- /dev/null +++ b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinition.java @@ -0,0 +1,58 @@ +package com.walmartlabs.concord.plugins.mock; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 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.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import org.immutables.value.Value; + +import java.util.Collections; +import java.util.Map; + +/** + mocks: + - name: "http" + in: + url: ".*" + method: "POST" + out: + ok: true + statusCode: 200 + */ + +@Value.Immutable +@Value.Style(jdkOnly = true) +@JsonSerialize(as = ImmutableMockDefinition.class) +@JsonDeserialize(as = ImmutableMockDefinition.class) +public interface MockDefinition { + + String name(); + + @Value.Default + default Map in() { + return Collections.emptyMap(); + } + + @Value.Default + default Map out() { + return Collections.emptyMap(); + } +} diff --git a/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinitionProvider.java b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinitionProvider.java new file mode 100644 index 0000000000..61814124aa --- /dev/null +++ b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockDefinitionProvider.java @@ -0,0 +1,53 @@ +package com.walmartlabs.concord.plugins.mock; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 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.fasterxml.jackson.databind.ObjectMapper; +import com.walmartlabs.concord.runtime.v2.sdk.Context; +import com.walmartlabs.concord.sdk.MapUtils; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +@Singleton +public class MockDefinitionProvider { + + @Inject + public MockDefinitionProvider() { + } + + public synchronized MockDefinition get(Context ctx, String taskName) { + List> mocks = ctx.variables().getList("mocks", Collections.emptyList()); + for (Iterator> iterator = mocks.iterator(); iterator.hasNext(); ) { + Map mock = iterator.next(); + String name = MapUtils.assertString(mock, "name"); + if (name.equals(taskName)) { + iterator.remove(); + return new ObjectMapper().convertValue(mock, MockDefinition.class); + } + } + return null; + } +} diff --git a/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockModule.java b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockModule.java new file mode 100644 index 0000000000..243bb3b9c5 --- /dev/null +++ b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockModule.java @@ -0,0 +1,37 @@ +package com.walmartlabs.concord.plugins.mock; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 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.google.inject.Binder; +import com.google.inject.multibindings.Multibinder; +import com.walmartlabs.concord.runtime.v2.sdk.TaskProvider; + +import javax.inject.Named; + +@Named +public class MockModule implements com.google.inject.Module { + + @Override + public void configure(Binder binder) { + Multibinder taskProviders = Multibinder.newSetBinder(binder, TaskProvider.class); + taskProviders.addBinding().to(MockTaskProvider.class); + } +} diff --git a/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTask.java b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTask.java new file mode 100644 index 0000000000..5c5891e00f --- /dev/null +++ b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTask.java @@ -0,0 +1,48 @@ +package com.walmartlabs.concord.plugins.mock; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 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.common.Matcher; +import com.walmartlabs.concord.runtime.v2.sdk.Task; +import com.walmartlabs.concord.runtime.v2.sdk.TaskResult; +import com.walmartlabs.concord.runtime.v2.sdk.UserDefinedException; +import com.walmartlabs.concord.runtime.v2.sdk.Variables; +import com.walmartlabs.concord.sdk.MapUtils; + +public class MockTask implements Task { + + private final MockDefinition mockDefinition; + + public MockTask(MockDefinition mockDefinition) { + this.mockDefinition = mockDefinition; + } + + @Override + public TaskResult execute(Variables input) { + if (!Matcher.matches(input.toMap(), mockDefinition.in())) { + throw new UserDefinedException("Input variables for '" + mockDefinition.name() + "' not matched with actual input params: " + input.toMap()); + } + + boolean success = MapUtils.getBoolean(mockDefinition.out(), "ok", true); + return TaskResult.of(success) + .values(mockDefinition.out()); + } +} diff --git a/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTaskProvider.java b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTaskProvider.java new file mode 100644 index 0000000000..eb86175bd9 --- /dev/null +++ b/plugins/tasks/mock/src/main/java/com/walmartlabs/concord/plugins/mock/MockTaskProvider.java @@ -0,0 +1,62 @@ +package com.walmartlabs.concord.plugins.mock; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 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.sdk.Context; +import com.walmartlabs.concord.runtime.v2.sdk.Task; +import com.walmartlabs.concord.runtime.v2.sdk.TaskProvider; +import org.eclipse.sisu.Priority; + +import javax.inject.Inject; +import java.util.Collections; +import java.util.Set; + +@Priority(-1) +public class MockTaskProvider implements TaskProvider { + + private final MockDefinitionProvider mockDefinitionProvider; + + @Inject + public MockTaskProvider(MockDefinitionProvider mockDefinitionProvider) { + this.mockDefinitionProvider = mockDefinitionProvider; + } + + @Override + public Task createTask(Context ctx, String key) { + MockDefinition def = mockDefinitionProvider.get(ctx, key); + if (def == null) { + return null; + } + + return new MockTask(def); + } + + @Override + public boolean hasTask(String key) { + return false; + } + + @Override + public Set names() { + return Collections.emptySet(); + } +}