Skip to content

Commit

Permalink
Merge pull request #7 from mtaylor/initial_factory_integration
Browse files Browse the repository at this point in the history
Initial factory integration
  • Loading branch information
jguiditta committed May 30, 2012
2 parents 6df6340 + 211e22d commit 3140058
Show file tree
Hide file tree
Showing 27 changed files with 229 additions and 17 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ group :development, :test do
gem "rspec-rails"
gem "cucumber-rails"
gem "database_cleaner"
gem "factory_girl"
end

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
diff-lcs (1.1.3)
erubis (2.6.6)
abstract (>= 1.0.0)
factory_girl (2.0.5)
ffi (1.0.11)
gherkin (2.9.3)
json (>= 1.4.6)
Expand Down Expand Up @@ -127,6 +128,7 @@ PLATFORMS
DEPENDENCIES
cucumber-rails
database_cleaner
factory_girl
rails (= 3.0.10)
rspec-rails
sqlite3
29 changes: 28 additions & 1 deletion app/models/image_management/target_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,32 @@ module ImageManagement
class TargetImage < ActiveRecord::Base
belongs_to :image_version
has_many :provider_images

before_save :create_factory_target_image

attr_accessible :target

def template
image_version.base_image.template
end

private
def create_factory_target_image
begin
target_image = ImageFactory::TargetImage.create(:template => template.to_xml,
:target => target)
populate_factory_fields(target_image)
# TODO Add in proper exception handling
rescue => e
raise e
end
end

def populate_factory_fields(factory_target_image)
self.status = factory_target_image.status
self.factory_id = factory_target_image.id
self.status_detail = factory_target_image.status_detail
self.progress = factory_target_image.percentage_complete
end
end
end
end
19 changes: 12 additions & 7 deletions lib/generators/image_management/image_management_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ def self.source_root

def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
# We use nano milli seconds here to avoid ID conflicts on multiple migrations
# Sleep here to avoid timestamp conflicts in migrations
sleep 1
time = Time.now.utc
"#{time.strftime("%Y%m%d%H%M%S")}#{time.usec}"
"#{time.strftime("%Y%m%d%H%M%S")}"
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

def create_migration_file
migration_template 'create_base_images.rb', 'db/migrate/create_base_images.rb'
migration_template 'create_image_versions.rb', 'db/migrate/create_image_versions.rb'
migration_template 'create_target_images.rb', 'db/migrate/create_target_images.rb'
migration_template 'create_provider_images.rb', 'db/migrate/create_provider_images.rb'
migration_template 'create_templates.rb', 'db/migrate/create_templates.rb'
migration_template 'migrations/create_base_images.rb', 'db/migrate/create_base_images.rb'
migration_template 'migrations/create_image_versions.rb', 'db/migrate/create_image_versions.rb'
migration_template 'migrations/create_target_images.rb', 'db/migrate/create_target_images.rb'
migration_template 'migrations/create_provider_images.rb', 'db/migrate/create_provider_images.rb'
migration_template 'migrations/create_templates.rb', 'db/migrate/create_templates.rb'
end

def copy_initializer_file
copy_file "intializers/image_management_engine.rb", "config/initializers/image_management_engine.rb"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
############################## Image Management Engine Initializer ################################

# Image Factory URL
ImageManagement::ImageFactory::Base.site = "http://localhost:8075/imagefactory"
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ def self.up
t.integer :id
t.string :factory_id
t.integer :image_version_id
# Provider returned String for target type
t.string :provider_type_id
t.string :target
t.string :status
t.string :status_details
t.string :status_detail
t.string :progress # Factory Target Image percent_complete

t.timestamps
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/image_factory/image_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "active_resource"
require File.join(File.dirname(__FILE__), 'model', 'base')
require File.join(File.dirname(__FILE__), 'model', 'target_image')
require File.join(File.dirname(__FILE__), 'model', 'provider_image')
77 changes: 77 additions & 0 deletions lib/image_factory/model/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module ImageManagement
module ImageFactory
class Base < ActiveResource::Base
self.format = :json
# TODO Verify Image Factory Certification Concerns that warrent removing SSL Verfication
self.ssl_options = {:verify_mode => OpenSSL::SSL::VERIFY_NONE}

