Friday, August 1, 2014

iReport not working

Hi, after changing my computer I could not run iReport on my ubuntu in versions 4.5.1, 4.6.0 and 5.6.0 . There was no errors even in console. The problem was with java version. My default was 8.0. The most important was to change javac version to lower(I choose 6.0)
sudo update-alternatives --config javac
but I changed also java version
sudo update-alternatives --config java
Hope it saves you few minutes. Leave me a comment if it helps

Thursday, July 31, 2014

Testing play reactivemongo applications

Hi, I'm not going to write here a complete step-by-step tutorial to test such applications. There should be already lot of them in the Internet. I want just present general way and solution to some problem, that might not be described yet.

First of all experience proved that its good to have controllers defined like this:

trait FooCtrl extends Controller {
    def fooRepo: FooRepo
    def foo() = Action {
        fooRepo.findAll()
        Ok
    }
}

object FooCtrl extends FooCtrl {
    override def fooRepo = FooMongoRepo
}
where FooMongoRepo is implementation of FooRepo based on play reactivemongo plugin.

Then you can write tests mocking mongo repositories, so you don't have to run DB during tests


class FooCtrlIT extends Specification with Mockito {
    class FooCtrlTest extends FooCtrl {
        val fooRepo = mock[FooRepo] //if you wonder 
               //why it is val, hmmm ... mock 
               //verifying doesn't work for me on override def
    }
    "Foo controler" should {
        "return ok" in {
            val fooTest = new FooCtrlTest

            val request = FakeRequest("GET", "/api/foos")
            val result = fooTest.foo()(request)

            status(result) must equalTo(OK)
        }
    }
}

Simple?
Some problem might occur if you need run test in
running(FakeApplication()) 

(Some of your classes eg. session parameters encryption needs running application) You can spot errors like:
[error] r.c.a.MongoDBSystem - (State: Closing) 
UNHANDLED MESSAGE: ChannelConnected(-951772015
[info] application - ReactiveMongo Connections stopped. 
[Success(Closed)] [INFO] [07/31/2014 21:12:39.933] 
[application-akka.actor.default-dispatcher-2] 
[akka://application/user/$b] 
Message [reactivemongo.core.actors.Close$] from 
Actor[akka://application/deadLetters] 
to Actor[akka://application/user/$b#162091841] 
was not delivered. [1] dead letters encountered. 
This logging can be turned off 
or adjusted with configuration settings 'akka.log-dead-letters' 
and 'akka.log-dead-letters-during-shutdown'.
 

The solution is to run test without mongo plugin. You don't need it because you mock all repositories.
running(FakeApplication(withoutPlugins = 
 Seq("play.modules.reactivemongo.ReactiveMongoPlugin"))) {
...
}
 


If it helps, then leave me some comment. Thx.

Saturday, July 12, 2014

Akka reactive stream examples

After my visit at ScalaDays 2014 where I attend "akka-http: (un)REST for your Actors" talk I started learning about reactive streams in Akka. There were two really good sources. First was talk by Roland Kuhn and Viktor Klang I missed at Scaladays (video) . Second is amazingly well commented code in akka-stream library.

I decided to present reactive streams to my colleges from javeo.eu . There (https://github.com/ppiotrow/javeo-akka-stream) are examples I presented them. Everything should be well documented in README. If something is not clear, then you can propose pull request;)

Akka testing parrent child relation

Testing child actors is not piece of cake in Akka. Its because this actors often communicate with parent using context.parrent
context.parrent ! FOOMsg("foo")
One of possible and reusable solution is to create:
import akka.testkit.{TestProbe, TestKit}
import org.scalatest.Suite
import akka.actor.{Actor, Props, ActorSystem}

trait TestParentChildRelation {
  this: TestKit with Suite =>
  def mockParentWithProbe(childProps: Props)
                     (implicit system: ActorSystem) = {
    val proxy = TestProbe()
    system.actorOf(Props(new Actor {
      val child = context.actorOf(childProps)
      def receive = {
        case x if sender == child => proxy.ref forward x
        case x => child forward x
      }
    }))
    proxy
  }
}
The trait can be mixed into test scenario like in example from (Link)
class ProcreatorActorTest 
extends TestKit(ActorSystem("ProcreatorTestActorSystem"))
with ImplicitSender
with WordSpecLike
with StopSystemAfterAll
with TestParentChildRelation {

  "A Procreator actor" must {
    "recombine the genome" in {
      val maleGenotype = SampleGenome(Seq(1, 3, 3, 7, 1))
      val femaleGenotype = SampleGenome(Seq(9, 8, 7, 6, 5))

      val male = TestActorRef(new Phenotype(maleGenotype))
      val female = TestActorRef(new Phenotype(femaleGenotype))
      val proxy = mockParentWithProbe(Props(
             new TestRecombineProcreator(male, female, 1.0)))

      val expectedGenome = SampleGenome(Seq(1, 3, 7, 6, 5))
      proxy.expectMsg(Descendant(expectedGenome))
    }
}
Examples come from https://github.com/ppiotrow/scalagen project. I'm waiting for your feedback

Wednesday, March 5, 2014

Fast access to ssh public key

I took it from github page. It's nice to have shortcut:)
xclip -sel clip < ~/.ssh/id_rsa.pub

Wednesday, February 26, 2014

SQL Case with only nulls problem

Last time client reported some strange SQL error.

None of the result expressions in a CASE specification can be NULL

It is caused by following statement

UPDATE MY_TABLE SET MY_COLUMN = CASE ID  WHEN 99 THEN null WHEN 100 THEN null  END

It looks for me ok, but the problem is with nulls. When CASE expression returns only nulls, then MSSQL interpreter do not know result type. I can think abount many workarounds for this problem but the best is to add "ELSE MY_COLUMN" expression. Corrected, stable form of statement is now

UPDATE MY_TABLE SET MY_COLUMN = CASE ID  WHEN 99 THEN null WHEN 100 THEN null  ELSE MY_COLUMN END

Now interpreter knowns result type and it works.