# Path: microcosm_pubsub/tracing.py
# AWS_SNS = "SNS"
# AWS_SQS = "SQS"
# OPAQUE_TAG_KEY = "x-dynatrace"
# class StubOutgoingTracer:
#     def set_vendor_message_id(self, id):
#     def set_correlation_id(self, id):
# def add_trace_to_message(tracer, pubsub_message):
# def trace_outgoing_message(topic_arn: str):
# def trace_incoming_message_process(opaque: Opaque, message: SQSMessage, queue_url: str):
# 
# Path: microcosm_pubsub/message.py
# class SQSMessage:
#     """
#     SQS message wrapper.
# 
#     """
#     def __init__(self,
#                  consumer,
#                  content,
#                  media_type,
#                  message_id,
#                  receipt_handle,
#                  topic_arn=None,
#                  approximate_receive_count=None,
#                  handler=None):
#         self.consumer = consumer
#         self.content = content
#         self.media_type = media_type
#         self.message_id = message_id
#         self.receipt_handle = receipt_handle
#         self.topic_arn = topic_arn
#         self.approximate_receive_count = approximate_receive_count
#         self.handler = handler
# 
#     def ack(self):
#         """
#         Acknowledge this message was processed successfully.
# 
#         """
#         self.consumer.ack(self)
# 
#     def nack(self, visibility_timeout_seconds=None):
#         """
#         Acknowledge this message was NOT processed successfully.
# 
#         """
#         self.consumer.nack(self, visibility_timeout_seconds)
# 
#     @property
#     def opaque_data(self):
#         if not self.content:
#             return dict()
#         return self.content.get("opaque_data", {})
# 
#     @property
#     def ttl(self):
#         if TTL_KEY not in self.opaque_data:
#             return None
#         return int(self.opaque_data[TTL_KEY])
# 
#     @property
#     def uri(self):
#         if not self.content:
#             return None
#         return self.content.get("uri")
# 
# Path: microcosm_pubsub/result.py
# class MessageHandlingResultType(Enum):
#     # Messaging handling aborted due to too many attempts.
#     EXPIRED = MessageHandlingResultTypeInfo(name="EXPIRED", level=WARNING)
# 
#     # Messaging handling failed.
#     FAILED = MessageHandlingResultTypeInfo(name="FAILED", level=WARNING, exc_info=True, retry=True)
# 
#     # Message was not handled.
#     IGNORED = MessageHandlingResultTypeInfo(name="IGNORED", level=DEBUG)
# 
#     # Messaging handling was intentionally retried.
#     RETRIED = MessageHandlingResultTypeInfo(name="RETRIED", level=INFO, retry=True)
# 
#     # Message handling succeeded.
#     SUCCEEDED = MessageHandlingResultTypeInfo(name="SUCCEEDED", level=INFO)
# 
#     # Message handling was skipped.
#     # ("Upon closer inspection these are loafers")
#     SKIPPED = MessageHandlingResultTypeInfo(name="SKIPPED", level=INFO)
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def exc_info(self):
#         return self.value.exc_info
# 
#     @property
#     def level(self):
#         return self.value.level
# 
#     @property
#     def retry(self):
#         return self.value.retry
# 
# Path: microcosm_pubsub/tests/fixtures.py
# class DerivedSchema(PubSubMessageSchema):
#     """
#     A schema that is derived from `PubSubMessageSchema`
# 
#     """
#     MEDIA_TYPE = "application/vnd.microcosm.derived"
# 
#     data = fields.String(required=True)
# 
# class ExampleDaemon(ConsumerDaemon):
# 
#     @property
#     def name(self):
#         return "example"
# 
#     @property
#     def components(self):
#         return super().components + [
#             "noop_handler",
#         ]


import json
from unittest.mock import ANY, patch
from hamcrest import assert_that, equal_to
from microcosm.api import create_object_graph
from microcosm_pubsub import tracing
from microcosm_pubsub.message import SQSMessage
from microcosm_pubsub.result import MessageHandlingResultType
from microcosm_pubsub.tests.fixtures import DerivedSchema, ExampleDaemon




def test_trace_message_publish():
    def loader(metadata):
        return dict(
            sns_topic_arns=dict(
                default="topic-name",
            )
        )

    graph = create_object_graph("example", testing=True, loader=loader)
    graph.sns_producer.sns_client.publish.return_value = dict(MessageId="message-id-1234")

    with patch.object(tracing, "oneagent") as oneagent, \
            patch.object(tracing, "ChannelType"), \

