b8402f6db3e2ec56a383d043b0906582e547f16b
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / commands / tests / test_data_format_cmd.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 '''
23 Tests data_format CLI commands
24 '''
25 import os
26 import json
27
28 import pytest
29 from click.testing import CliRunner
30
31 from dcae_cli.cli import cli
32 from dcae_cli.catalog import MockCatalog
33
34
35 TEST_DIR = os.path.dirname(__file__)
36
37
38 def _get_spec(path):
39     with open(path) as file:
40         return json.load(file)
41
42
43 def test_basic():
44
45     obj = {'catalog': MockCatalog(purge_existing=True, db_name='dcae_cli.test.db', enforce_image=False),
46            'config': {'user': 'test-user'}}
47
48     runner = CliRunner()
49     spec_file = os.path.join(TEST_DIR, 'mocked_components', 'model', 'int-class.format.json')
50     cmd = "data_format add {:}".format(spec_file).split()
51
52     # succeed the first time
53     result = runner.invoke(cli, cmd, obj=obj)
54
55     assert result.exit_code == 0
56
57     # adding a duplicate is an error
58     result = runner.invoke(cli, cmd, obj=obj)
59     assert result.exit_code == 1
60     assert 'exists' in result.output.lower()
61
62     # allow updates
63     cmd = "data_format add --update {:}".format(spec_file).split()
64     result = runner.invoke(cli, cmd, obj=obj)
65     assert result.exit_code == 0
66
67
68     # light test of list format command
69     cmd = 'data_format list'.split()
70     df_spec = _get_spec(spec_file)
71     df_name = df_spec['self']['name']
72     assert df_name in runner.invoke(cli, cmd, obj=obj).output
73
74
75     # light test of component info
76     cmd = "data_format show {:}".format(df_name).split()
77     spec_str = runner.invoke(cli, cmd, obj=obj).output
78     assert df_spec == json.loads(spec_str)
79
80     # test of generate
81     bad_dir = os.path.join(TEST_DIR, 'mocked_components', 'model', 'baddir')
82     cmd = "data_format generate --keywords \"name:1.0.2\" {:}".format(bad_dir).split()
83     err_str = runner.invoke(cli, cmd, obj=obj).output
84     assert "does not exist" in err_str 
85
86     empty_dir = os.path.join(TEST_DIR, 'mocked_components', 'model', 'emptydir')
87     try:
88       os.stat(empty_dir)
89     except:
90       os.mkdir(empty_dir)
91     cmd = "data_format generate --keywords \"name:1.0.2\" {:}".format(empty_dir).split()
92     err_str = runner.invoke(cli, cmd, obj=obj).output
93     assert "No JSON files found" in err_str 
94
95     bad_json = os.path.join(TEST_DIR, 'mocked_components', 'model', 'badjson')
96     cmd = "data_format generate --keywords \"name:1.0.2\" {:}".format(bad_json).split()
97     err_str = runner.invoke(cli, cmd, obj=obj).output
98     assert "Bad JSON file" in err_str 
99
100     generate_dir = os.path.join(TEST_DIR, 'mocked_components', 'model', 'generatedir')
101     cmd = "data_format generate --keywords name:1.0.2 {:} ".format(generate_dir).split()
102     out_str = runner.invoke(cli, cmd, obj=obj).output
103     assert '{\n    "dataformatversion": "1.0.0", \n    "jsonschema": {\n        "$schema": "http://json-schema.org/draft-04/schema#", \n        "description": "", \n        "properties": {\n            "foobar": {\n                "description": "", \n                "maxLength": 0, \n                "minLength": 0, \n                "pattern": "", \n                "type": "string"\n            }, \n            "foobar2": {\n                "description": "", \n                "maxLength": 0, \n                "minLength": 0, \n                "pattern": "", \n                "type": "string"\n            }\n        }, \n        "type": "object"\n    }, \n    "self": {\n        "description": "", \n        "name": "name", \n        "version": "1.0.2"\n    }\n}\n' ==  out_str
104
105     generate_dir = os.path.join(TEST_DIR, 'mocked_components', 'model', 'generatedir')
106     cmd = "data_format generate name:1.0.2 {:} ".format(generate_dir).split()
107     out_str = runner.invoke(cli, cmd, obj=obj).output
108     assert '{\n    "dataformatversion": "1.0.0", \n    "jsonschema": {\n        "$schema": "http://json-schema.org/draft-04/schema#", \n        "description": "", \n        "properties": {\n            "foobar": {\n                "description": "", \n                "type": "string"\n            }, \n            "foobar2": {\n                "description": "", \n                "type": "string"\n            }\n        }, \n        "type": "object"\n    }, \n    "self": {\n        "description": "", \n        "name": "name", \n        "version": "1.0.2"\n    }\n}\n' == out_str
109
110     generate_dir = os.path.join(TEST_DIR, 'mocked_components', 'model', 'generatedir', 'ex1.json')
111     cmd = "data_format generate name:1.0.2 {:} ".format(generate_dir).split()
112     out_str = runner.invoke(cli, cmd, obj=obj).output
113     assert '{\n    "dataformatversion": "1.0.0", \n    "jsonschema": {\n        "$schema": "http://json-schema.org/draft-04/schema#", \n        "additionalproperties": true, \n        "description": "", \n        "properties": {\n            "foobar": {\n                "description": "", \n                "type": "string"\n            }\n        }, \n        "required": [\n            "foobar"\n        ], \n        "type": "object"\n    }, \n    "self": {\n        "description": "", \n        "name": "name", \n        "version": "1.0.2"\n    }\n}\n' == out_str
114
115
116 if __name__ == '__main__':
117     '''Test area'''
118     pytest.main([__file__, ])