Skip to content

Commit eaf5019

Browse files
author
Lloyd Watkin
committed
Initial tests for <rating/> element
1 parent 5319303 commit eaf5019

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

src/main/java/org/buddycloud/channelserver/channel/ValidateEntry.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ValidateEntry {
2323
public static final String PARENT_ITEM_NOT_FOUND = "parent-item-not-found";
2424
public static final String TARGETED_ITEM_NOT_FOUND = "targeted-item-not-found";
2525
public static final String IN_REPLY_TO_MISSING = "missing-in-reply-to";
26+
public static final String TARGET_ELEMENT_MISSING = "missing-target";
2627
public static final String MISSING_TARGET_ID = "missing-target-id";
2728
public static final String TARGET_MUST_BE_IN_SAME_THREAD = "target-outside-thread";
2829

@@ -48,8 +49,8 @@ public class ValidateEntry {
4849
private Element entry;
4950

5051
private String errorMessage = "";
51-
private String inReplyTo;
52-
private String targetId;
52+
private String inReplyTo = null;
53+
private String targetId = null;
5354
private Element meta;
5455
private Element media;
5556

@@ -146,6 +147,10 @@ public boolean isValid() throws NodeStoreException {
146147
if (false == validateTargetElement(this.entry.element("target"))) {
147148
return false;
148149
}
150+
151+
if (false == validateRatingElement(this.entry.element("rating"))) {
152+
return false;
153+
}
149154

150155
Element meta = this.entry.element("meta");
151156
if (null != meta) {
@@ -305,4 +310,19 @@ private boolean validateTargetElement(Element target)
305310
}
306311
return true;
307312
}
313+
314+
private boolean validateRatingElement(Element rating) {
315+
if (null == rating) {
316+
return true;
317+
}
318+
if (null == inReplyTo) {
319+
this.errorMessage = IN_REPLY_TO_MISSING;
320+
return false;
321+
}
322+
if (null == targetId) {
323+
this.errorMessage = TARGET_ELEMENT_MISSING;
324+
return false;
325+
}
326+
return true;
327+
}
308328
}

src/test/java/org/buddycloud/channelserver/channel/ValidateEntryTest.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public class ValidateEntryTest extends TestHandler {
3737
private Element publishEntry;
3838
private IQ replyRequest;
3939
private Element replyEntry;
40-
private IQ targetRequest;
41-
private Element targetEntry;
40+
private IQ ratingRequest;
41+
private Element ratingEntry;
4242

4343
private ChannelManager channelManager;
4444

@@ -54,8 +54,8 @@ public void setUp() throws Exception {
5454
replyRequest = readStanzaAsIq("/iq/pubsub/publish/reply.stanza");
5555
replyEntry = replyRequest.getChildElement().element("publish")
5656
.element("item").element("entry");
57-
targetRequest = readStanzaAsIq("/iq/pubsub/publish/target.stanza");
58-
targetEntry = targetRequest.getChildElement().element("publish")
57+
ratingRequest = readStanzaAsIq("/iq/pubsub/publish/rating.stanza");
58+
ratingEntry = ratingRequest.getChildElement().element("publish")
5959
.element("item").element("entry");
6060

6161
channelManager = Mockito.mock(ChannelManager.class);
@@ -340,7 +340,7 @@ public void targetElementWithoutInReplyToReturnsError() throws Exception {
340340
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("2")))
341341
.thenReturn(null);
342342

343-
Element entry = (Element) this.targetEntry.clone();
343+
Element entry = (Element) this.ratingEntry.clone();
344344

345345
entry.element("in-reply-to").detach();
346346
validateEntry = getEntryObject(entry);
@@ -361,7 +361,7 @@ public void missingTargetIdElementReturnsError() throws Exception {
361361
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("2")))
362362
.thenReturn(null);
363363

364-
Element entry = (Element) this.targetEntry.clone();
364+
Element entry = (Element) this.ratingEntry.clone();
365365

366366
entry.element("target").element("id").detach();
367367
validateEntry = getEntryObject(entry);
@@ -382,7 +382,7 @@ public void emptyTargetIdElementReturnsError() throws Exception {
382382
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("2")))
383383
.thenReturn(null);
384384

385-
Element entry = (Element) this.targetEntry.clone();
385+
Element entry = (Element) this.ratingEntry.clone();
386386

387387
entry.element("target").element("id").detach();
388388
entry.element("target").addElement("id");
@@ -404,7 +404,7 @@ public void ifTargetedPostDoesntExistErrorIsReturned() throws Exception {
404404
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("2")))
405405
.thenReturn(null);
406406

407-
Element entry = (Element) this.targetEntry.clone();
407+
Element entry = (Element) this.ratingEntry.clone();
408408
validateEntry = getEntryObject(entry);
409409

410410
Assert.assertFalse(validateEntry.isValid());
@@ -423,7 +423,7 @@ public void ifTargetedPostIsntInSameThreadErrorIsReturnedParentCheck()
423423
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("2")))
424424
.thenReturn(item);
425425

426-
Element entry = (Element) this.targetEntry.clone();
426+
Element entry = (Element) this.ratingEntry.clone();
427427
validateEntry = getEntryObject(entry);
428428

429429
Assert.assertFalse(validateEntry.isValid());
@@ -445,7 +445,7 @@ public void ifTargetedPostIsntInSameThreadErrorIsReturnedThreadCheck()
445445
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("2")))
446446
.thenReturn(item2);
447447

448-
Element entry = (Element) this.targetEntry.clone();
448+
Element entry = (Element) this.ratingEntry.clone();
449449
entry.element("target").element("id").setText("B");
450450
validateEntry = getEntryObject(entry);
451451

@@ -462,7 +462,7 @@ public void ifTargetedIdIsSameAsReplyToIdOnlyOneDatabaseLookupPerformed()
462462
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("1")))
463463
.thenReturn(item);
464464

465-
Element entry = (Element) this.targetEntry.clone();
465+
Element entry = (Element) this.ratingEntry.clone();
466466
entry.element("target").element("id").setText("1");
467467

468468
validateEntry = getEntryObject(entry);
@@ -476,12 +476,7 @@ public void ifTargetedIdIsSameAsReplyToIdOnlyOneDatabaseLookupPerformed()
476476
@Test
477477
public void targetElementGetsAddedAsExpected() throws Exception {
478478

479-
NodeItem item = new NodeItemImpl(node, "1", new Date(), "<entry/>");
480-
Mockito.when(
481-
channelManager.getNodeItem(Mockito.eq(node), Mockito.eq("1")))
482-
.thenReturn(item);
483-
484-
Element entry = (Element) this.targetEntry.clone();
479+
Element entry = (Element) this.ratingEntry.clone();
485480
entry.element("target").element("id").setText("1");
486481

487482
validateEntry = getEntryObject(entry);
@@ -496,4 +491,29 @@ public void targetElementGetsAddedAsExpected() throws Exception {
496491
Assert.assertEquals("post",
497492
payload.element("target").elementText("object-type"));
498493
}
494+
495+
@Test
496+
public void missingInReplyToErrorsIfRatingElementPresent() throws Exception {
497+
498+
Element entry = (Element) this.ratingEntry.clone();
499+
entry.element("in-reply-to").detach();
500+
validateEntry = getEntryObject(entry);
501+
502+
Assert.assertFalse(validateEntry.isValid());
503+
Assert.assertEquals(ValidateEntry.IN_REPLY_TO_MISSING,
504+
validateEntry.getErrorMessage());
505+
}
506+
507+
@Test
508+
public void missingTargetErrorsIfRatingElementPresent() throws Exception {
509+
510+
Element entry = (Element) this.ratingEntry.clone();
511+
entry.element("target").detach();
512+
validateEntry = getEntryObject(entry);
513+
514+
Assert.assertFalse(validateEntry.isValid());
515+
Assert.assertEquals(ValidateEntry.TARGET_ELEMENT_MISSING,
516+
validateEntry.getErrorMessage());
517+
}
518+
499519
}

src/test/resources/stanzas/iq/pubsub/publish/rating.stanza

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
<jid xmlns="http://buddycloud.com/atom-elements-0">[email protected]</jid>
1414
</author>
1515
<content type="text">rating:5.0</content>
16-
<activity:verb>post</activity:verb>
16+
<activity:verb>rated</activity:verb>
1717
<activity:object>
1818
<activity:object-type>post</activity:object-type>
1919
</activity:object>
2020
<thr:in-reply-to ref="1" />
2121
<activity:target>
2222
<id>tag:channels.shakespeare.lit,/users/[email protected]/posts,2</id>
23-
<activity:object-type>post</activity:object-type>
23+
<activity:object-type>comment</activity:object-type>
2424
</activity:target>
2525
<review:rating>5.0</review:rating>
2626
</entry>

0 commit comments

Comments
 (0)