22
33import com .auth0 .json .JsonMatcher ;
44import com .auth0 .json .JsonTest ;
5+ import org .junit .Rule ;
56import org .junit .Test ;
7+ import org .junit .rules .ExpectedException ;
68
79import static org .hamcrest .MatcherAssert .assertThat ;
8- import static org .hamcrest .Matchers .is ;
9- import static org .hamcrest .Matchers .notNullValue ;
10+ import static org .hamcrest .Matchers .*;
1011
1112public class TwilioFactorProviderTest extends JsonTest <TwilioFactorProvider > {
1213
13- private static final String json = "{\" from\" :\" +12356789\" ,\" messaging_service_sid\" :\" id321\" ,\" auth_token\" :\" atokEn\" ,\" sid\" :\" id123\" }" ;
14+ @ Rule
15+ public ExpectedException exception = ExpectedException .none ();
16+
17+ private static final String JSON_WITH_FROM = "{\" from\" :\" +12356789\" ,\" auth_token\" :\" atokEn\" ,\" sid\" :\" id123\" }" ;
18+ private static final String JSON_WITH_MESSAGING_SERVICE_SID = "{\" messaging_service_sid\" :\" id321\" ,\" auth_token\" :\" atokEn\" ,\" sid\" :\" id123\" }" ;
1419
1520 @ Test
16- public void shouldSerializeWithDeprecatedSetters () throws Exception {
21+ public void shouldFailConstructionWithBothFromAndMessagingServiceSID () throws Exception {
22+ exception .expect (IllegalArgumentException .class );
23+ exception .expectMessage ("You must specify either `from` or `messagingServiceSID`, but not both" );
24+
25+ new TwilioFactorProvider ("+12356789" , "messaging_service_sid" , "atokEn" , "id123" );
26+ }
27+
28+ @ Test
29+ public void shouldFailWhenSettingFromAndMessagingServiceSIDWasAlreadySet () throws Exception {
30+ exception .expect (IllegalArgumentException .class );
31+ exception .expectMessage ("You must specify either `from` or `messagingServiceSID`, but not both" );
32+
1733 TwilioFactorProvider provider = new TwilioFactorProvider ();
18- provider .setAuthToken ("atokEn" );
1934 provider .setFrom ("+12356789" );
2035 provider .setMessagingServiceSID ("id321" );
36+ }
37+
38+ @ Test
39+ public void shouldFailWhenSettingMessagingServiceSIDAndFromWasAlreadySet () throws Exception {
40+ exception .expect (IllegalArgumentException .class );
41+ exception .expectMessage ("You must specify either `from` or `messagingServiceSID`, but not both" );
42+
43+ TwilioFactorProvider provider = new TwilioFactorProvider ();
44+ provider .setMessagingServiceSID ("id321" );
45+ provider .setFrom ("+12356789" );
46+ }
47+
48+ @ Test
49+ public void shouldSerializeWithDeprecatedSettersWithFrom () throws Exception {
50+ TwilioFactorProvider provider = new TwilioFactorProvider ();
51+ provider .setAuthToken ("atokEn" );
52+ provider .setFrom ("+12356789" );
2153 provider .setSID ("id123" );
2254
2355 String serialized = toJSON (provider );
2456 assertThat (serialized , is (notNullValue ()));
2557 assertThat (serialized , JsonMatcher .hasEntry ("from" , "+12356789" ));
58+ assertThat (serialized , JsonMatcher .hasEntry ("auth_token" , "atokEn" ));
59+ assertThat (serialized , JsonMatcher .hasEntry ("sid" , "id123" ));
60+ assertThat (serialized , not (containsString ("\" messaging_service_sid\" " )));
61+ }
62+
63+ @ Test
64+ public void shouldSerializeWithDeprecatedSettersWithMessagingServiceSID () throws Exception {
65+ TwilioFactorProvider provider = new TwilioFactorProvider ();
66+ provider .setAuthToken ("atokEn" );
67+ provider .setMessagingServiceSID ("id321" );
68+ provider .setSID ("id123" );
69+
70+ String serialized = toJSON (provider );
71+ assertThat (serialized , is (notNullValue ()));
72+
2673 assertThat (serialized , JsonMatcher .hasEntry ("messaging_service_sid" , "id321" ));
2774 assertThat (serialized , JsonMatcher .hasEntry ("auth_token" , "atokEn" ));
2875 assertThat (serialized , JsonMatcher .hasEntry ("sid" , "id123" ));
76+ assertThat (serialized , not (containsString ("\" from\" " )));
2977 }
3078
3179 @ Test
32- public void shouldSerialize () throws Exception {
33- TwilioFactorProvider provider = new TwilioFactorProvider ("+12356789" , "id321" , "atokEn" , "id123" );
80+ public void shouldSerializeWithFrom () throws Exception {
81+ TwilioFactorProvider provider = new TwilioFactorProvider ("+12356789" , null , "atokEn" , "id123" );
3482
3583 String serialized = toJSON (provider );
3684 assertThat (serialized , is (notNullValue ()));
3785 assertThat (serialized , JsonMatcher .hasEntry ("from" , "+12356789" ));
86+ assertThat (serialized , JsonMatcher .hasEntry ("auth_token" , "atokEn" ));
87+ assertThat (serialized , JsonMatcher .hasEntry ("sid" , "id123" ));
88+ assertThat (serialized , not (containsString ("\" messaging_service_sid\" " )));
89+ }
90+
91+ @ Test
92+ public void shouldSerializeWithMessaginServiceSID () throws Exception {
93+ TwilioFactorProvider provider = new TwilioFactorProvider (null , "id321" , "atokEn" , "id123" );
94+
95+ String serialized = toJSON (provider );
96+ assertThat (serialized , is (notNullValue ()));
3897 assertThat (serialized , JsonMatcher .hasEntry ("messaging_service_sid" , "id321" ));
3998 assertThat (serialized , JsonMatcher .hasEntry ("auth_token" , "atokEn" ));
4099 assertThat (serialized , JsonMatcher .hasEntry ("sid" , "id123" ));
100+ assertThat (serialized , not (containsString ("\" from\" " )));
41101 }
42102
43103 @ Test
44- public void shouldDeserialize () throws Exception {
45- TwilioFactorProvider provider = fromJSON (json , TwilioFactorProvider .class );
104+ public void shouldDeserializeWithFrom () throws Exception {
105+ TwilioFactorProvider provider = fromJSON (JSON_WITH_FROM , TwilioFactorProvider .class );
46106
47107 assertThat (provider , is (notNullValue ()));
48108 assertThat (provider .getAuthToken (), is ("atokEn" ));
49109 assertThat (provider .getFrom (), is ("+12356789" ));
50- assertThat (provider .getMessagingServiceSID (), is ("id321" ));
110+ assertThat (provider .getMessagingServiceSID (), is (nullValue () ));
51111 assertThat (provider .getSID (), is ("id123" ));
52112 }
53113
114+ @ Test
115+ public void shouldDeserializeWithMessagingServiceSID () throws Exception {
116+ TwilioFactorProvider provider = fromJSON (JSON_WITH_MESSAGING_SERVICE_SID , TwilioFactorProvider .class );
117+
118+ assertThat (provider , is (notNullValue ()));
119+ assertThat (provider .getAuthToken (), is ("atokEn" ));
120+ assertThat (provider .getFrom (), is (nullValue ()));
121+ assertThat (provider .getMessagingServiceSID (), is ("id321" ));
122+ assertThat (provider .getSID (), is ("id123" ));
123+ }
54124}
0 commit comments