mockws 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fc10784350f028ff9da70f9b9bf05b81931cfdcd618e1cdc09477fccbb132eb
4
- data.tar.gz: ff660ee38d9e40363f978c79f06d5ffee632cc2ae901a1af210d46515ea87b84
3
+ metadata.gz: 3e1de112950ca390d13246ecbef426a8536892e2d35c34d73d9c45e437b218c0
4
+ data.tar.gz: e0014929f12a7fa0676d4cf54c8d06f1fb287838a3fdfa23e3023c7abd754bca
5
5
  SHA512:
6
- metadata.gz: c11fb2e102a4333cc79eb486da3d050d064a50fa866314e3cf4082f054e6faf9bd0d172d38e8e01b55ae3c9f664e575f34bdf1b577c9408f4d19a07bebdd6260
7
- data.tar.gz: b2f5521c00364ffc72755a6907cd7b87f05319c4b7d730b5f192dea14afcc9444c07ad85936c7cf71a00ab8cb94221c12a6eea96d80c25f2b36e2ae79246b421
6
+ metadata.gz: 161e1e3bbc8c2e523e54a900f3063736b6b3232f1d3b78dca3fbd715a45ea640fd8d482f60b85ebb8e984bb505e0d85961cf80957dfe685e3418c725642469fa
7
+ data.tar.gz: 2443ee795e54babb3a8e6999cebc6f76a8778d2beaec79f4c3273c1c0274d34590b06621a110b22fd66010a3b6b531df6bfe679058a42ead0e33b042a7897d65
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/config/mockws.yml CHANGED
@@ -40,4 +40,21 @@
40
40
  :value2: "partial"
41
41
  :to: :json
42
42
  :response_time: 2
