aboutsummaryrefslogtreecommitdiff
path: root/kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala
blob: 5bc632f78179c4a45f29237e1bd22153cbbeb2dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
/* ===================================================
 * Copyright © 2013 the kamon project <http://kamon.io/>
 *
 * Licensed 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 kamon.newrelic

import akka.testkit.{TestActor, TestProbe, TestKit}
import akka.actor.{Props, ActorRef, ActorSystem}
import org.scalatest.WordSpecLike
import kamon.AkkaExtensionSwap
import spray.can.Http
import akka.io.IO
import akka.testkit.TestActor.{KeepRunning, AutoPilot}
import spray.http._
import spray.http.HttpRequest
import spray.http.HttpResponse

class AgentSpec extends TestKit(ActorSystem("agent-spec")) with WordSpecLike {

  setupFakeHttpManager

  "the Newrelic Agent" should {
    "try to connect upon creation" in {
      val agent = system.actorOf(Props[Agent])

      Thread.sleep(5000)
    }
  }

  def setupFakeHttpManager: Unit = {
    val fakeHttpManager = TestProbe()
    fakeHttpManager.setAutoPilot(new TestActor.AutoPilot {
      def run(sender: ActorRef, msg: Any): AutoPilot = {
        msg match {
          case HttpRequest(_, uri, _, _, _) if rawMethodIs("get_redirect_host", uri) =>
            sender ! jsonResponse(
              """
                | {
                |   "return_value": "collector-8.newrelic.com"
                | }
                | """.stripMargin)

            println("Selecting Collector")

          case HttpRequest(_, uri, _, _, _) if rawMethodIs("connect", uri) =>
            sender ! jsonResponse(
              """
                | {
                |   "return_value": {
                |     "agent_run_id": 161221111
                |   }
                | }
                | """.stripMargin)
            println("Connecting")
        }

        KeepRunning
      }

      def jsonResponse(json: String): HttpResponse = {
        HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, json))
      }

      def rawMethodIs(method: String, uri: Uri): Boolean = {
        uri.query.get("method").filter(_ == method).isDefined
      }
    })


    AkkaExtensionSwap.swap(system, Http, new IO.Extension {
      def manager: ActorRef = fakeHttpManager.ref
    })
  }
}