# Path: microcosm_pubsub/tracing.py
# AWS_SNS = "SNS"
# AWS_SQS = "SQS"
# OPAQUE_TAG_KEY = "x-dynatrace"
# class StubOutgoingTracer:
#     def set_vendor_message_id(self, id):
#     def set_correlation_id(self, id):
# def add_trace_to_message(tracer, pubsub_message):
# def trace_outgoing_message(topic_arn: str):
# def trace_incoming_message_process(opaque: Opaque, message: SQSMessage, queue_url: str):
# 
# Path: microcosm_pubsub/message.py
# class SQSMessage:
#     """
#     SQS message wrapper.
# 
#     """
#     def __init__(self,
#                  consumer,
#                  content,
#                  media_type,
#                  message_id,
#                  receipt_handle,
#                  topic_arn=None,
#                  approximate_receive_count=None,
#                  handler=None):
#         self.consumer = consumer
#         self.content = content
#         self.media_type = media_type
#         self.message_id = message_id
#         self.receipt_handle = receipt_handle
#         self.topic_arn = topic_arn
#         self.approximate_receive_count = approximate_receive_count
#         self.handler = handler
# 
#     def ack(self):
#         """
#         Acknowledge this message was processed successfully.
# 
#         """
#         self.consumer.ack(self)
# 
#     def nack(self, visibility_timeout_seconds=None):
#         """
#         Acknowledge this message was NOT processed successfully.
# 
#         """
#         self.consumer.nack(self, visibility_timeout_seconds)
# 
#     @property
#     def opaque_data(self):
#         if not self.content:
#             return dict()
#         return self.content.get("opaque_data", {})
# 
#     @property
#     def ttl(self):
#         if TTL_KEY not in self.opaque_data:
#             return None
#         return int(self.opaque_data[TTL_KEY])
# 
#     @property
#     def uri(self):
#         if not self.content:
#             return None
#         return self.content.get("uri")
# 
# Path: microcosm_pubsub/result.py
# class MessageHandlingResultType(Enum):
#     # Messaging handling aborted due to too many attempts.
#     EXPIRED = MessageHandlingResultTypeInfo(name="EXPIRED", level=WARNING)
# 
#     # Messaging handling failed.
#     FAILED = MessageHandlingResultTypeInfo(name="FAILED", level=WARNING, exc_info=True, retry=True)
# 
#     # Message was not handled.
#     IGNORED = MessageHandlingResultTypeInfo(name="IGNORED", level=DEBUG)
# 
#     # Messaging handling was intentionally retried.
#     RETRIED = MessageHandlingResultTypeInfo(name="RETRIED", level=INFO, retry=True)
# 
#     # Message handling succeeded.
#     SUCCEEDED = MessageHandlingResultTypeInfo(name="SUCCEEDED", level=INFO)
# 
#     # Message handling was skipped.
#     # ("Upon closer inspection these are loafers")
#     SKIPPED = MessageHandlingResultTypeInfo(name="SKIPPED", level=INFO)
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def exc_info(self):
#         return self.value.exc_info
# 
#     @property
#     def level(self):
#         return self.value.level
# 
#     @property
#     def retry(self):
#         return self.value.retry
# 
# Path: microcosm_pubsub/tests/fixtures.py
# class DerivedSchema(PubSubMessageSchema):
#     """
#     A schema that is derived from `PubSubMessageSchema`
# 
#     """
#     MEDIA_TYPE = "application/vnd.microcosm.derived"
# 
#     data = fields.String(required=True)
# 
# class ExampleDaemon(ConsumerDaemon):
# 
#     @property
#     def name(self):
#         return "example"
# 
#     @property
#     def components(self):
#         return super().components + [
#             "noop_handler",
#         ]


# Path: microcosm_pubsub/tests/test_tracing.py
import json
from unittest.mock import ANY, patch
from hamcrest import assert_that, equal_to
from microcosm.api import create_object_graph
from microcosm_pubsub import tracing
from microcosm_pubsub.message import SQSMessage
from microcosm_pubsub.result import MessageHandlingResultType
from microcosm_pubsub.tests.fixtures import DerivedSchema, ExampleDaemon





def test_trace_message_publish():
    def loader(metadata):
        return dict(
            sns_topic_arns=dict(
                default="topic-name",
            )
        )

    graph = create_object_graph("example", testing=True, loader=loader)
    graph.sns_producer.sns_client.publish.return_value = dict(MessageId="message-id-1234")

    with patch.object(tracing, "oneagent") as oneagent, \
            patch.object(tracing, "ChannelType"), \

            patch.object(tracing, "Channel") as channel, \