require 'spec_helper' describe ServiceMgrOptions do # The current set of all resources. This automatically updates if new # resources are added to the application RESOURCES = Resources.constants.map { | const | ActiveSupport::Inflector.underscore(const.to_s) } COMMANDS = RESOURCES.inject(Hash.new { | h, k | h[k] = [] }) do | acc, resource | acc.tap do | a | a[resource] = eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").instance_methods(false) end end before(:each) do @cut = ServiceMgrOptions.new end context 'Public Methods' do context '#parse_options!' do context 'without an api token' do it "should raise an exception if no API token has been specified" do argv = [] expect { @cut.parse_options!(argv) }. to raise_error(SystemExit) end end context 'with an api token' do it "should provide a default resource and command if none is specified on the command line" do argv = ['--token', 'the_token' ] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should use the specified resource and infer the command when the command is not specified" do argv = ['--token', 'the_token', 'service' ] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false }, # Default options 'service', # Specified resource 'list' ]) # Default command end it "should use the specified resource and command when specified" do argv = ['--token', 'the_token', 'service', 'show' ] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false }, # Default options 'service', # Specified resource 'show' ]) # Specified command end it "should render the help display and exit when the help option is specified" do argv = [ '--help' ] expect { @cut.parse_options!(argv) }. to raise_error(SystemExit) end it "should set the server option" do argv = ['--token', 'the_token', '--server', 'test.abaqis.int' ] expect(@cut.parse_options!(argv)). to eq([ { server: 'test.abaqis.int', use_ssl: true, api_token: 'the_token', trace: false }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should set the server/ssl option for local testing" do argv = ['--token', 'the_token', '--local'] expect(@cut.parse_options!(argv)). to eq([ { server: 'localhost:3000', use_ssl: false, api_token: 'the_token', trace: false }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should set the server/ssl option for uat testing" do argv = ['--token', 'the_token', '--uat'] expect(@cut.parse_options!(argv)). to eq([ { server: 'uat.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should display the resources/commands and exit" do argv = [ '--list' ] expect { @cut.parse_options!(argv) }. to raise_error(SystemExit) end it "should set the trace option" do argv = ['--token', 'the_token', '--trace'] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: true }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should the set the account mapping id" do argv = ['--token', 'the_token', '--account-mapping-id', "234"] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false, account_mapping_id: "234" }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should the set the configured account id" do argv = ['--token', 'the_token', '--configured-account-id', "234"] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false, configured_account_id: "234" }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should the set the facility mapping id" do argv = ['--token', 'the_token', '--facility-mapping-id', "234"] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false, facility_mapping_id: "234" }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should the set the service definition id" do argv = ['--token', 'the_token', '--service-definition-id', "234"] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false, service_definition_id: "234" }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should the set the service id" do argv = ['--token', 'the_token', '--service-id', "234"] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false, service_id: "234" }, # Default options 'service', # Default resource 'list' ]) # Default command end it "should the set the third party id" do argv = ['--token', 'the_token', '--third-party-id', "234"] expect(@cut.parse_options!(argv)). to eq([ { server: 'www.abaqis.com', use_ssl: true, api_token: 'the_token', trace: false, third_party_id: "234" }, # Default options 'service', # Default resource 'list' ]) # Default command end end end context '#invoke_command' do it "should construct the resource with the options and invoke the command on the resource" do expect(@cut).to receive(:parse_options!).and_return( [ { }, 'service', 'list' ] ) mock_service = double("Service") expect(mock_service).to receive(:send).with(:list) expect(Resources::Service).to receive(:new).with({}).and_return(mock_service) @cut.invoke_command end end end context 'Private Methods' do context '#valid_command?' do it "should return true if the specified resource has the specified command" do # binding.pry COMMANDS.each_pair do | resource, commands | commands.each do | command | expect(@cut.send(:valid_command?, resource, command.to_s)).to be_truthy, "'#{command}' should be a valid command for resource '#{resource}'" end end end it "should return false if the specified resource doesn't have the specified command" do expect(@cut.send(:valid_command?, 'service', 'invalid_command')). to be_falsey end it "should return false if the specified resource is nil" do expect(@cut.send(:valid_command?, nil, 'list')). to be_falsey end it "should return false if the specified command is nil" do expect(@cut.send(:valid_command?, 'service', nil)). to be_falsey end it "should do something horrible if the resource name is bogus" do expect { @cut.send(:valid_command?, 'bottle', 'list') }. to raise_error(NameError) end end context '#valid_resource' do it "should return true if the specified resource is valid" do expect(@cut.send(:valid_resource?, 'service')). to be_truthy end it "should return false if the specified resource is not valid" do expect(@cut.send(:valid_resource?, 'servicer')). to be_falsey end it "should return false if the specified resource is nil" do expect(@cut.send(:valid_resource?, nil)). to be_falsey end end end end