gokulakrishnansvm commented on code in PR #7424: URL: https://github.com/apache/trafficcontrol/pull/7424#discussion_r1159909953
########## traffic_ops/testing/api_contract/v4/test_cachegroups.py: ########## @@ -0,0 +1,100 @@ +# +# 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. +# + +"""API Contract Test Case for cachegroup endpoint.""" +import logging +import pytest +import requests + +from trafficops.tosession import TOSession + +# Create and configure logger +logger = logging.getLogger() + +primitive = bool | int | float | str | None + +@pytest.mark.parametrize('request_template_data', ["cachegroup"], indirect=True) +def test_cachegroup_contract(to_session: TOSession, request_template_data: + list[dict[str, object] | list[object] | primitive], + response_template_data: dict[str, object],cachegroup_post_data: dict[str, object]) -> None: + """ + Test step to validate keys, values and data types from cachegroup endpoint + response. + :param to_session: Fixture to get Traffic Ops session. + :param request_template_data: Fixture to get request template data from a prerequisites file. + :param response_template_data: Fixture to get response template data from a prerequisites file. + :param cachegroup_post_data: Fixture to get sample cachegroup data and actual cachegroup response. + """ + # validate CDN keys from cdns get response + logger.info("Accessing /cachegroup endpoint through Traffic ops session.") + + cachegroup = request_template_data[0] + if not isinstance(cachegroup, dict): + raise TypeError("malformed cachegroup in prerequisite data; not an object") + + cachegroup_name = cachegroup.get("name") + if not isinstance(cachegroup_name, str): + raise TypeError("malformed cachegroup in prerequisite data; 'name' not a string") + + cachegroup_get_response: tuple[ + dict[str, object] | list[dict[str, object] | list[object] | primitive] | primitive, + requests.Response + ] = to_session.get_cachegroups(query_params={"name": str(cachegroup_name)}) + try: + cachegroup_data = cachegroup_get_response[0] + if not isinstance(cachegroup_data, list): + raise TypeError("malformed API response; 'response' property not an array") + + first_cachegroup = cachegroup_data[0] + if not isinstance(first_cachegroup, dict): + raise TypeError("malformed API response; first Cache group in response is not an object") + cachegroup_keys = set(first_cachegroup.keys()) + + logger.info("Cache group Keys from cachegroup endpoint response %s", cachegroup_keys) + response_template = response_template_data.get("cachegroup").get("properties") + # validate cachegroup values from prereq data in cachegroup get response. + prereq_values = [cachegroup_post_data["name"],cachegroup_post_data["shortName"], + cachegroup_post_data["fallbackToClosest"],cachegroup_post_data["typeId"]] + get_values = [first_cachegroup["name"],first_cachegroup["shortName"], + first_cachegroup["fallbackToClosest"],first_cachegroup["typeId"]] + get_types = {} + for key in first_cachegroup: + get_types[key] = type(first_cachegroup[key]).__name__ Review Comment: yes, But in my case, I'm not actually validating the instance of the object. I have hardcoded response template json file. Example: ``` "cdns": { "type": "object", "properties": { "name": { "type": "str" }, "domainName": { "type": "str" }, "dnssecEnabled": { "type": "bool" }, "id": { "type": "int" }, "lastUpdated": { "type": "str" } } } ``` Here i'm checking the "type" in the response template with the key datatype from api get response. Thats why i'm extracting just the name of the value's object from the actual response and comparing it with the "type" value in response template -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@trafficcontrol.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org