class << self
## Remove format from the url for resources
def element_path(id, prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
end

## Remove format from the url for collections
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end

## For a collection call, ActiveResource formatting is not
## compliant with Factory's output.
def instantiate_collection(collection, prefix_options = {})
unless collection.kind_of? Array
[instantiate_record(collection, prefix_options)]
else
collection.collect! { |record| instantiate_record(record, prefix_options) }
end
end

## The objects returned from this method are not automatically converted into ActiveResource instances - they are ordinary Hashes.
## Modifications below ensures that you get ActiveResource instances.
def get(method_name, options = {})
object_array = connection.get(custom_method_collection_url(method_name, options), headers)
if object_array.class.to_s=="Array"
object_array.collect! {|record| self.class.new.load(record)}
else
self.class.new.load(object_array)
end
end

# This approach does mean you're limited to one server at a time
def config
defined?(@@config) ? @@config : {}
end
def config=(conf={})
@@config = conf
self.site = @@config[:site]
end

# Should we use OAuth?
def use_oauth?
config[:consumer_key] && config[:consumer_secret] && config[:site]
end
end

## Instance Methods: (modifying the ActiveRecord::CustomMethods).
## This modification is same as defined in above method
def get(method_name, options = {})
self.class.new.load(connection.get(custom_method_element_url(method_name, options), self.class.headers))
end

# Modifying the url formations to make them Factory compliant
def custom_method_element_url(method_name, options = {})
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/" +
"#{method_name}#{self.class.send!(:query_string, options)}"
end

# Modifying the url formations to make them Factory compliant
def self.custom_method_collection_url(method_name, options = {})
prefix_options, query_options = split_options(options)
url = "#{prefix(prefix_options)}#{collection_name}/#{method_name}#{query_string(query_options)}"
url
end
end
end
end
6 changes: 6 additions & 0 deletions lib/image_factory/model/provider_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ImageManagement
module ImageFactory
class ProviderImage < Base
end
end
end
6 changes: 6 additions & 0 deletions lib/image_factory/model/target_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ImageManagement
module ImageFactory
class TargetImage < Base
end
end
end
3 changes: 2 additions & 1 deletion lib/image_management.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require "image_management/engine"
require "image_management/engine"
require "image_factory/image_factory.rb"
5 changes: 5 additions & 0 deletions spec/factories/image_management/base_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :base_image, :class => ImageManagement::BaseImage do
association :template, :factory => :template
end
end
7 changes: 7 additions & 0 deletions spec/factories/image_management/image_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ImageManagement
FactoryGirl.define do
factory :image_version, :class => ImageManagement::ImageVersion do
association :base_image, :factory => :base_image
end
end
end
6 changes: 6 additions & 0 deletions spec/factories/image_management/target_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :target_image, :class => ImageManagement::TargetImage do
association :image_version, :factory => :image_version
target 'Mock'
end
end
5 changes: 5 additions & 0 deletions spec/factories/image_management/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :template, :class => ImageManagement::Template do
location 'http://localhost:3000/templates/1'
end
end
4 changes: 4 additions & 0 deletions spec/models/dummy/provider_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe ProviderType do
describe "Dummy Model relationships" do
before (:each) do
ImageManagement::TargetImage.any_instance.stub(:create_factory_target_image).and_return(true)
end

it 'should have many target images' do
provider_type = ProviderType.new
2.times do
Expand Down
4 changes: 4 additions & 0 deletions spec/models/image_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module ImageManagement
describe ImageVersion do
describe "Model relationships" do
before (:each) do
TargetImage.any_instance.stub(:create_factory_target_image).and_return(true)
end

it 'should have one base image' do
image_version = ImageVersion.new
image_version.base_image = BaseImage.new
Expand Down
4 changes: 4 additions & 0 deletions spec/models/provider_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module ImageManagement
describe ProviderImage do
before (:each) do
TargetImage.any_instance.stub(:create_factory_target_image).and_return(true)
end

describe "Model relationships" do
it 'should have one target image' do
provider_image = ProviderImage.new
Expand Down
30 changes: 30 additions & 0 deletions spec/models/target_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

module ImageManagement
describe TargetImage do

#TODO FIX: A bug in RSpec V2.1 means that any_instance propogates across context therefore we are stubbing each
#target_image. This bug is fixed in RSpec V2.6
describe "Model relationships" do
it 'should have one images version' do
target_image = TargetImage.new
target_image.stub(:create_factory_target_image).and_return(true)
target_image.image_version = ImageVersion.new
target_image.save!
TargetImage.find(target_image).image_version.should == target_image.image_version
end

it 'should have many provider images' do
target_image = TargetImage.new
target_image.stub(:create_factory_target_image).and_return(true)
2.times do
target_image.provider_images << ProviderImage.new
end
Expand All @@ -23,10 +28,35 @@ module ImageManagement
describe "Dummy model relationships" do
it "should have one provider type" do
target_image = TargetImage.new
target_image.stub(:create_factory_target_image).and_return(true)
target_image.provider_type = ProviderType.new
target_image.save!
TargetImage.find(target_image).provider_type.should == target_image.provider_type
end
end

describe "ImageFactory interactions" do
describe "Successful Requests" do
before(:each) do
@mock_fti = {:id => "4cc3b024-5fe7-4b0b-934b-c5d463b990b0",
:status => "NEW",
:status_detail => "",
:percentage_complete => 0}

ActiveResource::HttpMock.respond_to do |mock|
mock.post("/imagefactory/target_images", {}, @mock_fti.to_json, 201, {})
end
end

it "should create new target image with factory meta-data" do
ti = Factory.create(:target_image)

ti.factory_id.should == "4cc3b024-5fe7-4b0b-934b-c5d463b990b0"
ti.status.should == "NEW"
ti.status_detail.should == ""
ti.progress.should == 0
end
end
end
end
end
9 changes: 7 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Setup Rails Envinronment
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)

require File.expand_path("../../lib/image_factory/image_factory.rb", __FILE__)
require 'rspec'
require 'factory_girl'

Dir.glob(File.dirname(__FILE__) + "/factories/*/*").each do |factory|
require factory
end

RSpec.configure do |config|
config.color_enabled = true
config.formatter = 'documentation'
end
end
4 changes: 4 additions & 0 deletions test/dummy/config/initializers/image_management_engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
############################## Image Management Engine Initializer ################################

# Image Factory URL
ImageManagement::ImageFactory::Base.site = "http://localhost:8075/imagefactory"
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ def self.up
t.integer :id
t.string :factory_id
t.integer :image_version_id
# Provider returned String for target type
t.string :target
t.string :status
t.string :status_details
t.string :status_detail
t.string :progress # Factory Target Image percent_complete

t.timestamps
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
create_table "target_images", :force => true do |t|
t.string "factory_id"
t.integer "image_version_id"
t.string "target"
t.string "status"
t.string "status_details"
t.string "status_detail"
t.string "progress"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "provider_type_id"
Expand Down

0 comments on commit 3140058

Please sign in to comment.