-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/marker pagination fixes #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1adc4ce
24c3606
c03a5d7
d072422
db207d9
b0a93f9
68552a6
eac408d
59d016c
3091020
77393d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package org.atomhopper.hibernate; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertTrue; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
@@ -20,11 +21,12 @@ | |
import org.atomhopper.adapter.jpa.PersistedCategory; | ||
import org.atomhopper.adapter.jpa.PersistedEntry; | ||
import org.atomhopper.adapter.jpa.PersistedFeed; | ||
import org.atomhopper.dbal.AtomDatabaseException; | ||
import org.atomhopper.hibernate.actions.ComplexSessionAction; | ||
import org.atomhopper.dbal.PageDirection; | ||
import org.atomhopper.hibernate.actions.SimpleSessionAction; | ||
import org.atomhopper.hibernate.query.SimpleCategoryCriteriaGenerator; | ||
import org.hibernate.Session; | ||
import org.hibernate.criterion.Restrictions; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
@@ -39,11 +41,8 @@ | |
@RunWith(Enclosed.class) | ||
public class HibernateFeedRepositoryTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you removing the WhenPerformingSimpleAction and the WhenPerformingComplexAction test classes? Should WhenCreatingMisconfiguredFeedRepository be an additional class rather than replacing? |
||
|
||
public static class WhenPerformingSimpleAction { | ||
|
||
HibernateFeedRepository feedRepository; | ||
public static class WhenCreatingMisconfiguredFeedRepository{ | ||
Map<String, String> parameters; | ||
SimpleSessionAction simpleSessionAction; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
|
@@ -54,40 +53,11 @@ public void setup() throws Exception { | |
parameters.put("hibernate.connection.username", "sa"); | ||
parameters.put("hibernate.connection.password", ""); | ||
parameters.put("hibernate.hbm2ddl.auto", "update"); | ||
|
||
feedRepository = new HibernateFeedRepository(parameters); | ||
simpleSessionAction = mock(SimpleSessionAction.class); | ||
} | ||
|
||
@Test(expected=AtomDatabaseException.class) | ||
public void shouldThrowAtomDatabaseException() throws Exception { | ||
feedRepository.performSimpleAction(simpleSessionAction); | ||
} | ||
} | ||
|
||
public static class WhenPerformingComplexAction { | ||
|
||
HibernateFeedRepository feedRepository; | ||
Map<String, String> parameters; | ||
ComplexSessionAction complexSessionAction; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
parameters = new HashMap<String, String>(); | ||
parameters.put("hibernate.connection.driver_class", "org.h2.Driver"); | ||
parameters.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | ||
parameters.put("hibernate.connection.username", "sa"); | ||
parameters.put("hibernate.connection.password", ""); | ||
parameters.put("hibernate.hbm2ddl.auto", "update"); | ||
|
||
feedRepository = new HibernateFeedRepository(parameters); | ||
complexSessionAction = mock(ComplexSessionAction.class); | ||
} | ||
|
||
/*This should throw the error because */ | ||
@Test(expected=AtomDatabaseException.class) | ||
@Test(expected=UnsupportedOperationException.class) | ||
public void shouldThrowAtomDatabaseException() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we change the name of this test as its throwing a different exception? |
||
feedRepository.performComplexAction(complexSessionAction); | ||
new HibernateFeedRepository(parameters); | ||
} | ||
} | ||
|
||
|
@@ -99,7 +69,7 @@ public static void setup() throws Exception { | |
Map<String, String> parameters; | ||
parameters = new HashMap<String, String>(); | ||
parameters.put("hibernate.connection.driver_class", "org.h2.Driver"); | ||
parameters.put("hibernate.connection.url", "jdbc:h2:mem:WhenCreatingFeed"); | ||
parameters.put("hibernate.connection.url", "jdbc:h2:mem:WhenCreatingEntry"); | ||
parameters.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | ||
parameters.put("hibernate.connection.username", "sa"); | ||
parameters.put("hibernate.connection.password", ""); | ||
|
@@ -158,6 +128,110 @@ public void perform(Session liveSession) { | |
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void pagingForwardsShouldNotFindTheEntry() { | ||
final PersistedEntry entry = feedRepository.getEntry("entryId", "feedName"); | ||
final List<PersistedEntry> page = feedRepository.getFeedPage( | ||
"feedName", entry, PageDirection.FORWARD, new SimpleCategoryCriteriaGenerator(""), 1 | ||
); | ||
Assert.assertTrue(page.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void pagingBackwardsShouldFindTheEntry() { | ||
final PersistedEntry entry = feedRepository.getEntry("entryId", "feedName"); | ||
final List<PersistedEntry> page = feedRepository.getFeedPage( | ||
"feedName", entry, PageDirection.BACKWARD, new SimpleCategoryCriteriaGenerator(""), 1 | ||
); | ||
Assert.assertEquals(1, page.size()); | ||
Assert.assertEquals("entryId", page.get(0).getEntryId()); | ||
} | ||
} | ||
|
||
public static class WhenCreatingTwoEntries { | ||
static HibernateFeedRepository feedRepository; | ||
|
||
@BeforeClass | ||
public static void setup() throws Exception { | ||
Map<String, String> parameters; | ||
parameters = new HashMap<String, String>(); | ||
parameters.put("hibernate.connection.driver_class", "org.h2.Driver"); | ||
parameters.put("hibernate.connection.url", "jdbc:h2:mem:WhenCreatingTwoEntries"); | ||
parameters.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | ||
parameters.put("hibernate.connection.username", "sa"); | ||
parameters.put("hibernate.connection.password", ""); | ||
parameters.put("hibernate.hbm2ddl.auto", "update"); | ||
|
||
feedRepository = new HibernateFeedRepository(parameters); | ||
|
||
PersistedFeed feed = new PersistedFeed("feedName", "feedId"); | ||
|
||
PersistedEntry entry1 = new PersistedEntry("entry1"); | ||
Instant earlier = Instant.now().minusSeconds(60); | ||
entry1.setDateLastUpdated(earlier); | ||
entry1.setCreationDate(earlier); | ||
entry1.setFeed(feed); | ||
|
||
feedRepository.saveEntry(entry1); | ||
|
||
PersistedEntry entry2 = new PersistedEntry("entry2"); | ||
Instant now = Instant.now(); | ||
entry2.setDateLastUpdated(now); | ||
entry2.setCreationDate(now); | ||
entry2.setFeed(feed); | ||
|
||
feedRepository.saveEntry(entry2); | ||
} | ||
|
||
@Test | ||
public void entriesShouldBeSortedByRecency() throws Exception { | ||
final List<PersistedEntry> entries = feedRepository.getFeedHead( | ||
"feedName", new SimpleCategoryCriteriaGenerator(""), 2 | ||
); | ||
Assert.assertEquals(entries.get(0).getEntryId(), "entry2"); | ||
Assert.assertEquals(entries.get(1).getEntryId(), "entry1"); | ||
} | ||
|
||
@Test | ||
public void pagingBackwardsFromLatestShouldFindBoth() { | ||
final PersistedEntry latest = feedRepository.getEntry("entry2", "feedName"); | ||
final List<PersistedEntry> page = feedRepository.getFeedPage( | ||
"feedName", latest, PageDirection.BACKWARD, new SimpleCategoryCriteriaGenerator(""), 2 | ||
); | ||
Assert.assertEquals(2, page.size()); | ||
Assert.assertEquals("entry2", page.get(0).getEntryId()); | ||
Assert.assertEquals("entry1", page.get(1).getEntryId()); | ||
} | ||
|
||
@Test | ||
public void pagingForwardsFromLatestShouldNotFindAnything() { | ||
final PersistedEntry latest = feedRepository.getEntry("entry2", "feedName"); | ||
final List<PersistedEntry> page = feedRepository.getFeedPage( | ||
"feedName", latest, PageDirection.FORWARD, new SimpleCategoryCriteriaGenerator(""), 2 | ||
); | ||
Assert.assertTrue(page.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void pagingBackwardsFromOldestShouldOnlyFindOldest() { | ||
final PersistedEntry oldest = feedRepository.getEntry("entry1", "feedName"); | ||
final List<PersistedEntry> page = feedRepository.getFeedPage( | ||
"feedName", oldest, PageDirection.BACKWARD, new SimpleCategoryCriteriaGenerator(""), 2 | ||
); | ||
Assert.assertEquals(1, page.size()); | ||
Assert.assertEquals("entry1", page.get(0).getEntryId()); | ||
} | ||
|
||
@Test | ||
public void pagingForwardsFromOldestShouldFindLatest() { | ||
final PersistedEntry oldest = feedRepository.getEntry("entry1", "feedName"); | ||
final List<PersistedEntry> page = feedRepository.getFeedPage( | ||
"feedName", oldest, PageDirection.FORWARD, new SimpleCategoryCriteriaGenerator(""), 2 | ||
); | ||
Assert.assertEquals(1, page.size()); | ||
Assert.assertEquals("entry2", page.get(0).getEntryId()); | ||
} | ||
} | ||
|
||
public static class WhenGettingCategories { | ||
|
@@ -357,5 +431,58 @@ public Integer run() { | |
r1.get(3000); | ||
r2.get(3000); | ||
} | ||
|
||
@Test | ||
public void entriesShouldGetDistinctIds() throws Exception { | ||
|
||
final PersistedEntry existing = new PersistedEntry("existing"); | ||
final PersistedFeed feed = new PersistedFeed("feed1", "feed1"); | ||
feed.setEntries(Collections.singleton(existing)); | ||
existing.setFeed(feed); | ||
feedRepository.saveEntry(existing); | ||
|
||
final Runner.Future<PersistedEntry> r1 = runner1.run(new Runner.Operation<PersistedEntry>() { | ||
@Override | ||
public PersistedEntry run() { | ||
final PersistedEntry entry = new PersistedEntry("entry2a"); | ||
feed.setEntries(Collections.singleton(entry)); | ||
entry.setFeed(feed); | ||
try { | ||
barrier.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} catch (BrokenBarrierException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
feedRepository.saveEntry(entry); | ||
return entry; | ||
} | ||
}); | ||
final Runner.Future<PersistedEntry> r2 = runner2.run(new Runner.Operation<PersistedEntry>() { | ||
@Override | ||
public PersistedEntry run() { | ||
final PersistedEntry entry = new PersistedEntry("entry2b"); | ||
feed.setEntries(Collections.singleton(entry)); | ||
entry.setFeed(feed); | ||
try { | ||
barrier.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} catch (BrokenBarrierException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
feedRepository.saveEntry(entry); | ||
return entry; | ||
} | ||
}); | ||
final PersistedEntry entry1 = r1.get(3000); | ||
final PersistedEntry entry2 = r2.get(3000); | ||
|
||
assertFalse(entry1.getEntryId().equals(entry2.getEntryId())); | ||
assertFalse(entry1.getCreationDate().isBefore(existing.getCreationDate())); | ||
assertFalse(entry2.getCreationDate().isBefore(existing.getCreationDate())); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you flip the order on this?