Skip to content

Commit

Permalink
Add portfolio_item specs
Browse files Browse the repository at this point in the history
  • Loading branch information
syncrou committed Jan 7, 2019
1 parent aba1ac8 commit 1f03055
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 21 deletions.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def add_swagger_route(http_method, path, opts = {})
add_swagger_route 'GET', '/portfolios/{portfolio_id}', :controller_name => 'admins', :action_name => 'fetch_portfolio_with_id'
add_swagger_route 'PATCH', '/portfolios/{portfolio_id}', :controller_name => 'admins', :action_name => 'edit_portfolio'
add_swagger_route 'DELETE', '/portfolios/{portfolio_id}', :controller_name => 'admins', :action_name => 'destroy_portfolio'
add_swagger_route 'DELETE', '/portfolio_items/{portfolio_item_id}', :controller_name => 'admins', :action_name => 'destroy_portfolio_item'
add_swagger_route 'GET', '/orders/{order_id}/items/{order_item_id}', :controller_name => 'admins', :action_name => 'list_order_item'
add_swagger_route 'GET', '/orders/{order_id}/items', :controller_name => 'admins', :action_name => 'list_order_items'
add_swagger_route 'GET', '/orders', :controller_name => 'admins', :action_name => 'list_orders'
Expand Down
26 changes: 22 additions & 4 deletions spec/requests/portfolio_items_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
describe 'PortfolioItems API' do
include RequestSpecHelper
include ServiceSpecHelper

let!(:portfolio_item) { create(:portfolio_item) }
let(:portfolio_item_id) { portfolio_item.id }
Expand All @@ -13,7 +14,7 @@
%w(admin user).each do |tag|
describe "GET #{tag} tagged /portfolio_items" do
before do
get "/portfolio_items", headers: send("#{tag}_encode_key_with_tenant")
get "#{api}/portfolio_items", headers: send("#{tag}_encode_key_with_tenant")
end

context 'when portfolios exist' do
Expand All @@ -32,15 +33,32 @@
let(:valid_attributes) { { name: 'rspec 1', description: 'rspec 1 description' } }
context 'with wrong header' do
it 'returns a 404' do
expect(:post => "/portfolio_items").not_to be_routable
pending("Will work again when headers are checked")
expect(:post => "#{api}/portfolio_items").not_to be_routable
end
end
end

describe 'DELETE admin tagged /portfolio_items/:portfolio_item_id' do
let(:valid_attributes) { { :name => 'PatchPortfolio', :description => 'description for patched portfolio' } }

context 'when :portfolio_item_id is valid' do
before do
delete "#{api}/portfolio_items/#{portfolio_item_id}", :headers => admin_headers, :params => valid_attributes
end

it 'deletes the record' do
expect(response).to have_http_status(204)
end
end
end

describe 'POST admin tagged /portfolio_items' do
let(:valid_attributes) { { name: 'rspec 1', description: 'rspec 1 description', service_offering_ref: '10' } }
let(:valid_attributes) { { name: 'rh-mediawiki-apb', description: 'Mediawiki apb implementation', service_offering_ref: '21' } }
context 'when portfolio attributes are valid' do
before { post "/portfolio_items", params: valid_attributes, headers: admin_encode_key_with_tenant }
before do
post "#{api}/portfolio_items", params: valid_attributes, headers: admin_encode_key_with_tenant
end

it 'returns status code 200' do
expect(response).to have_http_status(200)
Expand Down
18 changes: 1 addition & 17 deletions spec/services/service_catalog/service_offering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,14 @@
[{:@name => name}, {:@description => description},
{:@service_offering_ref => service_offering_ref}]
end
let(:topology_service_offering) do
TopologicalInventoryApiClient::ServiceOffering.new('name' => name,
'id' => service_offering_ref,
'description' => description,
'source_ref' => '123',
'extra' => {},
'source_id' => '45')
end

let(:ti_class) { class_double("TopologicalInventory").as_stubbed_const(:transfer_nested_constants => true) }

before do
allow(ti_class).to receive(:call).and_yield(api_instance)
end

it "#{described_class}#find" do
expect(described_class).to receive(:new).and_return(service_offering)
expect(api_instance).to receive(:show_service_offering).with(service_offering_ref).and_return(topology_service_offering)
described_class.find(service_offering_ref)
end

it "#show" do
expect(api_instance).to receive(:show_service_offering).with(service_offering_ref).and_return(topology_service_offering)
service_offering.show(service_offering_ref)
expect(service_offering.show(service_offering_ref)).to be_a ServiceCatalog::ServiceOffering
end

it "#to_normalized_params" do
Expand Down
24 changes: 24 additions & 0 deletions spec/support/lib/topological_inventory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class TopologicalInventory
def self.api
Thread.current[:api_instance] ||= raw_api
end

def self.call
pass_thru_headers
yield api
rescue TopologicalInventoryApiClient::ApiError => err
Rails.logger.error("TopologicalInventoryApiClient::ApiError #{err.message} ")
raise ServiceCatalog::TopologyError, err.message
end

private_class_method def self.raw_api
TopologicalInventoryApiClient.configure do |config|
config.host = 'localhost'
config.scheme = 'http'
end
end

private_class_method def self.pass_thru_headers
{}
end
end
1 change: 1 addition & 0 deletions spec/support/service_catalog/service_offering.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"21","name":"rh-mediawiki-apb","description":"Mediawiki apb implementation"},{"id":"153","name":"s2i-fuse71-karaf-camel-rest-sql","description":"Camel example using Rest DSL with SQL Database in Karaf container. This example demonstrates how to use SQL via JDBC along with Camel's REST DSL to expose a RESTful API. The OpenShift MySQL container image should already be installed and running on your OpenShift installation, one simple way to run a MySQL service is following the documentation of the OpenShift MySQL container image related to the mysql-ephemeral template.."}]
51 changes: 51 additions & 0 deletions spec/support/service_catalog/service_offering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module ServiceCatalog
class ServiceOffering
def initialize
@name = nil
@description = nil
@service_offering_ref = nil
end

def self.find(id)
new.show(id)
end

def show(id)
@service_offering_ref = id
obj = nil
TopologicalInventory.call do |api_instance|
obj = JSON.parse(File.read("#{Rails.root}/spec/support/service_catalog/service_offering.json"))
end
resp = obj.select { |x| x['id'] == id }
@normalized = resp.first
self
end

def to_normalized_params
hashy = instance_variables.each_with_object({}) do |var, hash|
next if var == :@normalized
hash[var.to_s.delete("@")] = instance_variable_get(var)
end
if @normalized
@normalized["service_offering_ref"] = @normalized["id"]
@normalized
else
hashy.compact
end
end

private

def apply_instance_vars(obj)
uniq_ivars(obj).each do |ivar|
value = obj.instance_variable_get(ivar)
instance_variable_set(ivar, value)
end
self
end

def uniq_ivars(object)
instance_variables & object.instance_variables
end
end
end

0 comments on commit 1f03055

Please sign in to comment.