Skip to content

Commit 13496ed

Browse files
committed
adds expired status to archive and skips unknown properties when parsing. fixes #44
1 parent 7089662 commit 13496ed

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/main/java/com/opentok/Archive.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.opentok;
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.fasterxml.jackson.annotation.JsonValue;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78

89
/**
910
* Represents an archive of an OpenTok session.
1011
*/
12+
@JsonIgnoreProperties(ignoreUnknown=true)
1113
public class Archive {
1214
/**
1315
* Defines values returned by the {@link Archive#getStatus} method.
@@ -38,7 +40,9 @@ public enum Status {
3840
* The archive file is available at the target S3 bucket you set at the
3941
* <a href="https://dashboard.tokbox.com">OpenTok dashboard</a>.
4042
*/
41-
UPLOADED;
43+
UPLOADED,
44+
45+
EXPIRED;
4246

4347
@JsonValue public String toString() {
4448
return super.toString().toLowerCase();

src/main/java/com/opentok/OpenTok.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.fasterxml.jackson.core.JsonParseException;
1616
import com.fasterxml.jackson.core.JsonParser;
1717
import com.fasterxml.jackson.core.JsonProcessingException;
18+
import com.fasterxml.jackson.databind.DeserializationFeature;
1819
import com.fasterxml.jackson.databind.JsonMappingException;
1920
import com.fasterxml.jackson.databind.type.TypeFactory;
2021
import com.opentok.exception.OpenTokException;
@@ -24,6 +25,7 @@
2425
import com.opentok.util.HttpClient;
2526

2627
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import com.fasterxml.jackson.databind.ObjectReader;
2729
import org.xml.sax.InputSource;
2830

2931
/**
@@ -41,6 +43,8 @@ public class OpenTok {
4143
private int apiKey;
4244
private String apiSecret;
4345
protected HttpClient client;
46+
static protected ObjectReader archiveReader = new ObjectMapper()
47+
.reader(Archive.class);
4448

4549
/**
4650
* Creates an OpenTok object.
@@ -311,10 +315,9 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
311315
* @return The {@link Archive} object.
312316
*/
313317
public Archive getArchive(String archiveId) throws OpenTokException {
314-
ObjectMapper mapper = new ObjectMapper();
315318
String archive = this.client.getArchive(archiveId);
316319
try {
317-
return mapper.readValue(archive, Archive.class);
320+
return archiveReader.readValue(archive);
318321
} catch (Exception e) {
319322
throw new RequestException("Exception mapping json: " + e.getMessage());
320323
}
@@ -385,9 +388,8 @@ public Archive startArchive(String sessionId, String name) throws OpenTokExcepti
385388
}
386389
// TODO: do validation on sessionId and name
387390
String archive = this.client.startArchive(sessionId, name);
388-
ObjectMapper mapper = new ObjectMapper();
389391
try {
390-
return mapper.readValue(archive, Archive.class);
392+
return archiveReader.readValue(archive);
391393
} catch (Exception e) {
392394
throw new RequestException("Exception mapping json: " + e.getMessage());
393395
}
@@ -405,9 +407,8 @@ public Archive startArchive(String sessionId, String name) throws OpenTokExcepti
405407
public Archive stopArchive(String archiveId) throws OpenTokException {
406408

407409
String archive = this.client.stopArchive(archiveId);
408-
ObjectMapper mapper = new ObjectMapper();
409410
try {
410-
return mapper.readValue(archive, Archive.class);
411+
return archiveReader.readValue(archive);
411412
} catch (Exception e) {
412413
throw new RequestException("Exception mapping json: " + e.getMessage());
413414
}

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,53 @@ public void testDeleteArchive() throws OpenTokException {
548548

549549
// TODO: test delete archive failure scenarios
550550

551+
// NOTE: this test is pretty sloppy
552+
@Test public void testGetExpiredArchive() throws OpenTokException {
553+
String archiveId = "ARCHIVEID";
554+
stubFor(get(urlEqualTo("/v2/partner/"+this.apiKey+"/archive/"+archiveId))
555+
.willReturn(aResponse()
556+
.withStatus(200)
557+
.withHeader("Content-Type", "application/json")
558+
.withBody("{\n" +
559+
" \"createdAt\" : 1395187836000,\n" +
560+
" \"duration\" : 62,\n" +
561+
" \"id\" : \"" + archiveId + "\",\n" +
562+
" \"name\" : \"\",\n" +
563+
" \"partnerId\" : 123456,\n" +
564+
" \"reason\" : \"\",\n" +
565+
" \"sessionId\" : \"SESSIONID\",\n" +
566+
" \"size\" : 8347554,\n" +
567+
" \"status\" : \"expired\",\n" +
568+
" \"url\" : null\n" +
569+
" }")));
570+
571+
Archive archive = sdk.getArchive(archiveId);
572+
assertNotNull(archive);
573+
assertEquals(Archive.Status.EXPIRED, archive.getStatus());
574+
}
575+
576+
@Test public void testGetArchiveWithUnknownProperties() throws OpenTokException {
577+
String archiveId = "ARCHIVEID";
578+
stubFor(get(urlEqualTo("/v2/partner/"+this.apiKey+"/archive/"+archiveId))
579+
.willReturn(aResponse()
580+
.withStatus(200)
581+
.withHeader("Content-Type", "application/json")
582+
.withBody("{\n" +
583+
" \"createdAt\" : 1395187836000,\n" +
584+
" \"duration\" : 62,\n" +
585+
" \"id\" : \"" + archiveId + "\",\n" +
586+
" \"name\" : \"\",\n" +
587+
" \"partnerId\" : 123456,\n" +
588+
" \"reason\" : \"\",\n" +
589+
" \"sessionId\" : \"SESSIONID\",\n" +
590+
" \"size\" : 8347554,\n" +
591+
" \"status\" : \"expired\",\n" +
592+
" \"url\" : null,\n" +
593+
" \"thisisnotaproperty\" : null\n" +
594+
" }")));
595+
596+
Archive archive = sdk.getArchive(archiveId);
597+
598+
assertNotNull(archive);
599+
}
551600
}

0 commit comments

Comments
 (0)