require 'spec_helper' describe Xcal::Parktronic::ApiClient do let(:api_http_client){ Xcal::Parktronic::ApiClient.new(endpoint: 'http://api.mock', access_token: 'access_token') } let(:api_invalid_client){ Xcal::Parktronic::ApiClient.new(endpoint: 'http://api.mock') } context 'metrics route' do it 'should not be allowed without access_token' do expect{ api_invalid_client.get_paged_metrics(page: 1, per_page: 10) }.not_to raise_error expect{ api_invalid_client.get_metric(1) }.not_to raise_error expect{ api_invalid_client.post_metric({}) }.not_to raise_error end context 'getting' do it 'should respond with the correct set of metrics' do metrics = nil expect{ metrics = api_http_client.get_paged_metrics(page: 1, per_page: 10) }.not_to raise_error expect(metrics.first.id.to_s).to eql('7') expect(metrics.first.name).to eql('Metric 1') expect(metrics.last.id.to_s).to eql('16') expect(metrics.last.name).to eql('Metric 10') end it 'should respond with single metric' do metric = nil expect{ metric = api_http_client.get_metric(2) }.not_to raise_error expect(metric.id.to_s).to eql('7') end end context 'posting' do let(:metric) do { :name => 'metric name', :originating_system => 'Source System', :impact_level => 'low', :tag_list => %w(taga tagb) } end let(:metric_value) do { value: 22, created_at: '2013-07-24T13:21:16Z', custom_timestamp: nil, } end let(:device_error) do { value: 5, created_at: '2013-07-24T13:21:16Z', custom_timestamp: nil, value_groups: [ { group_type: 'Code', group_value: 33 }, { group_type: 'software_stack', group_value: 'stack' } ] } end it 'should post metrics successfully' do send_data = { :metric => metric, :metric_values => [metric_value], :device_error => device_error } expect{ api_http_client.post_metric(send_data) }.not_to raise_error expect{ api_invalid_client.post_metric(send_data) }.not_to raise_error end end context 'metric metric_values' do it 'should give an appropriate list of alarms' do metric = api_http_client.get_metric(2) expect{metric.get_metric_values.first.value}.not_to raise_error expect(metric.get_metric_values.first.value).to eql('5.339') expect(metric.get_metric_values.last.value).to eql('4.880') end end context 'metric device_errors' do it 'should give an appropriate list of alarms' do metric = api_http_client.get_metric(2) expect{metric.get_device_errors.first.value}.not_to raise_error expect(metric.get_device_errors.first.value).to eql(53) expect(metric.get_device_errors.last.value).to eql(32) end end end end