-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transformation Mappings Read and Create
- Loading branch information
Jillian Tullo
committed
Feb 12, 2018
1 parent
1e6328a
commit 3d832d6
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |