Skip to content

Commit 3a64569

Browse files
author
Lloyd Watkin
committed
Add code and tests for retrieving node owners
1 parent 2d32ce2 commit 3a64569

4 files changed

Lines changed: 76 additions & 16 deletions

File tree

src/main/java/org/buddycloud/channelserver/db/NodeStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ ResultSet<NodeAffiliation> getNodeAffiliations(String node
235235
* Get a list of node owners
236236
*
237237
* @param node
238+
* @throws NodeStoreException
238239
*/
239-
ArrayList<JID> getNodeOwners(String node);
240+
ArrayList<JID> getNodeOwners(String node) throws NodeStoreException;
240241

241242
/**
242243
* Count the number of affiliations for a node

src/main/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStore.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,30 @@ public ResultSet<NodeAffiliation> getNodeAffiliations(String nodeId,
584584
}
585585
}
586586

587+
@Override
588+
public ArrayList<JID> getNodeOwners(String node) throws NodeStoreException {
589+
PreparedStatement stmt = null;
590+
591+
try {
592+
stmt = conn.prepareStatement(dialect
593+
.selectNodeOwners());
594+
stmt.setString(1, node);
595+
596+
java.sql.ResultSet rs = stmt.executeQuery();
597+
ArrayList<JID> result = new ArrayList<JID>();
598+
599+
while (rs.next()) {
600+
result.add(new JID(rs.getString(1)));
601+
}
602+
603+
return result;
604+
} catch (SQLException e) {
605+
throw new NodeStoreException(e);
606+
} finally {
607+
close(stmt); // Will implicitly close the resultset if required
608+
}
609+
}
610+
587611
@Override
588612
public int countUserAffiliations(JID actorJid) throws NodeStoreException {
589613
PreparedStatement selectStatement = null;
@@ -1840,6 +1864,8 @@ public void close() throws NodeStoreException {
18401864
public interface NodeStoreSQLDialect {
18411865
String insertNode();
18421866

1867+
String selectNodeOwners();
1868+
18431869
String getUserItems();
18441870

18451871
String selectItemsForLocalNodesBeforeDate();

src/main/java/org/buddycloud/channelserver/db/jdbc/dialect/Sql92NodeStoreDialect.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class Sql92NodeStoreDialect implements NodeStoreSQLDialect {
2525
private static final String SELECT_AFFILIATIONS_FOR_USER = "SELECT \"node\", \"user\", \"affiliation\", \"updated\""
2626
+ " FROM \"affiliations\" WHERE \"user\" = ? ORDER BY \"updated\" ASC";
2727

28+
private static final String SELECT_NODE_OWNERS = "SELECT \"user\" FROM \"affiliations\" WHERE \"node\" = ? AND \"affiliation\" = 'owner';";
29+
2830
private static final String SELECT_AFFILIATION_CHANGES = ""
2931
+ "SELECT \"node\", \"user\", \"affiliation\", \"updated\" FROM \"affiliations\" "
3032
+ "WHERE \"updated\" >= ? AND \"updated\" <= ? AND \"node\" IN "
@@ -278,6 +280,11 @@ public String selectAffiliation() {
278280
public String selectAffiliationsForUser() {
279281
return SELECT_AFFILIATIONS_FOR_USER;
280282
}
283+
284+
@Override
285+
public String selectNodeOwners() {
286+
return SELECT_NODE_OWNERS;
287+
}
281288

282289
@Override
283290
public String selectAffiliationChanges() {

src/test/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStoreTest.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public class JDBCNodeStoreTest {
7777
private static final String TEST_SERVER1_HOSTNAME = "server1";
7878
private static final String TEST_SERVER2_HOSTNAME = "server2";
7979

80+
private static final String UNKNOWN_NODE = "/user/unknown@example.com/posts";
81+
8082
private static final HashMap<String, String> TEST_SERVER1_NODE1_CONF = new HashMap<String, String>() {
8183
{
8284
put("config1", "Value of config1");
@@ -1322,16 +1324,18 @@ public void testGetNodeItemsWithPaging() throws Exception {
13221324
dbTester.loadData("node_1");
13231325

13241326
long start = System.currentTimeMillis();
1325-
1327+
13261328
NodeItem[] items = new NodeItem[20];
1327-
1329+
13281330
for (int i = 0; i < 20; i++) {
1329-
items[i] = new NodeItemImpl(TEST_SERVER1_NODE1_ID, String
1330-
.valueOf(i), new Date(start + i * 10), "payload" + String.valueOf(i));
1331+
items[i] = new NodeItemImpl(TEST_SERVER1_NODE1_ID,
1332+
String.valueOf(i), new Date(start + i * 10), "payload"
1333+
+ String.valueOf(i));
13311334
store.addNodeItem(items[i]);
13321335
}
13331336

1334-
CloseableIterator<NodeItem> result = store.getNodeItems(TEST_SERVER1_NODE1_ID, "15", 3);
1337+
CloseableIterator<NodeItem> result = store.getNodeItems(
1338+
TEST_SERVER1_NODE1_ID, "15", 3);
13351339

13361340
assertEquals("Incorrect node item returned", items[14], result.next());
13371341
assertEquals("Incorrect node item returned", items[13], result.next());
@@ -1565,7 +1569,7 @@ public void testGetRecentItemsCanBePaged() throws Exception {
15651569
store.addUserSubscription(new NodeSubscriptionImpl(
15661570
TEST_SERVER1_NODE2_ID, TEST_SERVER1_USER1_JID,
15671571
Subscriptions.subscribed));
1568-
1572+
15691573
long now = System.currentTimeMillis();
15701574

15711575
NodeItem nodeItem1 = new NodeItemImpl(TEST_SERVER1_NODE1_ID, "123",
@@ -1585,7 +1589,9 @@ public void testGetRecentItemsCanBePaged() throws Exception {
15851589
store.addNodeItem(nodeItem4);
15861590

15871591
CloseableIterator<NodeItem> items = store.getRecentItems(
1588-
TEST_SERVER1_USER1_JID, since, -1, 2, new GlobalItemIDImpl(TEST_SERVER1_CHANNELS_JID, TEST_SERVER1_NODE1_ID, "124"), null);
1592+
TEST_SERVER1_USER1_JID, since, -1, 2,
1593+
new GlobalItemIDImpl(TEST_SERVER1_CHANNELS_JID,
1594+
TEST_SERVER1_NODE1_ID, "124"), null);
15891595

15901596
assertSameNodeItem(items.next(), nodeItem3);
15911597
assertSameNodeItem(items.next(), nodeItem1);
@@ -1646,7 +1652,7 @@ public void testCanPageGetRecentItemsUsingResultSetManagement()
16461652
dbTester.loadData("node_1");
16471653

16481654
Date since = new Date();
1649-
1655+
16501656
Thread.sleep(10);
16511657

16521658
store.addRemoteNode(TEST_SERVER1_NODE2_ID);
@@ -1659,8 +1665,9 @@ public void testCanPageGetRecentItemsUsingResultSetManagement()
16591665
store.addNodeItem(new NodeItemImpl(TEST_SERVER1_NODE1_ID, String
16601666
.valueOf(i), new Date(), "payload" + String.valueOf(i)));
16611667
}
1662-
1663-
GlobalItemID itemID = new GlobalItemIDImpl(TEST_SERVER1_CHANNELS_JID, TEST_SERVER1_NODE1_ID, "15");
1668+
1669+
GlobalItemID itemID = new GlobalItemIDImpl(TEST_SERVER1_CHANNELS_JID,
1670+
TEST_SERVER1_NODE1_ID, "15");
16641671

16651672
CloseableIterator<NodeItem> items = store.getRecentItems(
16661673
TEST_SERVER1_USER1_JID, since, -1, 10, itemID, null);
@@ -1785,8 +1792,8 @@ public void testCanGetItemThread() throws Exception {
17851792
@Test
17861793
public void testCanGetItemThreadWithResultSetManagement() throws Exception {
17871794
dbTester.loadData("node_1");
1788-
NodeItem testItemParent = new NodeItemImpl(TEST_SERVER1_NODE1_ID, "a100",
1789-
new Date(100), "<entry>payload parent</entry>");
1795+
NodeItem testItemParent = new NodeItemImpl(TEST_SERVER1_NODE1_ID,
1796+
"a100", new Date(100), "<entry>payload parent</entry>");
17901797
NodeItem testItem1 = new NodeItemImpl(TEST_SERVER1_NODE1_ID, "a6",
17911798
new Date(20), "<entry>payload</entry>", "a100");
17921799
NodeItem testItem2 = new NodeItemImpl(TEST_SERVER1_NODE1_ID, "a7",
@@ -2319,16 +2326,35 @@ private void assertSameNodeItem(NodeItem actual, NodeItem expected) {
23192326
assertEquals(expected.getPayload(), actual.getPayload());
23202327
assertEquals(expected.getInReplyTo(), actual.getInReplyTo());
23212328
}
2322-
2329+
23232330
@Test
23242331
public void testSelectNodeThreads() throws Exception {
23252332
dbTester.loadData("node_1");
2326-
assertEquals(5, store.getNodeThreads(TEST_SERVER1_NODE1_ID, null, 10).size());
2333+
assertEquals(5, store.getNodeThreads(TEST_SERVER1_NODE1_ID, null, 10)
2334+
.size());
23272335
}
2328-
2336+
23292337
@Test
23302338
public void testCountNodeThreads() throws Exception {
23312339
dbTester.loadData("node_1");
23322340
assertEquals(5, store.countNodeThreads(TEST_SERVER1_NODE1_ID));
23332341
}
2342+
2343+
@Test
2344+
public void testNoNodeOwnersReturnsEmptyList() throws Exception {
2345+
dbTester.loadData("node_1");
2346+
assertEquals(0, store.getNodeOwners(UNKNOWN_NODE).size());
2347+
}
2348+
2349+
@Test
2350+
public void testNodeOwnersReturnsExpectedList() throws Exception {
2351+
dbTester.loadData("node_1");
2352+
2353+
store.addUserSubscription(new NodeSubscriptionImpl(TEST_SERVER1_NODE1_ID, TEST_SERVER1_USER2_JID, Subscriptions.subscribed));
2354+
store.setUserAffiliation(TEST_SERVER1_NODE1_ID, TEST_SERVER1_USER2_JID, Affiliations.owner);
2355+
2356+
assertEquals(2, store.getNodeOwners(TEST_SERVER1_NODE1_ID).size());
2357+
assertEquals(TEST_SERVER1_USER1_JID, store.getNodeOwners(TEST_SERVER1_NODE1_ID).get(0));
2358+
assertEquals(TEST_SERVER1_USER2_JID, store.getNodeOwners(TEST_SERVER1_NODE1_ID).get(1));
2359+
}
23342360
}

0 commit comments

Comments
 (0)