mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
split client and server into maven modules
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.commafeed.backend;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FixedSizeSortedSetTest {
|
||||
|
||||
private static final Comparator<String> COMP = new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String o1, String o2) {
|
||||
return ObjectUtils.compare(o1, o2);
|
||||
}
|
||||
};
|
||||
|
||||
private FixedSizeSortedSet<String> set;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
set = new FixedSizeSortedSet<>(3, COMP);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSimpleAdd() {
|
||||
set.add("0");
|
||||
set.add("1");
|
||||
set.add("2");
|
||||
|
||||
Assertions.assertEquals("0", set.asList().get(0));
|
||||
Assertions.assertEquals("1", set.asList().get(1));
|
||||
Assertions.assertEquals("2", set.asList().get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsFull() {
|
||||
set.add("0");
|
||||
set.add("1");
|
||||
|
||||
Assertions.assertFalse(set.isFull());
|
||||
set.add("2");
|
||||
Assertions.assertTrue(set.isFull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOrder() {
|
||||
set.add("2");
|
||||
set.add("1");
|
||||
set.add("0");
|
||||
|
||||
Assertions.assertEquals("0", set.asList().get(0));
|
||||
Assertions.assertEquals("1", set.asList().get(1));
|
||||
Assertions.assertEquals("2", set.asList().get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEviction() {
|
||||
set.add("7");
|
||||
set.add("8");
|
||||
set.add("9");
|
||||
|
||||
set.add("0");
|
||||
set.add("1");
|
||||
set.add("2");
|
||||
|
||||
Assertions.assertEquals("0", set.asList().get(0));
|
||||
Assertions.assertEquals("1", set.asList().get(1));
|
||||
Assertions.assertEquals("2", set.asList().get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCapacity() {
|
||||
set.add("0");
|
||||
set.add("1");
|
||||
set.add("2");
|
||||
set.add("3");
|
||||
|
||||
Assertions.assertEquals(3, set.asList().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLast() {
|
||||
set.add("0");
|
||||
set.add("1");
|
||||
set.add("2");
|
||||
|
||||
Assertions.assertEquals("2", set.last());
|
||||
|
||||
set.add("3");
|
||||
|
||||
Assertions.assertEquals("2", set.last());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.commafeed.backend.feed;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FeedUtilsTest {
|
||||
|
||||
@Test
|
||||
void testNormalization() {
|
||||
String urla1 = "http://example.com/hello?a=1&b=2";
|
||||
String urla2 = "http://www.example.com/hello?a=1&b=2";
|
||||
String urla3 = "http://EXAmPLe.com/HELLo?a=1&b=2";
|
||||
String urla4 = "http://example.com/hello?b=2&a=1";
|
||||
String urla5 = "https://example.com/hello?a=1&b=2";
|
||||
|
||||
String urlb1 = "http://ftr.fivefilters.org/makefulltextfeed.php?url=http%3A%2F%2Ffeeds.howtogeek.com%2FHowToGeek&max=10&summary=1";
|
||||
String urlb2 = "http://ftr.fivefilters.org/makefulltextfeed.php?url=http://feeds.howtogeek.com/HowToGeek&max=10&summary=1";
|
||||
|
||||
String urlc1 = "http://feeds.feedburner.com/Frandroid";
|
||||
String urlc2 = "http://feeds2.feedburner.com/frandroid";
|
||||
String urlc3 = "http://feedproxy.google.com/frandroid";
|
||||
String urlc4 = "http://feeds.feedburner.com/Frandroid/";
|
||||
String urlc5 = "http://feeds.feedburner.com/Frandroid?format=rss";
|
||||
|
||||
String urld1 = "http://fivefilters.org/content-only/makefulltextfeed.php?url=http://feeds.feedburner.com/Frandroid";
|
||||
String urld2 = "http://fivefilters.org/content-only/makefulltextfeed.php?url=http://feeds2.feedburner.com/Frandroid";
|
||||
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla2));
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla3));
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla4));
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla5));
|
||||
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urlb1), FeedUtils.normalizeURL(urlb2));
|
||||
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc2));
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc3));
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc4));
|
||||
Assertions.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc5));
|
||||
|
||||
Assertions.assertNotEquals(FeedUtils.normalizeURL(urld1), FeedUtils.normalizeURL(urld2));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToAbsoluteUrl() {
|
||||
String expected = "http://a.com/blog/entry/1";
|
||||
|
||||
// usual cases
|
||||
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("http://a.com/blog/entry/1", "http://a.com/feed/", "http://a.com/feed/"));
|
||||
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("http://a.com/blog/entry/1", "http://a.com/feed", "http://a.com/feed"));
|
||||
|
||||
// relative links
|
||||
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("../blog/entry/1", "http://a.com/feed/", "http://a.com/feed/"));
|
||||
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("../blog/entry/1", "feed.xml", "http://a.com/feed/feed.xml"));
|
||||
|
||||
// root-relative links
|
||||
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("/blog/entry/1", "/feed", "http://a.com/feed"));
|
||||
|
||||
// real cases
|
||||
Assertions.assertEquals("https://github.com/erusev/parsedown/releases/tag/1.3.0", FeedUtils.toAbsoluteUrl(
|
||||
"/erusev/parsedown/releases/tag/1.3.0", "/erusev/parsedown/releases", "https://github.com/erusev/parsedown/tags.atom"));
|
||||
Assertions.assertEquals("http://ergoemacs.org/emacs/elisp_all_about_lines.html",
|
||||
FeedUtils.toAbsoluteUrl("elisp_all_about_lines.html", "blog.xml", "http://ergoemacs.org/emacs/blog.xml"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExtractDeclaredEncoding() {
|
||||
Assertions.assertNull(FeedUtils.extractDeclaredEncoding("<?xml ?>".getBytes()));
|
||||
Assertions.assertNull(FeedUtils.extractDeclaredEncoding("<feed></feed>".getBytes()));
|
||||
Assertions.assertEquals("UTF-8", FeedUtils.extractDeclaredEncoding("<?xml encoding=\"UTF-8\" ?>".getBytes()));
|
||||
Assertions.assertEquals("UTF-8", FeedUtils.extractDeclaredEncoding("<?xml encoding='UTF-8' ?>".getBytes()));
|
||||
Assertions.assertEquals("UTF-8", FeedUtils.extractDeclaredEncoding("<?xml encoding='UTF-8'?>".getBytes()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReplaceHtmlEntitiesWithNumericEntities() {
|
||||
String source = "<source>T´l´phone ′</source>";
|
||||
Assertions.assertEquals("<source>T´l´phone ′</source>", FeedUtils.replaceHtmlEntitiesWithNumericEntities(source));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveTrailingSlash() {
|
||||
final String url = "http://localhost/";
|
||||
final String result = FeedUtils.removeTrailingSlash(url);
|
||||
Assertions.assertEquals("http://localhost", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveTrailingSlashLastSlashOnly() {
|
||||
final String url = "http://localhost//";
|
||||
final String result = FeedUtils.removeTrailingSlash(url);
|
||||
Assertions.assertEquals("http://localhost/", result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.commafeed.backend.opml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.rometools.opml.feed.opml.Opml;
|
||||
import com.rometools.opml.feed.opml.Outline;
|
||||
|
||||
class OPMLExporterTest {
|
||||
|
||||
@Mock
|
||||
private FeedCategoryDAO feedCategoryDAO;
|
||||
@Mock
|
||||
private FeedSubscriptionDAO feedSubscriptionDAO;
|
||||
|
||||
private final User user = new User();
|
||||
|
||||
private final FeedCategory cat1 = new FeedCategory();
|
||||
private final FeedCategory cat2 = new FeedCategory();
|
||||
|
||||
private final FeedSubscription rootFeed = newFeedSubscription("rootFeed", "rootFeed.com");
|
||||
private final FeedSubscription cat1Feed = newFeedSubscription("cat1Feed", "cat1Feed.com");
|
||||
private final FeedSubscription cat2Feed = newFeedSubscription("cat2Feed", "cat2Feed.com");
|
||||
|
||||
private final List<FeedCategory> categories = new ArrayList<>();
|
||||
private final List<FeedSubscription> subscriptions = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
|
||||
user.setName("John Doe");
|
||||
|
||||
cat1.setId(1L);
|
||||
cat1.setName("cat1");
|
||||
cat1.setParent(null);
|
||||
cat1.setChildren(new HashSet<FeedCategory>());
|
||||
cat1.setSubscriptions(new HashSet<FeedSubscription>());
|
||||
|
||||
cat2.setId(2L);
|
||||
cat2.setName("cat2");
|
||||
cat2.setParent(cat1);
|
||||
cat2.setChildren(new HashSet<FeedCategory>());
|
||||
cat2.setSubscriptions(new HashSet<FeedSubscription>());
|
||||
|
||||
cat1.getChildren().add(cat2);
|
||||
|
||||
rootFeed.setCategory(null);
|
||||
cat1Feed.setCategory(cat1);
|
||||
cat2Feed.setCategory(cat2);
|
||||
|
||||
cat1.getSubscriptions().add(cat1Feed);
|
||||
cat2.getSubscriptions().add(cat2Feed);
|
||||
|
||||
categories.add(cat1);
|
||||
categories.add(cat2);
|
||||
|
||||
subscriptions.add(rootFeed);
|
||||
subscriptions.add(cat1Feed);
|
||||
subscriptions.add(cat2Feed);
|
||||
}
|
||||
|
||||
private Feed newFeed(String url) {
|
||||
Feed feed = new Feed();
|
||||
feed.setUrl(url);
|
||||
return feed;
|
||||
}
|
||||
|
||||
private FeedSubscription newFeedSubscription(String title, String url) {
|
||||
FeedSubscription feedSubscription = new FeedSubscription();
|
||||
feedSubscription.setTitle(title);
|
||||
feedSubscription.setFeed(newFeed(url));
|
||||
return feedSubscription;
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatesOpmlCorrectly() {
|
||||
Mockito.when(feedCategoryDAO.findAll(user)).thenReturn(categories);
|
||||
Mockito.when(feedSubscriptionDAO.findAll(user)).thenReturn(subscriptions);
|
||||
|
||||
Opml opml = new OPMLExporter(feedCategoryDAO, feedSubscriptionDAO).export(user);
|
||||
|
||||
List<Outline> rootOutlines = opml.getOutlines();
|
||||
Assertions.assertEquals(2, rootOutlines.size());
|
||||
Assertions.assertTrue(containsCategory(rootOutlines, "cat1"));
|
||||
Assertions.assertTrue(containsFeed(rootOutlines, "rootFeed", "rootFeed.com"));
|
||||
|
||||
Outline cat1Outline = getCategoryOutline(rootOutlines, "cat1");
|
||||
List<Outline> cat1Children = cat1Outline.getChildren();
|
||||
Assertions.assertEquals(2, cat1Children.size());
|
||||
Assertions.assertTrue(containsCategory(cat1Children, "cat2"));
|
||||
Assertions.assertTrue(containsFeed(cat1Children, "cat1Feed", "cat1Feed.com"));
|
||||
|
||||
Outline cat2Outline = getCategoryOutline(cat1Children, "cat2");
|
||||
List<Outline> cat2Children = cat2Outline.getChildren();
|
||||
Assertions.assertEquals(1, cat2Children.size());
|
||||
Assertions.assertTrue(containsFeed(cat2Children, "cat2Feed", "cat2Feed.com"));
|
||||
}
|
||||
|
||||
private boolean containsCategory(List<Outline> outlines, String category) {
|
||||
for (Outline o : outlines) {
|
||||
if (!"rss".equals(o.getType())) {
|
||||
if (category.equals(o.getTitle())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean containsFeed(List<Outline> outlines, String title, String url) {
|
||||
for (Outline o : outlines) {
|
||||
if ("rss".equals(o.getType())) {
|
||||
if (title.equals(o.getTitle()) && o.getAttributeValue("xmlUrl").equals(url)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Outline getCategoryOutline(List<Outline> outlines, String title) {
|
||||
for (Outline o : outlines) {
|
||||
if (o.getTitle().equals(title)) {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.commafeed.backend.opml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import com.commafeed.backend.cache.CacheService;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.service.FeedSubscriptionService;
|
||||
|
||||
class OPMLImporterTest {
|
||||
|
||||
@Test
|
||||
void testOpmlV10() throws IOException {
|
||||
testOpmlVersion("/opml/opml_v1.0.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOpmlV11() throws IOException {
|
||||
testOpmlVersion("/opml/opml_v1.1.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOpmlV20() throws IOException {
|
||||
testOpmlVersion("/opml/opml_v2.0.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOpmlNoVersion() throws IOException {
|
||||
testOpmlVersion("/opml/opml_noversion.xml");
|
||||
}
|
||||
|
||||
private void testOpmlVersion(String fileName) throws IOException {
|
||||
FeedCategoryDAO feedCategoryDAO = Mockito.mock(FeedCategoryDAO.class);
|
||||
FeedSubscriptionService feedSubscriptionService = Mockito.mock(FeedSubscriptionService.class);
|
||||
CacheService cacheService = Mockito.mock(CacheService.class);
|
||||
User user = Mockito.mock(User.class);
|
||||
|
||||
String xml = IOUtils.toString(getClass().getResourceAsStream(fileName), StandardCharsets.UTF_8);
|
||||
|
||||
OPMLImporter importer = new OPMLImporter(feedCategoryDAO, feedSubscriptionService, cacheService);
|
||||
importer.importOpml(user, xml);
|
||||
|
||||
Mockito.verify(feedSubscriptionService)
|
||||
.subscribe(Mockito.eq(user), Mockito.anyString(), Mockito.anyString(), Mockito.any(FeedCategory.class), Mockito.anyInt());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.commafeed.backend.service;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryContent;
|
||||
import com.commafeed.backend.service.FeedEntryFilteringService.FeedEntryFilterException;
|
||||
|
||||
class FeedEntryFilteringServiceTest {
|
||||
|
||||
private FeedEntryFilteringService service;
|
||||
|
||||
private FeedEntry entry;
|
||||
private FeedEntryContent content;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
service = new FeedEntryFilteringService();
|
||||
|
||||
entry = new FeedEntry();
|
||||
entry.setUrl("https://github.com/Athou/commafeed");
|
||||
|
||||
content = new FeedEntryContent();
|
||||
content.setAuthor("Athou");
|
||||
content.setTitle("Merge pull request #662 from Athou/dw8");
|
||||
content.setContent("Merge pull request #662 from Athou/dw8");
|
||||
entry.setContent(content);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyFilterMatchesFilter() throws FeedEntryFilterException {
|
||||
Assertions.assertTrue(service.filterMatchesEntry(null, entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankFilterMatchesFilter() throws FeedEntryFilterException {
|
||||
Assertions.assertTrue(service.filterMatchesEntry("", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleExpression() throws FeedEntryFilterException {
|
||||
Assertions.assertTrue(service.filterMatchesEntry("author.toString() eq 'athou'", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void newIsDisabled() throws FeedEntryFilterException {
|
||||
Assertions.assertThrows(FeedEntryFilterException.class,
|
||||
() -> service.filterMatchesEntry("null eq new ('java.lang.String', 'athou')", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getClassMethodIsDisabled() throws FeedEntryFilterException {
|
||||
Assertions.assertThrows(FeedEntryFilterException.class, () -> service.filterMatchesEntry("null eq ''.getClass()", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dotClassIsDisabled() throws FeedEntryFilterException {
|
||||
Assertions.assertTrue(service.filterMatchesEntry("null eq ''.class", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotLoopForever() throws FeedEntryFilterException {
|
||||
Assertions.assertThrows(FeedEntryFilterException.class, () -> service.filterMatchesEntry("while(true) {}", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handlesNullCorrectly() throws FeedEntryFilterException {
|
||||
entry.setUrl(null);
|
||||
entry.setContent(new FeedEntryContent());
|
||||
Assertions.assertDoesNotThrow(() -> service.filterMatchesEntry("author eq 'athou'", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void incorrectScriptThrowsException() throws FeedEntryFilterException {
|
||||
Assertions.assertThrows(FeedEntryFilterException.class, () -> service.filterMatchesEntry("aa eqz bb", entry));
|
||||
}
|
||||
|
||||
@Test
|
||||
void incorrectReturnTypeThrowsException() throws FeedEntryFilterException {
|
||||
Assertions.assertThrows(FeedEntryFilterException.class, () -> service.filterMatchesEntry("1", entry));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.commafeed.backend.service;
|
||||
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockserver.client.MockServerClient;
|
||||
import org.mockserver.junit.jupiter.MockServerExtension;
|
||||
import org.mockserver.model.HttpRequest;
|
||||
import org.mockserver.model.HttpResponse;
|
||||
import org.mockserver.model.MediaType;
|
||||
|
||||
import com.commafeed.CommaFeedConfiguration;
|
||||
import com.commafeed.backend.feed.FeedQueues;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
|
||||
@ExtendWith(MockServerExtension.class)
|
||||
class PubSubServiceTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private CommaFeedConfiguration config;
|
||||
|
||||
@Mock
|
||||
private FeedQueues queues;
|
||||
|
||||
@Mock
|
||||
private Feed feed;
|
||||
|
||||
private MockServerClient client;
|
||||
private PubSubService underTest;
|
||||
|
||||
@BeforeEach
|
||||
public void init(MockServerClient client) {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
|
||||
this.client = client;
|
||||
this.client.reset();
|
||||
|
||||
this.underTest = new PubSubService(config, queues);
|
||||
|
||||
Integer port = client.getPort();
|
||||
String hubUrl = String.format("http://localhost:%s/hub", port);
|
||||
|
||||
// setup feed
|
||||
feed = Mockito.mock(Feed.class);
|
||||
Mockito.when(feed.getPushHub()).thenReturn(hubUrl);
|
||||
Mockito.when(feed.getPushTopic()).thenReturn("foo");
|
||||
|
||||
// setup config
|
||||
Mockito.when(config.getApplicationSettings().getPublicUrl()).thenReturn(hubUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void subscribe200() {
|
||||
// Arrange
|
||||
client.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(200));
|
||||
|
||||
// Act
|
||||
underTest.subscribe(feed);
|
||||
|
||||
// Assert
|
||||
client.verify(HttpRequest.request()
|
||||
.withContentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.withHeader(HttpHeaders.USER_AGENT, "CommaFeed")
|
||||
.withMethod("POST")
|
||||
.withPath("/hub"));
|
||||
Mockito.verify(feed, Mockito.never()).setPushTopic(Mockito.anyString());
|
||||
Mockito.verifyNoInteractions(queues);
|
||||
}
|
||||
|
||||
@Test
|
||||
void subscribe400WithPushpressError() {
|
||||
// Arrange
|
||||
client.when(HttpRequest.request().withMethod("POST"))
|
||||
.respond(HttpResponse.response().withStatusCode(400).withBody(" is value is not allowed. You may only subscribe to"));
|
||||
|
||||
// Act
|
||||
underTest.subscribe(feed);
|
||||
|
||||
// Assert
|
||||
Mockito.verify(feed).setPushTopic(Mockito.anyString());
|
||||
Mockito.verify(queues).giveBack(feed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void subscribe400WithoutPushpressError() {
|
||||
// Arrange
|
||||
client.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(400));
|
||||
|
||||
// Act
|
||||
underTest.subscribe(feed);
|
||||
|
||||
// Assert
|
||||
Mockito.verify(feed, Mockito.never()).setPushTopic(Mockito.anyString());
|
||||
Mockito.verifyNoInteractions(queues);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package com.commafeed.backend.service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import com.commafeed.CommaFeedConfiguration;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.dao.UserDAO;
|
||||
import com.commafeed.backend.dao.UserRoleDAO;
|
||||
import com.commafeed.backend.dao.UserSettingsDAO;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.service.internal.PostLoginActivities;
|
||||
|
||||
class UserServiceTest {
|
||||
|
||||
private static final byte[] SALT = new byte[] { 1, 2, 3 };
|
||||
private static final byte[] ENCRYPTED_PASSWORD = new byte[] { 5, 6, 7 };
|
||||
|
||||
@Mock
|
||||
private CommaFeedConfiguration commaFeedConfiguration;
|
||||
@Mock
|
||||
private FeedCategoryDAO feedCategoryDAO;
|
||||
@Mock
|
||||
private FeedSubscriptionDAO feedSubscriptionDAO;
|
||||
@Mock
|
||||
private UserDAO userDAO;
|
||||
@Mock
|
||||
private UserSettingsDAO userSettingsDAO;
|
||||
@Mock
|
||||
private UserRoleDAO userRoleDAO;
|
||||
@Mock
|
||||
private PasswordEncryptionService passwordEncryptionService;
|
||||
@Mock
|
||||
private PostLoginActivities postLoginActivities;
|
||||
|
||||
private User disabledUser;
|
||||
private User normalUser;
|
||||
|
||||
private UserService userService;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
|
||||
userService = new UserService(feedCategoryDAO, feedSubscriptionDAO, userDAO, userRoleDAO, userSettingsDAO,
|
||||
passwordEncryptionService, commaFeedConfiguration, postLoginActivities);
|
||||
|
||||
disabledUser = new User();
|
||||
disabledUser.setDisabled(true);
|
||||
|
||||
normalUser = new User();
|
||||
normalUser.setDisabled(false);
|
||||
normalUser.setSalt(SALT);
|
||||
normalUser.setPassword(ENCRYPTED_PASSWORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldNotReturnUserObjectWhenGivenNullNameOrEmail() {
|
||||
Optional<User> user = userService.login(null, "password");
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldNotReturnUserObjectWhenGivenNullPassword() {
|
||||
Optional<User> user = userService.login("testusername", null);
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldLookupUserByName() {
|
||||
userService.login("test", "password");
|
||||
Mockito.verify(userDAO).findByName("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldLookupUserByEmailIfLookupByNameFailed() {
|
||||
Mockito.when(userDAO.findByName("test@test.com")).thenReturn(null);
|
||||
userService.login("test@test.com", "password");
|
||||
Mockito.verify(userDAO).findByEmail("test@test.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldNotReturnUserObjectIfCouldNotFindUserByNameOrEmail() {
|
||||
Mockito.when(userDAO.findByName("test@test.com")).thenReturn(null);
|
||||
Mockito.when(userDAO.findByEmail("test@test.com")).thenReturn(null);
|
||||
|
||||
Optional<User> user = userService.login("test@test.com", "password");
|
||||
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldNotReturnUserObjectIfUserIsDisabled() {
|
||||
Mockito.when(userDAO.findByName("test")).thenReturn(disabledUser);
|
||||
Optional<User> user = userService.login("test", "password");
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldTryToAuthenticateUserWhoIsNotDisabled() {
|
||||
Mockito.when(userDAO.findByName("test")).thenReturn(normalUser);
|
||||
Mockito.when(passwordEncryptionService.authenticate(Mockito.anyString(), Mockito.any(byte[].class), Mockito.any(byte[].class)))
|
||||
.thenReturn(false);
|
||||
|
||||
userService.login("test", "password");
|
||||
|
||||
Mockito.verify(passwordEncryptionService).authenticate("password", ENCRYPTED_PASSWORD, SALT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldNotReturnUserObjectOnUnsuccessfulAuthentication() {
|
||||
Mockito.when(userDAO.findByName("test")).thenReturn(normalUser);
|
||||
Mockito.when(passwordEncryptionService.authenticate(Mockito.anyString(), Mockito.any(byte[].class), Mockito.any(byte[].class)))
|
||||
.thenReturn(false);
|
||||
|
||||
Optional<User> authenticatedUser = userService.login("test", "password");
|
||||
|
||||
Assertions.assertFalse(authenticatedUser.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldExecutePostLoginActivitiesForUserOnSuccessfulAuthentication() {
|
||||
Mockito.when(userDAO.findByName("test")).thenReturn(normalUser);
|
||||
Mockito.when(passwordEncryptionService.authenticate(Mockito.anyString(), Mockito.any(byte[].class), Mockito.any(byte[].class)))
|
||||
.thenReturn(true);
|
||||
Mockito.doNothing().when(postLoginActivities).executeFor(Mockito.any(User.class));
|
||||
|
||||
userService.login("test", "password");
|
||||
|
||||
Mockito.verify(postLoginActivities).executeFor(normalUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callingLoginShouldReturnUserObjectOnSuccessfulAuthentication() {
|
||||
Mockito.when(userDAO.findByName("test")).thenReturn(normalUser);
|
||||
Mockito.when(passwordEncryptionService.authenticate(Mockito.anyString(), Mockito.any(byte[].class), Mockito.any(byte[].class)))
|
||||
.thenReturn(true);
|
||||
Mockito.doNothing().when(postLoginActivities).executeFor(Mockito.any(User.class));
|
||||
|
||||
Optional<User> authenticatedUser = userService.login("test", "password");
|
||||
|
||||
Assertions.assertTrue(authenticatedUser.isPresent());
|
||||
Assertions.assertEquals(normalUser, authenticatedUser.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiLoginShouldNotReturnUserIfApikeyNull() {
|
||||
Optional<User> user = userService.login(null);
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiLoginShouldLookupUserByApikey() {
|
||||
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(null);
|
||||
userService.login("apikey");
|
||||
Mockito.verify(userDAO).findByApiKey("apikey");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiLoginShouldNotReturnUserIfUserNotFoundFromLookupByApikey() {
|
||||
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(null);
|
||||
Optional<User> user = userService.login("apikey");
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiLoginShouldNotReturnUserIfUserFoundFromApikeyLookupIsDisabled() {
|
||||
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(disabledUser);
|
||||
Optional<User> user = userService.login("apikey");
|
||||
Assertions.assertFalse(user.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiLoginShouldPerformPostLoginActivitiesIfUserFoundFromApikeyLookupNotDisabled() {
|
||||
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(normalUser);
|
||||
userService.login("apikey");
|
||||
Mockito.verify(postLoginActivities).executeFor(normalUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiLoginShouldReturnUserIfUserFoundFromApikeyLookupNotDisabled() {
|
||||
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(normalUser);
|
||||
Optional<User> returnedUser = userService.login("apikey");
|
||||
Assertions.assertEquals(normalUser, returnedUser.get());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user