summaryrefslogtreecommitdiff
path: root/pse-server/src/test/java/org/psesquared/server/BaseTest.java
blob: 0d200e63fba96a125baf8c693b9b4e508704c7f9 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package org.psesquared.server;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.psesquared.server.authentication.api.data.access.AuthenticationDao;
import org.psesquared.server.episode.actions.api.data.access.EpisodeActionDao;
import org.psesquared.server.episode.actions.api.data.access.EpisodeDao;
import org.psesquared.server.model.Action;
import org.psesquared.server.model.Episode;
import org.psesquared.server.model.EpisodeAction;
import org.psesquared.server.model.Role;
import org.psesquared.server.model.Subscription;
import org.psesquared.server.model.SubscriptionAction;
import org.psesquared.server.model.User;
import org.psesquared.server.subscriptions.api.data.access.SubscriptionActionDao;
import org.psesquared.server.subscriptions.api.data.access.SubscriptionDao;
import org.psesquared.server.util.RssParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public abstract class BaseTest {

    @Autowired
    public AuthenticationDao authenticationDao;

    @Autowired
    public SubscriptionDao subscriptionDao;

    @Autowired
    public EpisodeDao episodeDao;

    @Autowired
    public SubscriptionActionDao subscriptionActionDao;

    @Autowired
    public EpisodeActionDao episodeActionDao;

    @Autowired
    public RssParser rssParser;

    public static int numberOfUsers = 1;
    public static int numberOfSubscriptionsPerUser = 2;
    public static int numberOfEpisodesPerSubscription = 3;

    @BeforeEach
    @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
    public void setUp() {
        setUpUsers(numberOfUsers, numberOfSubscriptionsPerUser, numberOfEpisodesPerSubscription);
    }

    // URL Scheme:
    // Subscriptions: file:/"your project path"/testfeeds/testPodcast0.xml
    // Episodes: /testfeeds/testPodcast0/episode0
    private void setUpUsers(int userCount, int subCount, int epCount) {
        for (int i = 0; i < userCount; i++) {
            User user = new User();
            user.setUsername("testUser" + i);
            user.setPassword("testPassword123!" + i);
            user.setEmail(user.getUsername() + "@mail.de");
            user.setRole(Role.USER);

            // Store user in database
            authenticationDao.save(user);

            // Check if the user exists in the database
            Optional<User> savedUser = authenticationDao.findByUsername("testuser" + i);
            assertNotNull(savedUser);

            setUpSubscriptionsAndEpisodes(subCount, epCount, user);
        }
    }

    private void setUpSubscriptionsAndEpisodes(int subCount, int epCount, User user) {
        for (int i = 0; i < subCount; i++) {
            Subscription subscription = new Subscription();
            subscription.setTitle("testPodcast" + i);
            String url = new File(String.format("testfeeds/testPodcast%d.xml", i)).toURI().toString();  
            subscription.setUrl(url);
            subscription.setTimestamp(i * 1000000);

            // Save the Subscription in the database
            subscriptionDao.save(subscription);

            // Check if the Subscription exists in the database
            Optional<Subscription> savedSubscription = subscriptionDao
                    .findByUrl(url);
            assertNotNull(savedSubscription);

            // create SubscriptionAction for User and Subscription
            SubscriptionAction subscriptionAction = new SubscriptionAction();
            subscriptionAction.setAdded(true);
            // subscriptionAction.setAdded(i % 2 == 0); (every other Action is marked
            // as inactive/removed)
            subscriptionAction.setUser(user);
            subscriptionAction.setTimestamp(i * 1000000);
            subscriptionAction.setSubscription(subscription);

            // save SubscriptionAction to the database
            subscriptionActionDao.save(subscriptionAction);

            // Check if the SubscriptionAction exists in the database
            Optional<SubscriptionAction> savedSubscriptionAction = subscriptionActionDao.findByUserAndSubscription(user,
                    subscription);
            assertNotNull(savedSubscriptionAction);

            for (int j = 0; j < epCount; j++) {
                Episode episode = new Episode();
                episode.setSubscription(subscription);
                
                String episodeUrl = String.format("/testfeeds/testPodcast%d/episode%d.mp3", i, j);
                episode.setUrl(episodeUrl);
                episode.setTotal((j + 1) * 100);
                episode.setTitle("testEpisode" + j);

                // save the Episdoe in the database
                episodeDao.save(episode);

                // Check if the Episode exists in the database
                Optional<Episode> savedEpisode = episodeDao.findByUrl(episodeUrl);
                assertNotNull(savedEpisode);

                // create EpisodeAction for User and Episode
                EpisodeAction episodeAction = new EpisodeAction();
                episodeAction.setEpisode(episode);
                episodeAction.setAction(Action.PLAY);
                episodeAction.setUser(user);
                episodeAction.setTimestamp(LocalDateTime.ofEpochSecond(j * 1000000,
                        0,
                        ZoneOffset.UTC));
                episodeAction.setStarted((j + 1));
                episodeAction.setPosition((j + 1) * 10);

                // save EpisodeAction in the Database
                episodeActionDao.save(episodeAction);

                // Check if the EpisodeAction exists in the database
                Optional<EpisodeAction> savedEpisodeAction = episodeActionDao.findById(episodeAction.getId());
                assertNotNull(savedEpisodeAction);
            }

        }
    }

    @AfterEach
    public void cleanUp() {
        authenticationDao.deleteAll();
        episodeDao.deleteAll();
        episodeActionDao.deleteAll();
        subscriptionDao.deleteAll();
        subscriptionActionDao.deleteAll();
    }

}