ProcessorCreatorTest.scala 7.9 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1
/*
2
 * Copyright (c) 2022 bitlap
梦境迷离's avatar
梦境迷离 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

梦境迷离's avatar
梦境迷离 已提交
22
package org.bitlap.tools
梦境迷离's avatar
梦境迷离 已提交
23 24

import com.alipay.sofa.jraft.rpc.{ RpcContext, RpcRequestClosure, RpcRequestProcessor }
梦境迷离's avatar
梦境迷离 已提交
25
import org.bitlap.tools.test.proto.BOpenSession.{ BOpenSessionReq, BOpenSessionResp }
梦境迷离's avatar
梦境迷离 已提交
26 27
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
28
import org.bitlap.tools.method.ProcessorCreator
梦境迷离's avatar
梦境迷离 已提交
29 30

import java.util.concurrent.Executor
31
import scala.jdk.CollectionConverters.MapHasAsScala
梦境迷离's avatar
梦境迷离 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

/**
 *
 * @author 梦境迷离
 * @version 1.0,2021/12/6
 */
class ProcessorCreatorTest extends AnyFlatSpec with Matchers {

  // please exec `sbt compile` to generate java class fof the protobuf
  // origin
  "ProcessorCreator1" should "compile ok" in {
    implicit val service = new NetService
    implicit val executor: Executor = new Executor {
      override def execute(command: Runnable): Unit = ()
    }
    val openSession = ProcessorCreator[RpcRequestClosure, RpcRequestProcessor, RpcContext, BOpenSessionReq, BOpenSessionResp, NetService, Executor](
      BOpenSessionResp.getDefaultInstance,
      (service, rpcRequestClosure, req) => {
        val username = req.getUsername
        val password = req.getPassword
        val configurationMap = req.getConfigurationMap
        val ret = service.openSession(username, password, configurationMap.asScala.toMap)
        BOpenSessionResp.newBuilder().setSessionHandle(ret).build()
      },
      (service, rpcContext, exception) => {
        BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
      }
    )

    println(openSession.defaultResp)

    println(openSession.getClass.getClass.getName)

    println(openSession.interest())
  }

  // simple v1
  "ProcessorCreator2" should "compile ok" in {
    implicit val service = new NetService
    val openSession = ProcessorCreator[RpcRequestClosure, RpcRequestProcessor, RpcContext, BOpenSessionReq, BOpenSessionResp, NetService](
      (service, _, req) => {
        val username = req.getUsername
        val password = req.getPassword
        val configurationMap = req.getConfigurationMap
        val ret = service.openSession(username, password, configurationMap.asScala.toMap)
        BOpenSessionResp.newBuilder().setSessionHandle(ret).build()
      },
      (_, _, exception) => {
        BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
      }
    )

    println(openSession.defaultResp)

    println(openSession.getClass.getClass.getName)

    println(openSession.interest())
  }

  // simple v2
  "ProcessorCreator3" should "compile ok" in {
    // NetService must be a class and with an no parameter construction
    val openSession = ProcessorCreator[NetService, RpcRequestClosure, RpcRequestProcessor, RpcContext, BOpenSessionReq, BOpenSessionResp](
      (service, rpc, req) => {
        val username = req.getUsername
        val password = req.getPassword
        val configurationMap = req.getConfigurationMap
        val ret = service.openSession(username, password, configurationMap.asScala.toMap)
        BOpenSessionResp.newBuilder().setSessionHandle(ret).build()
梦境迷离's avatar
梦境迷离 已提交
101 102 103 104 105
      })(
        (service, rpc, exception) => {
          BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
        }
      )
梦境迷离's avatar
梦境迷离 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    println(openSession.defaultResp)

    println(openSession.getClass.getClass.getName)

    println(openSession.interest())
  }

  // error
  "ProcessorCreator4" should "compile error" in {
    trait Service
    """
      | val openSession = ProcessorCreator[Service, RpcRequestClosure, RpcRequestProcessor, RpcContext, BOpenSessionReq, BOpenSessionResp](
      |      (service, rpc, req) => {
      |        BOpenSessionResp.newBuilder().setSessionHandle(null).build()
梦境迷离's avatar
梦境迷离 已提交
121
      |      })(
梦境迷离's avatar
梦境迷离 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      |      (service, rpc, exception) => {
      |        BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
      |      }
      |    )
      |
      |    println(openSession.defaultResp)
      |
      |    println(openSession.getClass.getClass.getName)
      |
      |    println(openSession.interest())
      |""".stripMargin shouldNot compile
  }

  // error
  "ProcessorCreator5" should "compile error" in {
    trait Service
    """
      | val openSession = ProcessorCreator[Service, RpcRequestProcessor, RpcRequestClosure, RpcContext, BOpenSessionReq, BOpenSessionResp](
      |      (service, rpc, req) => {
      |        BOpenSessionResp.newBuilder().setSessionHandle(null).build()
梦境迷离's avatar
梦境迷离 已提交
142
      |      })(
梦境迷离's avatar
梦境迷离 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      |      (service, rpc, exception) => {
      |        BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
      |      }
      |    )
      |
      |    println(openSession.defaultResp)
      |
      |    println(openSession.getClass.getClass.getName)
      |
      |    println(openSession.interest())
      |""".stripMargin shouldNot compile
  }

  // error
  "ProcessorCreator6" should "compile error" in {
    object Service
    """
梦境迷离's avatar
梦境迷离 已提交
160
      | val openSession = ProcessorCreator[Service.type, RpcRequestClosure, RpcRequestProcessor, RpcContext, BOpenSessionReq, BOpenSessionResp](
梦境迷离's avatar
梦境迷离 已提交
161 162
      |      (service, rpc, req) => {
      |        BOpenSessionResp.newBuilder().setSessionHandle(null).build()
梦境迷离's avatar
梦境迷离 已提交
163
      |      })(
梦境迷离's avatar
梦境迷离 已提交
164 165 166 167 168 169 170 171 172 173 174 175
      |      (service, rpc, exception) => {
      |        BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
      |      }
      |    )
      |
      |    println(openSession.defaultResp)
      |
      |    println(openSession.getClass.getClass.getName)
      |
      |    println(openSession.interest())
      |""".stripMargin shouldNot compile
  }
梦境迷离's avatar
梦境迷离 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

  // not support interface, because it use runtime reflect to create `NetService` instance.
  "ProcessorCreator7" should "compile ok" in {
    """
      |    trait NetService {
      |      def openSession(username: String, password: String, configuration: Map[String, String] = Map.empty): String
      |    }
      |    class NetServiceImpl extends NetService {
      |      override def openSession(username: String, password: String, configuration: Map[String, String] = Map.empty): String = {
      |        username + password
      |      }
      |    }
      |    implicit val service: NetService = null
      |    val openSession = ProcessorCreator[NetService, RpcRequestClosure, RpcRequestProcessor, RpcContext, BOpenSessionReq, BOpenSessionResp](
      |      (service, _, req) => {
      |        import scala.jdk.CollectionConverters.MapHasAsScala
      |        val username = req.getUsername
      |        val password = req.getPassword
      |        val configurationMap = req.getConfigurationMap
      |        val ret = service.openSession(username, password, configurationMap.asScala.toMap)
      |        BOpenSessionResp.newBuilder().setSessionHandle(ret).build()
      |      })(
      |      (_, _, exception) => {
      |        BOpenSessionResp.newBuilder().setStatus(exception.getLocalizedMessage).build()
      |      }
      |    )
      |""".stripMargin shouldNot compile
  }
梦境迷离's avatar
梦境迷离 已提交
204
}