Add dcae-cli and component-json-schemas projects
[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
81 if __name__ == '__main__':
82     '''Test area'''
83     pytest.main([__file__, ])