require 'spec_helper' require 'daengine/teamsite_metadata_processor' describe Daengine::TeamsiteMetadataProcessor do before { Daengine.configure(:assets_path => Dir.pwd+'/spec/mock_data', :daengine_yml_file => Dir.pwd+'/spec/mock_data/daengine.yml', :digital_assets_file_directory => Dir.pwd + '/spec/mock_data/', :digital_asset_service_url => 'http://localhost:3000/digital_assets') } subject { Daengine::TeamsiteMetadataProcessor } context '#parse_file' do it 'reads xml' do file = File.expand_path('./spec/mock_data/selective_new_package.xml') assets = subject.parse_file(open(file)) assets.count.should == 2 # hash with 2 keys list = assets['163742d3-fbc2-4c99-8396-6eabe7464b8f'] entered = list.first entered.digital_asset_id.should == '163742d3-fbc2-4c99-8396-6eabe7464b8f' entered.path.should == '/digitalAssets/SSC_Developer_Installation_Guide-163742d3-fbc2-4c99-8396-6eabe7464b8f.doc' entered.summary.should == 'first foo bar...' #entered.title.should == 'fake title' #entered.size.should == '12 kB' #entered.pages.should == 1 #entered.finra_path.should == "/digitalAssets/TEST_FINRA_DOC.doc" end it 'can read a whole bulk deploy xml file quickly' do start = Time.now file = File.expand_path('./spec/mock_data/bulk-ssc_deploy.xml') assets = subject.parse_file(open(file)) (Time.now - start).should < 2.seconds assets.count.should == 18 end end context '#select_1_asset_per_id' do let(:asset) { FactoryGirl.build :service_digital_asset } let(:other_asset) { FactoryGirl.build :service_digital_asset } let(:last_read) { 1.year.ago } it 'should return assets that are effective' do id = asset.digital_asset_id assets = {id => [asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset end it 'should remove assets missing expires_at value' do id = asset.digital_asset_id asset.expires_at = nil assets = {id => [asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should be_nil end it 'should remove assets that are not displayed on the web site' do id = asset.digital_asset_id asset.display_on_website = false assets = {id => [asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should be_nil end it 'should return the non-expired asset' do id = asset.digital_asset_id other_asset.expires_at = 1.day.ago assets = {id => [asset, other_asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset # order in list shouldn't matter assets = {id => [other_asset, asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset end it 'should return the non-manifest asset' do id = asset.digital_asset_id other_asset.path = '/foo/manifest' + other_asset.path assets = {id => [asset, other_asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset # order in list shouldn't matter assets = {id => [other_asset, asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset end it 'should return the non-finra asset' do id = asset.digital_asset_id other_asset.content_type = DigitalAsset::ContentType::FINRA assets = {id => [asset, other_asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset # order in list shouldn't matter assets = {id => [other_asset, asset]} results = subject.select_1_asset_per_id(assets, last_read) results[id].should == asset end it 'should set the finra_path on the returned asset' do id = asset.digital_asset_id other_asset.content_type = DigitalAsset::ContentType::FINRA finra_path = other_asset.path assets = {id => [asset, other_asset]} results = subject.select_1_asset_per_id(assets, last_read) result = results[id] result.should == asset result.finra_path.should == finra_path end it 'should remove assets that have not changed since the last_read time' do read_time = 1.day.ago id = asset.digital_asset_id assets = {id => [asset]} results = subject.select_1_asset_per_id(assets, read_time) results[id].should be_nil end end context '#add_file_attributes' do let(:asset) { FactoryGirl.build :service_digital_asset } it 'should not set the file attributes for an asset that does not have an associated file' do last_read = Time.now path = '/foobar.pdf' pages = -10 id = asset.digital_asset_id asset.path = path asset.pages = pages assets = {id => asset} asset.delete?.should == false results = subject.add_file_attributes(assets, last_read) result = results[id] result.delete?.should == true result.pages.should == pages end it 'should set the file attributes based upon the digital asset file' do last_read = 1.year.ago path = '/digitalAssets/SSC_Developer_Installation_Guide-163742d3-fbc2-4c99-8396-6eabe7464b8f.doc' id = asset.digital_asset_id asset.path = path assets = {id => asset} results = subject.add_file_attributes(assets, last_read) result = results[id] result.path.should == '/digitalAssets/SSC_Developer_Installation_Guide-163742d3-fbc2-4c99-8396-6eabe7464b8f.doc' result.size.should == '12 kB' result.pages.should == 1 result.mime_type.should == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' #result.keywords.should == [] #result.subject.should be_nil end end context '#add_fund_codes' do let(:asset) { FactoryGirl.build :service_digital_asset } it 'should add the fund code when found' do id = asset.digital_asset_id product_id = '666' fund_code = '777' asset.product_ids = [product_id] assets = {id => asset} DigitalAssetLookupService.should_receive(:fund_code_from_id).with(product_id).and_return(fund_code) results = subject.add_fund_codes(assets) result = results[id] result_codes = result.fund_codes result_codes.first.should == fund_code.rjust(5, '0') end it 'should not add nil if the fund code is not found' do id = asset.digital_asset_id product_id = '666' fund_code = nil asset.product_ids = [product_id] assets = {id => asset} DigitalAssetLookupService.should_receive(:fund_code_from_id).with(product_id).and_return(fund_code) results = subject.add_fund_codes(assets) result = results[id] result_codes = result.fund_codes result_codes.should == [] end end context '#call_service' do let(:asset) { FactoryGirl.build :service_digital_asset } let(:header) { {'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby' } } #let(:header) { {'Content-Type' => 'application/json'} } it 'should delete an asset that is flagged for deletion' do asset.expires_at = 1.day.ago assets = {asset.digital_asset_id => asset} path = "http://localhost:3000/digital_assets/#{asset.digital_asset_id}" options = {:method => :delete, :headers => header} response = Object.new response.should_receive(:success?).and_return(true) response.should_receive(:success?).and_return(true) Daengine::HTTP::Client.should_receive(:call).with(path, options).and_return(response) results = subject.call_service(assets) results[:deleted].should == 1 end it 'should add an asset that is not flagged for deletion' do assets = {asset.digital_asset_id => asset} path = "http://localhost:3000/digital_assets" options = {:method => :post, :query => asset.as_hash, :headers => header} response = Object.new response.should_receive(:success?).and_return(true) response.should_receive(:success?).and_return(true) Daengine::HTTP::Client.should_receive(:call).with(path, options).and_return(response) results = subject.call_service(assets) results[:updated].should == 1 end end end