Module: CPEE::Instantiation::Helpers

Included in:
InstantiateGit, InstantiateUrl, InstantiateXML
Defined in:
lib/cpee-instantiation/instantiation.rb

Overview

{{{

Instance Method Summary collapse

Instance Method Details

#add_running_to_testset(behavior, doc) ⇒ Object

}}}



124
125
126
127
128
129
130
131
132
# File 'lib/cpee-instantiation/instantiation.rb', line 124

def add_running_to_testset(behavior,doc) #{{{
  if behavior =~ /_running$/
    if ((doc.find('/*/prop:state')).empty?)
      doc.root.append('prop:state','running')
    else
      doc.find('/*/prop:state').first.text = 'running'
    end
  end
end

#add_to_testset(tdoc, what, data) ⇒ Object

{{{



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cpee-instantiation/instantiation.rb', line 37

def add_to_testset(tdoc,what,data) #{{{
  if data && !data.empty?
    data = JSON::parse(data) if data.is_a? String
    data.each do |k,v|
      ele = tdoc.find("/*/prop:#{what}/prop:#{k}")
      if ele.any?
        ele.first.text = CPEE::ValueHelper::generate(v)
      else
        ele = tdoc.find("/*/prop:#{what}")
        ele = if ele.any?
          ele.first
        else
          tdoc.root.add("prop:#{what}")
        end
        ele.add(k,CPEE::ValueHelper::generate(v))
      end
    end
  end
end

#add_waiting_to_testset(behavior, cb, doc, selfurl) ⇒ Object

}}}



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cpee-instantiation/instantiation.rb', line 102

def add_waiting_to_testset(behavior,cb,doc,selfurl) #{{{
  ckb = nil
  if behavior =~ /^wait/
    condition = behavior.match(/_([^_]+)_/)&.[](1) || 'finished'

    if cb
      cbk = 'instantiation_' + Digest::MD5.hexdigest(Kernel::rand().to_s)
      n = doc.find('/*/sub:subscriptions') rescue []
      if (n.empty?)
        n = doc.root.add('subscriptions')
        n.namespaces.add(nil,'http://riddl.org/ns/common-patterns/notifications-producer/2.0')
      else
        n = n.first
      end
      n.append('subscription', :id => cbk, :url => File.join(selfurl,'callback',cbk))
       .append('topic', :id => 'state')
       .append('event','change')
    end
  end
  [cbk, condition]
end

#augment_testset(tdoc, p, h) ⇒ Object

}}}



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cpee-instantiation/instantiation.rb', line 57

def augment_testset(tdoc,p,h) #{{{
  tdoc = XML::Smart.string(tdoc)
  tdoc.register_namespace 'desc', 'http://cpee.org/ns/description/1.0'
  tdoc.register_namespace 'prop', 'http://cpee.org/ns/properties/2.0'
  tdoc.register_namespace 'sub', 'http://riddl.org/ns/common-patterns/notifications-producer/2.0'

  if data = p.find{ |e| e.name == 'init' }&.value
    add_to_testset(tdoc,'dataelements',data)
  end
  if data = p.find{ |e| e.name == 'endpoints' }&.value
    add_to_testset(tdoc,'endpoints',data)
  end
  if data = p.find{ |e| e.name == 'attributes' }&.value
    add_to_testset(tdoc,'attributes',data)
  end
  data = {}
  data['parent_instance']            = h['CPEE_INSTANCE']      if h['CPEE_INSTANCE']
  data['parent_instance_uuid']       = h['CPEE_INSTANCE_UUID'] if h['CPEE_INSTANCE_UUID']
  data['parent_instance_model']      = h['CPEE_ATTR_INFO']     if h['CPEE_ATTR_INFO']
  data['parent_instance_task_id']    = h['CPEE_ACTIVITY']      if h['CPEE_ACTIVITY']
  data['parent_instance_task_label'] = h['CPEE_LABEL']         if h['CPEE_LABEL']
  add_to_testset(tdoc,'attributes',data)
  tdoc
end

#customize_testset(customization, doc) ⇒ Object

}}}



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cpee-instantiation/instantiation.rb', line 82

def customize_testset(customization,doc) #{{{
  if customization && !customization.empty?
    JSON.parse(customization).each do |e|
      begin
        customization = Typhoeus.get e['url']
        if customization.success?
          XML::Smart::string(customization.response_body) do |str|
            doc.find("//desc:call[@id=\"#{e['id']}\"]/desc:parameters/desc:customization").each do |ele|
              ele.replace_by str.root
            end
          end
        end
      rescue => e
        puts e.message
        puts e.backtrace
      end
    end
  end
end

#instantiate_testset(cpee, doc, behavior, cblist, cbk, cb, condition) ⇒ Object

}}}



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cpee-instantiation/instantiation.rb', line 134

def instantiate_testset(cpee,doc,behavior,cblist,cbk,cb,condition) #{{{
  status, res, headers = Riddl::Client.new(cpee).post Riddl::Parameter::Complex.new('testset', 'application/xml', doc.to_s)
  if status == 200
    instance = res.first.value
    uuid = headers['CPEE_INSTANCE_UUID']

    if cbk
      cblist.rpush(cbk, cb)
      cblist.rpush(cbk, condition)
      cblist.rpush(cbk, instance)
      cblist.rpush(cbk, uuid)
      cblist.rpush(cbk, File.join(cpee,instance))
      @headers << Riddl::Header.new('CPEE-CALLBACK','true')
    end

    send = {
      'CPEE-INSTANCE' => instance,
      'CPEE-INSTANCE-URL' => File.join(cpee,instance),
      'CPEE-INSTANCE-UUID' => uuid,
      'CPEE-BEHAVIOR' => behavior
    }
    @headers << Riddl::Header.new('CPEE-INSTANTIATION',JSON::generate(send))
    Riddl::Parameter::Complex.new('instance','application/json',JSON::generate(send))
  else
    @status = 500
  end
end