Add viewbuilder for document creation

This commit adds a ViewBuilder for document creation (the only
API endpoint for documents with an associated view). The view
returns the following body format:

  ---
  revision_id: <created_revision_id>
  documents:
    - <created_doc_id_1>
    - <created_doc_id_2>
    ...

This commit also includes unit tests.

Change-Id: Ic23124bebc861c55881ab1e0096d0418bdbe9bd5
This commit is contained in:
Felipe Monteiro
2017-08-11 15:15:22 +01:00
parent 702c6b783b
commit d40e8810ee
5 changed files with 88 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ from oslo_db import exception as db_exc
from oslo_log import log as logging
from deckhand.control import base as api_base
from deckhand.control.views import document as document_view
from deckhand.db.sqlalchemy import api as db_api
from deckhand.engine import document_validation
from deckhand import errors as deckhand_errors
@@ -64,4 +65,5 @@ class DocumentsResource(api_base.BaseResource):
resp.status = falcon.HTTP_201
resp.append_header('Content-Type', 'application/x-yaml')
resp.body = self.to_yaml_body(created_documents)
resp_body = document_view.ViewBuilder().list(created_documents)
resp.body = self.to_yaml_body(resp_body)

View File

@@ -0,0 +1,34 @@
# Copyright 2017 AT&T Intellectual Property. All other 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.
from deckhand.control import common
class ViewBuilder(common.ViewBuilder):
"""Model document API responses as a python dictionary."""
_collection_name = 'documents'
def list(self, documents):
resp_body = {
'documents': []
}
# TODO(fmontei): Convert these IDs to URLs instead once URL conversion
# is implemented.
for document in documents:
resp_body.setdefault('revision_id', document['revision_id'])
resp_body['documents'].append(document['id'])
return resp_body

View File

@@ -25,6 +25,7 @@ REVISION_EXPECTED_FIELDS = BASE_EXPECTED_FIELDS + (
"id", "documents", "validation_policies")
# TODO(fmontei): Move this into a separate module called `fixtures`.
class DocumentFixture(object):
@staticmethod

View File

@@ -0,0 +1,50 @@
# Copyright 2017 AT&T Intellectual Property. All other 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.
from oslo_utils import uuidutils
from deckhand.control.views import document
from deckhand import factories
from deckhand.tests.unit.db import base
class TestRevisionViews(base.TestDbBase):
def setUp(self):
super(TestRevisionViews, self).setUp()
self.view_builder = document.ViewBuilder()
self.factory = factories.ValidationPolicyFactory()
def _test_document_creation_view(self, count):
# Test document creation view with the number of documents being
# created specified by `count`.
payload = [base.DocumentFixture.get_minimal_fixture()
for _ in range(count)]
created_documents = self._create_documents(payload)
document_view = self.view_builder.list(created_documents)
expected_attrs = ('revision_id', 'documents')
for attr in expected_attrs:
self.assertIn(attr, document_view)
self.assertTrue(uuidutils.is_uuid_like(document_view['revision_id']))
self.assertEqual(count, len(document_view['documents']))
for doc_id in document_view['documents']:
self.assertTrue(uuidutils.is_uuid_like(doc_id))
def test_create_single_document(self):
self._test_document_creation_view(1)
def test_create_many_documents(self):
self._test_document_creation_view(4)