You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/08/14 09:07:32 UTC

[james-project] 04/13: JAMES-3349: Add test for handle serverFail

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit f162201fd61436c2712ce30700b90306487de27c
Author: duc91 <vd...@linagora.com>
AuthorDate: Tue Aug 11 16:11:16 2020 +0700

    JAMES-3349: Add test for handle serverFail
---
 .../jmap/rfc8621/contract/EchoMethodContract.scala |   4 +-
 .../contract/MailboxGetMethodContract.scala        |   2 +-
 .../org/apache/james/jmap/model/Invocation.scala   |  10 +-
 .../james/jmap/routes/JMAPApiRoutesTest.scala      | 103 ++++++++++++++++++++-
 4 files changed, 109 insertions(+), 10 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EchoMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EchoMethodContract.scala
index 2306bc7..fcc9564 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EchoMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EchoMethodContract.scala
@@ -49,7 +49,7 @@ object EchoMethodContract {
       |    [
       |      "error",
       |      {
-      |        "type": "Not implemented"
+      |        "type": "unknownMethod"
       |      },
       |      "notsupport"
       |    ]
@@ -71,7 +71,7 @@ object EchoMethodContract {
       |    [
       |      "error",
       |      {
-      |        "type": "Not implemented"
+      |        "type": "unknownMethod"
       |      },
       |      "notsupport"
       |    ]
diff --git a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxGetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxGetMethodContract.scala
index 0e4a2af..b2c2d31 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxGetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxGetMethodContract.scala
@@ -594,7 +594,7 @@ trait MailboxGetMethodContract {
          |  "sessionState": "75128aab4b1b",
          |  "methodResponses": [[
          |   "error", {
-         |     "type": "invalidArguments",
+         |     "type": "error",
          |     "description": "The following properties [invalidProperty] do not exist."
          |},
          |    "c1"]]
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala
index 7d80d0b..331d2f1 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/model/Invocation.scala
@@ -35,16 +35,14 @@ object Invocation {
   case class MethodCallId(value: NonEmptyString)
 
 
-  def error(errorCode: ErrorCode, description: NonEmptyString, methodCallId: MethodCallId): Invocation = {
-    Invocation(MethodName("error"),
+  def error(errorCode: ErrorCode, description: String, methodCallId: MethodCallId): Invocation = Invocation(MethodName("error"),
       Arguments(JsObject(Map("type" -> JsString(errorCode.code), "description" -> JsString(description)))),
       methodCallId)
-  }
-  def error(errorCode: ErrorCode, methodCallId: MethodCallId): Invocation = {
-    Invocation(MethodName("error"),
+
+  def error(errorCode: ErrorCode, methodCallId: MethodCallId): Invocation = Invocation(MethodName("error"),
       Arguments(JsObject(Map("type" -> JsString(errorCode.code)))),
       methodCallId)
-  }
+
 }
 
 sealed trait ErrorCode {
diff --git a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala
index 3cf728b..8c2f9b8 100644
--- a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala
+++ b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets
 import java.util.Base64
 
 import com.google.common.collect.ImmutableSet
+import eu.timepit.refined.auto._
 import io.netty.handler.codec.http.HttpHeaderNames.ACCEPT
 import io.restassured.RestAssured
 import io.restassured.builder.RequestSpecBuilder
@@ -38,6 +39,8 @@ import org.apache.james.jmap._
 import org.apache.james.jmap.http.{Authenticator, BasicAuthenticationStrategy, MailboxesProvisioner, UserProvisioning}
 import org.apache.james.jmap.json.Serializer
 import org.apache.james.jmap.method.{CoreEchoMethod, Method}
+import org.apache.james.jmap.model.CapabilityIdentifier.CapabilityIdentifier
+import org.apache.james.jmap.model.Invocation.MethodName
 import org.apache.james.jmap.model.RequestLevelErrorType
 import org.apache.james.jmap.routes.JMAPApiRoutesTest._
 import org.apache.james.mailbox.extension.PreDeletionHook
@@ -47,7 +50,8 @@ import org.apache.james.mailbox.store.StoreSubscriptionManager
 import org.apache.james.metrics.tests.RecordingMetricFactory
 import org.apache.james.user.memory.MemoryUsersRepository
 import org.hamcrest.Matchers.equalTo
-import org.mockito.Mockito.mock
+import org.mockito.ArgumentMatchers.any
+import org.mockito.Mockito.{doThrow, mock, when}
 import org.scalatest.BeforeAndAfter
 import org.scalatest.flatspec.AnyFlatSpec
 import org.scalatest.matchers.should.Matchers
@@ -98,6 +102,32 @@ object JMAPApiRoutesTest {
       |    ]
       |  ]
       |}""".stripMargin
+
+  private val REQUEST_OBJECT_WITH_2_ECHO_METHOD_CALL: String =
+    """{
+      |  "using": [
+      |    "urn:ietf:params:jmap:core"
+      |  ],
+      |  "methodCalls": [
+      |    [
+      |      "Core/echo",
+      |      {
+      |        "arg1": "arg1data",
+      |        "arg2": "arg2data"
+      |      },
+      |      "c1"
+      |    ],
+      |    [
+      |    "Core/echo",
+      |      {
+      |        "arg1": "arg1data",
+      |        "arg2": "arg2data"
+      |      },
+      |      "c2"
+      |     ]
+      |  ]
+      |}""".stripMargin
+
   private val REQUEST_OBJECT_WITH_UNSUPPORTED_METHOD: String =
     """{
       |  "using": [
@@ -136,6 +166,30 @@ object JMAPApiRoutesTest {
       |    ]
       |  ]
       |}""".stripMargin
+
+  private val SERVER_FAIL_RESPONSE_OBJECT: String =
+    """{
+      |  "sessionState": "75128aab4b1b",
+      |  "methodResponses": [
+      |    [
+      |      "error",
+      |      {
+      |        "type": "serverFail",
+      |        "description": "Unexpected Exception occur, the others method may proceed normally"
+      |      },
+      |      "c1"
+      |    ],
+      |    [
+      |      "Core/echo",
+      |      {
+      |        "arg1": "arg1data",
+      |        "arg2": "arg2data"
+      |      },
+      |      "c2"
+      |    ]
+      |  ]
+      |}""".stripMargin
+
   private val RESPONSE_OBJECT_WITH_UNSUPPORTED_METHOD: String =
     """{
       |  "sessionState": "75128aab4b1b",
@@ -378,4 +432,51 @@ class JMAPApiRoutesTest extends AnyFlatSpec with BeforeAndAfter with Matchers {
         .body("type", equalTo(RequestLevelErrorType.UNKNOWN_CAPABILITY.value))
         .body("detail", equalTo("The request used unsupported capabilities: Set(urn:ietf:params:jmap:core1)"))
   }
+
+  "RFC-8621 with random error when processing request " should "return 200, with serverFail error, others method call proceed normally" in {
+    val mockCoreEchoMethod = mock(classOf[CoreEchoMethod])
+
+    doThrow(new RuntimeException("Unexpected Exception occur, the others method may proceed normally"))
+      .doCallRealMethod()
+      .when(mockCoreEchoMethod)
+      .process(any[Set[CapabilityIdentifier]], any(), any())
+
+    when(mockCoreEchoMethod.methodName).thenReturn(MethodName("Core/echo"))
+
+    val methods: Set[Method] = Set(mockCoreEchoMethod)
+    val apiRoute: JMAPApiRoutes = new JMAPApiRoutes(AUTHENTICATOR, SERIALIZER, userProvisionner, mailboxesProvisioner, methods)
+    val routesHandler: ImmutableSet[JMAPRoutesHandler] = ImmutableSet.of(new JMAPRoutesHandler(Version.RFC8621, apiRoute))
+
+    val versionParser: VersionParser = new VersionParser(SUPPORTED_VERSIONS)
+    jmapServer = new JMAPServer(TEST_CONFIGURATION, routesHandler, versionParser)
+    jmapServer.start()
+
+    RestAssured.requestSpecification = new RequestSpecBuilder()
+      .setContentType(ContentType.JSON)
+      .setAccept(ContentType.JSON)
+      .setConfig(newConfig.encoderConfig(encoderConfig.defaultContentCharset(StandardCharsets.UTF_8)))
+      .setPort(jmapServer.getPort.getValue)
+      .setBasePath(JMAP)
+      .build
+
+    val headers: Headers = Headers.headers(
+      new Header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER),
+      new Header("Authorization", s"Basic ${userBase64String}")
+    )
+
+    val response = RestAssured
+      .`given`()
+        .headers(headers)
+        .body(REQUEST_OBJECT_WITH_2_ECHO_METHOD_CALL)
+      .when()
+        .post()
+      .`then`
+        .statusCode(HttpStatus.SC_OK)
+        .contentType(ContentType.JSON)
+      .extract()
+        .body()
+        .asString()
+
+    assertThatJson(response).isEqualTo(SERVER_FAIL_RESPONSE_OBJECT)
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org