cdf41c41807a705f7c8bd1db867411797e8d838a
[dcaegen2/platform.git] / adapter / acumos / tests / test_df.py
1 # ============LICENSE_START====================================================
2 # org.onap.dcae
3 # =============================================================================
4 # Copyright (c) 2019 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 from testing_helpers import get_json_fixture, get_fixture_path
20 from aoconversion import dataformat_gen
21
22 TEST_META = get_json_fixture("models/example-model/metadata.json")
23 DRAFT_4_SCHEMA = get_json_fixture("jsdraft4schema.json")
24 DF_101 = get_json_fixture("dataformat_101.json")
25
26
27 def test_get_needed_formats():
28     assert dataformat_gen._get_needed_formats(TEST_META) == ["NumbersIn", "NumberOut"]
29
30
31 def test_generate_dcae_data_formats():
32     """
33     Test generating data formats from the protobuf
34     """
35     test_proto_path = get_fixture_path("models/example-model/model.proto")
36     assert dataformat_gen._generate_dcae_data_formats(test_proto_path, TEST_META, DF_101, DRAFT_4_SCHEMA) == [
37         {
38             "self": {"name": "NumbersIn", "version": "1.0.0"},
39             "dataformatversion": "1.0.1",
40             "jsonschema": {
41                 "title": "NumbersIn",
42                 "type": "object",
43                 "properties": {
44                     "x": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991},
45                     "y": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991},
46                 },
47                 "$schema": "http://json-schema.org/draft-04/schema#",
48                 "definitions": {},
49             },
50         },
51         {
52             "self": {"name": "NumberOut", "version": "1.0.0"},
53             "dataformatversion": "1.0.1",
54             "jsonschema": {
55                 "title": "NumberOut",
56                 "type": "object",
57                 "properties": {"result": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991}},
58                 "$schema": "http://json-schema.org/draft-04/schema#",
59                 "definitions": {},
60             },
61         },
62     ]
63
64
65 def test_generate_dcae_data_formats_listofm():
66     """
67     Test generating data formats from the protobuf
68     This one tests the case where definitions needs to be populated in one of the data formats because it's referenced in a "top level" message
69     """
70     test_meta = get_json_fixture("models/example-model-listofm/metadata.json")
71     test_proto_path = get_fixture_path("models/example-model-listofm/model.proto")
72     assert dataformat_gen._generate_dcae_data_formats(test_proto_path, test_meta, DF_101, DRAFT_4_SCHEMA) == [
73         {
74             "self": {"name": "ArgsList", "version": "1.0.0"},
75             "dataformatversion": "1.0.1",
76             "jsonschema": {
77                 "title": "ArgsList",
78                 "type": "object",
79                 "properties": {"args": {"type": "array", "items": {"$ref": "#/definitions/Args"}}},
80                 "$schema": "http://json-schema.org/draft-04/schema#",
81                 "definitions": {
82                     "Args": {
83                         "title": "Args",
84                         "type": "object",
85                         "properties": {
86                             "x": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991},
87                             "y": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991},
88                         },
89                     }
90                 },
91             },
92         },
93         {
94             "self": {"name": "SumOut", "version": "1.0.0"},
95             "dataformatversion": "1.0.1",
96             "jsonschema": {
97                 "title": "SumOut",
98                 "type": "object",
99                 "properties": {
100                     "value": {
101                         "type": "array",
102                         "items": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991},
103                     }
104                 },
105                 "$schema": "http://json-schema.org/draft-04/schema#",
106                 "definitions": {},
107             },
108         },
109     ]