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/04/13 02:53:36 UTC

[james-project] 09/13: JAMES-2888: Finish work, add testcase, rebase from master

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 b5679e9485d99c1110ea2db1628ac3d7a080c8fb
Author: ducnv <du...@gmail.com>
AuthorDate: Fri Mar 27 16:47:10 2020 +0700

    JAMES-2888: Finish work, add testcase, rebase from master
---
 .../james/jmap/rfc/api/method/EchoMethod.scala     | 48 ---------------
 .../jmap/rfc/api/parser/RequestObjectParser.scala  | 16 -----
 .../james/jmap/rfc/api/routes/JMAPAPIRoute.scala   | 13 ----
 .../james/jmap/rfc/api/routes/JMAPApiRoutes.scala  | 50 ----------------
 .../apache/james/jmap/routes/JMAPApiRoutes.scala   | 69 ++++++++++++++++++++++
 .../apache/james/jmap/method/CoreEchoTest.scala}   | 32 ++++++----
 .../james/jmap/rfc/api/method/EchoMethodTest.scala | 26 --------
 7 files changed, 89 insertions(+), 165 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/method/EchoMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/method/EchoMethod.scala
deleted file mode 100644
index fabc9cc..0000000
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/method/EchoMethod.scala
+++ /dev/null
@@ -1,48 +0,0 @@
-/** **************************************************************
- * 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.james.jmap.rfc.api.method
-
-import eu.timepit.refined.auto._
-import org.apache.james.jmap.rfc.api.parser.RequestObjectParser
-import org.apache.james.jmap.rfc.api.routes.{JMAPAPIRoute, MethodName}
-import org.apache.james.jmap.rfc.model.{RequestObject, ResponseObject}
-import org.reactivestreams.Publisher
-import reactor.core.publisher.Mono
-import reactor.netty.http.server.HttpServerRequest
-
-class EchoMethod extends JMAPAPIRoute[ResponseObject] {
-
-  private val requestObjectParser: RequestObjectParser = new RequestObjectParser()
-
-  def toResponseObject(requestObject: RequestObject): ResponseObject = {
-    ResponseObject(ResponseObject.SESSION_STATE, requestObject.methodCalls)
-  }
-
-  override var methodName = MethodName("echoMethod")
-
-  override def process(httpServerRequest: HttpServerRequest): Publisher[ResponseObject] = {
-    httpServerRequest
-      .receive()
-      .asInputStream()
-      .flatMap(requestObjectParser.toRequestObject)
-      .flatMap((requestObject: RequestObject) => {
-        return Mono.just(new ResponseObject(ResponseObject.SESSION_STATE, requestObject.methodCalls))
-      })
-  }
-}
\ No newline at end of file
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/parser/RequestObjectParser.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/parser/RequestObjectParser.scala
deleted file mode 100644
index 154d6da..0000000
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/parser/RequestObjectParser.scala
+++ /dev/null
@@ -1,16 +0,0 @@
-package org.apache.james.jmap.rfc.api.parser
-
-import java.io.InputStream
-
-import org.apache.james.jmap.rfc.model.RequestObject
-import play.api.libs.json.{JsError, JsSuccess, Json}
-import reactor.core.publisher.Mono
-
-class RequestObjectParser {
-  def toRequestObject(inputStream: InputStream): Mono[RequestObject] = {
-    Json.fromJson[RequestObject](Json.parse(inputStream)) match {
-      case JsSuccess(requestObject, _) => Mono.just(requestObject)
-      case JsError(errors) => Mono.error(new RuntimeException(errors.toString()))
-    }
-  }
-}
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPAPIRoute.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPAPIRoute.scala
deleted file mode 100644
index d505795..0000000
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPAPIRoute.scala
+++ /dev/null
@@ -1,13 +0,0 @@
-package org.apache.james.jmap.rfc.api.routes
-
-import eu.timepit.refined.api.Refined
-import eu.timepit.refined.collection.NonEmpty
-import org.reactivestreams.Publisher
-import reactor.netty.http.server.HttpServerRequest
-
-case class MethodName(value: String Refined NonEmpty)
-trait JMAPAPIRoute[T] {
-  var methodName: MethodName
-  def process(httpServerRequest: HttpServerRequest):Publisher[T]
-}
-
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPApiRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPApiRoutes.scala
deleted file mode 100644
index 973ca77..0000000
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPApiRoutes.scala
+++ /dev/null
@@ -1,50 +0,0 @@
-/** **************************************************************
- * 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.james.jmap.rfc.api.routes
-
-import io.netty.handler.codec.http.HttpResponseStatus
-import org.apache.james.jmap.rfc.api.method.EchoMethod
-import reactor.core.publisher.Mono
-import reactor.netty.http.server.{HttpServerRequest, HttpServerResponse}
-
-object JMAPApiRoutes {
-  private val ECHO_METHOD = new EchoMethod();
-  private val METHOD_NAME_PARAMETER = "method-name"
-}
-
-class JMAPApiRoutes {
-  def post(httpServerRequest: HttpServerRequest, httpServerResponse: HttpServerResponse): Mono[Void] = {
-    Mono.just(httpServerRequest)
-      .flatMap(httpRequest => this.process(httpRequest, httpServerResponse))
-      .`then`()
-  }
-
-  def process(httpRequest: HttpServerRequest, httpServerResponse: HttpServerResponse) = {
-    httpRequest.param(JMAPApiRoutes.METHOD_NAME_PARAMETER) match {
-      case JMAPApiRoutes.ECHO_METHOD.methodName.value.value => Mono.just(httpServerResponse
-        .status(HttpResponseStatus.OK)
-        .sendObject(JMAPApiRoutes.ECHO_METHOD.process(httpRequest))).`then`()
-
-      case _ => Mono.just(httpServerResponse
-        .status(HttpResponseStatus.NOT_IMPLEMENTED)
-        .sendObject("Api not implemented")).`then`()
-    }
-  }
-}
-
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala
new file mode 100644
index 0000000..ddfe2fe
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala
@@ -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.james.jmap.routes
+
+import eu.timepit.refined.auto._
+import org.apache.james.jmap.json.Serializer
+import org.apache.james.jmap.method.CoreEcho
+import org.apache.james.jmap.model.Invocation.{Arguments, MethodName}
+import org.apache.james.jmap.model.{Invocation, RequestObject, ResponseObject}
+import org.reactivestreams.Publisher
+import play.api.libs.json.{JsError, JsSuccess, Json}
+import reactor.core.scala.publisher.SMono
+import reactor.netty.http.server.{HttpServerRequest, HttpServerResponse}
+
+object JMAPApiRoutes {
+  private val ECHO_METHOD = new CoreEcho()
+}
+
+class JMAPApiRoutes {
+  private val echoMethod = JMAPApiRoutes.ECHO_METHOD
+
+  def post(httpServerRequest: HttpServerRequest, httpServerResponse: HttpServerResponse): SMono[Void] = {
+    SMono.fromPublisher(extractRequestObject(httpServerRequest))
+      .flatMap(this.process)
+      .doOnError(e => new RuntimeException(e.getMessage))
+      .`then`()
+  }
+
+  private def process(requestObject: RequestObject): SMono[ResponseObject] = {
+    SMono.just(
+      requestObject.methodCalls.map((invocation: Invocation) =>
+        invocation.methodName match {
+          case echoMethod.methodName => echoMethod.process(invocation)
+          case _ => SMono.just(new Invocation(
+            MethodName("error"),
+            Arguments(Json.obj("type" -> "Not implemented")),
+            invocation.methodCallId))
+        }
+      )
+    ).flatMap((invocations: Seq[Invocation]) => SMono.just(ResponseObject(ResponseObject.SESSION_STATE, invocations)))
+  }
+
+  private def extractRequestObject(httpServerRequest: HttpServerRequest): Publisher[RequestObject] = {
+    httpServerRequest
+      .receive()
+      .asInputStream()
+      .flatMap(inputStream => new Serializer().deserializeRequestObject(inputStream) match {
+        case JsSuccess(requestObject, _) => SMono.just(new ResponseObject(ResponseObject.SESSION_STATE, requestObject.methodCalls))
+        case JsError(errors) => SMono.raiseError(new RuntimeException(errors.toString()))
+      })
+  }
+}
+
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/model/ResponseObject.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/method/CoreEchoTest.scala
similarity index 52%
rename from server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/model/ResponseObject.scala
rename to server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/method/CoreEchoTest.scala
index 90ee590..b723321 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/model/ResponseObject.scala
+++ b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/method/CoreEchoTest.scala
@@ -16,23 +16,31 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  * ***************************************************************/
+package org.apache.james.jmap.method
 
