content: package co.airy.core.sources.twilio;
  
  import co.airy.avro.communication.DeliveryState;
  import co.airy.avro.communication.Message;
  import co.airy.avro.communication.Metadata;
  import co.airy.core.sources.twilio.services.Api;
  import co.airy.core.sources.twilio.services.ApiException;
  import co.airy.kafka.schema.Topic;
  import co.airy.kafka.schema.application.ApplicationCommunicationChannels;
  import co.airy.kafka.schema.application.ApplicationCommunicationMessages;
  import co.airy.kafka.schema.application.ApplicationCommunicationMetadata;
  import co.airy.kafka.test.KafkaTestHelper;
  import co.airy.kafka.test.junit.SharedKafkaTestResource;
  import co.airy.model.metadata.MetadataKeys;
  import co.airy.spring.core.AirySpringBootApplication;
  import co.airy.spring.test.WebTestHelper;
  import com.fasterxml.jackson.databind.JsonNode;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.apache.kafka.clients.producer.ProducerRecord;
  import org.junit.jupiter.api.AfterAll;
  import org.junit.jupiter.api.BeforeAll;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.junit.jupiter.api.extension.RegisterExtension;
  import org.mockito.ArgumentCaptor;
  import org.mockito.InjectMocks;
  import org.mockito.MockitoAnnotations;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.boot.test.mock.mockito.MockBean;
  import org.springframework.test.context.TestPropertySource;
  import org.springframework.test.context.junit.jupiter.SpringExtension;
  
  import java.time.Instant;
  import java.util.List;
  import java.util.UUID;
  
  import static co.airy.test.Timing.retryOnException;
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.core.IsEqual.equalTo;
  import static org.junit.jupiter.api.Assertions.assertEquals;
  import static org.mockito.Mockito.doThrow;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  @SpringBootTest(classes = AirySpringBootApplication.class)
  @TestPropertySource(value = "classpath:test.properties")
  @AutoConfigureMockMvc
  @ExtendWith(SpringExtension.class)
  class SendMessageTest {
  
      @RegisterExtension
      public static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource();
      private static KafkaTestHelper kafkaTestHelper;
  
      private static final Topic applicationCommunicationChannels = new ApplicationCommunicationChannels();
      private static final Topic applicationCommunicationMessages = new ApplicationCommunicationMessages();
      private static final Topic applicationCommunicationMetadata = new ApplicationCommunicationMetadata();
  
      @MockBean
      Api api;
  
      @Autowired
      @InjectMocks
      private Connector worker;
  
      @Autowired
      private WebTestHelper webTestHelper;
  
      @Autowired
      private Stores stores;
  
      @BeforeAll
      static void beforeAll() throws Exception {
          kafkaTestHelper = new KafkaTestHelper(sharedKafkaTestResource,
                  applicationCommunicationChannels,
                  applicationCommunicationMessages,
                  applicationCommunicationMetadata
          );
  
          kafkaTestHelper.beforeAll();
      }
  
      @AfterAll
      static void afterAll() throws Exception {
          kafkaTestHelper.afterAll();
      }
  
      @BeforeEach
      void beforeEach() throws InterruptedException {
          MockitoAnnotations.openMocks(this);
          webTestHelper.waitUntilHealthy();
      }
  
      @Test
      void canSendMessages() throws Exception {
          final String conversationId = UUID.randomUUID().toString();
          final String messageId = UUID.randomUUID().toString();
          final String sourceConversationId = "+491234567";
          final String sourceChannelId = "+497654321";
          final String payload = "{\"Body\":\"Hello World\"}";
          final String errorMessage = "message delivery failed";
  
          ArgumentCaptor<String> payloadCaptor = ArgumentCaptor.forClass(String.class);
          ArgumentCaptor<String> fromCaptor = ArgumentCaptor.forClass(String.class);
          ArgumentCaptor<String> toCaptor = ArgumentCaptor.forClass(String.class);
  
          doThrow(new ApiException(errorMessage)).doNothing().when(api).sendMessage(fromCaptor.capture(), toCaptor.capture(), payloadCaptor.capture());
  
          // Test that phone number input gets cleaned up
          final String channelPayload = "{\"phone_number\":\"+49 765 4321 \",\"name\":\"Blips and Chitz\"}";
          final String response = webTestHelper.post("/channels.twilio.sms.connect", channelPayload)
                  .andExpect(status().isOk())
                  .andReturn().getResponse().getContentAsString();
          final JsonNode jsonNode = new ObjectMapper().readTree(response);
          final String channelId = jsonNode.get("id").textValue();
  
          kafkaTestHelper.produceRecords(List.of(
                  new ProducerRecord<>(applicationCommunicationMessages.name(), "other-message-id",
                          Message.newBuilder()
                                  .setId("other-message-id")
                                  .setSource("twilio.sms")
                                  .setSentAt(Instant.now().toEpochMilli())
                                  .setSenderId(sourceConversationId)
                                  .setIsFromContact(true)
                                  .setDeliveryState(DeliveryState.DELIVERED)
                                  .setConversationId(conversationId)
                                  .setChannelId(channelId)
                                  .setContent(payload)
                                  .build())
          ));
  
          kafkaTestHelper.produceRecords(List.of(
                  // This message should fail
                  new ProducerRecord<>(applicationCommunicationMessages.name(), messageId, Message.newBuilder()
                          .setId(messageId)
                          .setSentAt(Instant.now().toEpochMilli())
                          .setSenderId("user-id")
                          .setIsFromContact(false)
                          .setDeliveryState(DeliveryState.PENDING)
                          .setConversationId(conversationId)
                          .setChannelId(channelId)
                          .setSource("twilio.sms")
                          .setContent(payload)
                          .build()),
                  new ProducerRecord<>(applicationCommunicationMessages.name(), messageId,
                          Message.newBuilder()
                                  .setId(messageId)
                                  .setSentAt(Instant.now().toEpochMilli())
                                  .setSenderId("user-id")
                                  .setIsFromContact(false)
                                  .setDeliveryState(DeliveryState.PENDING)
                                  .setConversationId(conversationId)
                                  .setChannelId(channelId)
                                  .setSource("twilio.sms")
                                  .setContent(payload)
                                  .build())
          ));
  
          retryOnException(() -> {
              assertEquals(payload, payloadCaptor.getValue());
              assertEquals(sourceConversationId, toCaptor.getValue());
              assertEquals(sourceChannelId, fromCaptor.getValue());
          }, "Twilio API was not called");
  
          final List<Metadata> metadataList = kafkaTestHelper.consumeValues(2, applicationCommunicationMetadata.name());
  
          assertThat(metadataList.size(), equalTo(2));
          assertThat(metadataList.stream().anyMatch((metadata) ->
                  metadata.getKey().equals(MetadataKeys.MessageKeys.ERROR)
                          && metadata.getValue().equals(errorMessage)), equalTo(true));
      }
  }
  