summaryrefslogtreecommitdiffstats
path: root/dcaeapplib/tests/test_dcaeapplib.py
blob: 99952643635361b4d14ef1e257f432fa0603e775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# org.onap.dcae
# ============LICENSE_START====================================================
# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
# =============================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END======================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

import dcaeapplib
import requests
import json

class Stubs:
  def __init__(self):
    self.toreturn = [ 's1', 's2', 's3' ]
    self.config = {
        "anotherparameter": 1,
        "streams_subscribes": {
          "myinputstream": {
            "aaf_username": "user1",
            "aaf_password": "pass1",
            "dmaap_info": {
              "topic_url": "http://messagerouter.example.com:3904/events/topic1"
            }
          }
        },
        "streams_publishes": {
          "myoutputstream": {
            "aaf_username": None,
            "aaf_password": None,
            "dmaap_info": {
              "topic_url": "http://messagerouter.example.com:3904/events/topic2"
            }
          }
        }
      }
    self.health = True
    self.cc = False

  def raise_for_status(self):
    pass

  def json(self):
    return self.toreturn

def test_todo(monkeypatch):
  stuff = Stubs()
  def stub_config():
    return json.loads(json.dumps(stuff.config))
  def stub_hc():
    return stuff.health
  def stub_cc():
    stuff.cc = True
  monkeypatch.setattr(dcaeapplib, 'get_config', stub_config)
  monkeypatch.setattr(dcaeapplib, '_httpport', 0)
  env = dcaeapplib.DcaeEnv(healthCB=stub_hc, reconfigCB=stub_cc)
  env.stop() # exercise stop when not running
  env.start()
  print('Port is {0}'.format(env._server.server_port))
  monkeypatch.setattr(dcaeapplib, '_httpport', env._server.server_port)
  stuff.config['anotherparameter'] = 2
  assert env.getconfig()['anotherparameter'] == 1
  dcaeapplib.reconfigure()
  assert stuff.cc is True
  assert env.getconfig()['anotherparameter'] == 2
  resp = requests.get('http://localhost:{0}/healthcheck'.format(dcaeapplib._httpport))
  assert resp.status_code < 300
  stuff.health = False
  resp = requests.get('http://localhost:{0}/healthcheck'.format(dcaeapplib._httpport))
  assert resp.status_code >= 500
  resp = requests.get('http://localhost:{0}/invalid'.format(dcaeapplib._httpport))
  assert resp.status_code == 404
  env.start() # exercise start when already running
  env.stop()
  def stub_get(*args, **kwargs):
    stuff.auth = 'auth' in kwargs
    return stuff
  def stub_post(url, data, *args, **kwargs):
    assert data == '4.11.asdfhello world'
    stuff.auth = 'auth' in kwargs
    stuff.posted = True
    return stuff
  monkeypatch.setattr(requests, 'post', stub_post)
  stuff.posted = False
  stuff.auth = True
  env.senddata('myoutputstream', 'asdf', 'hello world')
  assert stuff.posted == True
  assert stuff.auth == False
  monkeypatch.setattr(requests, 'get', stub_get)
  assert env.hasdata('myinputstream') is False
  assert env.getdata('myinputstream') == 's1'
  assert stuff.auth == True
  stuff.toreturn = [ 'a1' ]
  assert env.getdata('myinputstream') == 's2'
  assert env.hasdata('myinputstream') is True
  assert env.getdata('myinputstream') == 's3'
  assert env.hasdata('myinputstream') is False
  assert env.getdata('myinputstream') == 'a1'
  stuff.toreturn = []
  assert env.hasdata('myinputstream') is False
  assert env.getdata('myinputstream') is None