|
| 1 | +package com.baeldung.twilio.whatsapp; |
| 2 | + |
| 3 | +import static org.mockserver.model.HttpRequest.request; |
| 4 | +import static org.mockserver.model.HttpResponse.response; |
| 5 | +import static org.mockserver.model.Parameter.param; |
| 6 | + |
| 7 | +import java.net.InetSocketAddress; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockserver.client.MockServerClient; |
| 12 | +import org.mockserver.model.ParameterBody; |
| 13 | +import org.mockserver.springtest.MockServerTest; |
| 14 | +import org.mockserver.verify.VerificationTimes; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 17 | +import org.springframework.boot.test.context.SpringBootTest; |
| 18 | +import org.springframework.test.context.ActiveProfiles; |
| 19 | + |
| 20 | +import com.twilio.Twilio; |
| 21 | + |
| 22 | +import net.bytebuddy.utility.RandomString; |
| 23 | + |
| 24 | +@SpringBootTest |
| 25 | +@MockServerTest |
| 26 | +@ActiveProfiles("integration-test") |
| 27 | +@EnableConfigurationProperties(TwilioConfigurationProperties.class) |
| 28 | +class WhatsappMessageDispatcherIntegrationTest { |
| 29 | + |
| 30 | + private MockServerClient mockServerClient; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private WhatsappMessageDispatcher whatsappMessageDispatcher; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private TwilioConfigurationProperties twilioConfigurationProperties; |
| 37 | + |
| 38 | + private String twilioApiPath; |
| 39 | + |
| 40 | + private static final String EMPTY_JSON = "{}"; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + String accountSid = twilioConfigurationProperties.getAccountSid(); |
| 45 | + String authToken = twilioConfigurationProperties.getAuthToken(); |
| 46 | + |
| 47 | + InetSocketAddress remoteAddress = mockServerClient.remoteAddress(); |
| 48 | + String host = remoteAddress.getHostName(); |
| 49 | + int port = remoteAddress.getPort(); |
| 50 | + |
| 51 | + TwilioProxyClient twilioProxyClient = new TwilioProxyClient(accountSid, authToken, host, port); |
| 52 | + Twilio.setRestClient(twilioProxyClient.getHttpClient()); |
| 53 | + |
| 54 | + twilioApiPath = String.format("/2010-04-01/Accounts/%s/Messages.json", accountSid); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void whenNewArticleNotificationDispatched_thenTwilioApiCalledWithCorrectParameters() { |
| 59 | + // Set up test data |
| 60 | + String contentSid = twilioConfigurationProperties.getNewArticleNotification().getContentSid(); |
| 61 | + String messagingSid = twilioConfigurationProperties.getMessagingSid(); |
| 62 | + String contactNumber = "+911001001000"; |
| 63 | + |
| 64 | + // Configure mock server expectations |
| 65 | + mockServerClient |
| 66 | + .when(request() |
| 67 | + .withMethod("POST") |
| 68 | + .withPath(twilioApiPath) |
| 69 | + .withBody(new ParameterBody( |
| 70 | + param("To", String.format("whatsapp:%s", contactNumber)), |
| 71 | + param("ContentSid", contentSid), |
| 72 | + param("MessagingServiceSid", messagingSid) |
| 73 | + )) |
| 74 | + ) |
| 75 | + .respond(response() |
| 76 | + .withStatusCode(200) |
| 77 | + .withBody(EMPTY_JSON)); |
| 78 | + |
| 79 | + // Invoke method under test |
| 80 | + whatsappMessageDispatcher.dispatchNewArticleNotification(contactNumber, RandomString.make()); |
| 81 | + |
| 82 | + // Verify the expected request was made |
| 83 | + mockServerClient.verify(request() |
| 84 | + .withMethod("POST") |
| 85 | + .withPath(twilioApiPath) |
| 86 | + .withBody(new ParameterBody( |
| 87 | + param("To", String.format("whatsapp:%s", contactNumber)), |
| 88 | + param("ContentSid", contentSid), |
| 89 | + param("MessagingServiceSid", messagingSid) |
| 90 | + )), |
| 91 | + VerificationTimes.once() |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void whenReplyMessageDispatched_thenTwilioApiCalledWithCorrectParameters() { |
| 97 | + // Set up test data |
| 98 | + String username = RandomString.make(); |
| 99 | + String messageBody = String.format("Hey %s, our team will get back to you shortly.", username); |
| 100 | + String messagingSid = twilioConfigurationProperties.getMessagingSid(); |
| 101 | + String contactNumber = "+911001001000"; |
| 102 | + |
| 103 | + // Configure mock server expectations |
| 104 | + mockServerClient |
| 105 | + .when(request() |
| 106 | + .withMethod("POST") |
| 107 | + .withPath(twilioApiPath) |
| 108 | + .withBody(new ParameterBody( |
| 109 | + param("To", String.format("whatsapp:%s", contactNumber)), |
| 110 | + param("MessagingServiceSid", messagingSid), |
| 111 | + param("Body", messageBody) |
| 112 | + )) |
| 113 | + ) |
| 114 | + .respond(response() |
| 115 | + .withStatusCode(200) |
| 116 | + .withBody(EMPTY_JSON)); |
| 117 | + |
| 118 | + // Invoke method under test |
| 119 | + whatsappMessageDispatcher.dispatchReplyMessage(contactNumber, username); |
| 120 | + |
| 121 | + // Verify the expected request was made |
| 122 | + mockServerClient.verify(request() |
| 123 | + .withMethod("POST") |
| 124 | + .withPath(twilioApiPath) |
| 125 | + .withBody(new ParameterBody( |
| 126 | + param("To", String.format("whatsapp:%s", contactNumber)), |
| 127 | + param("MessagingServiceSid", messagingSid), |
| 128 | + param("Body", messageBody) |
| 129 | + )), |
| 130 | + VerificationTimes.once() |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments