Skip to content

Commit

Permalink
Transformation Mappings Read and Create
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo committed Feb 7, 2018
1 parent aca3a26 commit de8045c
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/controllers/api/transformation_mappings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Api
class TransformationMappingsController < BaseController
def create_resource(_type, _id, data = {})
raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"]
TransformationMapping.new(data.except("transformation_mapping_items")).tap do |mapping|
mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"])
mapping.save!
end
rescue StandardError => err
raise BadRequestError, "Could not create Transformation Mapping - #{err}"
end

private

def create_mapping_items(items)
items.collect do |item|
raise "Must specify source and destination hrefs" unless item["source"] && item["destination"]
source_href = Api::Href.new(item["source"])
source = resource_search(source_href.subject_id, source_href.subject, collection_class(source_href.subject.to_sym))
dest_href = Api::Href.new(item["destination"])
destination = resource_search(dest_href.subject_id, dest_href.subject, collection_class(dest_href.subject.to_sym))
TransformationMappingItem.new(:source => source, :destination => destination)
end
end
end
end
18 changes: 18 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,24 @@
:identifier: service_edit
- :name: delete
:identifier: service_edit
:transformation_mappings:
:description: Transformation Mappings
:identifier: transformation_mapping
:verbs: *gp
:klass: TransformationMapping
:options:
- :collection
:collection_actions:
:get:
- :name: read
:identifier: transformation_mappings_show_list
:post:
- :name: create
:identifier: transformation_mappings_new
:resource_actions:
:get:
- :name: read
:identifier: transformation_mappings_show
:settings:
:description: Settings
:identifier: ops_settings
Expand Down
114 changes: 114 additions & 0 deletions spec/requests/transformation_mappings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
describe "Transformation Mappings" do
let(:transformation_mapping) { FactoryGirl.create(:transformation_mapping) }

describe "GET /api/transformation_mappings" do
context "with an appropriate role" do
it "retrieves transformation mappings with an appropriate role" do
api_basic_authorize(collection_action_identifier(:transformation_mappings, :read, :get))

get(api_transformation_mappings_url)

expect(response).to have_http_status(:ok)
end
end

context "without an appropriate role" do
it "is forbidden" do
api_basic_authorize

get(api_transformation_mappings_url)

expect(response).to have_http_status(:forbidden)
end
end
end

describe "GET /api/transformation_mappings/:id" do
context "with an appropriate role" do
it "returns the transformation mapping" do
api_basic_authorize(action_identifier(:transformation_mappings, :read, :resource_actions, :get))

get(api_transformation_mapping_url(nil, transformation_mapping))

expect(response.parsed_body).to include('id' => transformation_mapping.id.to_s)
expect(response).to have_http_status(:ok)
end
end

context "without an appropriate role" do
it "is forbidden" do
api_basic_authorize

get(api_transformation_mapping_url(nil, transformation_mapping))

expect(response).to have_http_status(:forbidden)
end
end
end

describe "POST /api/transformation_mappings" do
let(:cluster) { FactoryGirl.create(:ems_cluster) }
let(:cluster2) { FactoryGirl.create(:ems_cluster) }

context "with an appropriate role" do
it "can create a new transformation mapping" do
api_basic_authorize(collection_action_identifier(:transformation_mappings, :create))

new_mapping = {"name" => "new transformation mapping",
"transformation_mapping_items" => [
{ "source" => api_cluster_url(nil, cluster), "destination" => api_cluster_url(nil, cluster2) }
]}
post(api_transformation_mappings_url, :params => new_mapping)

expected = {
"results" => [a_hash_including("href" => a_string_including(api_transformation_mappings_url),
"name" => "new transformation mapping")]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will raise a bad request if mappings are not specified" do
api_basic_authorize(collection_action_identifier(:transformation_mappings, :create))

new_mapping = {"name" => "new transformation mapping"}
post(api_transformation_mappings_url, :params => new_mapping)

expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => /Must specify transformation_mapping_items/
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end

it "will raise a bad request if source and destination are not specified for mappings" do
api_basic_authorize(collection_action_identifier(:transformation_mappings, :create))

new_mapping = {"name" => "new transformation mapping", "transformation_mapping_items" => [{}]}
post(api_transformation_mappings_url, :params => new_mapping)

expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => /Must specify source and destination hrefs/
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end
end

context "without an appropriate role" do
it "is forbidden" do
api_basic_authorize

get(api_transformation_mapping_url(nil, transformation_mapping))

expect(response).to have_http_status(:forbidden)
end
end
end
end

0 comments on commit de8045c

Please sign in to comment.