diff options
Diffstat (limited to 'pse-server/src/test/java/org/psesquared/server/subscriptions')
3 files changed, 253 insertions, 0 deletions
diff --git a/pse-server/src/test/java/org/psesquared/server/subscriptions/api/data/access/SubscriptionActionDaoTests.java b/pse-server/src/test/java/org/psesquared/server/subscriptions/api/data/access/SubscriptionActionDaoTests.java new file mode 100644 index 0000000..f296a72 --- /dev/null +++ b/pse-server/src/test/java/org/psesquared/server/subscriptions/api/data/access/SubscriptionActionDaoTests.java @@ -0,0 +1,89 @@ +package org.psesquared.server.subscriptions.api.data.access; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.psesquared.server.BaseTest; +import org.psesquared.server.model.Subscription; +import org.psesquared.server.model.SubscriptionAction; +import org.psesquared.server.model.User; + +public class SubscriptionActionDaoTests extends BaseTest { + + @Test + public void existsByUserAndSubscriptionTest() { + String username = "testUser0"; + String subscriptionUrl = new File("testfeeds/testPodcast0.xml").toURI().toString(); + User user = authenticationDao.findByUsername(username).orElseThrow(); + Subscription subscription = subscriptionDao.findByUrl(subscriptionUrl).orElseThrow(); + boolean exists = subscriptionActionDao.existsByUserAndSubscription(user, subscription); + assertTrue(exists); + } + + @Test + public void findByUserAndSubscriptionTest() { + String username = "testUser0"; + String subscriptionUrl = new File("testfeeds/testPodcast0.xml").toURI().toString(); + User user = authenticationDao.findByUsername(username).orElseThrow(); + Subscription subscription = subscriptionDao.findByUrl(subscriptionUrl).orElseThrow(); + SubscriptionAction subscriptionAction = subscriptionActionDao.findByUserAndSubscription(user, subscription) + .orElseThrow(); + assertEquals(subscriptionAction.getSubscription().getUrl(), subscriptionUrl); + } + + @Test + public void findByUserUsernameAndTimestampGreaterThanEqualTest() { + String username = "testUser0"; + // Jede SubscriptionAction eines Nutzers hat einen Timestamp von zusätzlichen + // 1000000 zur Zeit 0 + int factor = 1; + int timeDifference = 1000000; + long since = factor * timeDifference; + List<SubscriptionAction> subscriptionActions = subscriptionActionDao + .findByUserUsernameAndTimestampGreaterThanEqual(username, since); + assertEquals(numberOfSubscriptionsPerUser - factor, subscriptionActions.size()); + } + + @Test + public void findByUserUsernameAndAddedTrueTest() { + String username = "testUser0"; + List<SubscriptionAction> subscriptionActions = subscriptionActionDao.findByUserUsernameAndAddedTrue(username); + assertEquals(subscriptionActions.size(), numberOfSubscriptionsPerUser); + } + + @Test + public void findByUserUsernameAndAddedTrueAndTimestampGreaterThanEqualTest() { + String username = "testUser0"; + // Jede SubscriptionAction eines Nutzers hat einen Timestamp von zusätzlichen + // 1000000 zur Zeit 0 + // int puffer = 1000; + int factor = 1; + int timeDifference = 1000000; + long since = factor * timeDifference; + List<SubscriptionAction> subscriptionActions = subscriptionActionDao + .findByUserUsernameAndTimestampGreaterThanEqual(username, since); + assertEquals(numberOfSubscriptionsPerUser - factor, subscriptionActions.size()); + // Eine EpisodeAction der gefundenen EpisodeActions auf added=false setzen + subscriptionActions.get(0).setAdded(false); + subscriptionActionDao.save(subscriptionActions.get(0)); + List<SubscriptionAction> subscriptionActions2 = subscriptionActionDao + .findByUserUsernameAndAddedTrueAndTimestampGreaterThanEqual(username, since); + assertEquals(numberOfSubscriptionsPerUser - factor - 1, subscriptionActions2.size()); + } + + @Test + public void deleteUserTest() { + String username = "testUser0"; + User user = authenticationDao.findByUsername(username).orElseThrow(); + Assertions.assertEquals(numberOfSubscriptionsPerUser, + subscriptionActionDao.findByUserUsernameAndAddedTrue(username).size()); + authenticationDao.delete(user); + Assertions.assertEquals(0, subscriptionActionDao.findByUserUsernameAndAddedTrue(username).size()); + } + +} diff --git a/pse-server/src/test/java/org/psesquared/server/subscriptions/api/data/access/SubscriptionDaoTests.java b/pse-server/src/test/java/org/psesquared/server/subscriptions/api/data/access/SubscriptionDaoTests.java new file mode 100644 index 0000000..3ae0089 --- /dev/null +++ b/pse-server/src/test/java/org/psesquared/server/subscriptions/api/data/access/SubscriptionDaoTests.java @@ -0,0 +1,36 @@ +package org.psesquared.server.subscriptions.api.data.access; + +import org.junit.jupiter.api.Test; +import org.psesquared.server.BaseTest; +import org.psesquared.server.model.Subscription; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.File; + +public class SubscriptionDaoTests extends BaseTest { + + @Test + public void findByUrlTest() { + String url = new File("testfeeds/testPodcast0.xml").toURI().toString(); + Subscription subscription = subscriptionDao.findByUrl(url).orElseThrow(); + assertEquals(subscription.getUrl(), url); + } + + @Test + public void existsByUrlTest() { + String url = new File("testfeeds/testPodcast0.xml").toURI().toString(); + boolean exists = subscriptionDao.existsByUrl(url); + assertTrue(exists); + boolean notExists = subscriptionDao.existsByUrl("blablabla"); + assertFalse(notExists); + } + + @Test + public void testCascadeDelete() { + String url = new File("testfeeds/testPodcast0.xml").toURI().toString(); + Subscription subscription = subscriptionDao.findByUrl(url).orElseThrow(); + assertDoesNotThrow(() -> subscriptionDao.delete(subscription)); + } + +} diff --git a/pse-server/src/test/java/org/psesquared/server/subscriptions/api/service/SubscriptionServiceTests.java b/pse-server/src/test/java/org/psesquared/server/subscriptions/api/service/SubscriptionServiceTests.java new file mode 100644 index 0000000..3345185 --- /dev/null +++ b/pse-server/src/test/java/org/psesquared/server/subscriptions/api/service/SubscriptionServiceTests.java @@ -0,0 +1,128 @@ +package org.psesquared.server.subscriptions.api.service; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.List; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.psesquared.server.BaseTest; +import org.psesquared.server.model.Subscription; +import org.psesquared.server.model.SubscriptionAction; +import org.psesquared.server.subscriptions.api.controller.SubscriptionDelta; +import org.psesquared.server.subscriptions.api.controller.SubscriptionTitles; +import org.springframework.beans.factory.annotation.Autowired; + +public class SubscriptionServiceTests extends BaseTest { + + @Autowired + SubscriptionService subscriptionService; + + @Test + public void uploadSubscriptionsTest() { + String username = "testUser0"; + assertDoesNotThrow(() -> authenticationDao.findByUsername(username).orElseThrow()); + String newSubscriptionUrl1 = new File("testfeeds/newTestSubscription1.xml").toURI().toString(); + String newSubscriptionUrl2 = new File("testfeeds/newTestSubscription2.xml").toURI().toString(); + List<String> subscriptionStrings = List.of(newSubscriptionUrl1, newSubscriptionUrl2); + + subscriptionService.uploadSubscriptions(username, subscriptionStrings); + List<SubscriptionAction> subscriptionActions = subscriptionActionDao.findByUserUsernameAndAddedTrue(username); + int size = subscriptionActions.size(); + assertEquals(numberOfSubscriptionsPerUser + 2, size); + + subscriptionActions.get(0).setAdded(false); + subscriptionActionDao.save(subscriptionActions.get(0)); + subscriptionActions = subscriptionActionDao.findByUserUsernameAndAddedTrue(username); + size = subscriptionActions.size(); + assertEquals(numberOfSubscriptionsPerUser + 1, size); + + String testPodcast0Url = new File("testfeeds/testPodcast0.xml").toURI().toString(); + List<String> subscriptionString = List.of(testPodcast0Url); + subscriptionService.uploadSubscriptions(username, subscriptionString); + subscriptionActions = subscriptionActionDao.findByUserUsernameAndAddedTrue(username); + size = subscriptionActions.size(); + assertEquals(numberOfSubscriptionsPerUser + 2, size); + } + + @Test + public void getSubscriptionsTest() { + String username = "testUser0"; + List<String> subscriptionStrings = subscriptionService.getSubscriptions(username); + assertEquals(subscriptionStrings.size(), numberOfSubscriptionsPerUser); + } + + @Test + public void applySubscriptionDeltaTest() { + String username = "testUser0"; + String subscriptionString = new File("testfeeds/testPodcast0.xml").toURI().toString();; + // Überprüfen, ob Anzahl der Subscriptions des Users mit der definierten Anzahl + // übereinstimmt + assertEquals(subscriptionService.getSubscriptions(username).size(), numberOfSubscriptionsPerUser); + // Subscription mithilfe des Subscription-Deltas entfernen + subscriptionService.applySubscriptionDelta(username, + new SubscriptionDelta(List.of(), List.of(subscriptionString))); + assertEquals(subscriptionService.getSubscriptions(username).size(), numberOfSubscriptionsPerUser - 1); + // Subscription mithilfe des Subscription-Deltas wieder hinzufügen + subscriptionService.applySubscriptionDelta(username, + new SubscriptionDelta(List.of(subscriptionString), List.of())); + assertEquals(subscriptionService.getSubscriptions(username).size(), numberOfSubscriptionsPerUser); + } + + @Test + public void getSubscriptionDeltaTest() { + String username = "testUser0"; + String subscriptionString = new File("testfeeds/testPodcast0.xml").toURI().toString(); + long since = 0; + // Überprüfen, ob Anzahl der Subscriptions des Users mit der definierten Anzahl + // übereinstimmt + SubscriptionDelta subscriptionDelta = subscriptionService.getSubscriptionDelta(username, since); + assertEquals(subscriptionDelta.getAdd().size(), numberOfSubscriptionsPerUser); + assertEquals(0, subscriptionDelta.getRemove().size()); + // Subscription mithilfe des Subscription-Deltas entfernen + subscriptionService.applySubscriptionDelta(username, + new SubscriptionDelta(List.of(), List.of(subscriptionString))); + subscriptionDelta = subscriptionService.getSubscriptionDelta(username, since); + assertEquals(subscriptionDelta.getAdd().size(), numberOfSubscriptionsPerUser - 1); + assertEquals(1, subscriptionDelta.getRemove().size()); + // Subscription mithilfe des Subscription-Deltas wieder hinzufügen + subscriptionService.applySubscriptionDelta(username, + new SubscriptionDelta(List.of(subscriptionString), List.of())); + + subscriptionDelta = subscriptionService.getSubscriptionDelta(username, since); + List<SubscriptionAction> subscriptionActions = subscriptionActionDao + .findByUserUsernameAndTimestampGreaterThanEqual(username, since); + LocalDateTime deltaTime = LocalDateTime + .ofEpochSecond(subscriptionDelta.getTimestamp(), 0, ZoneOffset.UTC); + LocalDateTime maxTime = deltaTime; + for (SubscriptionAction subscriptionAction : subscriptionActions) { + + LocalDateTime subTime = LocalDateTime + .ofEpochSecond(subscriptionAction.getTimestamp(), 0, ZoneOffset.UTC); + if (maxTime.isBefore(subTime)) { + maxTime = subTime; + } + } + assertEquals(deltaTime, maxTime); + assertEquals(subscriptionDelta.getAdd().size(), numberOfSubscriptionsPerUser); + assertEquals(0, subscriptionDelta.getRemove().size()); + } + + @Test + public void getTitlesTest() { + String username = "testUser0"; + List<SubscriptionTitles> subscriptionTitlesList = subscriptionService.getTitles(username); + assertEquals(numberOfSubscriptionsPerUser, subscriptionTitlesList.size()); + for (SubscriptionTitles subscriptionTitles : subscriptionTitlesList) { + assertEquals(numberOfEpisodesPerSubscription, subscriptionTitles.episodes().size()); + Optional<Subscription> savedSubscription = + subscriptionDao.findByUrl(subscriptionTitles.subscription().getUrl()); + assertTrue(savedSubscription.isPresent()); + } + } +} |