Skip to content

Commit 901ae94

Browse files
author
Doug Black
committed
add message and media test
1 parent 6928408 commit 901ae94

File tree

6 files changed

+348
-185
lines changed

6 files changed

+348
-185
lines changed

src/main/java/com/twilio/sdk/resource/instance/Media.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ public String getContentType() {
147147
return this.getProperty("content_type");
148148
}
149149

150+
/**
151+
* Gets the duration
152+
*
153+
* @return the duration
154+
*/
155+
public Integer getDuration() {
156+
Integer duration = (Integer) this.getObject("duration");
157+
if (duration != null) {
158+
return duration;
159+
} else {
160+
throw new IllegalStateException("The Media instance doesn't have the max size property set");
161+
}
162+
}
163+
164+
/**
165+
* Gets the api version.
166+
*
167+
* @return the api version
168+
*/
169+
public String getApiVersion() {
170+
return this.getProperty("api_version");
171+
}
172+
150173
/**
151174
* Gets the uri of this media
152175
*

src/main/java/com/twilio/sdk/resource/instance/Message.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Message extends InstanceResource {
1717
* Instantiates a new message.
1818
*
1919
* @param client the client
20-
*/
20+
*/
2121
public Message(TwilioRestClient client) {
2222
super(client);
2323
}
@@ -27,7 +27,7 @@ public Message(TwilioRestClient client) {
2727
*
2828
* @param client the client
2929
* @param sid the sid
30-
*/
30+
*/
3131
public Message(TwilioRestClient client, String sid) {
3232
super(client);
3333
if (sid == null) {
@@ -41,14 +41,14 @@ public Message(TwilioRestClient client, String sid) {
4141
*
4242
* @param client the client
4343
* @param properties the properties
44-
*/
44+
*/
4545
public Message(TwilioRestClient client, Map<String, Object> properties) {
4646
super(client, properties);
4747
}
4848

4949
/* (non-Javadoc)
5050
* @see com.twilio.sdk.resource.Resource#getResourceLocation()
51-
*/
51+
*/
5252
@Override
5353
protected String getResourceLocation() {
5454
return "/" + TwilioRestClient.DEFAULT_VERSION + "/Accounts/"
@@ -58,13 +58,13 @@ protected String getResourceLocation() {
5858

5959
/*
6060
* Property getters
61-
*/
61+
*/
6262

6363
/**
6464
* Gets the sid.
6565
*
6666
* @return the sid
67-
*/
67+
*/
6868
public String getSid() {
6969
return this.getProperty(SID_PROPERTY);
7070
}
@@ -74,7 +74,7 @@ public String getSid() {
7474
* Returns the a list of media.
7575
*
7676
* @return the MediaList associated with this message
77-
*/
77+
*/
7878
public MediaList getMedia() {
7979
MediaList media = new MediaList(this.getClient(), this.getSid());
8080
media.setRequestAccountSid(this.getRequestAccountSid());
@@ -87,7 +87,7 @@ public MediaList getMedia() {
8787
*
8888
* @param the sid of a media instance associated with this message
8989
* @return the MediaList associated with this message
90-
*/
90+
*/
9191
public Media getMedia(String mediaSid) {
9292
Media media = new Media(this.getClient(), this.getSid(), mediaSid);
9393
media.setRequestAccountSid(this.getRequestAccountSid());
@@ -98,7 +98,7 @@ public Media getMedia(String mediaSid) {
9898
* Gets the date created.
9999
*
100100
* @return the date created
101-
*/
101+
*/
102102
public Date getDateCreated() {
103103
return parseDate(this.getProperty("date_created"));
104104
}
@@ -107,7 +107,7 @@ public Date getDateCreated() {
107107
* Gets the date updated.
108108
*
109109
* @return the date updated
110-
*/
110+
*/
111111
public Date getDateUpdated() {
112112
return parseDate(this.getProperty("date_updated"));
113113
}
@@ -116,7 +116,7 @@ public Date getDateUpdated() {
116116
* Gets the date sent.
117117
*
118118
* @return the date sent
119-
*/
119+
*/
120120
public Date getDateSent() {
121121
return parseDate(this.getProperty("date_sent"));
122122
}
@@ -125,7 +125,7 @@ public Date getDateSent() {
125125
* Gets the account sid.
126126
*
127127
* @return the account sid
128-
*/
128+
*/
129129
public String getAccountSid() {
130130
return this.getProperty("account_sid");
131131
}
@@ -134,7 +134,7 @@ public String getAccountSid() {
134134
* Gets the to.
135135
*
136136
* @return the to
137-
*/
137+
*/
138138
public String getTo() {
139139
return this.getProperty("to");
140140
}
@@ -143,7 +143,7 @@ public String getTo() {
143143
* Gets the from.
144144
*
145145
* @return the from
146-
*/
146+
*/
147147
public String getFrom() {
148148
return this.getProperty("from");
149149
}
@@ -152,7 +152,7 @@ public String getFrom() {
152152
* Gets the body.
153153
*
154154
* @return the body
155-
*/
155+
*/
156156
public String getBody() {
157157
return this.getProperty("body");
158158
}
@@ -161,7 +161,7 @@ public String getBody() {
161161
* Gets the status.
162162
*
163163
* @return the status
164-
*/
164+
*/
165165
public String getStatus() {
166166
return this.getProperty("status");
167167
}
@@ -170,16 +170,25 @@ public String getStatus() {
170170
* Gets the price.
171171
*
172172
* @return the price
173-
*/
173+
*/
174174
public String getPrice() {
175175
return this.getProperty("price");
176176
}
177177

178+
/**
179+
* Gets the price.
180+
*
181+
* @return the price
182+
*/
183+
public String getPriceUnit() {
184+
return this.getProperty("price_unit");
185+
}
186+
178187
/**
179188
* Gets the number of segments used to deliver this message.
180189
*
181190
* @return the number of segments
182-
*/
191+
*/
183192
public int getNumSegments() {
184193
return Integer.parseInt(this.getProperty("num_segments"));
185194
}
@@ -188,7 +197,7 @@ public int getNumSegments() {
188197
* Gets the number of media associated with the message.
189198
*
190199
* @return the number of segments
191-
*/
200+
*/
192201
public int getNumMedia() {
193202
return Integer.parseInt(this.getProperty("num_media"));
194203
}
@@ -197,7 +206,7 @@ public int getNumMedia() {
197206
* Gets the direction.
198207
*
199208
* @return the direction
200-
*/
209+
*/
201210
public String getDirection() {
202211
return this.getProperty("direction");
203212
}
@@ -206,7 +215,7 @@ public String getDirection() {
206215
* Gets the api version.
207216
*
208217
* @return the api version
209-
*/
218+
*/
210219
public String getApiVersion() {
211220
return this.getProperty("api_version");
212221
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.twilio.sdk.resource.instance;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.stub;
7+
8+
import java.util.Map;
9+
import java.util.HashMap;
10+
import java.text.SimpleDateFormat;
11+
12+
import org.junit.Test;
13+
import org.mockito.Matchers;
14+
15+
import com.twilio.sdk.TwilioRestClient;
16+
import com.twilio.sdk.TwilioRestException;
17+
import com.twilio.sdk.TwilioRestResponse;
18+
import com.twilio.sdk.resource.instance.Media;
19+
import com.twilio.sdk.resource.list.MediaList;
20+
21+
public class MediaTest extends BasicRequestTester {
22+
23+
final String mediaSid = "ME557ce644e5ab84fa21cc21112e22c485";
24+
final String accountSid = "AC12345678901234567890123456789012";
25+
final String messageSid = "SM8dfedb55c129dd4d6bd1f59af9d11080";
26+
27+
TwilioRestClient client = mock(TwilioRestClient.class);
28+
TwilioRestResponse resp = mock(TwilioRestResponse.class);
29+
30+
private void setupMocks() {
31+
HashMap<String, Object> map = new HashMap<String, Object>();
32+
stub(resp.toMap()).toReturn(map);
33+
34+
map.put("content_type", "image/jpeg");
35+
map.put("sid", mediaSid);
36+
map.put("api_version", "2010-04-01");
37+
map.put("parent_sid", messageSid);
38+
map.put("account_sid", accountSid);
39+
}
40+
41+
@Test
42+
public void testCreation() throws Exception {
43+
setupMocks();
44+
stub(
45+
client.safeRequest(Matchers.eq("/2010-04-01/Accounts/" + accountSid + "/Media/" + mediaSid
46+
+ ".json"), Matchers.eq("GET"), Matchers.any(Map.class)))
47+
.toReturn(resp);
48+
Media media = new Media(client, mediaSid);
49+
media.setRequestAccountSid(accountSid);
50+
51+
assertTrue(media.getParentSid().equals("SM8dfedb55c129dd4d6bd1f59af9d11080"));
52+
assertTrue(media.getContentType().equals("image/jpeg"));
53+
assertTrue(media.getApiVersion().equals("2010-04-01"));
54+
assertTrue(media.getSid().equals("ME557ce644e5ab84fa21cc21112e22c485"));
55+
assertTrue(media.getAccountSid().equals("AC12345678901234567890123456789012"));
56+
}
57+
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.twilio.sdk.resource.instance;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.stub;
6+
7+
import java.util.HashMap; import java.util.Map;
8+
9+
import org.junit.Test;
10+
import org.mockito.Matchers;
11+
12+
import com.twilio.sdk.TwilioRestClient;
13+
import com.twilio.sdk.TwilioRestException;
14+
import com.twilio.sdk.TwilioRestResponse;
15+
import com.twilio.sdk.resource.instance.Message;
16+
17+
public class MessageTest {
18+
final String messageSid = "MM12345678901234567890123456789012";
19+
20+
final String accountSid = "AC12345678901234567890123456789012";
21+
TwilioRestClient client = mock(TwilioRestClient.class);
22+
TwilioRestResponse resp = mock(TwilioRestResponse.class);
23+
24+
private void setupMocks() {
25+
HashMap<String, Object> map = new HashMap<String, Object>();
26+
stub(resp.toMap()).toReturn(map);
27+
28+
map.put("to", "+19197404420");
29+
map.put("from", "+19192386150");
30+
map.put("body", "This message is a test!");
31+
map.put("direction", "outbound-api");
32+
map.put("price", "-0.010000");
33+
map.put("price_unit", "USD");
34+
map.put("api_version", "2010-04-01");
35+
map.put("sid", messageSid);
36+
map.put("account_sid", accountSid);
37+
}
38+
39+
@Test
40+
public void testCreation() throws TwilioRestException {
41+
setupMocks();
42+
stub(
43+
client.safeRequest(Matchers.eq("/2010-04-01/Accounts/" + accountSid + "/Messages/" + messageSid
44+
+ ".json"), Matchers.eq("GET"), Matchers.any(Map.class)))
45+
.toReturn(resp);
46+
Message m = new Message(client, messageSid);
47+
m.setRequestAccountSid(accountSid);
48+
49+
assertTrue(m.getTo().equals("+19197404420"));
50+
assertTrue(m.getFrom().equals("+19192386150"));
51+
assertTrue(m.getBody().equals("This message is a test!"));
52+
assertTrue(m.getDirection().equals("outbound-api"));
53+
assertTrue(m.getPrice().equals("-0.010000"));
54+
assertTrue(m.getPriceUnit().equals("USD"));
55+
assertTrue(m.getApiVersion().equals("2010-04-01"));
56+
assertTrue(m.getSid().equals(messageSid));
57+
assertTrue(m.getAccountSid().equals(accountSid));
58+
}
59+
60+
}

0 commit comments

Comments
 (0)