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 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 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 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 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 savedEpisodeAction = episodeActionDao.findById(episodeAction.getId()); assertNotNull(savedEpisodeAction); } } } @AfterEach public void cleanUp() { authenticationDao.deleteAll(); episodeDao.deleteAll(); episodeActionDao.deleteAll(); subscriptionDao.deleteAll(); subscriptionActionDao.deleteAll(); } }