Skip to content
50 changes: 26 additions & 24 deletions examples/BearerTokenAuthentication.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
```java
class BearerTokenAuthenticationExamples {
public static void main {

private static final String GRANT_TYPE = "grant_type_to_be_used";
private static final String CLIENT_SID =
public static void main {
private static final String GRANT_TYPE = "grant_type_to_be_used";
private static final String CLIENT_ID =
"client_id_of_the_organization";
private static final String CLIENT_SECRET = "client_secret_of_organization";
private static final String ORGANISATION_ID = "id_of_the_organization";

//Getting access token - Method #1
TwilioOrgsTokenAuth.init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);

//Getting access token - Method #2
//To provide custom token manager implementation
//Need not call init method if customer token manager is passed
//TwilioOrgsTokenAuth.setTokenManager(new CustomTokenManagerImpl(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET));

fetchAccountDetails();
}

private static void fetchAccountDetails() {
ResourceSet<Account> accountSet = Account.reader(ORGANISATION_ID).read();
String accountSid = accountSet.iterator().next().getAccountSid();
System.out.println(accountSid);
}
}
private static final String CLIENT_SECRET = "client_secret_of_organization";
private static final String ORGANISATION_ID = "id_of_the_organization";

//Getting access token - Method #1
TwilioOrgsTokenAuth.init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);

//Getting access token - Method #2
//To provide custom token manager implementation
//Need not call init method if customer token manager is passed
//TwilioOrgsTokenAuth.setTokenManager(new CustomTokenManagerImpl(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET));

fetchAccountDetails();
}

private static void fetchAccountDetails() {
ResourceSet<Account> accountSet = Account.reader(ORGANISATION_ID).read();
String accountSid = accountSet.iterator().next().getAccountSid();
System.out.println(accountSid);
}
}
```
4 changes: 3 additions & 1 deletion examples/Content.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
```java
public class ContentExamples {
public static void main {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Expand All @@ -22,4 +23,5 @@ public class ContentExamples {
Content newContent = Content.creator(createRequest).create();
}

}
}
```
2 changes: 1 addition & 1 deletion examples/FetchMessageUsingOAuth.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
```
```java
import com.twilio.Twilio;
import com.twilio.credential.ClientCredentialProvider;
import com.twilio.rest.api.v2010.account.Message;
Expand Down
4 changes: 3 additions & 1 deletion examples/PreviewMessaging.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
```java
class PreviewMessagingExamples {
public static void main {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Expand Down Expand Up @@ -25,4 +26,5 @@ class PreviewMessagingExamples {
Message message = Message.creator(createMessagesRequest).create();
}

}
}
```
18 changes: 15 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,21 @@
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/twilio/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class Assert {

Expand Down
27 changes: 13 additions & 14 deletions src/test/java/com/twilio/ClusterTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.twilio;

import static org.hamcrest.junit.MatcherAssume.assumeThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.twilio.base.Page;
import com.twilio.base.bearertoken.ResourceSet;
import com.twilio.credential.ClientCredentialProvider;
import com.twilio.http.CustomHttpClient;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.IncomingPhoneNumber;
Expand All @@ -14,18 +18,13 @@
import com.twilio.rest.events.v1.Subscription;
import com.twilio.rest.previewiam.organizations.Account;
import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ClusterTest {
String fromNumber;
Expand All @@ -38,13 +37,13 @@ public class ClusterTest {
String clientSecret;
String messageSid;
TwilioRestClient customRestClient;

String accountSid;

@Before
@BeforeEach
public void setUp() {
// only run when ClusterTest property is passed (mvn test -Dtest="ClusterTest"), skip test run on mvn test
Assume.assumeThat(System.getProperty("Test"), CoreMatchers.is("ClusterTest"));
assumeThat(System.getProperty("Test"), CoreMatchers.is("ClusterTest"));
fromNumber = System.getenv("TWILIO_FROM_NUMBER");
toNumber = System.getenv("TWILIO_TO_NUMBER");
String apiKey = System.getenv("TWILIO_API_KEY");
Expand All @@ -57,11 +56,11 @@ public void setUp() {
orgsClientSecret = System.getenv("TWILIO_ORGS_CLIENT_SECRET");
organisationSid = System.getenv("TWILIO_ORG_SID");
TwilioOrgsTokenAuth.init(grantType, orgsClientId, orgsClientSecret);

clientId = System.getenv("TWILIO_CLIENT_ID");
clientSecret = System.getenv("TWILIO_CLIENT_SECRET");
messageSid = System.getenv("TWILIO_MESSAGE_SID");

// CustomHttpClient
customRestClient = new TwilioRestClient.Builder(apiKey, secret).accountSid(accountSid).httpClient(new CustomHttpClient()).build();
}
Expand Down Expand Up @@ -175,7 +174,7 @@ public void testMultiPartFormData() {
// @Test
// public void testPublicOAuthFetchMessage() {
// Twilio.init(new ClientCredentialProvider(clientId, clientSecret), accountSid);
// // Fetching an existing message; if this test fails, the SID might be deleted,
// // Fetching an existing message; if this test fails, the SID might be deleted,
// // in that case, change TWILIO_MESSAGE_SID in twilio-java repo env variables
// Message message = Message.fetcher(messageSid).fetch();
// assertNotNull(message);
Expand Down
38 changes: 16 additions & 22 deletions src/test/java/com/twilio/TwilioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@

import java.util.Arrays;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.when;

public class TwilioTest {
Expand All @@ -45,7 +46,7 @@ public static String serialize(List list) {
@Mock
private NetworkHttpClient networkHttpClient;

@Before
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}
Expand All @@ -55,26 +56,19 @@ public void testGetExecutorService() {
assertNotNull(Twilio.getExecutorService());
}

@Test(expected = AuthenticationException.class)
@Test
public void testGetRestClientNullAccountSid() {
Twilio.setRestClient(null);
Twilio.setUsername(null);
Twilio.setPassword(null);

Twilio.getRestClient();
fail("AuthenticationException was expected");
assertThrows(AuthenticationException.class, Twilio::getRestClient);
}

@Test(expected = AuthenticationException.class)
@Test
public void testSetAccountSidNull() {
Twilio.setUsername(null);
fail("AuthenticationException was expected");
assertThrows(AuthenticationException.class, () -> Twilio.setUsername(null));
}

@Test(expected = AuthenticationException.class)
public void testSetAuthTokenNull() {
Twilio.setPassword(null);
fail("AuthenticationException was expected");
@Test
public void testSetPasswordNull() {
assertThrows(AuthenticationException.class, () -> Twilio.setPassword(null));
}

@Test
Expand Down
40 changes: 21 additions & 19 deletions src/test/java/com/twilio/base/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.rest.api.v2010.account.CallReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Collections;

import static org.mockito.Mockito.when;
Expand All @@ -21,51 +23,51 @@ public class ReaderTest {
@Mock
Page<Call> page;

@Before
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testNoPagingDefaults() {
Reader<Call> reader = new CallReader();
Assert.assertNull(reader.getLimit());
Assert.assertNull(reader.getPageSize());
assertNull(reader.getLimit());
assertNull(reader.getPageSize());
}

@Test
public void testSetPageSize() {
Reader<Call> reader = new CallReader().pageSize(100);
Assert.assertEquals(100, reader.getPageSize().intValue());
Assert.assertNull(reader.getLimit());
assertEquals(100, reader.getPageSize().intValue());
assertNull(reader.getLimit());
}

@Test
public void testMaxPageSize() {
Reader<Call> reader = new CallReader().pageSize(Integer.MAX_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, reader.getPageSize().intValue());
Assert.assertNull(reader.getLimit());
assertEquals(Integer.MAX_VALUE, reader.getPageSize().intValue());
assertNull(reader.getLimit());
}

@Test
public void testSetLimit() {
Reader<Call> reader = new CallReader().limit(100);
Assert.assertEquals(100, reader.getLimit().intValue());
Assert.assertEquals(100, reader.getPageSize().intValue());
assertEquals(100, reader.getLimit().intValue());
assertEquals(100, reader.getPageSize().intValue());
}

@Test
public void testSetLimitMaxPageSize() {
Reader<Call> reader = new CallReader().limit(Integer.MAX_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, reader.getLimit().intValue());
Assert.assertEquals(Integer.MAX_VALUE, reader.getPageSize().intValue());
assertEquals(Integer.MAX_VALUE, reader.getLimit().intValue());
assertEquals(Integer.MAX_VALUE, reader.getPageSize().intValue());
}

@Test
public void testSetPageSizeLimit() {
Reader<Call> reader = new CallReader().limit(1000).pageSize(5);
Assert.assertEquals(1000, reader.getLimit().intValue());
Assert.assertEquals(5, reader.getPageSize().intValue());
assertEquals(1000, reader.getLimit().intValue());
assertEquals(5, reader.getPageSize().intValue());
}

@Test
Expand All @@ -74,7 +76,7 @@ public void testNoPageLimit() {

Reader<Call> reader = new CallReader();
ResourceSet<Call> set = new ResourceSet<>(reader, client, page);
Assert.assertEquals(Long.MAX_VALUE, set.getPageLimit());
assertEquals(Long.MAX_VALUE, set.getPageLimit());
}


Expand All @@ -85,15 +87,15 @@ public void testHasPageLimit() {

Reader<Call> reader = new CallReader().limit(100);
ResourceSet<Call> set = new ResourceSet<>(reader, client, page);
Assert.assertEquals(2, set.getPageLimit());
assertEquals(2, set.getPageLimit());
}
@Test
public void testUnevenHasPageLimit() {
when(page.getRecords()).thenReturn(Collections.emptyList());
when(page.getPageSize()).thenReturn(50);
Reader<Call> reader = new CallReader().limit(125);
ResourceSet<Call> set = new ResourceSet<>(reader, client, page);
Assert.assertEquals(3, set.getPageLimit());
assertEquals(3, set.getPageLimit());
}

}
8 changes: 4 additions & 4 deletions src/test/java/com/twilio/compliance/ComplianceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.library.GeneralCodingRules.*;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -43,7 +43,7 @@ public boolean apply(final JavaClass input) {
};
}

@Before
@BeforeEach
public void setUp() {
assertTrue(twilioClasses.size() > 0);
assertTrue(resourceClasses.size() > 1);
Expand Down
Loading
Loading