Author: krishantha
Date: Mon Jan 21 01:52:30 2008
New Revision: 12582
Log:
Added:
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/RatingTest.java
Added:
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/RatingTest.java
==============================================================================
--- (empty file)
+++
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/RatingTest.java
Mon Jan 21 01:52:30 2008
@@ -0,0 +1,220 @@
+package org.wso2.registry.app;
+
+import junit.framework.TestCase;
+import org.wso2.registry.Registry;
+import org.wso2.registry.Resource;
+import org.wso2.registry.RegistryException;
+
+import java.net.URL;
+
+/*
+ * Created by IntelliJ IDEA.
+ * User: krishantha
+ * Date: Jan 18, 2008
+ * Time: 8:57:09 AM
+ * To change this template use File | Settings | File Templates.
+ */
+public class RatingTest extends TestCase {
+
+ RegistryServer server = new RegistryServer();
+ private static Registry registry = null;
+
+ public void setUp() {
+ try {
+ if (registry == null) {
+ server.start();
+ registry = new RemoteRegistry(new
URL("http://localhost:8080/wso2registry/atom"), "admin", "admin");
+ }
+ } catch (Exception e) {
+ fail("Failed to initialize the registry.");
+ }
+ }
+
+ public void testAddResourceRating(){
+
+ try{
+ Resource r1 = new Resource();
+ r1.setAuthorUserName("Author R1");
+ byte[] r1content = "R1 content".getBytes();
+ r1.setContent(r1content);
+
+ registry.put("/d16/d17/r1", r1);
+
+ } catch (
+ RegistryException e) {
+ fail("Failed to put test resources.");
+ }
+
+ try {
+ registry.rateResource("/d16/d17/r1", 5);
+ } catch (RegistryException e) {
+ fail("Couldn't rate the resource /d16/d17/r1");
+ }
+
+ float rating = 0;
+ try {
+ rating = registry.getAverageRating("/d16/d17/r1");
+
+ } catch (RegistryException e) {
+ fail("Couldn't get the rating of the resource /d16/d17/r1");
+ }
+
+ System.out.println("Start rating:" + rating);
+ assertEquals("Rating of the resource /d16/d17/r1 should be 5.",
rating, (float) 5.0, (float) 0.01);
+
+
+ /* try {
+ registry.delete("/d16/d17/r1");
+ } catch (RegistryException e) {
+ fail("Failed to delete test resources.");
+ } */
+ }
+
+ public void testAddCollectionRating(){
+
+ try{
+ Resource r1 = new Resource();
+ r1.setAuthorUserName("Author R2");
+ byte[] r1content = "R2 content".getBytes();
+ r1.setContent(r1content);
+
+ registry.put("/d16/d17", r1);
+
+ } catch (
+ RegistryException e) {
+ fail("Failed to put test resources.");
+ }
+
+ try {
+ registry.rateResource("/d16/d17", 4);
+ } catch (RegistryException e) {
+ fail("Couldn't rate the resource /d16/d17");
+ }
+
+ float rating = 0;
+ try {
+ rating = registry.getAverageRating("/d16/d17");
+
+ } catch (RegistryException e) {
+ fail("Couldn't get the rating of the resource /d16/d17");
+ }
+
+ System.out.println("Start rating:" + rating);
+ assertEquals("Rating of the resource /d16/d17 should be 5.", rating,
(float) 4.0, (float) 0.01);
+
+
+ /* try {
+ registry.delete("/d16/d17/r1");
+ } catch (RegistryException e) {
+ fail("Failed to delete test resources.");
+ } */
+ }
+
+ public void testEditResourceRating(){
+
+ try{
+ Resource r1 = new Resource();
+ r1.setAuthorUserName("Author R1");
+ byte[] r1content = "R1 content".getBytes();
+ r1.setContent(r1content);
+
+ registry.put("/d16/d17/d18/r1", r1);
+
+ } catch (
+ RegistryException e) {
+ fail("Failed to put test resources.");
+ }
+
+ try {
+ registry.rateResource("/d16/d17/d18/r1", 5);
+ } catch (RegistryException e) {
+ fail("Couldn't rate the resource /d16/d17/d18/r1");
+ }
+
+ float rating = 0;
+ try {
+ rating = registry.getAverageRating("/d16/d17/d18/r1");
+
+ } catch (RegistryException e) {
+ fail("Couldn't get the rating of the resource /d16/d17/d18/r1");
+ }
+
+ System.out.println("Start rating:" + rating);
+
+ assertEquals("Rating of the resource /d16/d17/d18/r1 should be 5.",
rating, (float) 5.0, (float) 0.01);
+
+ /*rate the same resource again*/
+
+ try {
+ registry.rateResource("/d16/d17/d18/r1", 3);
+ } catch (RegistryException e) {
+ fail("Couldn't rate the resource /d16/d17/d18/r1");
+ }
+
+ float rating_edit = 0;
+ try {
+ rating_edit = registry.getAverageRating("/d16/d17/d18/r1");
+
+ } catch (RegistryException e) {
+ fail("Couldn't get the rating of the resource /d16/d17/d18/r1");
+ }
+
+ System.out.println("Start rating:" + rating_edit);
+
+ assertEquals("Rating of the resource /d16/d17/d18/r1 should be 5.",
rating_edit, (float) 3.0, (float) 0.01);
+ /* try {
+ registry.delete("/d16/d17/r1");
+ } catch (RegistryException e) {
+ fail("Failed to delete test resources.");
+ } */
+ }
+
+ public void testRatingsPath() {
+ Resource r5 = new Resource();
+ String r5Content = "this is r5 content";
+ r5.setContent(r5Content.getBytes());
+ r5.setDescription("production ready.");
+ String r5Path = "/c1/r5";
+
+ try {
+ registry.put(r5Path, r5);
+ } catch (RegistryException e) {
+ fail("Valid put scenario failed.");
+ }
+
+ try {
+ registry.rateResource("/c1/r5", 3);
+ } catch (RegistryException e) {
+ fail("Valid tagging scenario failed.");
+ }
+
+ Resource ratings = null;
+ try {
+ ratings = registry.get("/c1/r5;ratings");
+ } catch (RegistryException e) {
+ fail("Failed to get ratings using URL.");
+ }
+
+ String[] ratingPaths = (String[]) ratings.getContent();
+
+ int rating = 0;
+ try {
+ Resource c1 = registry.get(ratingPaths[0]);
+
+ Object o = c1.getContent();
+ if (o instanceof Integer) {
+ rating = (Integer) o;
+ } else {
+ rating = Integer.parseInt(o.toString());
+ }
+
+ } catch (RegistryException e) {
+ fail("Failed to get rating using a path.");
+ }
+
+ assertEquals("Ratings are not retrieved properly as resources.",
rating, 3);
+ }
+
+
+}
+
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev