/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["../datasource","test/lib/common","test/specs/helpers"],function(a){var b,c,d;return{setters:[function(a){d=a},function(a){b=a},function(a){c=a}],execute:function(){b.describe("CloudWatchDatasource",function(){function a(a,c){b.describe("metricFindQuery "+a,function(){var d={};d.setup=function(c){b.beforeEach(function(){c(),e.backendSrv.datasourceRequest=function(a){return d.request=a,e.$q.when({data:d.requestResponse})},e.ds.metricFindQuery(a).then(function(a){d.result=a}),e.$rootScope.$apply()})},c(d)})}var e=new c["default"].ServiceTestContext,f={jsonData:{defaultRegion:"us-east-1",access:"proxy"}};b.beforeEach(b.angularMocks.module("grafana.core")),b.beforeEach(b.angularMocks.module("grafana.services")),b.beforeEach(b.angularMocks.module("grafana.controllers")),b.beforeEach(e.providePhase(["templateSrv","backendSrv"])),b.beforeEach(b.angularMocks.inject(function(a,b,c,g){e.$q=a,e.$httpBackend=c,e.$rootScope=b,e.ds=g.instantiate(d.CloudWatchDatasource,{instanceSettings:f}),c.when("GET",/\.html$/).respond("")})),b.describe("When performing CloudWatch query",function(){var a,c={range:{from:"now-1h",to:"now"},targets:[{region:"us-east-1",namespace:"AWS/EC2",metricName:"CPUUtilization",dimensions:{InstanceId:"i-12345678"},statistics:["Average"],period:300}]},d={Datapoints:[{Average:1,Timestamp:"Wed Dec 31 1969 16:00:00 GMT-0800 (PST)"},{Average:2,Timestamp:"Wed Dec 31 1969 16:05:00 GMT-0800 (PST)"},{Average:5,Timestamp:"Wed Dec 31 1969 16:15:00 GMT-0800 (PST)"}],Label:"CPUUtilization"};b.beforeEach(function(){e.backendSrv.datasourceRequest=function(b){return a=b,e.$q.when({data:d})}}),b.it("should generate the correct query",function(d){e.ds.query(c).then(function(){var e=a.data.parameters;b.expect(e.namespace).to.be(c.targets[0].namespace),b.expect(e.metricName).to.be(c.targets[0].metricName),b.expect(e.dimensions[0].Name).to.be(Object.keys(c.targets[0].dimensions)[0]),b.expect(e.dimensions[0].Value).to.be(c.targets[0].dimensions[Object.keys(c.targets[0].dimensions)[0]]),b.expect(e.statistics).to.eql(c.targets[0].statistics),b.expect(e.period).to.be(c.targets[0].period),d()}),e.$rootScope.$apply()}),b.it("should return series list",function(a){e.ds.query(c).then(function(c){b.expect(c.data[0].target).to.be("CPUUtilization_Average"),b.expect(c.data[0].datapoints[0][0]).to.be(d.Datapoints[0].Average),a()}),e.$rootScope.$apply()}),b.it("should return null for missing data point",function(a){e.ds.query(c).then(function(c){b.expect(c.data[0].datapoints[2][0]).to.be(null),a()}),e.$rootScope.$apply()})}),a("regions()",function(a){a.setup(function(){a.requestResponse=[{text:"us-east-1"}]}),b.it("should call __GetRegions and return result",function(){b.expect(a.result[0].text).to.contain("us-east-1"),b.expect(a.request.data.action).to.be("__GetRegions")})}),a("namespaces()",function(a){a.setup(function(){a.requestResponse=[{text:"AWS/EC2"}]}),b.it("should call __GetNamespaces and return result",function(){b.expect(a.result[0].text).to.contain("AWS/EC2"),b.expect(a.request.data.action).to.be("__GetNamespaces")})}),a("metrics(AWS/EC2)",function(a){a.setup(function(){a.requestResponse=[{text:"CPUUtilization"}]}),b.it("should call __GetMetrics and return result",function(){b.expect(a.result[0].text).to.be("CPUUtilization"),b.expect(a.request.data.action).to.be("__GetMetrics")})}),a("dimension_keys(AWS/EC2)",function(a){a.setup(function(){a.requestResponse=[{text:"InstanceId"}]}),b.it("should call __GetDimensions and return result",function(){b.expect(a.result[0].text).to.be("InstanceId"),b.expect(a.request.data.action).to.be("__GetDimensions")})}),a("dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)",function(a){a.setup(function(){a.requestResponse={Metrics:[{Namespace:"AWS/EC2",MetricName:"CPUUtilization",Dimensions:[{Name:"InstanceId",Value:"i-12345678"}]}]}}),b.it("should call __ListMetrics and return result",function(){b.expect(a.result[0].text).to.be("i-12345678"),b.expect(a.request.data.action).to.be("ListMetrics")})})})}}});