11package test .com .qiniu .streaming ;
22
3- import com .google .gson .JsonSyntaxException ;
43import com .qiniu .common .QiniuException ;
54import com .qiniu .streaming .StreamingManager ;
65import com .qiniu .streaming .model .ActivityRecords ;
76import com .qiniu .streaming .model .StreamAttribute ;
87import com .qiniu .streaming .model .StreamListing ;
9- import com .qiniu .streaming .model .StreamStatus ;
108import com .qiniu .util .Auth ;
11- import com .qiniu .util .Json ;
129import org .junit .Test ;
1310import test .com .qiniu .TestConfig ;
1411
1512import static org .junit .Assert .*;
1613
1714/**
1815 * Created by bailong on 16/9/22
16+ * Updated by panyuan on 19/3/12
1917 */
2018public class StreamingTest {
19+
2120 private Auth auth = TestConfig .testAuth ;
22-
2321 private String hub = "pilisdktest" ;
24- private String streamKeyPrefix = "pilijava" + System .currentTimeMillis ();
22+ private String stream = "javasdk" ;
23+ private String streamNoExist = "javasdk" + "NoExist" ;
24+ private String streamKeyPrefix = "javasdk" ;
2525 private StreamingManager manager = new StreamingManager (auth , hub );
2626
27-
28- //@Test
27+ /**
28+ * 测试获取不存在的流的信息
29+ * 检测返回状态码是否是612
30+ */
31+ @ Test
2932 public void testGetNoExistStream () {
3033 try {
31- manager .attribute ("nnnoexist" );
34+ manager .attribute (streamNoExist );
3235 fail ("should not exist" );
3336 } catch (QiniuException e ) {
34- e .printStackTrace ();
3537 assertEquals (612 , e .code ());
3638 }
3739 }
3840
39- // CHECKSTYLE:OFF
40- //@Test
41+ /**
42+ * 测试创建、禁用、启用、获取流信息、列举
43+ * @throws QiniuException
44+ */
45+ @ Test
4146 public void testStreamOperation () throws QiniuException {
42- // CHECKSTYLE:ON
43- String streamKey = streamKeyPrefix + "-a" ;
44-
45- manager .create (streamKey );
46-
47- StreamAttribute attr = manager .attribute (streamKey );
47+ StreamAttribute attr = manager .attribute (stream );
4848 assertEquals (0 , attr .disabledTill );
4949 assertNotEquals (0 , attr .createdAt );
5050
5151 try {
52- manager .create (streamKey );
52+ manager .create (stream );
5353 fail ("has already existed" );
5454 } catch (QiniuException e ) {
5555 assertEquals (614 , e .code ());
5656 }
5757
58- manager .disableTill (streamKey , -1 );
58+ manager .disableTill (stream , -1 );
5959
60- attr = manager .attribute (streamKey );
60+ attr = manager .attribute (stream );
6161 assertEquals (-1 , attr .disabledTill );
6262 assertNotEquals (0 , attr .updatedAt );
6363
64- manager .enable (streamKey );
65- attr = manager .attribute (streamKey );
64+ manager .enable (stream );
65+ attr = manager .attribute (stream );
6666 assertEquals (0 , attr .disabledTill );
6767 assertNotEquals (0 , attr .updatedAt );
6868
6969 long t = System .currentTimeMillis () / 1000 + 3600 ;
70- manager .disableTill (streamKey , t );
71- attr = manager .attribute (streamKey );
70+ manager .disableTill (stream , t );
71+ attr = manager .attribute (stream );
7272 assertEquals (t , attr .disabledTill );
7373 assertNotEquals (0 , attr .updatedAt );
7474
75- manager .enable (streamKey );
76- attr = manager .attribute (streamKey );
75+ manager .enable (stream );
76+ attr = manager .attribute (stream );
7777 assertEquals (0 , attr .disabledTill );
7878 assertNotEquals (0 , attr .updatedAt );
7979
8080 try {
81- StreamStatus status = manager .status (streamKey );
81+ manager .status (stream );
8282 fail ();
8383 } catch (QiniuException e ) {
8484 assertEquals (619 , e .code ());
8585 }
8686
8787 try {
88- manager .saveAs (streamKey , null , 0 , 0 );
88+ manager .saveAs (stream , null , 0 , 0 );
8989 fail ();
9090 } catch (QiniuException e ) {
9191 assertEquals (619 , e .code ());
9292 }
9393
94- ActivityRecords records = manager .history (streamKey , System .currentTimeMillis () / 1000 - 1000 , 0 );
94+ ActivityRecords records = manager .history (stream , System .currentTimeMillis () / 1000 - 1000 , 0 );
9595 assertEquals (0 , records .items .length );
9696
9797 StreamListing l = manager .listStreams (false , streamKeyPrefix , null );
@@ -118,34 +118,31 @@ public void testStreamOperation() throws QiniuException {
118118 assertFalse (it .hasNext ());
119119 }
120120
121- //@Test
121+ /**
122+ * 测试saveas
123+ * 检测返回状态码是否是404
124+ * @throws QiniuException
125+ */
126+ @ Test
122127 public void testSaveAs () throws QiniuException {
123128 try {
124- manager .saveAs ("test--sd" , "f\" ff.m3u8" );
129+ manager .saveAs (streamNoExist , "f\" ff.m3u8" );
125130 } catch (QiniuException e ) {
126- // 619 , no data; 612 stream not found, 但请求正常 //
127- if (e .code () != 619 && e .code () != 612 ) {
128- throw e ;
129- }
131+ assertEquals (404 , e .response .statusCode );
130132 }
131133 }
132134
133- //@Test
135+ /**
136+ * 测试创建流
137+ * 检测返回状态码是否为614
138+ * @throws QiniuException
139+ */
140+ @ Test
134141 public void testCreate () throws QiniuException {
135142 try {
136- String body = String .format ("{\" key\" :\" %s\" }" , "stream\" Key" );
137- System .out .println (body );
138- Json .decode (body );
139- fail ("json 解析不正确" );
140- } catch (JsonSyntaxException e ) {
141-
142- }
143- try {
144- manager .create ("streamKey" );
143+ manager .create (stream );
145144 } catch (QiniuException e ) {
146- if (e .code () != 614 ) {
147- throw e ;
148- }
145+ assertEquals (614 , e .code ());
149146 }
150147 }
151148
0 commit comments