You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/08/25 00:12:03 UTC

[GitHub] WillemJiang closed pull request #269: [SCB-864] Add SQLTransport and JacksonSQLFormat

WillemJiang closed pull request #269: [SCB-864] Add SQLTransport and JacksonSQLFormat
URL: https://github.com/apache/incubator-servicecomb-saga/pull/269
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/saga-core/src/main/java/org/apache/servicecomb/saga/core/Operation.java b/saga-core/src/main/java/org/apache/servicecomb/saga/core/Operation.java
index 99e760ba..59508656 100644
--- a/saga-core/src/main/java/org/apache/servicecomb/saga/core/Operation.java
+++ b/saga-core/src/main/java/org/apache/servicecomb/saga/core/Operation.java
@@ -21,6 +21,7 @@
 
   String TYPE_NOP = "NOP";
   String TYPE_REST = "rest";
+  String TYPE_SQL = "sql";
   SagaResponse SUCCESSFUL_SAGA_RESPONSE = new SuccessfulSagaResponse("success");
 
   SagaResponse send(String address);
diff --git a/saga-core/src/main/java/org/apache/servicecomb/saga/core/SQLOperation.java b/saga-core/src/main/java/org/apache/servicecomb/saga/core/SQLOperation.java
new file mode 100644
index 00000000..6444e2b7
--- /dev/null
+++ b/saga-core/src/main/java/org/apache/servicecomb/saga/core/SQLOperation.java
@@ -0,0 +1,58 @@
+/*
+ * 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.servicecomb.saga.core;
+
+import java.util.Collections;
+import java.util.List;
+
+public class SQLOperation implements Operation {
+
+  private final String sql;
+  private final List<String> params;
+
+  public SQLOperation(String sql, List<String> params) {
+    this.sql = sql;
+    this.params = params == null ? Collections.<String>emptyList() : params;
+  }
+
+  public String sql() {
+    return sql;
+  }
+
+  public List<String> params() {
+    return params;
+  }
+
+  @Override
+  public String toString() {
+    return "SQLOperation{" +
+        "sql='" + sql + '\'' +
+        ", params=" + params +
+        '}';
+  }
+
+  @Override
+  public SagaResponse send(String datasource) {
+    return SUCCESSFUL_SAGA_RESPONSE;
+  }
+
+  @Override
+  public SagaResponse send(String datasource, SagaResponse response) {
+    return send(datasource);
+  }
+}
diff --git a/saga-core/src/main/java/org/apache/servicecomb/saga/transports/SQLTransport.java b/saga-core/src/main/java/org/apache/servicecomb/saga/transports/SQLTransport.java
new file mode 100644
index 00000000..6e534dd4
--- /dev/null
+++ b/saga-core/src/main/java/org/apache/servicecomb/saga/transports/SQLTransport.java
@@ -0,0 +1,29 @@
+/*
+ * 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.servicecomb.saga.transports;
+
+import java.util.List;
+
+import org.apache.servicecomb.saga.core.SagaResponse;
+import org.apache.servicecomb.saga.core.Transport;
+
+public interface SQLTransport extends Transport {
+
+  SagaResponse with(String datasource, String sql, List<String> params);
+
+}
diff --git a/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLCompensation.java b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLCompensation.java
new file mode 100644
index 00000000..6a61bf32
--- /dev/null
+++ b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLCompensation.java
@@ -0,0 +1,48 @@
+/*
+ * 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.servicecomb.saga.format;
+
+import java.util.List;
+
+import org.apache.servicecomb.saga.core.Compensation;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class JacksonSQLCompensation extends JacksonSQLOperation implements Compensation {
+
+  private final int retries;
+
+  public JacksonSQLCompensation(String sql, List<String> params) {
+    this(sql, params, DEFAULT_RETRIES);
+  }
+
+  @JsonCreator
+  public JacksonSQLCompensation(
+      @JsonProperty("sql") String sql,
+      @JsonProperty("params") List<String> params,
+      @JsonProperty("retries") int retries) {
+    super(sql, params);
+    this.retries = retries <= 0? DEFAULT_RETRIES : retries;
+  }
+
+  @Override
+  public int retries() {
+    return this.retries;
+  }
+}
diff --git a/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLOperation.java b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLOperation.java
new file mode 100644
index 00000000..500bdfd5
--- /dev/null
+++ b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLOperation.java
@@ -0,0 +1,49 @@
+/*
+ * 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.servicecomb.saga.format;
+
+import java.util.List;
+
+import org.apache.servicecomb.saga.core.Operation;
+import org.apache.servicecomb.saga.core.SQLOperation;
+import org.apache.servicecomb.saga.core.SagaResponse;
+import org.apache.servicecomb.saga.transports.SQLTransport;
+import org.apache.servicecomb.saga.transports.TransportFactory;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
+public class JacksonSQLOperation extends SQLOperation implements TransportAware<SQLTransport> {
+
+  @JsonIgnore
+  private SQLTransport transport;
+
+  public JacksonSQLOperation(String sql, List<String> params) {
+    super(sql, params);
+  }
+
+  @Override
+  public Operation with(TransportFactory<SQLTransport> transport) {
+    this.transport = transport.getTransport();
+    return this;
+  }
+
+  @Override
+  public SagaResponse send(String datasource) {
+    return transport.with(datasource, sql(), params());
+  }
+}
diff --git a/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLTransaction.java b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLTransaction.java
new file mode 100644
index 00000000..e85c0366
--- /dev/null
+++ b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JacksonSQLTransaction.java
@@ -0,0 +1,35 @@
+/*
+ * 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.servicecomb.saga.format;
+
+import java.util.List;
+
+import org.apache.servicecomb.saga.core.Transaction;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class JacksonSQLTransaction extends JacksonSQLOperation implements Transaction {
+
+  @JsonCreator
+  public JacksonSQLTransaction(
+      @JsonProperty("sql") String sql,
+      @JsonProperty("params") List<String> params) {
+    super(sql, params);
+  }
+}
diff --git a/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSQLSagaRequest.java b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSQLSagaRequest.java
new file mode 100644
index 00000000..e1d95996
--- /dev/null
+++ b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSQLSagaRequest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.servicecomb.saga.format;
+
+import static org.apache.servicecomb.saga.format.JacksonFallback.NOP_TRANSPORT_AWARE_FALLBACK;
+
+import org.apache.servicecomb.saga.core.Fallback;
+import org.apache.servicecomb.saga.core.Operation;
+import org.apache.servicecomb.saga.core.SagaRequestImpl;
+import org.apache.servicecomb.saga.transports.SQLTransport;
+import org.apache.servicecomb.saga.transports.TransportFactory;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class JsonSQLSagaRequest extends SagaRequestImpl implements JsonSagaRequest<SQLTransport> {
+
+  private JacksonSQLTransaction transaction;
+  private JacksonSQLCompensation compensation;
+
+  @JsonCreator
+  public JsonSQLSagaRequest(
+      @JsonProperty("id") String id,
+      @JsonProperty("datasource") String datasource,
+      @JsonProperty("type") String type,
+      @JsonProperty("transaction") JacksonSQLTransaction transaction,
+      @JsonProperty("compensation") JacksonSQLCompensation compensation,
+      @JsonProperty("fallback") Fallback fallback,
+      @JsonProperty("parents") String[] parents,
+      @JsonProperty("failRetryDelayMilliseconds") int failRetryDelayMilliseconds) {
+    super(id, datasource, type, transaction, compensation,
+        fallback == null ? NOP_TRANSPORT_AWARE_FALLBACK : fallback,
+        parents, failRetryDelayMilliseconds);
+
+    checkNull(transaction, "transaction");
+    checkNull(compensation, "compensation");
+
+    this.transaction = transaction;
+    this.compensation = compensation;
+  }
+
+  @Override
+  public JsonSagaRequest with(TransportFactory transportFactory) {
+    transaction.with(transportFactory);
+    compensation.with(transportFactory);
+    return this;
+  }
+
+  private void checkNull(Operation operation, String operationName) {
+    if (operation == null) {
+      throw new IllegalArgumentException("Invalid request with NO " + operationName + " specified");
+    }
+  }
+}
diff --git a/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSagaRequest.java b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSagaRequest.java
index a25542f3..ed6c6ce8 100644
--- a/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSagaRequest.java
+++ b/saga-format/src/main/java/org/apache/servicecomb/saga/format/JsonSagaRequest.java
@@ -32,7 +32,8 @@
     visible = true,
     property = "type")
 @JsonSubTypes({
-    @Type(value = JsonRestSagaRequest.class, name = Operation.TYPE_REST)
+    @Type(value = JsonRestSagaRequest.class, name = Operation.TYPE_REST),
+    @Type(value = JsonSQLSagaRequest.class, name = Operation.TYPE_SQL)
 })
 public interface JsonSagaRequest<T extends Transport> extends SagaRequest {
 
diff --git a/saga-format/src/test/java/org/apache/servicecomb/saga/format/JacksonFromJsonFormatForSQLTest.java b/saga-format/src/test/java/org/apache/servicecomb/saga/format/JacksonFromJsonFormatForSQLTest.java
new file mode 100644
index 00000000..ef40d17e
--- /dev/null
+++ b/saga-format/src/test/java/org/apache/servicecomb/saga/format/JacksonFromJsonFormatForSQLTest.java
@@ -0,0 +1,212 @@
+/*
+ * 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.servicecomb.saga.format;
+
+import static com.seanyinx.github.unit.scaffolding.Randomness.uniquify;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertThat;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.servicecomb.saga.core.Operation;
+import org.apache.servicecomb.saga.core.SagaDefinition;
+import org.apache.servicecomb.saga.core.SagaException;
+import org.apache.servicecomb.saga.core.SagaRequest;
+import org.apache.servicecomb.saga.core.SagaResponse;
+import org.apache.servicecomb.saga.core.SuccessfulSagaResponse;
+import org.apache.servicecomb.saga.core.application.interpreter.FromJsonFormat;
+import org.apache.servicecomb.saga.transports.SQLTransport;
+import org.apache.servicecomb.saga.transports.TransportFactory;
+import org.junit.Test;
+
+import com.seanyinx.github.unit.scaffolding.AssertUtils;
+
+public class JacksonFromJsonFormatForSQLTest {
+  private static final String requestJson = "{\n"
+      + "    \"requests\":[\n"
+      + "        {\n"
+      + "            \"id\":\"first-sql-sharding-1\",\n"
+      + "            \"type\":\"sql\",\n"
+      + "            \"datasource\":\"ds_0\",\n"
+      + "            \"parents\":[],\n"
+      + "            \"transaction\":{\n"
+      + "                \"sql\":\"INSERT INTO TABLE ds_0.tb_0 (id, value) values (?, ?)\",\n"
+      + "                \"params\":[\"1\", \"xxx\"]\n"
+      + "            },\n"
+      + "            \"compensation\":{\n"
+      + "                \"sql\":\"DELETE FROM ds_0.tb_0 WHERE id=?\",\n"
+      + "                \"params\":[\"1\"]\n"
+      + "            }\n"
+      + "        },\n"
+      + "        {\n"
+      + "            \"id\":\"first-sql-sharding-2\",\n"
+      + "            \"type\":\"sql\",\n"
+      + "            \"datasource\":\"ds_0\",\n"
+      + "            \"parents\":[],\n"
+      + "            \"transaction\":{\n"
+      + "                \"sql\":\"INSERT INTO TABLE ds_0.tb_1 (id, value) values (?, ?)\",\n"
+      + "                \"params\":[\"2\", \"xxx\"]\n"
+      + "            },\n"
+      + "            \"compensation\":{\n"
+      + "                \"sql\":\"DELETE FROM ds_0.tb_1 WHERE id=?\",\n"
+      + "                \"params\":[\"2\"]\n"
+      + "            }\n"
+      + "        },\n"
+      + "        {\n"
+      + "            \"id\":\"second-sql-sharding-1\",\n"
+      + "            \"type\":\"sql\",\n"
+      + "            \"datasource\":\"ds_1\",\n"
+      + "            \"parents\":[\"first-sql-sharding-1\",\"first-sql-sharding-2\"],\n"
+      + "            \"transaction\":{\n"
+      + "                \"sql\":\"INSERT INTO TABLE ds_1.tb_2 (id, value) values (?, ?)\",\n"
+      + "                \"params\":[\"3\", \"xxx\"]\n"
+      + "            },\n"
+      + "            \"compensation\":{\n"
+      + "                \"sql\":\"DELETE FROM ds_1.tb_2 WHERE id=?\",\n"
+      + "                \"params\":[\"3\"]\n"
+      + "            }\n"
+      + "        },\n"
+      + "        {\n"
+      + "            \"id\":\"second-sql-sharding-2\",\n"
+      + "            \"type\":\"sql\",\n"
+      + "            \"datasource\":\"ds_1\",\n"
+      + "            \"parents\":[\"first-sql-sharding-1\",\"first-sql-sharding-2\"],\n"
+      + "            \"transaction\":{\n"
+      + "                \"sql\":\"INSERT INTO TABLE ds_1.tb_3 (id, value) values (?, ?)\",\n"
+      + "                \"params\":[\"4\", \"xxx\"]\n"
+      + "            },\n"
+      + "            \"compensation\":{\n"
+      + "                \"sql\":\"DELETE FROM ds_1.tb_3 WHERE id=?\",\n"
+      + "                \"params\":[\"4\"]\n"
+      + "            }\n"
+      + "        }\n"
+      + "    ]\n"
+      + "}\n";
+
+  private final SagaResponse responseDefault = new SuccessfulSagaResponse(uniquify("responseDefault"));
+
+  private final SQLTransport sqlTransport = new SQLTransport() {
+    @Override
+    public SagaResponse with(String datasource, String sql, List<String> params) {
+      if (null == sql || sql.trim().length() == 0) {
+        return responseDefault;
+      }
+
+      for (String param : params) {
+        sql = sql.replaceFirst("\\?", param);
+      }
+
+      return new SuccessfulSagaResponse(datasource + " execute sql : " + sql);
+    }
+  };
+
+  private final TransportFactory<SQLTransport> transportFactory = new TransportFactory<SQLTransport>() {
+    @Override
+    public SQLTransport getTransport() {
+      return sqlTransport;
+    }
+  };
+
+  private final FromJsonFormat<SagaDefinition> format = new JacksonFromJsonFormat(transportFactory);
+
+  private final Function<SagaRequest, String> getRequestId = new Function<SagaRequest, String>() {
+    @Override
+    public String apply(SagaRequest sagaRequest) {
+      return sagaRequest.id();
+    }
+  };
+
+  private final Function<SagaRequest, String> getRequestServiceName = new Function<SagaRequest, String>() {
+    @Override
+    public String apply(SagaRequest sagaRequest) {
+      return sagaRequest.serviceName();
+    }
+  };
+
+  private final Function<SagaRequest, String> getRequestType = new Function<SagaRequest, String>() {
+    @Override
+    public String apply(SagaRequest sagaRequest) {
+      return sagaRequest.type();
+    }
+  };
+
+  @Test
+  public void addTransportToDeserializedRequests() {
+    SagaRequest[] requests = format.fromJson(requestJson).requests();
+
+    assertThat(collect(requests, getRequestId),
+        contains("first-sql-sharding-1", "first-sql-sharding-2", "second-sql-sharding-1", "second-sql-sharding-2"));
+    assertThat(collect(requests, getRequestServiceName), contains("ds_0", "ds_0", "ds_1", "ds_1"));
+    assertThat(collect(requests, getRequestType),
+        contains(Operation.TYPE_SQL, Operation.TYPE_SQL, Operation.TYPE_SQL, Operation.TYPE_SQL));
+
+    SagaResponse sagaResponse = null;
+
+    sagaResponse = requests[0].transaction().send(requests[0].serviceName());
+    assertThat(sagaResponse.body(), is("ds_0 execute sql : INSERT INTO TABLE ds_0.tb_0 (id, value) values (1, xxx)"));
+    sagaResponse = requests[0].compensation().send(requests[0].serviceName());
+    assertThat(sagaResponse.body(), is("ds_0 execute sql : DELETE FROM ds_0.tb_0 WHERE id=1"));
+
+    sagaResponse = requests[1].transaction().send(requests[1].serviceName());
+    assertThat(sagaResponse.body(), is("ds_0 execute sql : INSERT INTO TABLE ds_0.tb_1 (id, value) values (2, xxx)"));
+    sagaResponse = requests[1].compensation().send(requests[1].serviceName());
+    assertThat(sagaResponse.body(), is("ds_0 execute sql : DELETE FROM ds_0.tb_1 WHERE id=2"));
+
+    sagaResponse = requests[2].transaction().send(requests[2].serviceName());
+    assertThat(sagaResponse.body(), is("ds_1 execute sql : INSERT INTO TABLE ds_1.tb_2 (id, value) values (3, xxx)"));
+    sagaResponse = requests[2].compensation().send(requests[2].serviceName());
+    assertThat(sagaResponse.body(), is("ds_1 execute sql : DELETE FROM ds_1.tb_2 WHERE id=3"));
+
+    sagaResponse = requests[3].transaction().send(requests[3].serviceName());
+    assertThat(sagaResponse.body(), is("ds_1 execute sql : INSERT INTO TABLE ds_1.tb_3 (id, value) values (4, xxx)"));
+    sagaResponse = requests[3].compensation().send(requests[3].serviceName());
+    assertThat(sagaResponse.body(), is("ds_1 execute sql : DELETE FROM ds_1.tb_3 WHERE id=4"));
+
+    assertArrayEquals(new String[] {"first-sql-sharding-1", "first-sql-sharding-2"}, requests[2].parents());
+    assertArrayEquals(new String[] {"first-sql-sharding-1", "first-sql-sharding-2"}, requests[3].parents());
+  }
+
+  @Test
+  public void blowsUpWhenJsonIsInvalid() throws IOException {
+    String invalidRequest = "invalid-json";
+
+    try {
+      format.fromJson(invalidRequest);
+      AssertUtils.expectFailing(SagaException.class);
+    } catch (SagaException e) {
+      assertThat(e.getMessage(), is("Failed to interpret JSON invalid-json"));
+    }
+  }
+
+  private <T> Collection<T> collect(SagaRequest[] requests, Function<SagaRequest, T> mapper) {
+    List<T> result = new LinkedList<T>();
+    for (SagaRequest request : requests) {
+      result.add(mapper.apply(request));
+    }
+    return result;
+  }
+
+  private interface Function<T, R> {
+    R apply(T t);
+  }
+}
diff --git a/saga-format/src/test/java/org/apache/servicecomb/saga/format/JsonSQLSagaRequestTest.java b/saga-format/src/test/java/org/apache/servicecomb/saga/format/JsonSQLSagaRequestTest.java
new file mode 100644
index 00000000..15336c95
--- /dev/null
+++ b/saga-format/src/test/java/org/apache/servicecomb/saga/format/JsonSQLSagaRequestTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.servicecomb.saga.format;
+
+import static com.seanyinx.github.unit.scaffolding.AssertUtils.expectFailing;
+import static com.seanyinx.github.unit.scaffolding.Randomness.uniquify;
+import static org.apache.servicecomb.saga.core.Operation.TYPE_REST;
+import static org.apache.servicecomb.saga.format.JacksonFallback.NOP_TRANSPORT_AWARE_FALLBACK;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class JsonSQLSagaRequestTest {
+
+  private final JacksonSQLTransaction transaction = Mockito.mock(JacksonSQLTransaction.class);
+  private final JacksonSQLCompensation compensation = Mockito.mock(JacksonSQLCompensation.class);
+
+  @Test
+  public void blowsUpIfTransactionIsNotSpecified() {
+    try {
+      newSagaRequest(null, compensation, NOP_TRANSPORT_AWARE_FALLBACK);
+
+      expectFailing(IllegalArgumentException.class);
+    } catch (IllegalArgumentException e) {
+      assertThat(e.getMessage(), is("Invalid request with NO transaction specified"));
+    }
+  }
+
+  @Test
+  public void blowsUpIfCompensationIsNotSpecified() {
+    try {
+      newSagaRequest(transaction, null, NOP_TRANSPORT_AWARE_FALLBACK);
+
+      expectFailing(IllegalArgumentException.class);
+    } catch (IllegalArgumentException e) {
+      assertThat(e.getMessage(), is("Invalid request with NO compensation specified"));
+    }
+  }
+
+  private JsonSQLSagaRequest newSagaRequest(
+      JacksonSQLTransaction transaction,
+      JacksonSQLCompensation compensation,
+      JacksonFallback fallback) {
+
+    return new JsonSQLSagaRequest(
+        uniquify("id"),
+        uniquify("serviceName"),
+        TYPE_REST,
+        transaction,
+        compensation,
+        fallback,
+        null,
+        0);
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services