From c98572c000e30da111c13e6e4aeee70aa2335a5e Mon Sep 17 00:00:00 2001 From: Leonardo Menezes <leonardo.menezes@xing.com> Date: Mon, 4 Apr 2016 13:59:59 +0200 Subject: [PATCH] added test to GetIndexMappingController --- ....scala => GetIndexMappingController.scala} | 2 +- conf/routes | 2 +- .../GetIndexMappingControllerSpec.scala | 51 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) rename app/controllers/{GetIndexMapping.scala => GetIndexMappingController.scala} (67%) create mode 100644 test/controllers/GetIndexMappingControllerSpec.scala diff --git a/app/controllers/GetIndexMapping.scala b/app/controllers/GetIndexMappingController.scala similarity index 67% rename from app/controllers/GetIndexMapping.scala rename to app/controllers/GetIndexMappingController.scala index 6f95196..ca77cb0 100644 --- a/app/controllers/GetIndexMapping.scala +++ b/app/controllers/GetIndexMappingController.scala @@ -1,6 +1,6 @@ package controllers -class GetIndexMapping extends ElasticActionController { +class GetIndexMappingController extends ElasticActionController { def processElasticRequest = (request, client) => client.getIndexMapping(request.get("index"), request.host) diff --git a/conf/routes b/conf/routes index b340f04..99543de 100644 --- a/conf/routes +++ b/conf/routes @@ -16,7 +16,7 @@ POST /apis/clear_indices_cache @controllers.ClearIndexCacheControl POST /apis/refresh_indices @controllers.RefreshIndexController.execute POST /apis/delete_indices @controllers.DeleteIndexController.execute POST /apis/get_index_settings @controllers.GetIndexSettings.execute -POST /apis/get_index_mapping @controllers.GetIndexMapping.execute +POST /apis/get_index_mapping @controllers.GetIndexMappingController.execute POST /apis/update_cluster_settings @controllers.PutClusterSettings.execute POST /apis/get_node_stats @controllers.NodeStatsController.execute POST /apis/disable_shard_allocation @controllers.DisableShardAllocationController.execute diff --git a/test/controllers/GetIndexMappingControllerSpec.scala b/test/controllers/GetIndexMappingControllerSpec.scala new file mode 100644 index 0000000..11a6ec9 --- /dev/null +++ b/test/controllers/GetIndexMappingControllerSpec.scala @@ -0,0 +1,51 @@ +package controllers + +import elastic.{ElasticResponse, ElasticClient} +import exceptions.MissingRequiredParamException +import models.CerebroRequest +import org.specs2.Specification +import org.specs2.mock.Mockito +import play.api.libs.json.Json +import play.api.test.FakeApplication + +import scala.concurrent.duration.Duration +import scala.concurrent.{Await, Future} + +object GetIndexMappingControllerSpec extends Specification with Mockito { + + def is = + s2""" + GetIndexMappingController should ${step(play.api.Play.start(FakeApplication()))} + invoke getIndexMapping $getIndexMapping + should throw exception if index param is missing $missingIndex + ${step(play.api.Play.stop(FakeApplication()))} + """ + + val controller = new GetIndexMappingController + + def getIndexMapping = { + val expectedResponse = Json.parse( + """ + |{ + | "someIndex": { + | "mappings": {} + | } + |} + """.stripMargin + ) + val body = Json.obj("host" -> "somehost", "index" -> "someIndex") + val client = mock[ElasticClient] + client.getIndexMapping("someIndex", "somehost") returns Future.successful(ElasticResponse(200, expectedResponse)) + val response = Await.result(controller.processElasticRequest(CerebroRequest(body), client), Duration("1s")) + there was one(client).getIndexMapping("someIndex", "somehost") + response.body mustEqual expectedResponse + response.status mustEqual 200 + } + + def missingIndex = { + val body = Json.obj("host" -> "somehost") + val client = mock[ElasticClient] + controller.processElasticRequest(CerebroRequest(body), client) must throwA[MissingRequiredParamException] + } + +} -- GitLab