# coding: utf-8

"""
    Event Detection API

    Event detection occurs when ThousandEyes identifies that error signals related to a component (proxy, network node, AS, server etc) have deviated from the baselines established by events. * To determine this, ThousandEyes takes the test results from all accounts groups within an organization, and analyzes that data. * Noisy test results (those that have too many errors in a short window) are removed until they stabilize, and the rest of the results are tagged with the components associated with that test result (for example, proxy, network, or server). * Next, any increase in failures from the test results and each component helps in determining the problem domain and which component may be at fault. * When this failure rate increases beyond a pre-defined threshold (set by the algorithm), an event is triggered and an email notification is sent to the user (if they've enabled email alerts).  With the Events API, you can perform the following tasks on the ThousandEyes platform: * **Retrieve Events**: Obtain a list of events and detailed information for each event. For more information about events, see [Event Detection](https://docs.thousandeyes.com/product-documentation/event-detection). 

    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


import json
import unittest
import thousandeyes_sdk.event_detection.models

from thousandeyes_sdk.core.exceptions import ApiException
from thousandeyes_sdk.event_detection.api.events_api import EventsApi
from .integration_test_utils import IntegrationTestBase
from .test_utils import assert_constructed_model_matches_example_json


class TestEventsApiIntegration(IntegrationTestBase):
    """EventsApi integration test stubs"""

    def setUp(self) -> None:
        super().setUp()
        self.api = EventsApi(self.api_client)


    def test_get_event_happy_path(self) -> None:
        """Integration test for get_event success path"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        response_body_json = """
                {
                  "severity" : "medium",
                  "summary" : "Significant number of issues detected with 66.29.146.15",
                  "agentType" : "cloud-enterprise-agent",
                  "affectedTests" : {
                    "total" : 5,
                    "tests" : [ {
                      "affectedTargetIds" : [ "123", "1234" ],
                      "affectedAgentIds" : [ "2954", "2953" ],
                      "_links" : {
                        "test" : {
                          "hreflang" : "hreflang",
                          "templated" : true,
                          "profile" : "profile",
                          "name" : "name",
                          "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                          "type" : "type",
                          "deprecation" : "deprecation",
                          "title" : "title"
                        }
                      },
                      "name" : "Google test",
                      "testType" : "agent-to-server",
                      "testId" : "226770"
                    }, {
                      "affectedTargetIds" : [ "123", "1234" ],
                      "affectedAgentIds" : [ "2954", "2953" ],
                      "_links" : {
                        "test" : {
                          "hreflang" : "hreflang",
                          "templated" : true,
                          "profile" : "profile",
                          "name" : "name",
                          "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                          "type" : "type",
                          "deprecation" : "deprecation",
                          "title" : "title"
                        }
                      },
                      "name" : "Google test",
                      "testType" : "agent-to-server",
                      "testId" : "226770"
                    } ],
                    "inAccountGroup" : 2
                  },
                  "endDate" : "2020-04-23T13:43:16Z",
                  "_links" : {
                    "self" : {
                      "hreflang" : "hreflang",
                      "templated" : true,
                      "profile" : "profile",
                      "name" : "name",
                      "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                      "type" : "type",
                      "deprecation" : "deprecation",
                      "title" : "title"
                    }
                  },
                  "typeName" : "Network Issue",
                  "cause" : [ "Network Loss and/or High RTT" ],
                  "affectedTargets" : {
                    "total" : 5,
                    "inAccountGroup" : 2,
                    "targets" : [ {
                      "affectedAgentIds" : [ "2954", "2953" ],
                      "ip" : "216.239.32.10",
                      "name" : "google.com",
                      "affectedTestIds" : [ "123", "1234" ],
                      "serverId" : "123"
                    }, {
                      "affectedAgentIds" : [ "2954", "2953" ],
                      "ip" : "216.239.32.10",
                      "name" : "google.com",
                      "affectedTestIds" : [ "123", "1234" ],
                      "serverId" : "123"
                    } ]
                  },
                  "type" : "target",
                  "grouping" : {
                    "target" : "google.com"
                  },
                  "affectedAgents" : {
                    "total" : 5,
                    "inAccountGroup" : 2,
                    "agents" : [ {
                      "affectedTargetIds" : [ "123", "1234" ],
                      "agentId" : "2954",
                      "_links" : {
                        "agent" : {
                          "hreflang" : "hreflang",
                          "templated" : true,
                          "profile" : "profile",
                          "name" : "name",
                          "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                          "type" : "type",
                          "deprecation" : "deprecation",
                          "title" : "title"
                        }
                      },
                      "countryCode" : "BR",
                      "name" : "São Paulo, Brazil - agent",
                      "location" : "São Paulo, Brazil",
                      "affectedTestIds" : [ "2954", "2953" ],
                      "type" : "enterprise"
                    }, {
                      "affectedTargetIds" : [ "123", "1234" ],
                      "agentId" : "2954",
                      "_links" : {
                        "agent" : {
                          "hreflang" : "hreflang",
                          "templated" : true,
                          "profile" : "profile",
                          "name" : "name",
                          "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                          "type" : "type",
                          "deprecation" : "deprecation",
                          "title" : "title"
                        }
                      },
                      "countryCode" : "BR",
                      "name" : "São Paulo, Brazil - agent",
                      "location" : "São Paulo, Brazil",
                      "affectedTestIds" : [ "2954", "2953" ],
                      "type" : "enterprise"
                    } ]
                  },
                  "id" : "e9c3bf02-a48c-4aa8-9e5f-898800d6f569",
                  "state" : "resolved",
                  "aid" : "1234",
                  "startDate" : "2020-04-23T13:43:16Z"
                }
                """
        expected_response = json.loads(response_body_json)
        response = self.api.get_event(

            id=id,

            aid=aid,

            _headers=self.te_headers("get_event"),
        )
        assert_constructed_model_matches_example_json(response, expected_response)


    def test_get_event_error_401(self) -> None:
        """Integration test for get_event error path (HTTP 401)"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        error_body_json = """
                {
                  "error_description" : "Invalid access token",
                  "error" : "invalid_token"
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(401)
        ) as context:
            self.api.get_event(

                id=id,

                aid=aid,

                _headers=self.te_headers("get_event", error_status="401"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_event_error_403(self) -> None:
        """Integration test for get_event error path (HTTP 403)"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(403)
        ) as context:
            self.api.get_event(

                id=id,

                aid=aid,

                _headers=self.te_headers("get_event", error_status="403"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_event_error_404(self) -> None:
        """Integration test for get_event error path (HTTP 404)"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(404)
        ) as context:
            self.api.get_event(

                id=id,

                aid=aid,

                _headers=self.te_headers("get_event", error_status="404"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_event_error_429(self) -> None:
        """Integration test for get_event error path (HTTP 429)"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(429)
        ) as context:
            self.api.get_event(

                id=id,

                aid=aid,

                _headers=self.te_headers("get_event", error_status="429"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_event_error_500(self) -> None:
        """Integration test for get_event error path (HTTP 500)"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(500)
        ) as context:
            self.api.get_event(

                id=id,

                aid=aid,

                _headers=self.te_headers("get_event", error_status="500"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_event_error_502(self) -> None:
        """Integration test for get_event error path (HTTP 502)"""
        id = 'e9c3bf02-a48c-4aa8-9e5f-898800d6f569'
        aid = '1234'
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(502)
        ) as context:
            self.api.get_event(

                id=id,

                aid=aid,

                _headers=self.te_headers("get_event", error_status="502"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)




    def test_get_events_happy_path(self) -> None:
        """Integration test for get_events success path"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        response_body_json = """
                {
                  "endDate" : "2022-07-18T22:00:54Z",
                  "_links" : {
                    "next" : {
                      "hreflang" : "hreflang",
                      "templated" : true,
                      "profile" : "profile",
                      "name" : "name",
                      "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                      "type" : "type",
                      "deprecation" : "deprecation",
                      "title" : "title"
                    },
                    "self" : {
                      "hreflang" : "hreflang",
                      "templated" : true,
                      "profile" : "profile",
                      "name" : "name",
                      "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                      "type" : "type",
                      "deprecation" : "deprecation",
                      "title" : "title"
                    }
                  },
                  "aid" : "1234",
                  "startDate" : "2022-07-17T22:00:54Z",
                  "events" : [ {
                    "severity" : "medium",
                    "agentType" : "cloud-enterprise-agent",
                    "affectedTests" : {
                      "total" : 5,
                      "inAccountGroup" : 2
                    },
                    "endDate" : "2020-04-23T13:43:16Z",
                    "_links" : {
                      "self" : {
                        "hreflang" : "hreflang",
                        "templated" : true,
                        "profile" : "profile",
                        "name" : "name",
                        "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                        "type" : "type",
                        "deprecation" : "deprecation",
                        "title" : "title"
                      }
                    },
                    "typeName" : "Network Issue",
                    "title" : "Affecting destinations in google.com",
                    "type" : "target",
                    "affectedTargets" : {
                      "total" : 5,
                      "inAccountGroup" : 2
                    },
                    "affectedAgents" : {
                      "total" : 5,
                      "inAccountGroup" : 2
                    },
                    "id" : "e9c3bf02-a48c-4aa8-9e5f-898800d6f569",
                    "state" : "resolved",
                    "startDate" : "2020-04-23T13:43:16Z"
                  }, {
                    "severity" : "medium",
                    "agentType" : "cloud-enterprise-agent",
                    "affectedTests" : {
                      "total" : 5,
                      "inAccountGroup" : 2
                    },
                    "endDate" : "2020-04-23T13:43:16Z",
                    "_links" : {
                      "self" : {
                        "hreflang" : "hreflang",
                        "templated" : true,
                        "profile" : "profile",
                        "name" : "name",
                        "href" : "https://api.thousandeyes.com/v7/link/to/resource/id",
                        "type" : "type",
                        "deprecation" : "deprecation",
                        "title" : "title"
                      }
                    },
                    "typeName" : "Network Issue",
                    "title" : "Affecting destinations in google.com",
                    "type" : "target",
                    "affectedTargets" : {
                      "total" : 5,
                      "inAccountGroup" : 2
                    },
                    "affectedAgents" : {
                      "total" : 5,
                      "inAccountGroup" : 2
                    },
                    "id" : "e9c3bf02-a48c-4aa8-9e5f-898800d6f569",
                    "state" : "resolved",
                    "startDate" : "2020-04-23T13:43:16Z"
                  } ]
                }
                """
        expected_response = json.loads(response_body_json)
        response = self.api.get_events(

            aid=aid,

            window=window,

            start_date=start_date,

            end_date=end_date,

            max=max,

            cursor=cursor,

            ongoing=ongoing,

            _headers=self.te_headers("get_events"),
        )
        assert_constructed_model_matches_example_json(response, expected_response)


    def test_get_events_error_400(self) -> None:
        """Integration test for get_events error path (HTTP 400)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "errors" : [ {
                    "code" : "code",
                    "field" : "field",
                    "message" : "message"
                  }, {
                    "code" : "code",
                    "field" : "field",
                    "message" : "message"
                  } ],
                  "status" : 0
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(400)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="400"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_events_error_401(self) -> None:
        """Integration test for get_events error path (HTTP 401)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "error_description" : "Invalid access token",
                  "error" : "invalid_token"
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(401)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="401"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_events_error_403(self) -> None:
        """Integration test for get_events error path (HTTP 403)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(403)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="403"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_events_error_404(self) -> None:
        """Integration test for get_events error path (HTTP 404)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(404)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="404"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_events_error_429(self) -> None:
        """Integration test for get_events error path (HTTP 429)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(429)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="429"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_events_error_500(self) -> None:
        """Integration test for get_events error path (HTTP 500)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(500)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="500"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)


    def test_get_events_error_502(self) -> None:
        """Integration test for get_events error path (HTTP 502)"""
        aid = '1234'
        window = '12h'
        start_date = '2022-07-17T22:00:54Z'
        end_date = '2022-07-18T22:00:54Z'
        max = 5
        cursor = 'cursor_example'
        ongoing = true
        error_body_json = """
                {
                  "instance" : "instance",
                  "detail" : "detail",
                  "type" : "type",
                  "title" : "title",
                  "status" : 6
                }
                """
        expected_error = json.loads(error_body_json)
        with self.assertRaises(
            ApiException.exception_class_for_http_status(502)
        ) as context:
            self.api.get_events(

                aid=aid,

                window=window,

                start_date=start_date,

                end_date=end_date,

                max=max,

                cursor=cursor,

                ongoing=ongoing,

                _headers=self.te_headers("get_events", error_status="502"),
            )
        assert_constructed_model_matches_example_json(context.exception.data, expected_error)






if __name__ == '__main__':
    unittest.main()
