Skip to content

Commit

Permalink
Merge pull request #30 from lenovo/connection_url
Browse files Browse the repository at this point in the history
Connection url
  • Loading branch information
walteraa authored Jun 21, 2017
2 parents f2ae2ac + b7f7608 commit a8c26a9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
14 changes: 11 additions & 3 deletions lib/xclarity_client/xclarity_base.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'faraday'
require 'json'
require 'uri'
require 'uri/https'

module XClarityClient
class XClarityBase

token_auth = '/session'.freeze

attr_reader :conn

def initialize(conf, uri)
Expand All @@ -15,8 +15,16 @@ def initialize(conf, uri)

def connection_builder(conf, uri)
$lxca_log.info "XClarityClient::XClarityBase connection_builder", "Creating connection to #{conf.host + uri}"
#Building configuration
@conn = Faraday.new(url: conf.host + uri) do |faraday|
# Building configuration, convert to https if protocol not specified
hostname = URI.parse(conf.host)
unless hostname.scheme
hostname = URI::HTTPS.build({ :host => hostname.host,
:port => hostname.port,
:path => hostname.path,
:query => hostname.query,
:fragment => hostname.fragment })
end
@conn = Faraday.new(:url => "#{hostname}#{uri}") do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT -- This line, should be uncommented if you wanna inspect the URL Request
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
Expand Down
5 changes: 2 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
require 'apib/mock_server'
require 'webmock/rspec'


base_url = "http://example.com"
base_url = 'https://example.com'
# These environment variables must be defined
ENV['LXCA_USERNAME'] ||= ''
ENV['LXCA_PASSWORD'] ||= ''
ENV['LXCA_HOST'] ||= base_url
ENV['LXCA_AUTH_TYPE'] ||= ''
ENV['LXCA_VERIFY_SSL'] ||= 'NONE'

blueprints = ""
blueprints = ''
Dir.glob('docs/apib/*.apib') do |blueprint|
blueprints << File.open(blueprint).read
end
Expand Down
56 changes: 26 additions & 30 deletions spec/xclarity_client_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe XClarityClient do
before :all do
WebMock.allow_net_connect! # -- This line should be uncommented if you're using external mock test
# -- The next line should be uncommented
# if you're using external mock test
WebMock.allow_net_connect!

conf = XClarityClient::Configuration.new(
username: ENV['LXCA_USERNAME'],
Expand All @@ -13,33 +15,32 @@
)

@client = XClarityClient::Client.new(conf)
@host = ENV['LXCA_HOST']

@includeAttributes = %w(accessState activationKeys)
@excludeAttributes = %w(accessState activationKeys)
@include_attributes = %w(access_state activation_keys)
@exclude_attributes = %w(access_state activation_keys)
end

before :each do
@uuidArray = @client.discover_nodes.map { |node| node.uuid }
@uuid_array = @client.discover_nodes.map(&:uuid)
end

it 'has a version number' do
expect(XClarityClient::VERSION).not_to be nil
end

describe 'GET /nodes' do

it 'should respond with an array' do
expect(@client.discover_nodes.class).to eq(Array)
end

end

describe 'GET /nodes/UUID' do
context 'with include attributes' do
it 'required attributes should not be nil' do
response = @client.fetch_nodes(@uuidArray, @includeAttributes,nil)
response = @client.fetch_nodes(@uuid_array, @include_attributes, nil)
response.map do |node|
@includeAttributes.map do |attribute|
@include_attributes.map do |attribute|
expect(node.send(attribute)).not_to be_nil
end
end
Expand All @@ -48,9 +49,9 @@

context 'with excludeAttributes' do
it 'excluded attributes should to be nil' do
response = @client.fetch_nodes(@uuidArray, nil, @excludeAttributes)
response = @client.fetch_nodes(@uuid_array, nil, @exclude_attributes)
response.map do |node|
@excludeAttributes.map do |attribute|
@exclude_attributes.map do |attribute|
expect(node.send(attribute)).to be_nil
end
end
Expand All @@ -59,12 +60,11 @@
end

