require'json'require'gds_api/test_helpers/json_client_helper'moduleGdsApimoduleTestHelpersmoduleRummagerRUMMAGER_ENDPOINT=Plek.current.find('search')defstub_any_rummager_post(index: nil)ifindexstub_request(:post,%r{#{RUMMAGER_ENDPOINT}/#{index}/documents}).to_return(status: [202,"Accepted"])elsestub_request(:post,%r{#{RUMMAGER_ENDPOINT}/documents}).to_return(status: [202,"Accepted"])endenddefassert_rummager_posted_item(attributes,index: nil,**options)ifindexurl=RUMMAGER_ENDPOINT+"/#{index}/documents"elseurl=RUMMAGER_ENDPOINT+"/documents"endassert_requested(:post,url,**options)do|req|data=JSON.parse(req.body)attributes.to_a.all?do|key,value|data[key.to_s]==valueendendenddefstub_any_rummager_searchstub_request(:get,%r{#{RUMMAGER_ENDPOINT}/search.json})enddefstub_any_rummager_search_to_return_no_resultsstub_any_rummager_search.to_return(body: {results: []}.to_json)enddefassert_rummager_search(options)assert_requested:get,"#{RUMMAGER_ENDPOINT}/search.json",**optionsenddefstub_any_rummager_delete(index: nil)ifindexstub_request(:delete,%r{#{RUMMAGER_ENDPOINT}/#{index}/documents/.*})else# use rummager's default indexstub_request(:delete,%r{#{RUMMAGER_ENDPOINT}/documents/.*})endenddefstub_any_rummager_delete_contentstub_request(:delete,%r{#{RUMMAGER_ENDPOINT}/content.*})enddefassert_rummager_deleted_item(id,index: nil,**options)ifid=~%r{^/}raiseArgumentError,'Rummager id must not start with a slash'endifindexassert_requested(:delete,%r{#{RUMMAGER_ENDPOINT}/#{index}/documents/#{id}},**options)elseassert_requested(:delete,%r{#{RUMMAGER_ENDPOINT}/documents/#{id}},**options)endenddefassert_rummager_deleted_content(base_path,**options)assert_requested(:delete,%r{#{RUMMAGER_ENDPOINT}/content.*#{base_path}},**options)enddefstub_rummager_has_services_and_info_data_for_organisationstub_request_for(search_results_found)run_example_queryenddefstub_rummager_has_no_services_and_info_data_for_organisationstub_request_for(no_search_results_found)run_example_queryenddefstub_rummager_has_specialist_sector_organisations(_sub_sector)stub_request_for(sub_sector_organisations_results)run_example_queryenddefstub_rummager_has_no_policies_for_any_typestub_request(:get,%r{/search.json}).to_return(body: no_search_results_found)enddefstub_rummager_has_policies_for_every_type(options={})ifoptions[:count]stub_request(:get,%r{/search.json.*count=#{options[:count]}.*}).to_return(body: first_n_results(new_policies_results,n: options[:count]))elsestub_request(:get,%r{/search.json}).to_return(body: new_policies_results)endend# Aliases for DEPRECATED methodsalias_method:rummager_has_services_and_info_data_for_organisation,:stub_rummager_has_services_and_info_data_for_organisationalias_method:rummager_has_no_services_and_info_data_for_organisation,:stub_rummager_has_no_services_and_info_data_for_organisationalias_method:rummager_has_specialist_sector_organisations,:stub_rummager_has_specialist_sector_organisationsalias_method:rummager_has_no_policies_for_any_type,:stub_rummager_has_no_policies_for_any_typealias_method:rummager_has_policies_for_every_type,:stub_rummager_has_policies_for_every_typeprivatedefstub_request_for(result_set)stub_request(:get,/example.com\/search/).to_return(body: result_set)enddefrun_example_queryclient.search(example_query)enddefsearch_results_foundFile.read(File.expand_path("../../../../test/fixtures/services_and_info_fixture.json",__FILE__))enddefno_search_results_foundFile.read(File.expand_path("../../../../test/fixtures/no_services_and_info_data_found_fixture.json",__FILE__))enddefsub_sector_organisations_resultsFile.read(File.expand_path("../../../../test/fixtures/sub_sector_organisations.json",__FILE__))enddefnew_policies_resultsFile.read(File.expand_path("../../../../test/fixtures/new_policies_for_dwp.json",__FILE__))enddefold_policies_resultsFile.read(File.expand_path("../../../../test/fixtures/old_policies_for_dwp.json",__FILE__))enddeffirst_n_results(results,options)n=options[:n]results=JSON.parse(results)results["results"]=results["results"][0...n]results.to_jsonenddefclientGdsApi::Rummager.new("http://example.com")enddefexample_query{filter_organisations: ["an-organisation-slug"],facet_specialist_sectors: "1000,examples:4,example_scope:global,order:value.title"}endendendend