require 'gooddata/models/schedule' describe GoodData::Schedule do TEST_CRON = '0 15 27 7 *' TEST_DATA = { :timezone => 'UTC', :cron => '2 2 2 2 *' } TEST_DATA_WITH_OPTIONAL_PARAM = { :timezone => 'UTC', :cron => '2 2 2 2 *', :reschedule => 15 } TEST_PROCESS_ID = 'dc143d80-58a1-4acd-96b6-8d11fc4571de' SCHEDULE_ID = '53e03c25e4b0c7f987a2f6fd' SCHEDULE_URL = '/gdc/projects/we1vvh4il93r0927r809i3agif50d7iz/schedules/53e03c25e4b0c7f987a2f6fd' before(:each) do ConnectionHelper.create_default_connection GoodData.project = ProjectHelper::PROJECT_ID @project = GoodData.project @project_executable = "./graph/graph.grf" end after(:each) do GoodData.disconnect end describe '#[]' do it 'Returns all schedules when :all passed' do res = GoodData::Schedule[:all] res.should_not be_nil res.should be_a_kind_of(Array) res.each do |schedule| schedule.should be_a_kind_of(GoodData::Schedule) end end it 'Returns specific schedule when schedule ID passed' do res = GoodData::Schedule[SCHEDULE_ID] res.should_not be_nil res.should be_a_kind_of(GoodData::Schedule) end it 'Returns specific schedule when schedule URL passed' do res = GoodData::Schedule[SCHEDULE_URL] res.should_not be_nil res.should be_a_kind_of(GoodData::Schedule) end end describe '#all' do it 'Returns all schedules' do res = GoodData::Schedule.all res.should_not be_nil res.should be_a_kind_of(Array) res.each do |schedule| schedule.should be_a_kind_of(GoodData::Schedule) end end end describe '#create' do it 'Creates new schedule if mandatory params passed' do sched = nil expect { sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) }.not_to raise_error sched.should_not be_nil sched.delete end it 'Creates new schedule if mandatory params passed and optional params are present' do sched = nil expect { sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA_WITH_OPTIONAL_PARAM) }.not_to raise_error sched.should_not be_nil sched.delete end it 'Throws exception when no process ID specified' do expect { sched = GoodData::Schedule.create(nil, TEST_CRON, @project_executable, TEST_DATA) }.to raise_error 'Process ID has to be provided' end it 'Throws exception when no executable specified' do expect { sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, nil, TEST_DATA) }.to raise_error 'Executable has to be provided' end it 'Throws exception when no cron is specified' do data = TEST_DATA.deep_dup data[:cron] = nil expect { sched = GoodData::Schedule.create(TEST_PROCESS_ID, nil, @project_executable, data) }.to raise_error 'Cron schedule has to be provided' end it 'Throws exception when no timezone specified' do data = TEST_DATA.deep_dup data[:timezone] = nil expect { sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, data) }.to raise_error 'A timezone has to be provided' end it 'Throws exception when no timezone specified' do data = TEST_DATA.deep_dup data[:type] = nil expect { sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, data) }.to raise_error 'Schedule type has to be provided' end end describe '#cron' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return cron as string' do res = @schedule.cron res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#cron=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the cron and marks the object dirty' do test_cron = '2 2 2 2 *' @schedule.cron = test_cron expect(@schedule.cron).to eq(test_cron) expect(@schedule.dirty).to eq(true) end end describe '#delete' do it 'Should delete schedule' do pending 'Setup test environment first' sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) proc = GoodData::Process[TEST_PROCESS_ID] # Delete created schedule sched.delete proc.schedules.each do |tmp_sched| tmp_sched.uri.should_not equal(sched.uri) end end end describe '#executable' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return executable as string' do res = @schedule.executable res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#executable=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the executable and marks the object dirty' do test_executable = 'this/is/test.gr' @schedule.executable = test_executable expect(@schedule.executable).to eq(test_executable) expect(@schedule.dirty).to eq(true) end end describe '#execute' do it 'Executes schedule on process' do pending 'Setup environment for this test' # Create one a schedule sched = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) execution_time = Time.new execution_request = sched.execute # Call execute executed = false start_time = Time.new while (Time.new - start_time < 30) # Check if the last execution time sched.executions.each do |execution| next if execution['execution'].nil? || execution['execution']['startTime'].nil? parsed_time = Time.parse(execution['execution']['startTime']) executed_schedule = execution_request['execution']['links']['self'] == execution['execution']['links']['self'] if (execution_time <= parsed_time && executed_schedule) executed = true break end end break if executed sleep 1 end expect(executed).to be(true) end end describe '#execution_url' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return execution URL as string' do res = @schedule.execution_url res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#type' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return execution type as string' do res = @schedule.type res.should_not be_nil res.should be_a_kind_of(String) end end describe '#hidden_params' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return execution hidden_params as hash' do res = @schedule.hidden_params res.should_not be_nil res.should be_a_kind_of(Hash) end end describe '#hidden_params=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the hidden params and marks the object dirty' do @old_params = @schedule.hidden_params test_params = { 'PROCESS_ID' => '1-2-3-4' } @schedule.hidden_params = test_params expect(@schedule.hidden_params).to eq(@old_params.merge(test_params)) expect(@schedule.dirty).to eq(true) end end describe '#params' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return execution params as hash' do res = @schedule.params res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(Hash) end end describe '#params=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the params and marks the object dirty' do @old_params = @schedule.params test_params = { 'PROCESS_ID' => '1-2-3-4' } @schedule.params = test_params expect(@schedule.params).to eq(@old_params.merge(test_params)) expect(@schedule.dirty).to eq(true) end end describe '#process_id' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return process id as string' do res = @schedule.process_id res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#process_id=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the process_id and marks the object dirty' do test_process_id = '1-2-3-4' @schedule.process_id = test_process_id expect(@schedule.process_id).to eq(test_process_id) expect(@schedule.dirty).to eq(true) end end describe '#save' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end it 'Should save a schedule' do expect(GoodData::Schedule[@schedule.uri]).to eq @schedule expect(GoodData::Project[ProjectHelper::PROJECT_ID].schedules).to include(@schedule) end end describe '#state' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return execution state as string' do res = @schedule.state res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#type' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return execution type as string' do res = @schedule.type res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#type=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the type the object dirty' do test_type = 'TEST' @schedule.type = test_type expect(@schedule.type).to eq(test_type) expect(@schedule.dirty).to eq(true) end end describe '#timezone' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Should return timezone as string' do res = @schedule.timezone res.should_not be_nil res.should_not be_empty res.should be_a_kind_of(String) end end describe '#timezone=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA) end after(:each) do @schedule.delete end it 'Assigns the timezone and marks the object dirty' do test_timezone = 'PST' @schedule.timezone = test_timezone expect(@schedule.timezone).to eq(test_timezone) expect(@schedule.dirty).to eq(true) end end describe '#reschedule' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA_WITH_OPTIONAL_PARAM) end after(:each) do @schedule.delete end it 'Should return reschedule as integer' do res = @schedule.reschedule res.should_not be_nil res.should be_a_kind_of(Integer) end end describe '#reschedule=' do before(:each) do @schedule = GoodData::Schedule.create(TEST_PROCESS_ID, TEST_CRON, @project_executable, TEST_DATA_WITH_OPTIONAL_PARAM) end after(:each) do @schedule.delete end it 'Assigns the reschedule and marks the object dirty' do test_reschedule = 45 @schedule.reschedule = test_reschedule expect(@schedule.reschedule).to eq(test_reschedule) expect(@schedule.dirty).to eq(true) end end end