migrate to junit5

This commit is contained in:
Athou
2022-07-15 19:28:11 +02:00
parent a0190143fe
commit 9ab52aeaf2
11 changed files with 214 additions and 200 deletions

23
pom.xml
View File

@@ -1,5 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.commafeed</groupId>
@@ -38,6 +37,16 @@
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
@@ -501,8 +510,8 @@
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -512,9 +521,9 @@
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-rule</artifactId>
<version>5.11.1</version>
<artifactId>mockserver-junit-jupiter</artifactId>
<version>5.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>

View File

@@ -3,11 +3,11 @@ package com.commafeed.backend;
import java.util.Comparator;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class FixedSizeSortedSetTest {
class FixedSizeSortedSetTest {
private static final Comparator<String> COMP = new Comparator<String>() {
@Override
@@ -18,45 +18,45 @@ public class FixedSizeSortedSetTest {
private FixedSizeSortedSet<String> set;
@Before
@BeforeEach
public void init() {
set = new FixedSizeSortedSet<>(3, COMP);
}
@Test
public void testSimpleAdd() {
void testSimpleAdd() {
set.add("0");
set.add("1");
set.add("2");
Assert.assertEquals("0", set.asList().get(0));
Assert.assertEquals("1", set.asList().get(1));
Assert.assertEquals("2", set.asList().get(2));
Assertions.assertEquals("0", set.asList().get(0));
Assertions.assertEquals("1", set.asList().get(1));
Assertions.assertEquals("2", set.asList().get(2));
}
@Test
public void testIsFull() {
void testIsFull() {
set.add("0");
set.add("1");
Assert.assertFalse(set.isFull());
Assertions.assertFalse(set.isFull());
set.add("2");
Assert.assertTrue(set.isFull());
Assertions.assertTrue(set.isFull());
}
@Test
public void testOrder() {
void testOrder() {
set.add("2");
set.add("1");
set.add("0");
Assert.assertEquals("0", set.asList().get(0));
Assert.assertEquals("1", set.asList().get(1));
Assert.assertEquals("2", set.asList().get(2));
Assertions.assertEquals("0", set.asList().get(0));
Assertions.assertEquals("1", set.asList().get(1));
Assertions.assertEquals("2", set.asList().get(2));
}
@Test
public void testEviction() {
void testEviction() {
set.add("7");
set.add("8");
set.add("9");
@@ -65,31 +65,31 @@ public class FixedSizeSortedSetTest {
set.add("1");
set.add("2");
Assert.assertEquals("0", set.asList().get(0));
Assert.assertEquals("1", set.asList().get(1));
Assert.assertEquals("2", set.asList().get(2));
Assertions.assertEquals("0", set.asList().get(0));
Assertions.assertEquals("1", set.asList().get(1));
Assertions.assertEquals("2", set.asList().get(2));
}
@Test
public void testCapacity() {
void testCapacity() {
set.add("0");
set.add("1");
set.add("2");
set.add("3");
Assert.assertEquals(3, set.asList().size());
Assertions.assertEquals(3, set.asList().size());
}
@Test
public void testLast() {
void testLast() {
set.add("0");
set.add("1");
set.add("2");
Assert.assertEquals("2", set.last());
Assertions.assertEquals("2", set.last());
set.add("3");
Assert.assertEquals("2", set.last());
Assertions.assertEquals("2", set.last());
}
}

View File

@@ -1,12 +1,12 @@
package com.commafeed.backend.feed;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class FeedUtilsTest {
class FeedUtilsTest {
@Test
public void testNormalization() {
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";
@@ -25,72 +25,72 @@ public class FeedUtilsTest {
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";
Assert.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla2));
Assert.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla3));
Assert.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla4));
Assert.assertEquals(FeedUtils.normalizeURL(urla1), FeedUtils.normalizeURL(urla5));
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));
Assert.assertEquals(FeedUtils.normalizeURL(urlb1), FeedUtils.normalizeURL(urlb2));
Assertions.assertEquals(FeedUtils.normalizeURL(urlb1), FeedUtils.normalizeURL(urlb2));
Assert.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc2));
Assert.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc3));
Assert.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc4));
Assert.assertEquals(FeedUtils.normalizeURL(urlc1), FeedUtils.normalizeURL(urlc5));
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));
Assert.assertNotEquals(FeedUtils.normalizeURL(urld1), FeedUtils.normalizeURL(urld2));
Assertions.assertNotEquals(FeedUtils.normalizeURL(urld1), FeedUtils.normalizeURL(urld2));
}
@Test
public void testToAbsoluteUrl() {
void testToAbsoluteUrl() {
String expected = "http://a.com/blog/entry/1";
// usual cases
Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("http://a.com/blog/entry/1", "http://a.com/feed/", "http://a.com/feed/"));
Assert.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/"));
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("http://a.com/blog/entry/1", "http://a.com/feed", "http://a.com/feed"));
// relative links
Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("../blog/entry/1", "http://a.com/feed/", "http://a.com/feed/"));
Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("../blog/entry/1", "feed.xml", "http://a.com/feed/feed.xml"));
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
Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("/blog/entry/1", "/feed", "http://a.com/feed"));
Assertions.assertEquals(expected, FeedUtils.toAbsoluteUrl("/blog/entry/1", "/feed", "http://a.com/feed"));
// real cases
Assert.assertEquals("https://github.com/erusev/parsedown/releases/tag/1.3.0", FeedUtils.toAbsoluteUrl(
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"));
Assert.assertEquals("http://ergoemacs.org/emacs/elisp_all_about_lines.html",
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
public void testExtractDeclaredEncoding() {
Assert.assertNull(FeedUtils.extractDeclaredEncoding("<?xml ?>".getBytes()));
Assert.assertNull(FeedUtils.extractDeclaredEncoding("<feed></feed>".getBytes()));
Assert.assertEquals("UTF-8", FeedUtils.extractDeclaredEncoding("<?xml encoding=\"UTF-8\" ?>".getBytes()));
Assert.assertEquals("UTF-8", FeedUtils.extractDeclaredEncoding("<?xml encoding='UTF-8' ?>".getBytes()));
Assert.assertEquals("UTF-8", FeedUtils.extractDeclaredEncoding("<?xml encoding='UTF-8'?>".getBytes()));
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
public void testReplaceHtmlEntitiesWithNumericEntities() {
void testReplaceHtmlEntitiesWithNumericEntities() {
String source = "<source>T&acute;l&acute;phone &prime;</source>";
Assert.assertEquals("<source>T&#180;l&#180;phone &#8242;</source>", FeedUtils.replaceHtmlEntitiesWithNumericEntities(source));
Assertions.assertEquals("<source>T&#180;l&#180;phone &#8242;</source>", FeedUtils.replaceHtmlEntitiesWithNumericEntities(source));
}
@Test
public void testRemoveTrailingSlash() {
void testRemoveTrailingSlash() {
final String url = "http://localhost/";
final String result = FeedUtils.removeTrailingSlash(url);
Assert.assertEquals("http://localhost", result);
Assertions.assertEquals("http://localhost", result);
}
@Test
public void testRemoveTrailingSlashLastSlashOnly() {
void testRemoveTrailingSlashLastSlashOnly() {
final String url = "http://localhost//";
final String result = FeedUtils.removeTrailingSlash(url);
Assert.assertEquals("http://localhost/", result);
Assertions.assertEquals("http://localhost/", result);
}
}

View File

@@ -4,9 +4,9 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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;
@@ -20,7 +20,7 @@ import com.commafeed.backend.model.User;
import com.rometools.opml.feed.opml.Opml;
import com.rometools.opml.feed.opml.Outline;
public class OPMLExporterTest {
class OPMLExporterTest {
@Mock
private FeedCategoryDAO feedCategoryDAO;
@@ -39,7 +39,7 @@ public class OPMLExporterTest {
private final List<FeedCategory> categories = new ArrayList<>();
private final List<FeedSubscription> subscriptions = new ArrayList<>();
@Before
@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
@@ -88,27 +88,27 @@ public class OPMLExporterTest {
}
@Test
public void generatesOpmlCorrectly() {
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();
Assert.assertEquals(2, rootOutlines.size());
Assert.assertTrue(containsCategory(rootOutlines, "cat1"));
Assert.assertTrue(containsFeed(rootOutlines, "rootFeed", "rootFeed.com"));
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();
Assert.assertEquals(2, cat1Children.size());
Assert.assertTrue(containsCategory(cat1Children, "cat2"));
Assert.assertTrue(containsFeed(cat1Children, "cat1Feed", "cat1Feed.com"));
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();
Assert.assertEquals(1, cat2Children.size());
Assert.assertTrue(containsFeed(cat2Children, "cat2Feed", "cat2Feed.com"));
Assertions.assertEquals(1, cat2Children.size());
Assertions.assertTrue(containsFeed(cat2Children, "cat2Feed", "cat2Feed.com"));
}
private boolean containsCategory(List<Outline> outlines, String category) {

View File

@@ -4,7 +4,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.commafeed.backend.cache.CacheService;
@@ -13,25 +13,25 @@ import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.User;
import com.commafeed.backend.service.FeedSubscriptionService;
public class OPMLImporterTest {
class OPMLImporterTest {
@Test
public void testOpmlV10() throws IOException {
void testOpmlV10() throws IOException {
testOpmlVersion("/opml/opml_v1.0.xml");
}
@Test
public void testOpmlV11() throws IOException {
void testOpmlV11() throws IOException {
testOpmlVersion("/opml/opml_v1.1.xml");
}
@Test
public void testOpmlV20() throws IOException {
void testOpmlV20() throws IOException {
testOpmlVersion("/opml/opml_v2.0.xml");
}
@Test
public void testOpmlNoVersion() throws IOException {
void testOpmlNoVersion() throws IOException {
testOpmlVersion("/opml/opml_noversion.xml");
}

View File

@@ -1,21 +1,21 @@
package com.commafeed.backend.service;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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;
public class FeedEntryFilteringServiceTest {
class FeedEntryFilteringServiceTest {
private FeedEntryFilteringService service;
private FeedEntry entry;
private FeedEntryContent content;
@Before
@BeforeEach
public void init() {
service = new FeedEntryFilteringService();
@@ -31,55 +31,56 @@ public class FeedEntryFilteringServiceTest {
}
@Test
public void emptyFilterMatchesFilter() throws FeedEntryFilterException {
Assert.assertTrue(service.filterMatchesEntry(null, entry));
void emptyFilterMatchesFilter() throws FeedEntryFilterException {
Assertions.assertTrue(service.filterMatchesEntry(null, entry));
}
@Test
public void blankFilterMatchesFilter() throws FeedEntryFilterException {
Assert.assertTrue(service.filterMatchesEntry("", entry));
void blankFilterMatchesFilter() throws FeedEntryFilterException {
Assertions.assertTrue(service.filterMatchesEntry("", entry));
}
@Test
public void simpleExpression() throws FeedEntryFilterException {
Assert.assertTrue(service.filterMatchesEntry("author.toString() eq 'athou'", entry));
}
@Test(expected = FeedEntryFilterException.class)
public void newIsDisabled() throws FeedEntryFilterException {
service.filterMatchesEntry("null eq new ('java.lang.String', 'athou')", entry);
}
@Test(expected = FeedEntryFilterException.class)
public void getClassMethodIsDisabled() throws FeedEntryFilterException {
service.filterMatchesEntry("null eq ''.getClass()", entry);
void simpleExpression() throws FeedEntryFilterException {
Assertions.assertTrue(service.filterMatchesEntry("author.toString() eq 'athou'", entry));
}
@Test
public void dotClassIsDisabled() throws FeedEntryFilterException {
Assert.assertTrue(service.filterMatchesEntry("null eq ''.class", entry));
}
@Test(expected = FeedEntryFilterException.class)
public void cannotLoopForever() throws FeedEntryFilterException {
service.filterMatchesEntry("while(true) {}", entry);
void newIsDisabled() throws FeedEntryFilterException {
Assertions.assertThrows(FeedEntryFilterException.class,
() -> service.filterMatchesEntry("null eq new ('java.lang.String', 'athou')", entry));
}
@Test
public void handlesNullCorrectly() throws FeedEntryFilterException {
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());
service.filterMatchesEntry("author eq 'athou'", entry);
Assertions.assertDoesNotThrow(() -> service.filterMatchesEntry("author eq 'athou'", entry));
}
@Test(expected = FeedEntryFilterException.class)
public void incorrectScriptThrowsException() throws FeedEntryFilterException {
service.filterMatchesEntry("aa eqz bb", entry);
@Test
void incorrectScriptThrowsException() throws FeedEntryFilterException {
Assertions.assertThrows(FeedEntryFilterException.class, () -> service.filterMatchesEntry("aa eqz bb", entry));
}
@Test(expected = FeedEntryFilterException.class)
public void incorrectReturnTypeThrowsException() throws FeedEntryFilterException {
service.filterMatchesEntry("1", entry);
@Test
void incorrectReturnTypeThrowsException() throws FeedEntryFilterException {
Assertions.assertThrows(FeedEntryFilterException.class, () -> service.filterMatchesEntry("1", entry));
}
}

View File

@@ -1,16 +1,15 @@
package com.commafeed.backend.service;
import org.apache.http.HttpHeaders;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit.MockitoJUnitRunner;
import org.mockito.MockitoAnnotations;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.junit.jupiter.MockServerExtension;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.MediaType;
@@ -19,14 +18,8 @@ import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.feed.FeedQueues;
import com.commafeed.backend.model.Feed;
@RunWith(MockitoJUnitRunner.class)
public class PubSubServiceTest {
@Rule
public final MockServerRule mockServerRule = new MockServerRule(this, 22441);
private PubSubService underTest;
private MockServerClient mockServerClient;
@ExtendWith(MockServerExtension.class)
class PubSubServiceTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private CommaFeedConfiguration config;
@@ -37,29 +30,40 @@ public class PubSubServiceTest {
@Mock
private Feed feed;
@Before
public void init() {
underTest = new PubSubService(config, queues);
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("http://localhost:22441/hub");
Mockito.when(feed.getPushHub()).thenReturn(hubUrl);
Mockito.when(feed.getPushTopic()).thenReturn("foo");
// setup config
Mockito.when(config.getApplicationSettings().getPublicUrl()).thenReturn("http://localhost:22441/hub");
Mockito.when(config.getApplicationSettings().getPublicUrl()).thenReturn(hubUrl);
}
@Test
public void subscribe200() {
void subscribe200() {
// Arrange
mockServerClient.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(200));
client.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(200));
// Act
underTest.subscribe(feed);
// Assert
mockServerClient.verify(HttpRequest.request()
client.verify(HttpRequest.request()
.withContentType(MediaType.APPLICATION_FORM_URLENCODED)
.withHeader(HttpHeaders.USER_AGENT, "CommaFeed")
.withMethod("POST")
@@ -69,9 +73,9 @@ public class PubSubServiceTest {
}
@Test
public void subscribe400WithPushpressError() {
void subscribe400WithPushpressError() {
// Arrange
mockServerClient.when(HttpRequest.request().withMethod("POST"))
client.when(HttpRequest.request().withMethod("POST"))
.respond(HttpResponse.response().withStatusCode(400).withBody(" is value is not allowed. You may only subscribe to"));
// Act
@@ -83,9 +87,9 @@ public class PubSubServiceTest {
}
@Test
public void subscribe400WithoutPushpressError() {
void subscribe400WithoutPushpressError() {
// Arrange
mockServerClient.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(400));
client.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(400));
// Act
underTest.subscribe(feed);

View File

@@ -2,9 +2,9 @@ package com.commafeed.backend.service;
import java.util.Optional;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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;
@@ -18,7 +18,7 @@ import com.commafeed.backend.dao.UserSettingsDAO;
import com.commafeed.backend.model.User;
import com.commafeed.backend.service.internal.PostLoginActivities;
public class UserServiceTest {
class UserServiceTest {
private static final byte[] SALT = new byte[] { 1, 2, 3 };
private static final byte[] ENCRYPTED_PASSWORD = new byte[] { 5, 6, 7 };
@@ -45,7 +45,7 @@ public class UserServiceTest {
private UserService userService;
@Before
@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
@@ -62,49 +62,49 @@ public class UserServiceTest {
}
@Test
public void callingLoginShouldNotReturnUserObjectWhenGivenNullNameOrEmail() {
void callingLoginShouldNotReturnUserObjectWhenGivenNullNameOrEmail() {
Optional<User> user = userService.login(null, "password");
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void callingLoginShouldNotReturnUserObjectWhenGivenNullPassword() {
void callingLoginShouldNotReturnUserObjectWhenGivenNullPassword() {
Optional<User> user = userService.login("testusername", null);
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void callingLoginShouldLookupUserByName() {
void callingLoginShouldLookupUserByName() {
userService.login("test", "password");
Mockito.verify(userDAO).findByName("test");
}
@Test
public void callingLoginShouldLookupUserByEmailIfLookupByNameFailed() {
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
public void callingLoginShouldNotReturnUserObjectIfCouldNotFindUserByNameOrEmail() {
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");
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void callingLoginShouldNotReturnUserObjectIfUserIsDisabled() {
void callingLoginShouldNotReturnUserObjectIfUserIsDisabled() {
Mockito.when(userDAO.findByName("test")).thenReturn(disabledUser);
Optional<User> user = userService.login("test", "password");
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void callingLoginShouldTryToAuthenticateUserWhoIsNotDisabled() {
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);
@@ -115,18 +115,18 @@ public class UserServiceTest {
}
@Test
public void callingLoginShouldNotReturnUserObjectOnUnsuccessfulAuthentication() {
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");
Assert.assertFalse(authenticatedUser.isPresent());
Assertions.assertFalse(authenticatedUser.isPresent());
}
@Test
public void callingLoginShouldExecutePostLoginActivitiesForUserOnSuccessfulAuthentication() {
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);
@@ -138,7 +138,7 @@ public class UserServiceTest {
}
@Test
public void callingLoginShouldReturnUserObjectOnSuccessfulAuthentication() {
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);
@@ -146,49 +146,49 @@ public class UserServiceTest {
Optional<User> authenticatedUser = userService.login("test", "password");
Assert.assertTrue(authenticatedUser.isPresent());
Assert.assertEquals(normalUser, authenticatedUser.get());
Assertions.assertTrue(authenticatedUser.isPresent());
Assertions.assertEquals(normalUser, authenticatedUser.get());
}
@Test
public void apiLoginShouldNotReturnUserIfApikeyNull() {
void apiLoginShouldNotReturnUserIfApikeyNull() {
Optional<User> user = userService.login(null);
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void apiLoginShouldLookupUserByApikey() {
void apiLoginShouldLookupUserByApikey() {
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(null);
userService.login("apikey");
Mockito.verify(userDAO).findByApiKey("apikey");
}
@Test
public void apiLoginShouldNotReturnUserIfUserNotFoundFromLookupByApikey() {
void apiLoginShouldNotReturnUserIfUserNotFoundFromLookupByApikey() {
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(null);
Optional<User> user = userService.login("apikey");
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void apiLoginShouldNotReturnUserIfUserFoundFromApikeyLookupIsDisabled() {
void apiLoginShouldNotReturnUserIfUserFoundFromApikeyLookupIsDisabled() {
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(disabledUser);
Optional<User> user = userService.login("apikey");
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void apiLoginShouldPerformPostLoginActivitiesIfUserFoundFromApikeyLookupNotDisabled() {
void apiLoginShouldPerformPostLoginActivitiesIfUserFoundFromApikeyLookupNotDisabled() {
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(normalUser);
userService.login("apikey");
Mockito.verify(postLoginActivities).executeFor(normalUser);
}
@Test
public void apiLoginShouldReturnUserIfUserFoundFromApikeyLookupNotDisabled() {
void apiLoginShouldReturnUserIfUserFoundFromApikeyLookupNotDisabled() {
Mockito.when(userDAO.findByApiKey("apikey")).thenReturn(normalUser);
Optional<User> returnedUser = userService.login("apikey");
Assert.assertEquals(normalUser, returnedUser.get());
Assertions.assertEquals(normalUser, returnedUser.get());
}
}

View File

@@ -2,7 +2,7 @@ package com.commafeed.frontend.auth;
import java.util.Optional;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.commafeed.backend.model.User;
@@ -10,10 +10,10 @@ import com.commafeed.backend.service.UserService;
import com.commafeed.backend.service.internal.PostLoginActivities;
import com.commafeed.frontend.session.SessionHelper;
public class SecurityCheckFactoryTest {
class SecurityCheckFactoryTest {
@Test
public void cookieLoginShouldPerformPostLoginActivities() {
void cookieLoginShouldPerformPostLoginActivities() {
User userInSession = new User();
SessionHelper sessionHelper = Mockito.mock(SessionHelper.class);

View File

@@ -3,7 +3,7 @@ package com.commafeed.frontend.resource;
import java.util.Arrays;
import java.util.Optional;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;
import org.mockito.Mockito;
@@ -15,10 +15,10 @@ import com.commafeed.frontend.model.request.LoginRequest;
import com.commafeed.frontend.model.request.RegistrationRequest;
import com.commafeed.frontend.session.SessionHelper;
public class UserRestTest {
class UserRestTest {
@Test
public void loginShouldNotPopulateHttpSessionIfUnsuccessfull() {
void loginShouldNotPopulateHttpSessionIfUnsuccessfull() {
// Absent user
Optional<User> absentUser = Optional.empty();
@@ -39,7 +39,7 @@ public class UserRestTest {
}
@Test
public void loginShouldPopulateHttpSessionIfSuccessfull() {
void loginShouldPopulateHttpSessionIfSuccessfull() {
// Create a user
User user = new User();
@@ -60,7 +60,7 @@ public class UserRestTest {
}
@Test
public void registerShouldRegisterAndThenLogin() {
void registerShouldRegisterAndThenLogin() {
// Create UserService mock
UserService service = Mockito.mock(UserService.class);
@@ -81,7 +81,7 @@ public class UserRestTest {
}
@Test
public void registerShouldPopulateHttpSession() {
void registerShouldPopulateHttpSession() {
// Create a user
User user = new User();

View File

@@ -5,18 +5,18 @@ import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.commafeed.backend.model.User;
public class SessionHelperTest {
class SessionHelperTest {
private static final String SESSION_KEY_USER = "user";
@Test
public void gettingUserDoesNotCreateSession() {
void gettingUserDoesNotCreateSession() {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
SessionHelper sessionHelper = new SessionHelper(request);
@@ -26,18 +26,18 @@ public class SessionHelperTest {
}
@Test
public void gettingUserShouldNotReturnUserIfThereIsNoPreexistingHttpSession() {
void gettingUserShouldNotReturnUserIfThereIsNoPreexistingHttpSession() {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getSession(false)).thenReturn(null);
SessionHelper sessionHelper = new SessionHelper(request);
Optional<User> user = sessionHelper.getLoggedInUser();
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void gettingUserShouldNotReturnUserIfUserNotPresentInHttpSession() {
void gettingUserShouldNotReturnUserIfUserNotPresentInHttpSession() {
HttpSession session = Mockito.mock(HttpSession.class);
Mockito.when(session.getAttribute(SESSION_KEY_USER)).thenReturn(null);
@@ -47,11 +47,11 @@ public class SessionHelperTest {
SessionHelper sessionHelper = new SessionHelper(request);
Optional<User> user = sessionHelper.getLoggedInUser();
Assert.assertFalse(user.isPresent());
Assertions.assertFalse(user.isPresent());
}
@Test
public void gettingUserShouldReturnUserIfUserPresentInHttpSession() {
void gettingUserShouldReturnUserIfUserPresentInHttpSession() {
User userInSession = new User();
HttpSession session = Mockito.mock(HttpSession.class);
@@ -63,8 +63,8 @@ public class SessionHelperTest {
SessionHelper sessionHelper = new SessionHelper(request);
Optional<User> user = sessionHelper.getLoggedInUser();
Assert.assertTrue(user.isPresent());
Assert.assertEquals(userInSession, user.get());
Assertions.assertTrue(user.isPresent());
Assertions.assertEquals(userInSession, user.get());
}
}