Cukes C0 Coverage Information - Simploco - RCov

library/generic/read_from_yml.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
library/generic/read_from_yml.rb 295 134
75.25%
45.52%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

2 =begin
3 *Name           : Read_From_YML
4 *Description    : class definition for reading data value from yml file
5 *Author         : Chandra sekaran
6 *Creation Date  : 23/08/2014
7 *Updation Date  :
8 =end
9 
10 module CUKES
11   class Read_From_YML
12     include FileLibrary     # module that define methods for all file manipulations
13 
14     # Description       : opens the yml file
15     # Author            : Chandra sekaran
16     # Arguments         :
17     #   str_file_path   : relative path of the yml file
18     #
19     def initialize(str_file_path = "")
20       raise "Exception : ReadFromYML instance cannot be created with filename" if str_file_path.nil?
21       raise "Exception : file does not exists #{str_file_path}" if !is_file_exists(str_file_path)
22       @yml = open_yml_file(str_file_path)
23       @yml_file_path = str_file_path
24     rescue Exception => ex
25       $log.error("Error in opening file '#{str_file_path}' : #{ex}")
26       exit
27     end
28 
29     # Description      : returns the value for the given input ypath
30     # Author           : Chandra sekaran
31     # Arguments        :
32     #   str_ypath      : ypath of the key element
33     # Return value     :
34     #   req_val        : value of the key element
35     #
36     def get_value(str_ypath = "")
37       raise "Exception : ReadFromYML - ypath cannot be empty" if str_ypath.nil?
38       req_val = traverse_yml(str_ypath)
39       return req_val
40     rescue Exception => ex
41       $log.error("Error in getting value for ypath '#{str_ypath}' : #{ex}")
42       exit
43     end
44 
45     # Description      : returns the hash value for the given unique key
46     # Author           : Chandra sekaran
47     # Arguments        :
48     #   str_key        : name of the unique key
49     # Return value     :
50     #   @req_hash      : hash value of the unique key element
51     #
52     def get_values(str_key)
53       raise "Exception : ReadFromYML - key cannot be empty" if str_key.nil?
54       @yml.each do |key, value|
55         @req_hash = ''
56         if key == str_key
57           @req_hash = value
58           break
59         else
60           if value.size >= 1 and value.class.to_s == "Hash"
61             @req_hash = traverse_hash(value, str_key)
62             return @req_hash if @req_hash.class.to_s == "Hash"
63           end
64         end
65       end
66     rescue Exception => ex
67       $log.error("Error in getting value for '#{str_key}' : #{ex}")
68       exit
69     end
70 
71     # Description      : returns the hash from the given input hash that matches the given key
72     # Author           : Chandra sekaran
73     # Arguments        :
74     #   hash_val       : hash element
75     #   req_key        : key to be found inside the hash element
76     # Return value     :
77     #    @hash_value   : value of the key element
78     #
79     def traverse_hash(hash_val, req_key)
80       hash_val.each do |key, value|
81         traverse_hash(value, req_key) if !value.nil? and value.size >= 1 and value.class.to_s == "Hash"
82         if key == req_key
83           @hash_value = value
84           break
85         end
86       end
87       return @hash_value
88     rescue Exception => ex
89       $log.error("Error in traversing hash '#{hash_val}' for '#{req_key}' : #{ex}")
90       exit
91     end
92 
93     # Description      : traverses the yml file and gets the value for the given input ypath
94     # Author           : Chandra sekaran
95     # Arguments        :
96     #   str_ypath      : ypath of the key element
97     # Return value     :
98     #    str_value     : value of the key element
99     #
100     def traverse_yml(str_ypath)
101       str_value = ''
102       arr_key = str_ypath.split('/')
103       num_level = arr_key.size
104 
105       case num_level
106         when 1
107           str_value = @yml[arr_key[0]]
108         when 2
109           str_value = @yml[arr_key[0]][arr_key[1]]
110         when 3
111           str_value = @yml[arr_key[0]][arr_key[1]][arr_key[2]]
112         when 4
113           str_value = @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]]
114         when 5
115           str_value = @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]][arr_key[4]]
116         when 6
117           str_value = @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]][arr_key[4]][arr_key[5]]
118         when 7
119           str_value = @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]][arr_key[4]][arr_key[5]][arr_key[6]]
120         else
121           raise "Given YML path cannot be traversed"
122       end
123       return str_value
124     rescue Exception => ex
125       $log.error("Error in traverse_yml '#{str_ypath}' : #{ex}")
126       exit
127     end
128 
129     # Description      : returns the profile name that is currently available
130     # Author           : Chandra sekaran
131     # Arguments        :
132     #   str_box        : profile name whether development/test
133     # Return value     :
134     #    str_key       : key name of the profile
135     #
136     def get_specific_profile(str_box)
137       str_key = ""
138       # iterates through the @yml content to find the BOX key and reads the value of :in_use key and returns the key
139       # if its value is "no"
140       @yml.each do |key, value|
141         value.each do |k, v|
142         if k.downcase == BOX.downcase
143         v.each do |k1, v1|
144           if k1.downcase.include? str_box.downcase
145             v1.each do |k2, v2|
146               if k2.downcase.include? "in_use"
147                 if v2.downcase == "no"
148                   str_key = k1
149                   set_value("application/#{BOX}/#{str_key}/in_use", "yes")    # updates the key values to "yes"
150                   change_execution_count("environment/parallel_execution_count", get_value("application/#{BOX}/#{str_key}/in_use"))
151                   break
152                 end
153               end
154             end
155             break if str_key != ""
156           end
157           break if str_key != ""
158         end
159         break if str_key != ""
160         end
161         break if str_key != ""
162         end
163         break if str_key != ""
164       end
165       raise "All the profiles (for Login credentials) are in use and hence stopping the execution for #{BOX}/#{str_box}" if str_key == ""
166       $log.info("Currently using #{str_key} under #{str_box} profile under #{BOX} box")
167       str_key
168     rescue Exception => ex
169       $log.error("Error in getting free BOX config values : #{ex}")
170       exit
171     end
172 
173     # Description      : returns the box and profile names that is currently available
174     # Author           : Chandra sekaran
175     # Arguments        :
176     #   str_box        : profile name whether development/test
177     # Return value     :
178     #    str_box       : key name of the box
179     #    str_key       : key name of the profile
180     #
181     def get_any_profile(str_box)
182       str_key = ""
183       str_box_avail = ""
184       # iterates through the @yml content to find the BOX key and reads the value of :in_use key and returns the key
185       # if its value is "no"
186       @yml.each do |key, value|
187         value.each do |k, v|
188           str_box_avail = k
189           v.each do |k1, v1|
190             if k1.downcase.include? str_box.downcase
191               v1.each do |k2, v2|
192                 if k2.downcase.include? "in_use"
193                   if v2.downcase == "no"
194                     str_key = k1
195                     set_value("application/#{str_box_avail}/#{str_key}/in_use", "yes")    # updates the key values to "yes"
196                     change_execution_count("environment/parallel_execution_count", get_value("application/#{str_box_avail}/#{str_key}/in_use"))
197                     break
198                   end
199                 end
200                 break if str_key != ""
201               end
202               #break if str_key != ""
203             end
204             break if str_key != ""
205           end
206           break if str_key != ""
207         end
208         break if str_key != ""
209       end
210       raise "All the profiles (for Login credentials) are in use and hence stopping the execution for #{str_box}" if str_key == ""
211       $log.info("Currently using #{str_key} under #{str_box} profile under #{str_box_avail} box")
212       return str_box_avail, str_key
213     rescue Exception => ex
214       $log.error("Error in getting free PROFILE config values : #{ex}")
215       exit
216     end
217 
218     # Description      : updates the parallel execution counter in config.yml file
219     # Author           : Gomathi
220     # Arguments        :
221     #   str_ypath      : ypath of yml content
222     #   str_value      : boolean value to update the counter
223     #
224     def change_execution_count(str_ypath, str_value)
225       if str_value == "yes"
226         $parallel_execution_count += 1
227       else
228         $parallel_execution_count -= 1
229       end
230       set_value(str_ypath, $parallel_execution_count)
231     end
232 
233     # Description      : sets the value for the given ypath
234     # Author           : Chandra sekaran
235     # Arguments        :
236     #   str_ypath      : ypath of yml content
237     # Return value     :
238     #    str_value     : value to be set for ypath
239     #
240     def set_value(str_ypath, str_value = "")
241       raise "Exception : ReadFromYML - ypath cannot be empty" if str_ypath.nil?
242 
243       @yml = open_yml_file(@yml_file_path)
244 
245       arr_key = str_ypath.split('/')
246       num_level = arr_key.size
247 
248       case num_level
249         when 1
250           @yml[arr_key[0]] = str_value
251         when 2
252           @yml[arr_key[0]][arr_key[1]] = str_value
253         when 3
254           @yml[arr_key[0]][arr_key[1]][arr_key[2]] = str_value
255         when 4
256           @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]] = str_value
257         when 5
258           @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]][arr_key[4]] = str_value
259         when 6
260           @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]][arr_key[4]][arr_key[5]] = str_value
261         when 7
262           @yml[arr_key[0]][arr_key[1]][arr_key[2]][arr_key[3]][arr_key[4]][arr_key[5]][arr_key[6]] = str_value
263         else
264           raise "Given YML path cannot be traversed"
265       end
266       File.open(@yml_file_path, 'w') { |h| h.write @yml.to_yaml }   # updates the yml file content with the updated hash
267       @yml = open_yml_file(@yml_file_path)
268     rescue Exception => ex
269       $log.error("Error in setting value for ypath '#{str_ypath}' : #{ex}")
270       exit
271     end
272 
273     # Description      : resets all the unused profiles
274     # Author           : Chandra sekaran
275     #
276     def release_all_profiles
277       @yml.each do |key, value|
278         if key.downcase == "application"
279           value.each do |k, v|
280             v.each do |k1, v1|
281               v1.each do |k2, v2|
282                 if k2.downcase.include? "in_use"
283                   set_value("#{key}/#{k}/#{k1}/#{k2}", "no") if v2.downcase == "yes"  # set "no" to in_use key for all profiles
284                 end
285               end
286             end
287           end
288         end
289       end
290     rescue Exception => ex
291       $log.error("Error in releasing all profiles : #{ex}")
292       exit
293     end
294 
295   end
296 end

Generated on 2015-05-08 10:40:30 +0530 with SimpleCov-RCov 0.2.3