describe 'GET /nodes/UUID,UUID,...,UUID' do

context 'with includeAttributes' do
it 'required attributes shoud not be nil ' do
response = @client.fetch_nodes(@uuidArray, @includeAttributes,nil)
response = @client.fetch_nodes(@uuid_array, @include_attributes, nil)
response.map do |node|
@includeAttributes.map do |attribute|
@include_attributes.map do |attribute|
expect(node.send(attribute)).not_to be_nil
end
end
Expand All @@ -73,9 +73,9 @@

context 'with excludeAttributes' do
it 'excluded attributes shoud to be nil' do
response = @client.fetch_nodes(@uuidArray, nil, @excludeAttributes)
response = @client.fetch_nodes(@uuid_array, nil, @exclude_attributes)
response.map do |node|
@excludeAttributes.map do |attribute|
@exclude_attributes.map do |attribute|
expect(node.send(attribute)).to be_nil
end
end
Expand All @@ -84,12 +84,11 @@
end

describe 'GET /nodes' do

context 'with includeAttributes' do
it 'required attributes should not be nil' do
response = @client.fetch_nodes(nil,@includeAttributes,nil)
response = @client.fetch_nodes(nil, @include_attributes, nil)
response.first do |node|
@includeAttributes.map do |attribute|
@include_attributes.map do |attribute|
expect(node.send(attribute)).not_to be_nil
end
end
Expand All @@ -98,9 +97,9 @@

context 'with excludeAttributes' do
it 'excluded attributes should be nil' do
response = @client.fetch_nodes(nil,nil,@excludeAttributes)
response = @client.fetch_nodes(nil, nil, @exclude_attributes)
response.map do |node|
@excludeAttributes.map do |attribute|
@exclude_attributes.map do |attribute|
expect(node.send(attribute)).to be_nil
end
end
Expand All @@ -110,38 +109,35 @@

describe 'Get /node' do
it 'should power down system' do
response = @client.power_off_node(@uuidArray[0])
response = @client.power_off_node(@uuid_array[0])
expect(response.status).to eq(200)

end
end

describe 'PUT /nodes/UUID' do
context 'with a leds object' do
context 'with state == "On" and name == "Identify"' do
it 'turns on the location led' do
@client.turn_on_loc_led(@uuidArray[0])
uri = "http://example.com/nodes/#{@uuidArray[0]}"
@client.turn_on_loc_led(@uuid_array[0])
uri = "#{@host}/nodes/#{@uuid_array[0]}"
request_body = { 'body' => { 'leds' => [{ 'name' => 'Identify',
'state' => 'On' }] } }
expect(a_request(:put, uri).with(request_body)).to have_been_made
end
end

context 'with state == "Off" and name == "Identify"' do
it 'turns off the location led' do
@client.turn_off_loc_led(@uuidArray[0])
uri = "http://example.com/nodes/#{@uuidArray[0]}"
@client.turn_off_loc_led(@uuid_array[0])
uri = "#{@host}/nodes/#{@uuid_array[0]}"
request_body = { 'body' => { 'leds' => [{ 'name' => 'Identify',
'state' => 'Off' }] } }
expect(a_request(:put, uri).with(request_body)).to have_been_made
end
end

context 'with state == "Blinking" and name == "Identify"' do
it 'turns on the blinking location led' do
@client.blink_loc_led(@uuidArray[0])
uri = "http://example.com/nodes/#{@uuidArray[0]}"
@client.blink_loc_led(@uuid_array[0])
uri = "#{@host}/nodes/#{@uuid_array[0]}"
request_body = { 'body' => { 'leds' => [{ 'name' => 'Identify',
'state' => 'Blinking' }] } }
expect(a_request(:put, uri).with(request_body)).to have_been_made
Expand Down

0 comments on commit a8c26a9

Please sign in to comment.