summaryrefslogtreecommitdiff
path: root/pse-server/src/test/java/org/psesquared/server/util/RssParserTests.java
blob: 73c54976292b70e1cdaa2c0fd30be02de8acebc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.psesquared.server.util;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;

import org.junit.jupiter.api.Test;
import org.psesquared.server.BaseTest;
import org.psesquared.server.TestAsyncConfig;
import org.psesquared.server.model.Episode;
import org.psesquared.server.model.Subscription;
import org.springframework.test.context.ContextConfiguration;

// Disable async behavior for this Test Class
@ContextConfiguration(classes = TestAsyncConfig.class)
public class RssParserTests extends BaseTest {

    private static final String TAGESSCHAU_URL = "https://www.tagesschau.de/multimedia/podcasts/mal-angenommen-feed-101.xml";
    private final Subscription tagesschauPodcast = Subscription.builder()
            .url(TAGESSCHAU_URL)
            .build();
    private final String relativePathToTestFeeds = "./testfeeds/";

    @Test
    public void testSubscriptionInvalid() {
        Subscription nullSubscription = null;
        Subscription nullUrlSubscription = Subscription.builder().build();
        Subscription invalidUrlSubscription = Subscription.builder()
                .url("Not a url")
                .title("Some inventive Title")
                .build();

        assertDoesNotThrow(() -> rssParser.validate(nullSubscription));
        assertDoesNotThrow(() -> rssParser.validate(nullUrlSubscription));
        assertDoesNotThrow(() -> rssParser.validate(invalidUrlSubscription));
        assertFalse(subscriptionDao.findByUrl(invalidUrlSubscription.getUrl()).isPresent());
    }

    // Does currently only work in debugger by making sure the Parser has time to
    // validate, before the Test evaluates the Assertions.
    @Test
    public void testValidSubscription() {
        final String expectedURL = "https://media.tagesschau.de/audio/2023/0125/AU-20230125-1854-5200.hi.mp3";
        final String expectedGuid = "tagesschau-podcast-mal-angenommen-tierrechte-101";
        final String expectedTitle = "Gleiche Rechte für Tiere? Was dann?";
        final int expectedTotal = 1442;
        Episode expectedEpisode = Episode.builder().url(expectedURL).guid(expectedGuid).title(expectedTitle)
                .total(expectedTotal).subscription(tagesschauPodcast).build();
        Episode testEpisode = Episode.builder().url(expectedURL).id(expectedEpisode.getId()).build();
        tagesschauPodcast.addEpisode(testEpisode);

        assertDoesNotThrow(() -> rssParser.validate(tagesschauPodcast));
        assertTrue(subscriptionDao.findByUrl(TAGESSCHAU_URL).isPresent());
    }

    @Test
    public void currentDirTest() {
        //System.out.println(System.getProperty("user.dir"));
        final String pathToTestFile = relativePathToTestFeeds + "dirtest.txt";
        File testFile = new File(pathToTestFile);
        assertTrue(testFile.exists() && !testFile.isDirectory());
    }

    public void testByteHamsterEdgeCasePodcast() {
        final String subscriptionURL = "https://tools.bytehamster.com/podcast/rss.xml";
        final String firstEpisodeURL = "http://tools.bytehamster.com/podcast/piano.mp3?1.mp3";
        final String lastEpisodeURL = "http://tools.bytehamster.com/podcast/piano.mp3?13.mp3";

        Subscription subscription = Subscription.builder().url(subscriptionURL).build();
        Episode firstEpisode = Episode.builder().url(firstEpisodeURL)
                .subscription(subscription).build();
        subscription.addEpisode(firstEpisode);

        Episode lastEpisode = Episode.builder().url(lastEpisodeURL)
                .subscription(subscription).build();
        subscription.addEpisode(lastEpisode);
        subscriptionDao.save(subscription);
        episodeDao.save(firstEpisode);
        episodeDao.save(lastEpisode);

        // Feed contains an Episode that does not meet minimum requirements, so
        // Subscription and its Episodes should be deleted
        assertDoesNotThrow(() -> rssParser.validate(subscription));
        assertFalse(subscriptionDao.findByUrl(subscriptionURL).isPresent());
        assertFalse(episodeDao.findByUrl(firstEpisodeURL).isPresent());
        assertFalse(episodeDao.findByUrl(lastEpisodeURL).isPresent());
    }

    @Test
    public void testDeletionOfEpisodeNotInFeed() {
        final String subscriptionURL = "https://tools.bytehamster.com/podcast/alwaysNew.php";
        final String notIncludedURL = "http://tools.bytehamster.com/podcast/piano.mp3?2023-03-15-21:53:21.mp3";

        Subscription subscription = Subscription.builder().url(subscriptionURL).build();
        Episode episodeToDelete = Episode.builder().url(notIncludedURL)
                .subscription(subscription).build();
        subscription.addEpisode(episodeToDelete);
        subscriptionDao.save(subscription);
        episodeDao.save(episodeToDelete);

        // Feed does not contain the Episode, so the Episode should be deleted, but the
        // Subscription remains
        assertDoesNotThrow(() -> rssParser.validate(subscription));
        assertTrue(subscriptionDao.findByUrl(subscriptionURL).isPresent());
        assertFalse(episodeDao.findByUrl(notIncludedURL).isPresent());
    }

    @Test
    public void testValidateBaseTestFeed() {
        final String testPodcastUrl = new File("testfeeds/testPodcast0.xml").toURI().toString();
        Subscription subscription = subscriptionDao.findByUrl(testPodcastUrl).orElseThrow();
        assertDoesNotThrow(() -> rssParser.validate(subscription));
        assertNotNull(subscriptionDao.findByUrl(testPodcastUrl));
    }

    @Test
    public void testTimeParsing() {
        final String subscriptionUrl = new File("testfeeds/timeTestPodcast.xml").toURI().toString();
        Subscription subscription = subscriptionDao.save(Subscription.builder().url(subscriptionUrl).build());
        assertDoesNotThrow(() -> rssParser.validate(subscription));
        assertFalse(subscriptionDao.findByUrl(subscriptionUrl).isPresent());
    }

    @Test
    public void doubleEnclosureTest() {
        final String subscriptionUrl = new File("testfeeds/multipleEnclosuresFeed.xml").toURI().toString();
        Subscription subscription = subscriptionDao.save(Subscription.builder().url(subscriptionUrl).build());
        assertDoesNotThrow(() -> rssParser.validate(subscription));
        assertFalse(subscriptionDao.findByUrl(subscriptionUrl).isPresent());
    }

}