spec/lib/flapjack/gateways/jsonapi/entity_methods_spec.rb in flapjack-1.1.0 vs spec/lib/flapjack/gateways/jsonapi/entity_methods_spec.rb in flapjack-1.2.0rc1

- old
+ new

@@ -19,10 +19,12 @@ it "retrieves all entities" do expect(entity).to receive(:id).exactly(4).times.and_return(entity_core['id']) expect(Flapjack::Data::Entity).to receive(:contact_ids_for). with([entity_core['id']], :redis => redis).and_return({}) + expect(Flapjack::Data::Entity).to receive(:check_ids_for). + with([entity_core['id']], :redis => redis).and_return({}) expect(entity).to receive(:to_jsonapi).and_return(entity_core.to_json) expect(Flapjack::Data::Entity).to receive(:all).with(:enabled => nil, :redis => redis). and_return([entity]) aget '/entities' @@ -34,10 +36,12 @@ idless_entity = double(Flapjack::Data::Entity, :id => '') expect(entity).to receive(:id).exactly(4).times.and_return(entity_core['id']) expect(Flapjack::Data::Entity).to receive(:contact_ids_for). with([entity_core['id']], :redis => redis).and_return({}) + expect(Flapjack::Data::Entity).to receive(:check_ids_for). + with([entity_core['id']], :redis => redis).and_return({}) expect(entity).to receive(:to_jsonapi).and_return(entity_core.to_json) expect(idless_entity).not_to receive(:to_jsonapi) expect(Flapjack::Data::Entity).to receive(:all).with(:enabled => nil, :redis => redis). and_return([entity, idless_entity]) @@ -51,10 +55,12 @@ 'name' => 'www.example.com'} expect(entity).to receive(:id).twice.and_return('1234') expect(Flapjack::Data::Entity).to receive(:contact_ids_for). with(['1234'], :redis => redis).and_return({}) + expect(Flapjack::Data::Entity).to receive(:check_ids_for). + with(['1234'], :redis => redis).and_return({}) expect(entity).to receive(:to_jsonapi).and_return(entity_core.to_json) expect(Flapjack::Data::Entity).to receive(:find_by_id). with('1234', :logger => @logger, :redis => redis). and_return(entity) @@ -71,10 +77,13 @@ 'name' => 'www.example2.com'} expect(Flapjack::Data::Entity).to receive(:contact_ids_for). with(['1234', '5678'], :redis => redis).and_return({}) + expect(Flapjack::Data::Entity).to receive(:check_ids_for). + with(['1234', '5678'], :redis => redis).and_return({}) + expect(entity).to receive(:id).twice.and_return('1234') expect(entity_2).to receive(:id).twice.and_return('5678') expect(entity).to receive(:to_jsonapi).and_return(entity_core.to_json) expect(entity_2).to receive(:to_jsonapi).and_return(entity_core_2.to_json) @@ -146,9 +155,84 @@ expect(Flapjack::Data::Entity).to receive(:find_by_id). with('1234', :redis => redis).and_return(entity) apatch "/entities/1234", [{:op => 'add', :path => '/entities/0/links/contacts/-', :value => '32'}].to_json, + jsonapi_patch_env + expect(last_response.status).to eq(204) + end + + it "adds tags to an entity" do + expect(Flapjack::Data::Entity).to receive(:find_by_id). + with('1234', :redis => redis).and_return(entity) + + expect(entity).to receive(:add_tags).with('database') + + apatch "/entities/1234", + [{:op => 'add', :path => '/entities/0/links/tags', :value => 'database'}].to_json, + jsonapi_patch_env + expect(last_response.status).to eq(204) + end + + it "removes tags from an entity" do + expect(Flapjack::Data::Entity).to receive(:find_by_id). + with('1234', :redis => redis).and_return(entity) + + expect(entity).to receive(:delete_tags).with('database') + expect(entity).to receive(:delete_tags).with('virtualised') + + apatch "/entities/1234", + [{:op => 'remove', :path => '/entities/0/links/tags/database'}, + {:op => 'remove', :path => '/entities/0/links/tags/virtualised'}].to_json, + jsonapi_patch_env + expect(last_response.status).to eq(204) + end + + it "renames an entity when an entity with the new name doesn't exist" do + expect(entity).to receive(:name).and_return('example.com') + expect(Flapjack::Data::Entity).to receive(:find_by_id). + with('1234', :redis => redis).and_return(entity) + + expect(Flapjack::Data::Entity).to receive(:find_by_name). + with('example2.com', :redis => redis).and_return(nil) + + expect(Flapjack::Data::Entity).to receive(:rename). + with('example.com', 'example2.com', :redis => redis) + + apatch "/entities/1234", + [{:op => 'replace', :path => '/entities/0/name', :value => 'example2.com'}].to_json, + jsonapi_patch_env + expect(last_response.status).to eq(204) + end + + it "merges an entity when an entity with the new name exists" do + expect(entity).to receive(:name).and_return('example.com') + expect(Flapjack::Data::Entity).to receive(:find_by_id). + with('1234', :redis => redis).and_return(entity) + + expect(Flapjack::Data::Entity).to receive(:find_by_name). + with('example2.com', :redis => redis).and_return(double(Flapjack::Data::Entity)) + + expect(Flapjack::Data::Entity).to receive(:merge). + with('example.com', 'example2.com', :redis => redis) + + apatch "/entities/1234", + [{:op => 'replace', :path => '/entities/0/name', :value => 'example2.com'}].to_json, + jsonapi_patch_env + expect(last_response.status).to eq(204) + end + + it 'skips a rename if old and new names are the same' do + expect(entity).to receive(:name).and_return('example.com') + expect(Flapjack::Data::Entity).to receive(:find_by_id). + with('1234', :redis => redis).and_return(entity) + + expect(Flapjack::Data::Entity).not_to receive(:find_by_name) + expect(Flapjack::Data::Entity).not_to receive(:rename) + + apatch "/entities/1234", + [{:op => 'replace', :path => '/entities/0/name', :value => 'example.com'}].to_json, + jsonapi_patch_env expect(last_response.status).to eq(204) end it "creates acknowledgements for all checks on an entity" do