schemata-staging 0.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,214 @@
1
+ require 'schemata/component/foo'
2
+ require 'schemata/common/error'
3
+ require 'support/helpers'
4
+
5
+ describe Schemata::Component::Foo do
6
+ describe "V10" do
7
+ it "should define accessors for the schema" do
8
+ v10_obj = Schemata::Component::Foo::V10.new
9
+ v10_obj.respond_to?(:foo1).should be_true
10
+ v10_obj.respond_to?(:foo1=).should be_true
11
+ v10_obj.respond_to?(:foo2).should be_true
12
+ v10_obj.respond_to?(:foo2=).should be_true
13
+ end
14
+
15
+ it "should not raise an error if an incomplete but valid hash is given" do
16
+ v10_hash = {"foo1" => "foo"}
17
+ expect { Schemata::Component::Foo::V10.new(v10_hash) }.to_not raise_error
18
+ end
19
+
20
+ it "should raise an error if an incomplete and invalid hash is given" do
21
+ v10_hash = {"foo1" => 1}
22
+ expect {
23
+ Schemata::Component::Foo::V10.new(v10_hash)
24
+ }.to raise_error(Schemata::UpdateAttributeError)
25
+ end
26
+
27
+ it "should not raise an error if an attribute is correctly assigned" do
28
+ foo_obj = Schemata::Component.mock_foo 10
29
+ expect { foo_obj.foo1 = "new name" }.to_not raise_error
30
+ end
31
+
32
+ it "should raise an error if an attribute is incorrectly assigned" do
33
+ foo_obj = Schemata::Component.mock_foo 10
34
+ expect { foo_obj.foo1 = 1 }.to raise_error(Schemata::UpdateAttributeError)
35
+ end
36
+
37
+ it "should not raise an error if an incomplete msg_obj is updated \
38
+ correctly" do
39
+ v10_hash = {"foo1" => "foo"}
40
+ v10_obj = Schemata::Component::Foo::V10.new(v10_hash)
41
+ expect { v10_obj.foo1 = "new name" }.to_not raise_error
42
+ end
43
+
44
+ it "should raise an error if an incomplete msg_obj is updated \
45
+ incorrectly" do
46
+ v10_hash = {"foo1" => "foo"}
47
+ v10_obj = Schemata::Component::Foo::V10.new(v10_hash)
48
+ expect { v10_obj.foo1 = 1 }.to raise_error(Schemata::UpdateAttributeError)
49
+ end
50
+ end
51
+
52
+ describe "V11" do
53
+ it "should define accessors for the schema" do
54
+ v11_obj = Schemata::Component::Foo::V11.new
55
+ v11_obj.respond_to?(:foo1).should be_true
56
+ v11_obj.respond_to?(:foo1=).should be_true
57
+ v11_obj.respond_to?(:foo2).should be_true
58
+ v11_obj.respond_to?(:foo2=).should be_true
59
+ v11_obj.respond_to?(:foo3).should be_true
60
+ v11_obj.respond_to?(:foo3=).should be_true
61
+ end
62
+
63
+ it "should not raise an error if an incomplete but valid hash is given" do
64
+ v11_hash = {"foo1" => "foo"}
65
+ expect { Schemata::Component::Foo::V11.new(v11_hash) }.to_not raise_error
66
+ end
67
+
68
+ it "should raise an error if an incomplete and invalid hash is given" do
69
+ v11_hash = {"foo1" => 1}
70
+ expect {
71
+ v11_obj = Schemata::Component::Foo::V11.new(v11_hash)
72
+ }.to raise_error(Schemata::UpdateAttributeError)
73
+ end
74
+
75
+ it "should not raise an error if an attribute is correctly assigned" do
76
+ foo_obj = Schemata::Component.mock_foo 11
77
+ expect { foo_obj.foo2 = 10 }.to_not raise_error
78
+ end
79
+
80
+ it "should raise an error if an attribute is incorrectly assigned" do
81
+ foo_obj = Schemata::Component.mock_foo 11
82
+ expect {
83
+ foo_obj.foo2 = "foo"
84
+ }.to raise_error(Schemata::UpdateAttributeError)
85
+ end
86
+
87
+ it "should not raise an error if an incomplete msg_obj is updated \
88
+ correctly" do
89
+ v11_hash = {"foo1" => "foo"}
90
+ v11_obj = Schemata::Component::Foo::V11.new(v11_hash)
91
+ expect { v11_obj.foo1 = "new name" }.to_not raise_error
92
+ end
93
+
94
+ it "should raise an error if an incomplete msg_obj is updated \
95
+ incorrectly" do
96
+ v11_hash = {"foo1" => "foo"}
97
+ v11_obj = Schemata::Component::Foo::V11.new(v11_hash)
98
+ expect {
99
+ v11_obj.foo1 = 1
100
+ }.to raise_error(Schemata::UpdateAttributeError)
101
+ end
102
+ end
103
+
104
+ describe "V12" do
105
+ it "should define accessors for the schema" do
106
+ v12_obj = Schemata::Component::Foo::V12.new
107
+ v12_obj.respond_to?(:foo1).should be_true
108
+ v12_obj.respond_to?(:foo1=).should be_true
109
+ v12_obj.respond_to?(:foo2).should be_true
110
+ v12_obj.respond_to?(:foo2=).should be_true
111
+ v12_obj.respond_to?(:foo3).should be_true
112
+ v12_obj.respond_to?(:foo3=).should be_true
113
+ end
114
+
115
+ it "should not raise an error if an incomplete but valid hash is given" do
116
+ v12_hash = {"foo1" => "foo"}
117
+ expect { Schemata::Component::Foo::V12.new(v12_hash) }.to_not raise_error
118
+ end
119
+
120
+ it "should raise an error if an incomplete and invalid hash is given" do
121
+ v12_hash = {"foo1" => 1}
122
+ expect {
123
+ Schemata::Component::Foo::V12.new(v12_hash)
124
+ }.to raise_error(Schemata::UpdateAttributeError)
125
+ end
126
+
127
+ it "should not raise an error if an attribute is correctly assigned" do
128
+ foo_obj = Schemata::Component.mock_foo 12
129
+ expect { foo_obj.foo3 = [1, 2] }.to_not raise_error
130
+ end
131
+
132
+ it "should raise an error if an attribute is incorrectly assigned" do
133
+ foo_obj = Schemata::Component.mock_foo 12
134
+ expect {
135
+ foo_obj.foo3 = 1
136
+ }.to raise_error(Schemata::UpdateAttributeError)
137
+ end
138
+
139
+ it "should not raise an error if an incomplete msg_obj is updated \
140
+ correctly" do
141
+ v12_hash = {"foo1" => "foo"}
142
+ v12_obj = Schemata::Component::Foo::V12.new(v12_hash)
143
+ expect { v12_obj.foo1 = "new name" }.to_not raise_error
144
+ end
145
+
146
+ it "should raise an error if an incomplete msg_obj is updated \
147
+ incorrectly" do
148
+ v12_hash = {"foo1" => "foo"}
149
+ v12_obj = Schemata::Component::Foo::V12.new(v12_hash)
150
+ expect {
151
+ v12_obj.foo1 = 1
152
+ }.to raise_error(Schemata::UpdateAttributeError)
153
+ end
154
+ end
155
+
156
+ describe "V13" do
157
+ it "should define accessors for the schema" do
158
+ v13_obj = Schemata::Component::Foo::V13.new
159
+ v13_obj.respond_to?(:foo1).should be_true
160
+ v13_obj.respond_to?(:foo1=).should be_true
161
+ v13_obj.respond_to?(:foo3).should be_true
162
+ v13_obj.respond_to?(:foo3=).should be_true
163
+ v13_obj.respond_to?(:foo4).should be_true
164
+ v13_obj.respond_to?(:foo4=).should be_true
165
+ end
166
+
167
+ it "should not raise an error if an incomplete but valid hash is given" do
168
+ v13_hash = {"foo1" => "foo"}
169
+ expect { Schemata::Component::Foo::V13.new(v13_hash) }.to_not raise_error
170
+ end
171
+
172
+ it "should raise an error if an incomplete and invalid hash is given" do
173
+ v13_hash = {"foo1" => 1}
174
+ expect {
175
+ Schemata::Component::Foo::V13.new(v13_hash)
176
+ }.to raise_error(Schemata::UpdateAttributeError)
177
+ end
178
+
179
+ it "should not raise an error if an attribute is correctly assigned" do
180
+ foo_obj = Schemata::Component.mock_foo 13
181
+ expect { foo_obj.foo4 = "foobar" }.to_not raise_error
182
+ end
183
+
184
+ it "should raise an error if an attribute is incorrectly assigned" do
185
+ foo_obj = Schemata::Component.mock_foo 13
186
+ expect {
187
+ foo_obj.foo4 =1
188
+ }.to raise_error(Schemata::UpdateAttributeError)
189
+ end
190
+
191
+ it "should raise an error if an accessed field was removed" do
192
+ foo_obj = Schemata::Component.mock_foo 13
193
+ expect {
194
+ foo_obj.foo2
195
+ }.to raise_error
196
+ end
197
+
198
+ it "should not raise an error if an incomplete msg_obj is updated \
199
+ correctly" do
200
+ v13_hash = {"foo1" => "foo"}
201
+ v13_obj = Schemata::Component::Foo::V13.new(v13_hash)
202
+ expect { v13_obj.foo1 = "new name" }.to_not raise_error
203
+ end
204
+
205
+ it "should raise an error if an incomplete msg_obj is updated \
206
+ incorrectly" do
207
+ v13_hash = {"foo1" => "foo"}
208
+ v13_obj = Schemata::Component::Foo::V13.new(v13_hash)
209
+ expect {
210
+ v13_obj.foo1 = 1
211
+ }.to raise_error(Schemata::UpdateAttributeError)
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,186 @@
1
+ require 'yajl'
2
+ require 'support/helpers'
3
+ require 'schemata/staging'
4
+
5
+ describe Schemata::Staging do
6
+ describe ".mock_message" do
7
+ it "should return a V1 object" do
8
+ msg_obj = Schemata::Staging.mock_message(1)
9
+ msg_obj.class.should == Schemata::Staging::Message::V1
10
+ end
11
+ end
12
+ end
13
+
14
+ describe Schemata::Staging::Message do
15
+ describe ".decode" do
16
+ context "current version is 1" do
17
+
18
+ before :each do
19
+ set_current_version(Schemata::Staging::Message, 1)
20
+ end
21
+
22
+ after :each do
23
+ reset_version(Schemata::Staging::Message)
24
+ end
25
+
26
+ it "should return a V1 object when given a flat hash" do
27
+ json = Yajl::Encoder.encode(
28
+ Schemata::Staging::Message::V1::MOCK_VALUES)
29
+ msg_obj = Schemata::Staging::Message.decode(json)
30
+ msg_obj.class.should == Schemata::Staging::Message::V1
31
+
32
+ msg_obj.app_id.should == 1
33
+ msg_obj.download_uri.should == "http://foobar@172.0.0.0:100/download"
34
+ msg_obj.upload_uri.should == "http://foobar@172.0.0.0:100/upload"
35
+ msg_obj.properties.should ==
36
+ Schemata::Staging::Message::V1::MOCK_VALUES["properties"]
37
+ end
38
+
39
+ it "should return a V1 object when given a V1-encoded json" do
40
+ v1_hash = Schemata::HashCopyHelpers.deep_copy(
41
+ Schemata::Staging::Message::V1::MOCK_VALUES)
42
+ msg_hash = {"V1" => v1_hash, "min_version" => 1}
43
+
44
+ json = Yajl::Encoder.encode(msg_hash)
45
+
46
+ msg_obj = Schemata::Staging::Message.decode(json)
47
+ msg_obj.class.should == Schemata::Staging::Message::V1
48
+
49
+ msg_obj.app_id.should == 1
50
+ msg_obj.download_uri.should == "http://foobar@172.0.0.0:100/download"
51
+ msg_obj.upload_uri.should == "http://foobar@172.0.0.0:100/upload"
52
+ msg_obj.properties.should ==
53
+ Schemata::Staging::Message::V1::MOCK_VALUES["properties"]
54
+ end
55
+
56
+ it "should return a V1 object when given a mixed (flat hash + V1-encoded) json" do
57
+ msg_hash = Schemata::HashCopyHelpers.deep_copy(
58
+ Schemata::Staging::Message::V1::MOCK_VALUES)
59
+
60
+ v1_hash = Schemata::HashCopyHelpers.deep_copy(msg_hash)
61
+ msg_hash["V1"] = v1_hash
62
+
63
+ msg_hash["min_version"] = 1
64
+
65
+ json = Yajl::Encoder.encode(msg_hash)
66
+
67
+ msg_obj = Schemata::Staging::Message.decode(json)
68
+ msg_obj.class.should == Schemata::Staging::Message::V1
69
+
70
+ msg_obj.app_id.should == 1
71
+ msg_obj.download_uri.should == "http://foobar@172.0.0.0:100/download"
72
+ msg_obj.upload_uri.should == "http://foobar@172.0.0.0:100/upload"
73
+ msg_obj.properties.should ==
74
+ Schemata::Staging::Message::V1::MOCK_VALUES["properties"]
75
+ end
76
+
77
+ it "should return a V1 object when given a V2-encoded json message" do
78
+ v2_hash = Schemata::HashCopyHelpers.deep_copy(
79
+ Schemata::Staging::Message::V1::MOCK_VALUES
80
+ )
81
+ msg_hash = {
82
+ "V2" => v2_hash, "V1" => {}, "min_version" => 1
83
+ }
84
+
85
+ json = Yajl::Encoder.encode(msg_hash)
86
+
87
+ msg_obj = Schemata::Staging::Message.decode(json)
88
+ msg_obj.class.should == Schemata::Staging::Message::V1
89
+
90
+ msg_obj.app_id.should == 1
91
+ msg_obj.download_uri.should == "http://foobar@172.0.0.0:100/download"
92
+ msg_obj.upload_uri.should == "http://foobar@172.0.0.0:100/upload"
93
+ msg_obj.properties.should ==
94
+ Schemata::Staging::Message::V1::MOCK_VALUES["properties"]
95
+ end
96
+
97
+ it "should raise an error if the input does not have the valid Schemata structure or a complete flat hash of the data" do
98
+ json = Yajl::Encoder.encode({"app_id" => 1})
99
+ expect {
100
+ msg_obj = Schemata::Staging::Message.decode(json)
101
+ }.to raise_error(Schemata::DecodeError)
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ describe Schemata::Staging::Message::V1 do
108
+ before :each do
109
+ set_current_version(Schemata::Staging::Message, 1)
110
+ end
111
+
112
+ after :each do
113
+ reset_version(Schemata::Staging::Message)
114
+ end
115
+
116
+ describe "#new" do
117
+ it "should create a V1 obj with an incomplete hash" do
118
+ msg_obj = Schemata::Staging::Message::V1.new({"app_id" => 1})
119
+ end
120
+
121
+ it "should raise an error if the hash contains incorrect types" do
122
+ expect {
123
+ msg_obj = Schemata::Staging::Message::V1.new({"app_id" => "foo"})
124
+ }.to raise_error(Schemata::UpdateAttributeError)
125
+ end
126
+ end
127
+
128
+ describe "#encode" do
129
+ it "should return a json string, with V1 hash also in the raw payload" do
130
+ msg_obj = Schemata::Staging.mock_message(1)
131
+ json = msg_obj.encode
132
+ json_hash = Yajl::Parser.parse(json)
133
+
134
+ json_hash.should have_key "V1"
135
+ json_hash.should have_key "min_version"
136
+
137
+ v1_hash = json_hash['V1']
138
+ min_version = json_hash['min_version']
139
+ json_hash.delete('V1')
140
+ json_hash.delete('min_version')
141
+
142
+ v1_hash.should == json_hash
143
+ end
144
+
145
+ it "should raise an error if the object is not complete" do
146
+ msg_obj = Schemata::Staging::Message::V1.new({"app_id" => 1})
147
+ expect {
148
+ json = msg_obj.encode
149
+ }.to raise_error(Schemata::EncodeError)
150
+ end
151
+ end
152
+
153
+ describe "#app_id" do
154
+ it "should return the app_id if it was specified at instantiation" do
155
+ msg_obj = Schemata::Staging::Message::V1.new({"app_id" => 1})
156
+ msg_obj.app_id.should == 1
157
+ end
158
+
159
+ it "should return the app_id if it was set with an attr wrtier" do
160
+ msg_obj = Schemata::Staging::Message::V1.new({})
161
+ msg_obj.app_id = 1
162
+ msg_obj.app_id.should == 1
163
+ end
164
+
165
+ it "should return nil if the app_id was never set" do
166
+ msg_obj = Schemata::Staging::Message::V1.new({})
167
+ msg_obj.app_id.should be_nil
168
+ end
169
+ end
170
+
171
+ describe "#app_id=" do
172
+ it "should change the app_id and return the new value" do
173
+ msg_obj = Schemata::Staging::Message::V1.new({"app_id" => 1})
174
+ ret = (msg_obj.app_id = 2)
175
+ msg_obj.app_id.should == 2
176
+ ret.should == 2
177
+ end
178
+
179
+ it "should raise an error if the wrong type is written" do
180
+ msg_obj = Schemata::Staging::Message::V1.new({})
181
+ expect {
182
+ msg_obj.app_id = "foo"
183
+ }.to raise_error(Schemata::UpdateAttributeError)
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,27 @@
1
+ def set_current_version(msg_type, version)
2
+ msg_type.stub(:current_version).and_return(version)
3
+ @curr_class = msg_type.current_class
4
+
5
+ @future_versions = {}
6
+ msg_type::constants.each do |v|
7
+ next if !is_message_version?(v) || !greater_version?(v, version)
8
+ @future_versions[v] = msg_type::const_get(v)
9
+ msg_type.send(:remove_const, v)
10
+ end
11
+ end
12
+
13
+ def reset_version(msg_type)
14
+ @future_versions.each do |v, klass|
15
+ msg_type::const_set(v, klass)
16
+ end
17
+ @future_versions
18
+ end
19
+
20
+ def is_message_version?(constant)
21
+ !!(constant =~ /^V[0-9]+$/)
22
+ end
23
+
24
+ def greater_version?(version, benchmark)
25
+ version = version[1..-1].to_i
26
+ version > benchmark
27
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schemata-staging
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - dsabeti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: membrane
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yajl-ruby
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: ci_reporter
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: ! " Specify schema for staging messages and validate messages\n against
95
+ defined schema\n"
96
+ email:
97
+ - support@cloudfoundry.org
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - lib/schemata/staging/message/message_v1.rb
103
+ - lib/schemata/staging/version.rb
104
+ - lib/schemata/staging/message.rb
105
+ - lib/schemata/common/msgtypebase.rb
106
+ - lib/schemata/common/parsed_msg.rb
107
+ - lib/schemata/common/msgbase.rb
108
+ - lib/schemata/common/error.rb
109
+ - lib/schemata/staging.rb
110
+ - lib/schemata/helpers/hash_copy.rb
111
+ - spec/cloud_controller/cc_bar_spec.rb
112
+ - spec/common/helpers_spec.rb
113
+ - spec/common/parsed_msg_spec.rb
114
+ - spec/component/aux_data_spec.rb
115
+ - spec/component/component_bar_spec.rb
116
+ - spec/component/component_foo_spec.rb
117
+ - spec/component/foo_spec.rb
118
+ - spec/staging/staging_message_spec.rb
119
+ - spec/support/helpers.rb
120
+ homepage: http://www.cloudfoundry.org
121
+ licenses: []
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.24
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: validation for cloundfoundry stager messages
144
+ test_files:
145
+ - spec/cloud_controller/cc_bar_spec.rb
146
+ - spec/common/helpers_spec.rb
147
+ - spec/common/parsed_msg_spec.rb
148
+ - spec/component/aux_data_spec.rb
149
+ - spec/component/component_bar_spec.rb
150
+ - spec/component/component_foo_spec.rb
151
+ - spec/component/foo_spec.rb
152
+ - spec/staging/staging_message_spec.rb
153
+ - spec/support/helpers.rb