Add ut for dmaap in catalog 85/98985/3
authoryangyan <yangyanyj@chinamobile.com>
Mon, 2 Dec 2019 01:47:22 +0000 (09:47 +0800)
committerYan Yang <yangyanyj@chinamobile.com>
Mon, 2 Dec 2019 06:44:31 +0000 (06:44 +0000)
Change-Id: I5940800c621f309fd6d95872141b2b1c5a7cf73a
Issue-ID: VFC-1592
Signed-off-by: yangyan <yangyanyj@chinamobile.com>
catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/__init__.py
catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/consumer.py
catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/publisher.py
catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py [new file with mode: 0644]
catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py [new file with mode: 0644]

index 0c1e8e1..7ae04f0 100644 (file)
@@ -1,8 +1,11 @@
 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
-# Licensed under the Apache License, Version 2.0 (the "License")
+
+# 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.
index e5677d8..dcc7f95 100644 (file)
@@ -24,7 +24,8 @@ logger = logging.getLogger(__name__)
 
 
 class ConsumerClient:
-    def __init__(self, host, topic, consumer_group, consumer_id, timeout_ms=-1, limit=-1, filter=''):
+    def __init__(self, host, topic, consumer_group, consumer_id, timeout_ms=-1, limit=-1, filter='',
+                 api_key='', api_secret=''):
         self.host = host
         self.topic = topic
         self.group = consumer_group
@@ -32,8 +33,11 @@ class ConsumerClient:
         self.timeout_ms = timeout_ms
         self.limit = limit
         self.filter = filter
+        self.api_key = api_key
+        self.api_secret = api_secret
 
     def set_api_credentials(self, api_key, api_secret):
+
         self.api_key = api_key
         self.api_secret = api_secret
 
index e5d1987..e9ff1b0 100644 (file)
@@ -38,6 +38,8 @@ class BatchPublisherClient:
         self.closed = False
         self.dont_send_until_ms = 0
         self.scheduler = Scheduler(standalone=False)
+        self.api_key = '',
+        self.api_secret = ''
 
         @self.scheduler.interval_schedule(second=1)
         def crawl_job():
diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py
new file mode 100644 (file)
index 0000000..2d0447e
--- /dev/null
@@ -0,0 +1,81 @@
+# Copyright (c) 2019, CMCC Technologies Co., Ltd.
+
+# 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.
+
+import base64
+import datetime
+import hmac
+import unittest
+from _sha1 import sha1
+
+from dmaap.consumer import ConsumerClient
+
+
+class CreateApiKeyTest(unittest.TestCase):
+    def setUp(self):
+        self.apiKey = "7TuwzpLJ4QfQs4O"
+        self.apiSecret = "7TuwzpLJ4QfQs4O"
+        self.host = '127.0.0.1'
+        self.topic = 'abc'
+        self.group = 'def'
+        self.comsumer_id = '123'
+        self.timeout_ms = 3
+        self.limit = 3
+        self.filter = 'test'
+
+    def tearDown(self):
+        self.ret_url = ""
+
+    def test_create_url(self):
+        exp_url = 'http://127.0.0.1/events/abc/def/123'
+        consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id)
+        ret_url = consumer.create_url()
+        self.assertEqual(exp_url, ret_url)
+
+    def test_create_timeout_url(self):
+        exp_url = 'http://127.0.0.1/events/abc/def/123?timeout=3'
+        consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id, self.timeout_ms)
+        ret_url = consumer.create_url()
+        self.assertEqual(exp_url, ret_url)
+
+    def test_create_limit_url(self):
+
+        exp_url = 'http://127.0.0.1/events/abc/def/123?timeout=3&limit=3'
+        consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id,
+                                  self.timeout_ms, self.limit)
+        ret_url = consumer.create_url()
+        self.assertEqual(exp_url, ret_url)
+
+    def test_create_filter_url(self):
+
+        exp_url = "http://127.0.0.1/events/abc/def/123?timeout=3&limit=3&filter=b'test'"
+        consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id,
+                                  self.timeout_ms, self.limit, self.filter)
+        ret_url = consumer.create_url()
+        self.assertEqual(exp_url, ret_url)
+
+    def test_create_headers(self):
+        data = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') + '-04:00'
+        hmac_code = hmac.new(self.apiSecret.encode(), data.encode(), sha1).digest()
+        signature = base64.b64encode(hmac_code).decode()
+        auth = self.apiKey + ':' + signature
+        exp_headers = {
+            'X-CambriaDate': data,
+            'X-CambriaAuth': auth
+        }
+
+        consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id,
+                                  self.timeout_ms, self.limit, self.filter, self.apiKey, self.apiSecret)
+        consumer.set_api_credentials(self.apiKey, self.apiSecret)
+        rea_headers = consumer.create_headers()
+        self.assertEqual(exp_headers, rea_headers)
diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py
new file mode 100644 (file)
index 0000000..87f63f4
--- /dev/null
@@ -0,0 +1,38 @@
+# Copyright (c) 2019, CMCC Technologies Co., Ltd.
+
+# 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.
+
+import unittest
+import mock
+
+from dmaap.identity import IdentityClient
+
+
+class CreateApiKeyTest(unittest.TestCase):
+    def setUp(self):
+        self.apiKey = "7TuwzpLJ4QfQs4O"
+        self.apiSecret = "7TuwzpLJ4QfQs4O"
+        self.host = '127.0.0.1'
+
+    def tearDown(self):
+        self.ret_url = ""
+
+    @mock.patch.object(IdentityClient, 'create_apikey')
+    def test_create_apiKey(self, mock_create_apikey):
+        mock_create_apikey.return_value = {
+            'apiKey': "7TuwzpLJ4QfQs4O",
+            'apiSecret': "7TuwzpLJ4QfQs4O"
+        }
+        resp_data = IdentityClient(self.host).create_apikey('', 'description')
+        self.assertEqual(self.apiKey, resp_data.get("apiKey"))
+        self.assertEqual(self.apiSecret, resp_data.get("apiSecret"))