Skip to content

Commit 79f4f05

Browse files
author
Lloyd Watkin
committed
Add tests for rating parsing
1 parent eaf5019 commit 79f4f05

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.dom4j.Element;
1313
import org.dom4j.dom.DOMElement;
1414
import org.xmpp.packet.JID;
15-
import org.xmpp.packet.PacketError;
1615

1716
public class ValidateEntry {
1817

@@ -25,6 +24,8 @@ public class ValidateEntry {
2524
public static final String IN_REPLY_TO_MISSING = "missing-in-reply-to";
2625
public static final String TARGET_ELEMENT_MISSING = "missing-target";
2726
public static final String MISSING_TARGET_ID = "missing-target-id";
27+
public static final String RATING_OUT_OF_RANGE = "rating-out-of-range";
28+
public static final String INVALID_RATING_VALUE = "invalid-rating";
2829
public static final String TARGET_MUST_BE_IN_SAME_THREAD = "target-outside-thread";
2930

3031
public static final String CONTENT_TEXT = "text";
@@ -51,6 +52,7 @@ public class ValidateEntry {
5152
private String errorMessage = "";
5253
private String inReplyTo = null;
5354
private String targetId = null;
55+
private int itemRating = 0;
5456
private Element meta;
5557
private Element media;
5658

@@ -323,6 +325,20 @@ private boolean validateRatingElement(Element rating) {
323325
this.errorMessage = TARGET_ELEMENT_MISSING;
324326
return false;
325327
}
328+
try {
329+
double itemRatingFloat = Double.parseDouble(rating.getTextTrim());
330+
if (itemRatingFloat != Math.floor(itemRatingFloat)) {
331+
throw new NumberFormatException("Non-integer rating provided");
332+
}
333+
itemRating = (int) itemRatingFloat;
334+
} catch (NumberFormatException e) {
335+
this.errorMessage = INVALID_RATING_VALUE;
336+
return false;
337+
}
338+
if ((itemRating < 1) || (itemRating > 5)) {
339+
this.errorMessage = RATING_OUT_OF_RANGE;
340+
return false;
341+
}
326342
return true;
327343
}
328344
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ public void missingInReplyToErrorsIfRatingElementPresent() throws Exception {
508508
public void missingTargetErrorsIfRatingElementPresent() throws Exception {
509509

510510
Element entry = (Element) this.ratingEntry.clone();
511+
entry.element("target").element("id").setText("1");
511512
entry.element("target").detach();
512513
validateEntry = getEntryObject(entry);
513514

@@ -516,4 +517,42 @@ public void missingTargetErrorsIfRatingElementPresent() throws Exception {
516517
validateEntry.getErrorMessage());
517518
}
518519

520+
@Test
521+
public void invalidRatingValueReturnsError() throws Exception {
522+
523+
Element entry = (Element) this.ratingEntry.clone();
524+
entry.element("target").element("id").setText("1");
525+
entry.element("rating").setText("awesome");
526+
validateEntry = getEntryObject(entry);
527+
528+
Assert.assertFalse(validateEntry.isValid());
529+
Assert.assertEquals(ValidateEntry.INVALID_RATING_VALUE,
530+
validateEntry.getErrorMessage());
531+
}
532+
533+
@Test
534+
public void outOfRangeRatingReturnsError() throws Exception {
535+
536+
Element entry = (Element) this.ratingEntry.clone();
537+
entry.element("rating").setText("6.0");
538+
entry.element("target").element("id").setText("1");
539+
validateEntry = getEntryObject(entry);
540+
541+
Assert.assertFalse(validateEntry.isValid());
542+
Assert.assertEquals(ValidateEntry.RATING_OUT_OF_RANGE,
543+
validateEntry.getErrorMessage());
544+
}
545+
546+
@Test
547+
public void nonWholeNumberRatingReturnsError() throws Exception {
548+
Element entry = (Element) this.ratingEntry.clone();
549+
entry.element("rating").setText("4.1");
550+
entry.element("target").element("id").setText("1");
551+
validateEntry = getEntryObject(entry);
552+
553+
Assert.assertFalse(validateEntry.isValid());
554+
Assert.assertEquals(ValidateEntry.INVALID_RATING_VALUE,
555+
validateEntry.getErrorMessage());
556+
}
557+
519558
}

0 commit comments

Comments
 (0)