no suitable method found for configured finagle in Java code - scala

I've Java polyglot application which mainly has Java code and uses few Scala libraries as well.
The Scala below code compiles fine.
import com.twitter.finagle.Http.Client
import com.twitter.finagle.Http
import com.twitter.finagle.param.Logger
object Main extends App {
private val loggerFinagle = java.util.logging.Logger.getLogger("FinagleLogger")
val client: Client = Http.client
.configured(Logger(loggerFinagle))
}
I write the same code in java as below
import com.twitter.finagle.Http;
import com.twitter.finagle.param.Logger;
public class JavaMain {
public static void main(String[] args) {
java.util.logging.Logger loggerFinagle = java.util.logging.Logger.getLogger("FinagleLogger");
Http.client()
.configured(new Logger(loggerFinagle));
}
}
When I compile the code I get below error -
[info] Compiling 1 Scala source and 1 Java source to /Users/rajkumar.natarajan/Documents/Coding/finagle-errors/target/scala-2.12/classes ...
[error] /Users/rajkumar.natarajan/Documents/Coding/finagle-errors/src/main/java/JavaMain.java:10:1: no suitable method found for configured(com.twitter.finagle.param.Logger)
[error] method com.twitter.finagle.Stack.Parameterized.<P>configured(P,com.twitter.finagle.Stack.Param<P>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (actual and formal argument lists differ in length))
[error] method com.twitter.finagle.Stack.Parameterized.<P>configured(scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (argument mismatch; com.twitter.finagle.param.Logger cannot be converted to scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>))
[error] method com.twitter.finagle.client.StackClient.<P>configured(P,com.twitter.finagle.Stack.Param<P>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (actual and formal argument lists differ in length))
[error] method com.twitter.finagle.client.StackClient.<P>configured(scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (argument mismatch; com.twitter.finagle.param.Logger cannot be converted to scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>))
[error] method com.twitter.finagle.client.EndpointerStackClient.<P>configured(P,com.twitter.finagle.Stack.Param<P>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (actual and formal argument lists differ in length))
[error] method com.twitter.finagle.client.EndpointerStackClient.<P>configured(scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (argument mismatch; com.twitter.finagle.param.Logger cannot be converted to scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>))
[error] method com.twitter.finagle.Http.Client.<P>configured(scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>) is not applicable
[error] (cannot infer type-variable(s) P
[error] (argument mismatch; com.twitter.finagle.param.Logger cannot be converted to scala.Tuple2<P,com.twitter.finagle.Stack.Param<P>>))
[error] Http.client()
[error] .configured
Looks like I've to pass the Tuple but I don't know how.
The project in github is here.
Below are dependency details of my project -
Scala Version - 2.12.6
Java Version - 1.8.0_151
finagle version - 7.1.0

According to the docs, you need to call the mk() method on the param, so something like the following should create the proper tuple:
Http.client()
.configured(new Logger(loggerFinagle).mk());

Related

Object kkapi is not a member of package

I am trying to import a module from another project and did as following:
As you can see on the image, the imported library is /home/developer/...kafka-api.
I am using the importing library in testing.
When I compile my spec files with statement test:compile and I've got following error:
[IJ]sbt:auth_stream> test:compile
[info] Compiling 3 Scala sources to /home/developer/Desktop/microservices/bary/auth-stream/target/scala-2.12/test-classes ...
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:14:20: object kkapi is not a member of package io.khinkali
[error] import io.khinkali.kkapi.consumer.{KkConsumer, KkConsumerConfig, KkConsumerCreator}
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:15:20: object kkapi is not a member of package io.khinkali
[error] import io.khinkali.kkapi.producer.{KkProducer, KkProducerCreator, MaxBlockMsConfig}
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:24:56: not found: value KkConsumer
[error] private val consumer: IO[Consumer[String, String]] = KkConsumer.create(createConsumer())
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:52:5: not found: type KkConsumerCreator
[error] : KkConsumerCreator
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:25:56: not found: value KkProducer
[error] private val producer: IO[Producer[String, String]] = KkProducer.create(createProducer())
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:46:5: not found: type KkProducerCreator
[error] : KkProducerCreator
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:47:5: not found: value KkProducerCreator
[error] = KkProducerCreator(sys.env.get("KAFKA_SERVER").get,
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:49:10: not found: value MaxBlockMsConfig
[error] List(MaxBlockMsConfig(2000)))
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:53:5: not found: value KkConsumerCreator
[error] = KkConsumerCreator(sys.env.get("KAFKA_SERVER").get,
[error] ^
[error] /home/developer/Desktop/microservices/bary/auth-stream/src/test/scala/io/khinkali/auth/AppSpec.scala:57:16: not found: type KkConsumerConfig
[error] List.empty[KkConsumerConfig])
[error] ^
[error] 10 errors found
[error] (test:compileIncremental) Compilation failed
What am I doing wrong?
Hint, that the package of both project starts with the name, namely:
The current project:
The imported project:
As you can see, the name differs only at the end. Could it be the problem?
What I am trying to approach is, to use a function for kafka-api project.