43
-
43
+ :crud_test:
44
+ :type: :crud
45
+ :object: post
46
+ :to: :json
47
+ :model:
48
+ :name:
49
+ :type: :string
50
+ :unicity: true
51
+ :mandatory: true
52
+ :title:
53
+ :type: :string
54
+ :unicity: false
55
+ :mandatory: true
56
+ :description:
57
+ :type: :string
58
+ :unicity: false
59
+ :mandatory: false
60
+ :primary_key: :name
@@ -0,0 +1,254 @@
1
+ require 'ostruct'
2
+ require 'uuid'
3
+
4
+ module MockWS
5
+
6
+
7
+
8
+ class Store
9
+
10
+ TYPE_MAP = {
11
+ :string => :to_s,
12
+ :integer => :to_i,
13
+ :other => :to_s
14
+
15
+ }
16
+ def initialize( )
17
+ @models = {}
18
+ @definitions = {}
19
+ @keys = {}
20
+
21
+ end
22
+
23
+ def models
24
+ return @models.keys
25
+ end
26
+
27
+ def add_model(definition:)
28
+ @models[definition[:object]] = []
29
+ @definitions[definition[:object]] = definition[:model]
30
+ @keys[definition[:object]] = definition[:primary_key]
31
+ end
32
+
33
+ def delete_model(name:)
34
+ @models.delete(name)
35
+ end
36
+
37
+ def create(model:, data:)
38
+ data[:id] = UUID.generate
39
+ status = check_record(model: model, data: data)
40
+ @models[model].push data if status[:status] == true
41
+ return status
42
+ end
43
+
44
+
45
+ def retrieve(model:, key:)
46
+ return @models[model].select{|value| value[@keys[model]] == key}.first
47
+ end
48
+
49
+
50
+ def update(model:, key:, data:)
51
+ record = @models[model].select{|value| value[@keys[model]] == key}.first
52
+ status = check_record(model: model, data: data, update: true)
53
+ record.merge! data if status[:status] == true
54
+ return status
55
+ end
56
+
57
+
58
+ def destroy(model:, key:)
59
+ res = @models[model].reject!{|value| value[@keys[model]] == key}
60
+ return (res.nil?)? false : true
61
+ end
62
+
63
+ def list(model: )
64
+ return @models[model]
65
+ end
66
+
67
+ def exist?(model:, key:)
68
+ return @models[model].index{|value| value[@keys[model]] == key} == 1
69
+ end
70
+
71
+ private
72
+ def check_record(model:, data: , update: false)
73
+ result = {status: true, unknowns: [],mandatories: [], unicity: []}
74
+ result[:unknowns].concat check_unknown(model: model, data: data)
75
+ result[:mandatories].concat check_mandatory(model: model, data: data) unless update
76
+ result[:unicity].concat check_unicity(model: model, data: data)
77
+ result[:status] = false if (!result[:unknowns].empty? or !result[:mandatories].empty? or !result[:unicity].empty?)
78
+ return result
79
+ end
80
+
81
+ def check_unknown(model:, data: )
82
+ unknowns = []
83
+ data.keys.each do |item|
84
+ unknowns.push item unless @definitions[model].keys.include? item
85
+ end
86
+ unknowns.delete :id
87
+ return unknowns
88
+ end
89
+
90
+ def check_mandatory(model:, data: )
91
+ mandatories = []
92
+ @definitions[model].select {|_key, value| value.dig(:mandatory) == true }.keys.each do |key|
93
+ mandatories.push key unless data.keys.include? key
94
+ end
95
+ return mandatories
96
+ end
97
+
98
+ def check_unicity(model:, data: )
99
+ unicity = []
100
+ data.each do |key,value|
101
+ if @definitions[model].dig(key, :unicity) then
102
+ unicity.push key unless @models[model].select {|item| item[key] == value }.empty?
103
+ end
104
+ end
105
+ return unicity
106
+ end
107
+
108
+
109
+ end
110
+
111
+
112
+
113
+
114
+
115
+
116
+ class CRUDManager
117
+
118
+ extend Carioca::Injector
119
+ inject service: :output
120
+ inject service: :configuration
121
+
122
+ DEFAULT_STATUS = 200
123
+
124
+ def self.get_response_time(value)
125
+ return {value: value[:response_time], type: :static} if value.include? :response_time
126
+ return {value: rand(1..value[:random_time]), type: :random} if value.include? :random_time
127
+ return {value: 0, type: :instant}
128
+ end
129
+
130
+
131
+ def self.configure(service)
132
+ @@service = service
133
+ unless configuration.settings.services.select {|key,value| value[:type] == :crud }.empty? then
134
+ @@store = MockWS::Store::new
135
+ output.info "Mock CRUD initialisation : "
136
+ configuration.settings.services.select {|key,value| value[:type] == :crud }.each do |_key, definition|
137
+ @@store.add_model definition: definition
138
+ output.item "Create JSON CRUD for #{definition[:object]}"
139
+ output.arrow "GET /crud/#{definition[:object]}/list"
140
+ output.arrow "GET /crud/#{definition[:object]}/<KEY>"
141
+ output.arrow "POST /crud/#{definition[:object]} [JSON BODY]"
142
+ output.arrow "PUT /crud/#{definition[:object]} [JSON BODY]"
143
+ output.arrow "DELETE /crud/#{definition[:object]}/<KEY> "
144
+
145
+ end
146
+ create_crud_service
147
+ end
148
+ end
149
+
150
+ private
151
+
152
+ def self.create_crud_service
153
+ @@service.get '/crud/:model/list' do |model|
154
+ result = finisher.secure_api_return(return_case: :status_ok, structured: true, json: true) do
155
+ finisher.secure_raise message: "Error model #{model} not found !", error_case: :bad_request unless @@store.models.include? model
156
+ @@store.list(model: model)
157
+ end
158
+ status result[:code]
159
+ result[:data]
160
+ end
161
+
162
+ @@service.get '/crud/:model/:item' do |model,item|
163
+ result = finisher.secure_api_return(return_case: :accepted, structured: true, json: true) do
164
+ finisher.secure_raise message: "Error model #{model} not found !", error_case: :bad_request unless @@store.models.include? model
165
+ res = @@store.retrieve(model: model, key: item)
166
+ finisher.secure_raise message: "No record found", error_case: :no_content if res.nil?
167
+ res
168
+ end
169
+ status result[:code]
170
+ result[:data]
171
+ end
172
+
173
+ @@service.put '/crud/:model/:item' do |model,item|
174
+ result = finisher.secure_api_return(return_case: :accepted, structured: true, json: true) do
175
+ finisher.secure_raise message: "Error model #{model} not found !", error_case: :bad_request unless @@store.models.include? model
176
+ data = JSON.parse(request.body.read, symbolize_names: true)
177
+ res = @@store.update model: model, key: item, data: data
178
+ code = res.delete(:status)
179
+ res = "Record successfully updated"
180
+ finisher.secure_raise message: "Update Error : #{res} ", error_case: :status_ko unless code
181
+ res
182
+
183
+ end
184
+ status result[:code]
185
+ result[:data]
186
+ end
187
+
188
+ @@service.post '/crud/:model' do |model|
189
+ result = finisher.secure_api_return(return_case: :created, structured: true, json: true) do
190
+ data = JSON.parse(request.body.read, symbolize_names: true)
191
+ finisher.secure_raise message: "Error model #{model} not found !", error_case: :bad_request unless @@store.models.include? model
192
+ res = @@store.create model: model, data: data
193
+ code = res.delete(:status)
194
+ res = "Record successfully created"
195
+ finisher.secure_raise message: "Creation Error : #{res} ", error_case: :bad_request unless code
196
+ res
197
+ end
198
+ status result[:code]
199
+ result[:data]
200
+ end
201
+
202
+ @@service.delete '/crud/:model/:item' do |model,item|
203
+ result = finisher.secure_api_return(return_case: :accepted, structured: true, json: true) do
204
+ finisher.secure_raise message: "Error model #{model} not found !", error_case: :bad_request unless @@store.models.include? model
205
+ if @@store.destroy(model: model, key: item) then
206
+ res = "Record #{item} successfully deleted"
207
+ else
208
+ finisher.secure_raise message: "Suppression failed : #{item} ", error_case: :bad_request unless @@store.destroy(model: model, key: item)
209
+ end
210
+ res
211
+ end
212
+ status result[:code]
213
+ result[:data]
214
+ end
215
+
216
+
217
+ end
218
+
219
+
220
+ end
221
+
222
+ end
223
+
224
+
225
+
226
+
227
+
228
+ # store = MockWS::Store::new
229
+ # store.add_model definition: {:type=>:crud,
230
+ # :object=>"post",
231
+ # :primary_key => :name,
232
+ # :model=>{
233
+ # :name => {:type => :string, :mandatory => true, :unicity => true},
234
+ # :title=> {:type => :string, :mandatory => true, :unicity => false},
235
+ # :description=> {:type => :string, :mandatory => false, :unicity => false}
236
+ # }
237
+ # }
238
+ # print "* create good post1 : " ;puts (store.create model: "post", data: {name: "post1", title: "mon titre", description: "bla blah"})? "created" : "error"
239
+ # print "* create good post2 : " ;puts (store.create model: "post", data: {name: "post2", title: "mon titre", description: "bla blah"})? "created" : "error"
240
+ # print "* create new post2 (duplicate): " ;puts (store.create model: "post", data: {name: "post2", title: "mon titre", description: "bla blah"})? "created" : "error"
241
+ # print "* create bad post3 (unknown key) : " ;puts (store.create model: "post", data: {name: "post3", title: "mon titre", description: "bla blah", toto: "test"})? "created" : "error"
242
+ # print "* create bad post5 (mandatory title) : " ;puts (store.create model: "post", data: {name: "post5", description: "bla blah"})? "created" : "error"
243
+ # print "* list : " ;p store.list model: "post"
244
+ # print "* get post1 : " ; p store.retrieve model: "post", key: "post1"
245
+ # print "* update : " ;p store.update model: "post", key: "post2", data: {title: "toto"}
246
+ # print "* update (not a mandatory): " ;p store.update model: "post", key: "post2", data: {description: "desc 2"}
247
+ # print "* update bad (unknown key): " ;p store.update model: "post", key: "post2", data: {title: "toto", toto: "test"}
248
+ # print "* update bad (unicity): " ;p store.update model: "post", key: "post2", data: {title: "toto", name: "post1" }
249
+ # print "* update change name: " ;p store.update model: "post", key: "post2", data: { name: "post4" }
250
+ # print "* list : " ;p store.list model: "post"
251
+ # print "* exists post4 : " ;p store.exist? model: "post", key: "post4"
252
+ # print "* exists post3 : " ;p store.exist? model: "post", key: "post3"
253
+ # print "* delete post1 : " ;p store.destroy model: "post", key: "post1"
254
+ # print "* list : " ; p store.list model: "post"
@@ -17,7 +17,7 @@ module MockWS
17
17
  def self.configure(service)
