|
| 1 | +package com.mailjet.client; |
| 2 | + |
| 3 | +import com.mailjet.client.errors.MailjetClientRequestException; |
| 4 | +import com.mailjet.client.errors.MailjetException; |
| 5 | +import com.mailjet.client.resource.sms.SmsSend; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Ignore; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class SendSmsIT { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void sendSms_UnsupportedCountry_ThrowsMailjetException() { |
| 14 | + // arrange |
| 15 | + MailjetClient mailjetClient = new MailjetClient(ClientOptions |
| 16 | + .builder() |
| 17 | + .apiAccessToken(System.getenv("MJ_APITOKEN")) |
| 18 | + .build()); |
| 19 | + |
| 20 | + String ukrainePhoneNumber = "+380507363100"; |
| 21 | + |
| 22 | + MailjetRequest mailjetRequest = new MailjetRequest(SmsSend.resource) |
| 23 | + .property(SmsSend.FROM, "MJPilot") |
| 24 | + .property(SmsSend.TO, ukrainePhoneNumber) |
| 25 | + .property(SmsSend.TEXT, "Have a nice SMS flight with Mailjet!"); |
| 26 | + |
| 27 | + // act |
| 28 | + MailjetClientRequestException requestException = Assert.assertThrows(MailjetClientRequestException.class, () -> mailjetClient.post(mailjetRequest)); |
| 29 | + |
| 30 | + // assert |
| 31 | + Assert.assertEquals(400, requestException.getStatusCode()); |
| 32 | + Assert.assertTrue(requestException.getMessage().endsWith("\"ErrorCode\":\"sms-0002\",\"StatusCode\":400,\"ErrorMessage\":\"Unsupported country code.\",\"ErrorRelatedTo\":[\"To\"]}")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @Ignore("This test will send the real sms") |
| 37 | + public void sendSms_SupportedCountry_ReturnsSuccessResponse() throws MailjetException { |
| 38 | + // arrange |
| 39 | + MailjetClient mailjetClient = new MailjetClient(ClientOptions |
| 40 | + .builder() |
| 41 | + .apiAccessToken(System.getenv("MJ_APITOKEN")) |
| 42 | + .build()); |
| 43 | + |
| 44 | + // to verify other countries, free services like https://receive-smss.com/ can be used |
| 45 | + String germanyPhoneNumber = "+4915207831169"; |
| 46 | + |
| 47 | + MailjetRequest mailjetRequest = new MailjetRequest(SmsSend.resource) |
| 48 | + .property(SmsSend.FROM, "MJPilot") |
| 49 | + .property(SmsSend.TO, germanyPhoneNumber) |
| 50 | + .property(SmsSend.TEXT, "Have a nice SMS flight with Mailjet!"); |
| 51 | + |
| 52 | + // act |
| 53 | + MailjetResponse response = mailjetClient.post(mailjetRequest); |
| 54 | + |
| 55 | + // assert |
| 56 | + Assert.assertEquals(200, response.getStatus()); |
| 57 | + Assert.assertEquals("Message is being sent", response.getData().getJSONObject(0).getJSONObject("Status").getString("Description")); |
| 58 | + Assert.assertEquals("Have a nice SMS flight with Mailjet!", response.getString("Text")); |
| 59 | + } |
| 60 | +} |
0 commit comments