Custom converters in Scallop library

I am trying to use Scallop (https://github.com/scallop/scallop/) to parse the command-line arguments in Scala.
However, I cannot compile the example for converting arguments to a case class as shown on https://github.com/scallop/scallop/wiki/Custom-converters.
I get two errors at compile-time:
[error] found : org.rogach.scallop.ValueConverter[center.scala.sbk.Commands.Person]{val nameRgx: scala.util.matching.Regex; val phoneRgx: scala.util.matching.Regex}
[error] required: String
[error] Error occurred in an application involving default arguments.
[error] val person = opt[Person](personConverter)
[error] ^
and
[error] ...: could not find implicit value for parameter conv: org.rogach.scallop.ValueConverter[center.scala.sbk.Commands.Person]
[error] Error occurred in an application involving default arguments.
[error] val person = opt[Person](personConverter)
[error] ^
Thank you for your help!
The documentation has a typo.
Use
val person = opt[Person]()(personConverter) // note the parentheses
instead of
val person = opt[Person](personConverter)
since personConverter is passed in implicit arguments.

No Json serializer as JsObject found for type play.api.libs.json.JsValue

I'm trying to migrate my project from using play2-reactivemongo version 0.10.5.0.akka23 to using version 0.11.7.play23. I've already added the following import to fix the problems addressed in this question:
import play.modules.reactivemongo.json._
With the former version, the following code worked:
val updateEntity = Json.obj("_id" -> Json.obj("$oid" -> id))
val entity = Json.parse(stringJson)
collection.update(updateEntity, entity)
however, using the new version, the third line gives a compile error:
[error] No Json serializer as JsObject found for type play.api.libs.json.JsValue. Try to implement an implicit OWrites or OFormat for this type.
[error] collection.update(updateEntity, entity)
[error] ^
I've tried introducing an implicit OWriter:
implicit val toJsObject: OWrites[JsValue] = OWrites.apply(_.as[JsObject])
but that gives an implicit declaration conflict:
[error] ambiguous implicit values:
[error] both value toJsObject of type play.api.libs.json.OWrites[play.api.libs.json.JsValue]
[error] and object JsObjectDocumentWriter in trait ImplicitBSONHandlers of type play.modules.reactivemongo.json.JsObjectDocumentWriter.type
[error] match expected type collection.pack.Writer[play.api.libs.json.JsObject]
[error] collection.update(updateEntity, entity)
[error] ^
changing the second line to
val entity = Json.parse(stringJson).as[JsObject]
fixes the problem, but I have a lot of these around my code and I was hoping for a simpler solution.
Just got bit by this too. The trick is to remove
import play.modules.reactivemongo.json._
and instead use
import reactivemongo.play.json._
The play.modules version doesn't provide the identity OWriter.

sbt assembly can't find type in the same package

I have the code shown here
http://scastie.org/10156
report the error
when running sbt 0.13.8
plugin assembly
I get
[error] /home/xx/workspace/sync-hsae-middleware/src/test/scala/com/example/MyServiceSpec.scala:8: not found: type MyService
[error] class MyServiceSpec extends Specification with Specs2RouteTest with MyService {
[error] ^
[error] /home/xx/workspace/sync-hsae-middleware/src/test/scala/com/example/MyServiceSpec.scala:14: not found: value myRoute
[error] Get() ~> myRoute ~> check {
[error] ^
[error] two errors found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 5 s, completed 10/06/2015 15:00:15
*/
I can't figure out what is the issue because when I use
sbt run it works well.

Is it possible to mock Scala methods with view bounds with Mockito and Specs2?

I've come across a problem when mocking methods that have view bounds on the type of their parameters using Mockito and Specs2. Simply put, since a view bound translates to an extra implicit argument to the method, Mockito cannot reconcile the call described by an expectation to the actual arguments the mock recieves:
For example:
//Receiver.scala
class Receiver {
def methodWithViewBound[T <% WrappedString](w : T) : Unit = {
//noop!
}
}
//WrappedString.scala
class WrappedString(val wrapped : String) {
}
//TestMockedMethodWithViewBound.scala
import org.specs2.mutable._
import org.specs2.specification._
import org.specs2.mock._
import org.mockito.Matchers
class TestMockedMethodWithViewBound extends Specification with Mockito {
implicit def wrapString(s : String) : WrappedString = new WrappedString(s);
implicit def unwrapString(w : WrappedString) : String = w.wrapped;
"Mockito" should {
"Allow mocking of methods whose argument types include a view bound, using a matcher" in {
val receiver = mock[Receiver]
receiver.methodWithViewBound("Testing")
there was one(receiver).methodWithViewBound(Matchers.eq("Testing"))
}
"Allow mocking of methods whose argument types include a view bound, using an argument literal" in {
val receiver = mock[Receiver]
receiver.methodWithViewBound("Testing")
there was one(receiver).methodWithViewBound("Testing")
}
}
}
The output of this gives:
[15:18:46] [tim#ahtanum ../tim/Projects/MockitoTest] $ sbt test
[info] Set current project to Mockito View Bounds Test (in build file:/home/tim/Projects/MockitoTest/)
[info] Compiling 1 Scala source to /home/tim/Projects/MockitoTest/target/scala-2.9.1/test-classes...
[info] Mockito should
[error] x Allow mocking of methods whose argument types include a view bound
[error] The mock was not called as expected:
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded.
[error] This exception may occur if matchers are combined with raw values:
[error] //incorrect:
[error] someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error] //correct:
[error] someMethod(anyObject(), eq("String by matcher"));
[error]
[error] For more info see javadoc for Matchers class. (TestMockedMethodWithViewBound.scala:12)
[info]
[info]
[info] Total for specification TestMockedMethodWithViewBound
[info] Finished in 175 ms
[info] 1 example, 1 failure, 0 error
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0
[error] Failed tests:
[error] TestMockedMethodWithViewBound
[error] {file:/home/tim/Projects/MockitoTest/}default-3adcf2/test:test: Tests unsuccessful
[error] Total time: 7 s, completed 07-Mar-2012 15:19:47
[15:19:47] [tim#ahtanum ../tim/Projects/MockitoTest] $ sbt test
[info] Set current project to Mockito View Bounds Test (in build file:/home/tim/Projects/MockitoTest/)
[info] Compiling 1 Scala source to /home/tim/Projects/MockitoTest/target/scala-2.9.1/test-classes...
[info] Mockito should
[error] x Allow mocking of methods whose argument types include a view bound, using a matcher
[error] The mock was not called as expected:
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded.
[error] This exception may occur if matchers are combined with raw values:
[error] //incorrect:
[error] someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error] //correct:
[error] someMethod(anyObject(), eq("String by matcher"));
[error]
[error] For more info see javadoc for Matchers class. (TestMockedMethodWithViewBound.scala:12)
[error] x Allow mocking of methods whose argument types include a view bound, using an argument literal
[error] The mock was not called as expected:
[error] Argument(s) are different! Wanted:
[error] receiver.methodWithViewBound(
[error] "Testing",
[error] ($anonfun$apply$mcV$sp$2) <function1>
[error] );
[error] -> at TestMockedMethodWithViewBound$$anonfun$1$$anonfun$apply$6$$anonfun$apply$2.apply$mcV$sp(TestMockedMethodWithViewBound.scala:22)
[error] Actual invocation has different arguments:
[error] receiver.methodWithViewBound(
[error] "Testing",
[error] ($anonfun$apply$7) <function1>
[error] );
[error] -> at TestMockedMethodWithViewBound$$anonfun$1$$anonfun$apply$6.apply(TestMockedMethodWithViewBound.scala:21)
[error] (TestMockedMethodWithViewBound.scala:19)
[info]
[info]
[info] Total for specification TestMockedMethodWithViewBound
[info] Finished in 325 ms
[info] 2 examples (+1), 2 expectations (+1), 2 failures (+1), 0 error
[error] Failed: : Total 2, Failed 2, Errors 0, Passed 0, Skipped 0
[error] Failed tests:
[error] TestMockedMethodWithViewBound
[error] {file:/home/tim/Projects/MockitoTest/}default-3adcf2/test:test: Tests unsuccessful
[error] Total time: 7 s, completed 07-Mar-2012 15:23:05
...has anyone come across this problem before, or come across any way of mocking methods with view bounds or implicit parameters using specs2?
Thanks,
Tim
Tim, I've added the possibility to verify converted parameters to the latest specs2 1.9-SNAPSHOT.
This is still experimental as I don't what kind of undesired effect could come out of that.
One thing, for example, is that any Function1 parameter will now be verified as ok by default because Mockito can not know which functions are implicit conversions and which are "regular" parameters.