Refactor and add a test.
Change-Id: Ic32fe45fd70a1c74e165b890584034df3e723606
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
--- /dev/null
+# Copyright 2018 ZTE Corporation.
+#
+# 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 os
+
+from catalog.pub.config.config import CATALOG_ROOT_PATH
+from catalog.pub.utils import fileutil
+
+
+def save(remote_file, descriptor_id):
+ local_file_name = remote_file.name
+ local_file_dir = os.path.join(CATALOG_ROOT_PATH, descriptor_id)
+ local_file_name = os.path.join(local_file_dir, local_file_name)
+ if not os.path.exists(local_file_dir):
+ fileutil.make_dirs(local_file_dir)
+ with open(local_file_name, 'wb') as local_file:
+ for chunk in remote_file.chunks(chunk_size=1024 * 8):
+ local_file.write(chunk)
+ return local_file_name
from catalog.pub.utils import fileutil, toscaparser
from catalog.pub.utils.values import ignore_case_get
from catalog.packages.const import PKG_STATUS
+from catalog.packages.biz.common import save
logger = logging.getLogger(__name__)
if not ns_pkgs.exists():
logger.info('NSD(%s) does not exist.' % nsd_info_id)
raise CatalogException('NSD(%s) does not exist.' % nsd_info_id)
-
ns_pkgs.update(onboardingState=PKG_STATUS.UPLOADING)
- local_file_name = remote_file.name
- local_file_dir = os.path.join(CATALOG_ROOT_PATH, nsd_info_id)
- local_file_name = os.path.join(local_file_dir, local_file_name)
- if not os.path.exists(local_file_dir):
- fileutil.make_dirs(local_file_dir)
- with open(local_file_name, 'wb') as local_file:
- for chunk in remote_file.chunks(chunk_size=1024 * 8):
- local_file.write(chunk)
+
+ local_file_name = save(remote_file, nsd_info_id)
logger.info('NSD(%s) content has been uploaded.' % nsd_info_id)
return local_file_name
from catalog.pub.utils import fileutil, toscaparser
from catalog.pub.utils.values import ignore_case_get
from catalog.packages.const import PKG_STATUS
+from catalog.packages.biz.common import save
logger = logging.getLogger(__name__)
if not pnf_pkgs.exists():
logger.info('PNFD(%s) does not exist.' % pnfd_info_id)
raise CatalogException('PNFD (%s) does not exist.' % pnfd_info_id)
-
pnf_pkgs.update(onboardingState=PKG_STATUS.UPLOADING)
- local_file_name = remote_file.name
- local_file_dir = os.path.join(CATALOG_ROOT_PATH, pnfd_info_id)
- local_file_name = os.path.join(local_file_dir, local_file_name)
- if not os.path.exists(local_file_dir):
- fileutil.make_dirs(local_file_dir)
- with open(local_file_name, 'wb') as local_file:
- for chunk in remote_file.chunks(chunk_size=1024 * 8):
- local_file.write(chunk)
+
+ local_file_name = save(remote_file, pnfd_info_id)
logger.info('PNFD(%s) content has been uploaded.' % pnfd_info_id)
return local_file_name
logger.info('Start to process PNFD(%s)...' % pnfd_info_id)
pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id)
pnf_pkgs.update(onboardingState=PKG_STATUS.PROCESSING)
-
+ PnfPackageModel
pnfd_json = toscaparser.parse_pnfd(local_file_name)
pnfd = json.JSONDecoder().decode(pnfd_json)
import copy
import json
-import os
import mock
+import os
from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient
-from catalog.pub.database.models import PnfPackageModel, NSPackageModel
-from catalog.pub.utils import toscaparser
+
+from catalog.packages.biz.pnf_descriptor import PnfPackage
from catalog.packages.const import PKG_STATUS
from catalog.packages.tests.const import pnfd_data
-from catalog.packages.biz.pnf_descriptor import PnfPackage
+from catalog.pub.database.models import PnfPackageModel, NSPackageModel
+from catalog.pub.utils import toscaparser
class TestPnfDescriptor(TestCase):
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(None, resp.data)
+ def test_delete_single_pnfd_when_not_exist(self):
+ resp = self.client.delete("/api/nsd/v1/pnf_descriptors/22", format='json')
+ self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
+ self.assertEqual(None, resp.data)
+
@mock.patch.object(toscaparser, "parse_pnfd")
def test_pnfd_content_upload_normal(self, mock_parse_pnfd):
user_defined_data_json = json.JSONEncoder().encode(self.user_defined_data)