18
18
  @service = service
19
19
  output.info "Mock routes initialisation : "
20
- configuration.settings.services.each do |_key, definition|
20
+ configuration.settings.services.select {|key,value| [:static,:inline].include? value[:type]}.each do |_key, definition|
21
21
  create_route(definition)
22
22
  end
23
23
  end
data/lib/mockws.rb CHANGED
@@ -72,6 +72,7 @@ end
72
72
 
73
73
 
74
74
  require_relative 'mockws/route_manager'
75
+ require_relative 'mockws/crud_manager'
75
76
  require_relative 'mockws/data_manager'
76
77
  require_relative 'mockws/daemon_controller'
77
78
 
@@ -87,11 +88,13 @@ module MockWS
87
88
  extend Carioca::Injector
88
89
  inject service: :output
89
90
  inject service: :configuration
91
+ inject service: :finisher
90
92
 
91
93
 
92
94
 
93
95
  def self.init
94
96
  MockWS::RouteManager::configure(self)
97
+ MockWS::CRUDManager::configure(self)
95
98
  end
96
99
 
97
100
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mockws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camille Paquet
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2023-10-03 00:00:00.000000000 Z
13
+ date: 2023-10-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: code_statistics
@@ -201,6 +201,7 @@ files:
201
201
  - exe/mockws
202
202
  - lib/mockws.rb
203
203
  - lib/mockws/cli.rb
204
+ - lib/mockws/crud_manager.rb
204
205
  - lib/mockws/daemon_controller.rb
205
206
  - lib/mockws/data_manager.rb
206
207
  - lib/mockws/route_manager.rb