-package org.apache.james.jmap.rfc.model
+import org.apache.james.jmap.json.Fixture.{invocation1, invocation2}
+import org.apache.james.jmap.model.Invocation
+import org.scalatest.matchers.should.Matchers
+import org.scalatest.wordspec.AnyWordSpec
 
-import eu.timepit.refined.types.string.NonEmptyString
-import org.apache.james.jmap.rfc.model.ResponseObject.SessionState
-import play.api.libs.json.{JsResult, Json}
+class CoreEchoTest extends AnyWordSpec with Matchers {
+  private val echoMethod: CoreEcho = new CoreEcho()
 
-case class ResponseObject(sessionState: SessionState, methodResponses: Seq[Invocation])
+  "CoreEcho" should {
+    "Process" should {
+      "success and return the same with parameters as the invocation request" in {
+        val expectedResponse: Invocation = invocation1
+        val dataResponse = SMono.fromPublisher(echoMethod.process(invocation1)).block()
 
-object ResponseObject {
+        dataResponse shouldBe expectedResponse
+      }
 
-  case class SessionState(value: NonEmptyString)
+      "success and not return anything else different than the original invocation" in {
+        val wrongExpected: Invocation = invocation2
+        val dataResponse = SMono.fromPublisher(echoMethod.process(invocation1)).block()
 
-  implicit val sessionStateFormat = Json.valueFormat[SessionState]
-  implicit val responseObjectFormat = Json.format[ResponseObject]
-
-  def deserialize(input: String): JsResult[ResponseObject] = {
-    Json.parse(input).validate[ResponseObject]
+        dataResponse should not be(wrongExpected)
+      }
+    }
   }
 }
diff --git a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/rfc/api/method/EchoMethodTest.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/rfc/api/method/EchoMethodTest.scala
deleted file mode 100644
index 564745f..0000000
--- a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/rfc/api/method/EchoMethodTest.scala
+++ /dev/null
@@ -1,26 +0,0 @@
-/** **************************************************************
- * 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.james.jmap.rfc.api.method
-
-import org.scalatestplus.play.PlaySpec
-
-class EchoMethodTest extends PlaySpec {
-  "EchoMethod succeed" must {
-  }
-}


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