2018-02-22 15:52:38 +01:00
package org.schabi.newpipe.extractor.services.youtube ;
import org.junit.BeforeClass ;
import org.junit.Test ;
2019-04-28 22:03:16 +02:00
import org.schabi.newpipe.DownloaderTestImpl ;
2018-02-22 15:52:38 +01:00
import org.schabi.newpipe.extractor.NewPipe ;
import org.schabi.newpipe.extractor.ServiceList ;
2018-07-13 18:02:40 +02:00
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory ;
2018-05-08 21:19:03 +02:00
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSubscriptionExtractor ;
2018-02-22 15:52:38 +01:00
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor ;
import org.schabi.newpipe.extractor.subscription.SubscriptionItem ;
import java.io.ByteArrayInputStream ;
import java.io.File ;
import java.io.FileInputStream ;
import java.util.Arrays ;
import java.util.List ;
import static org.junit.Assert.* ;
/ * *
* Test for { @link YoutubeSubscriptionExtractor }
* /
public class YoutubeSubscriptionExtractorTest {
private static YoutubeSubscriptionExtractor subscriptionExtractor ;
2018-07-13 18:02:40 +02:00
private static LinkHandlerFactory urlHandler ;
2018-02-22 15:52:38 +01:00
@BeforeClass
public static void setupClass ( ) {
2019-04-28 22:03:16 +02:00
NewPipe . init ( DownloaderTestImpl . getInstance ( ) ) ;
2018-02-22 15:52:38 +01:00
subscriptionExtractor = new YoutubeSubscriptionExtractor ( ServiceList . YouTube ) ;
2018-08-05 14:14:36 +02:00
urlHandler = ServiceList . YouTube . getChannelLHFactory ( ) ;
2018-02-22 15:52:38 +01:00
}
@Test
public void testFromInputStream ( ) throws Exception {
2018-04-06 07:45:52 +02:00
File testFile = new File ( " extractor/src/test/resources/youtube_export_test.xml " ) ;
if ( ! testFile . exists ( ) ) testFile = new File ( " src/test/resources/youtube_export_test.xml " ) ;
2018-02-22 15:52:38 +01:00
List < SubscriptionItem > subscriptionItems = subscriptionExtractor . fromInputStream ( new FileInputStream ( testFile ) ) ;
assertTrue ( " List doesn't have exactly 8 items (had " + subscriptionItems . size ( ) + " ) " , subscriptionItems . size ( ) = = 8 ) ;
for ( SubscriptionItem item : subscriptionItems ) {
assertNotNull ( item . getName ( ) ) ;
assertNotNull ( item . getUrl ( ) ) ;
assertTrue ( urlHandler . acceptUrl ( item . getUrl ( ) ) ) ;
assertFalse ( item . getServiceId ( ) = = - 1 ) ;
}
}
@Test
public void testEmptySourceException ( ) throws Exception {
String emptySource = " <opml version= \" 1.1 \" ><body> " +
" <outline text= \" Testing \" title= \" 123 \" /> " +
" </body></opml> " ;
List < SubscriptionItem > items = subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( emptySource . getBytes ( " UTF-8 " ) ) ) ;
assertTrue ( items . isEmpty ( ) ) ;
}
2019-04-26 19:59:23 +02:00
@Test
public void testSubscriptionWithEmptyTitleInSource ( ) throws Exception {
2019-04-28 14:19:33 +02:00
String channelId = " AA0AaAa0AaaaAAAAAA0aa0AA " ;
String source = " <opml version= \" 1.1 \" ><body><outline text= \" YouTube Subscriptions \" title= \" YouTube Subscriptions \" > " +
" <outline text= \" \" title= \" \" type= \" rss \" xmlUrl= \" https://www.youtube.com/feeds/videos.xml?channel_id= " + channelId + " \" /> " +
" </outline></body></opml> " ;
2019-04-26 19:59:23 +02:00
2019-04-28 14:19:33 +02:00
List < SubscriptionItem > items = subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( source . getBytes ( " UTF-8 " ) ) ) ;
assertTrue ( " List doesn't have exactly 1 item (had " + items . size ( ) + " ) " , items . size ( ) = = 1 ) ;
assertTrue ( " Item does not have an empty title (had \" " + items . get ( 0 ) . getName ( ) + " \" ) " , items . get ( 0 ) . getName ( ) . isEmpty ( ) ) ;
assertTrue ( " Item does not have the right channel id \" " + channelId + " \" (the whole url is \" " + items . get ( 0 ) . getUrl ( ) + " \" ) " , items . get ( 0 ) . getUrl ( ) . endsWith ( channelId ) ) ;
}
2019-04-26 19:59:23 +02:00
2019-04-28 14:19:33 +02:00
@Test
public void testSubscriptionWithInvalidUrlInSource ( ) throws Exception {
String source = " <opml version= \" 1.1 \" ><body><outline text= \" YouTube Subscriptions \" title= \" YouTube Subscriptions \" > " +
" <outline text= \" invalid \" title= \" url \" type= \" rss \" xmlUrl= \" https://www.youtube.com/feeds/videos.xml?channel_not_id=||||||| \" /> " +
" <outline text= \" fail \" title= \" fail \" type= \" rss \" xmlUgrl= \" invalidTag \" /> " +
" <outline text= \" invalid \" title= \" url \" type= \" rss \" xmlUrl= \" \" /> " +
" <outline text= \" \" title= \" \" type= \" rss \" xmlUrl= \" \" /> " +
2019-04-26 19:59:23 +02:00
" </outline></body></opml> " ;
2019-04-28 14:19:33 +02:00
List < SubscriptionItem > items = subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( source . getBytes ( " UTF-8 " ) ) ) ;
assertTrue ( items . isEmpty ( ) ) ;
2019-04-26 19:59:23 +02:00
}
2018-02-22 15:52:38 +01:00
@Test
public void testInvalidSourceException ( ) {
List < String > invalidList = Arrays . asList (
" <xml><notvalid></notvalid></xml> " ,
" <opml><notvalid></notvalid></opml> " ,
" <opml><body></body></opml> " ,
" " ,
null ,
" \ uD83D \ uDC28 \ uD83D \ uDC28 \ uD83D \ uDC28 " ,
" gibberish " ) ;
for ( String invalidContent : invalidList ) {
try {
if ( invalidContent ! = null ) {
byte [ ] bytes = invalidContent . getBytes ( " UTF-8 " ) ;
subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( bytes ) ) ;
2019-04-28 14:19:33 +02:00
fail ( " Extracting from \" " + invalidContent + " \" didn't throw an exception " ) ;
2018-02-22 15:52:38 +01:00
} else {
subscriptionExtractor . fromInputStream ( null ) ;
2019-04-28 14:19:33 +02:00
fail ( " Extracting from null String didn't throw an exception " ) ;
2018-02-22 15:52:38 +01:00
}
} catch ( Exception e ) {
// System.out.println(" -> " + e);
boolean isExpectedException = e instanceof SubscriptionExtractor . InvalidSourceException ;
assertTrue ( " \" " + e . getClass ( ) . getSimpleName ( ) + " \" is not the expected exception " , isExpectedException ) ;
}
}
}
}