[jboss-svn-commits] JBL Code SVN: r36023 - labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 22 12:19:36 EST 2010


Author: tsurdilovic
Date: 2010-11-22 12:19:36 -0500 (Mon, 22 Nov 2010)
New Revision: 36023

Modified:
   labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/FeedServletTest.java
Log:
BRMS-443 - fixed test case

Modified: labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/FeedServletTest.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/FeedServletTest.java	2010-11-22 17:18:30 UTC (rev 36022)
+++ labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/FeedServletTest.java	2010-11-22 17:19:36 UTC (rev 36023)
@@ -16,20 +16,18 @@
 
 package org.drools.guvnor.server.files;
 
+import junit.framework.TestCase;
 import org.drools.repository.RulesRepository;
 import org.drools.repository.PackageItem;
 import org.drools.repository.AssetItem;
+import org.drools.guvnor.server.security.MockIdentity;
 import org.drools.guvnor.server.util.TestEnvironmentSessionHelper;
 import org.drools.guvnor.server.ServiceImplementation;
 import org.apache.util.Base64;
-import org.jboss.seam.security.AuthorizationException;
-import org.junit.After;
-import org.junit.Test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertFalse;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.contexts.Lifecycle;
 
+
 import javax.servlet.http.HttpServletResponse;
 import java.io.ByteArrayOutputStream;
 import java.util.Map;
@@ -38,16 +36,30 @@
 /**
  * @author Michael Neale
  */
-public class FeedServletTest {
+public class FeedServletTest extends TestCase {
 
-	@Test
-    public void testPackageFeed() throws Exception {
+    public void testPackageFeed() throws Exception {        
         RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession( true ) );
         PackageItem pkg = repo.createPackage("testPackageFeed", "");
         AssetItem asset = pkg.addAsset("asset1", "desc");
         asset.updateFormat("drl");
         asset.checkin("");
+        
+        //Mock up SEAM contexts
+        Map application = new HashMap<String, Object>();
+        Lifecycle.beginApplication( application );
+        Lifecycle.beginCall();
+        MockIdentity midentity = new MockIdentity();
+        midentity.setIsLoggedIn(false);
+        midentity.setAllowLogin(false);
+        midentity.setCheckPermission(true);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
 
+ 
         Map<String, String> headers = new HashMap<String, String>() {
             {
                 put("Irrelevant", "garbage");
@@ -56,15 +68,16 @@
 
         MockHTTPRequest req = new MockHTTPRequest("/org.foo/feed/package?name=...", headers);
         FeedServlet fs = new FeedServlet();
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        MockHTTPResponse res = new MockHTTPResponse(out);
+        MockHTTPResponse res = new MockHTTPResponse();
         fs.doGet(req, res);
         assertEquals(HttpServletResponse.SC_UNAUTHORIZED, res.errorCode);
 
-        //try again with bad password
+        //try again with valid user and password
+        midentity.setAllowLogin(true);
+
         headers = new HashMap<String, String>() {
             {
-                put("Authorization", "BASIC " + new String(Base64.encode("test:password".getBytes())));
+                put("Authorization", "BASIC " + new String(Base64.encode("testuser:password".getBytes())));
             }
         };
         req = new MockHTTPRequest("/org.foo/feed/package", headers, new HashMap<String, String>() {
@@ -74,12 +87,10 @@
             }
         });
         fs = new FeedServlet();
-        out = new ByteArrayOutputStream();
-        res = new MockHTTPResponse(out);
+        res = new MockHTTPResponse();
         fs.doGet(req, res);
 
-
-        String r = new String(out.toByteArray());
+        String r = res.extractContent();
         assertNotNull(r);
 
         assertTrue(r.indexOf("asset1") > -1);
@@ -93,11 +104,10 @@
             }
         });
         fs = new FeedServlet();
-        out = new ByteArrayOutputStream();
-        res = new MockHTTPResponse(out);
+        res = new MockHTTPResponse();
         fs.doGet(req, res);
 
-        r = new String(out.toByteArray());
+        r = res.extractContent();
         assertNotNull(r);
 
         assertFalse(r.indexOf("asset1.drl") > -1);
@@ -110,17 +120,16 @@
             }
         });
         fs = new FeedServlet();
-        out = new ByteArrayOutputStream();
-        res = new MockHTTPResponse(out);
+        res = new MockHTTPResponse();
         fs.doGet(req, res);
 
-        r = new String(out.toByteArray());
+        r = res.extractContent();
         assertNotNull(r);
         assertTrue(r.indexOf("asset1") > -1);
-
+        
+        Lifecycle.endApplication();
     }
 
-	@Test
     public void testCategoryFeed() throws Exception {
         RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession( true ) );
         PackageItem pkg = repo.createPackage("testCategoryFeed", "");
@@ -130,11 +139,26 @@
         asset.updateCategoryList(new String[] {"testCategoryFeedCat"});
         asset.checkin("");
 
+        //Mock up SEAM contexts
+        Map application = new HashMap<String, Object>();
+        Lifecycle.beginApplication( application );
+        Lifecycle.beginCall();
+        MockIdentity midentity = new MockIdentity();
+        midentity.setIsLoggedIn(false);
+        midentity.setAllowLogin(true);
+        midentity.setCheckPermission(true);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );
+        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
 
-        //try again with bad password
+
+        //try with valid password
         HashMap<String, String> headers = new HashMap<String, String>() {
             {
-                put("Authorization", "BASIC " + new String(Base64.encode("test:password".getBytes())));
+                put("Authorization", "BASIC " + new String(Base64.encode("testuser:password".getBytes())));
             }
         };
         MockHTTPRequest req = new MockHTTPRequest("/org.foo/feed/category", headers, new HashMap<String, String>() {
@@ -143,12 +167,11 @@
                 put("viewUrl", "http://foo.bar");
             }
         });
-        MockFeedServlet fs = new MockFeedServlet();
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        MockHTTPResponse res = new MockHTTPResponse(out);
+        FeedServlet fs = new FeedServlet();
+        MockHTTPResponse res = new MockHTTPResponse();
         fs.doGet(req, res);
 
-        String r = new String(out.toByteArray());
+        String r = res.extractContent();
         assertNotNull(r);
 
         assertTrue(r.indexOf("asset1") > -1);
@@ -162,37 +185,47 @@
                 put("status", "*");
             }
         });
-        fs = new MockFeedServlet();
-        out = new ByteArrayOutputStream();
-        res = new MockHTTPResponse(out);
+        fs = new FeedServlet();
+        res = new MockHTTPResponse();
         fs.doGet(req, res);
 
-        r = new String(out.toByteArray());
+        r = res.extractContent();
         assertNotNull(r);
 
         assertTrue(r.indexOf("asset1") > -1);
         assertTrue(r.indexOf("http://foo.bar") > -1);
 
-
-        fs = new MockFeedServlet();
-        fs.throwAuthException = true;
-        out = new ByteArrayOutputStream();
-        res = new MockHTTPResponse(out);
+        
+        midentity.setAllowLogin(false);
+        fs = new FeedServlet();
+        res = new MockHTTPResponse();
         fs.doGet(req, res);
-
         assertEquals(HttpServletResponse.SC_UNAUTHORIZED, res.errorCode);
 
-
+        Lifecycle.endApplication();
     }
 
-
-	@Test
     public void testDiscussionFeed() throws Exception {
         RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession( true ) );
         PackageItem pkg = repo.createPackage("testDiscussionFeed", "");
         AssetItem asset = pkg.addAsset("asset1", "desc");
         asset.updateFormat("drl");
         asset.checkin("");
+        
+        //Mock up SEAM contexts
+        Map application = new HashMap<String, Object>();
+        Lifecycle.beginApplication( application );
+        Lifecycle.beginCall();
+        MockIdentity midentity = new MockIdentity();
+        midentity.setIsLoggedIn(false);
+        midentity.setAllowLogin(false);
+        midentity.setCheckPermission(true);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );
+        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
 
         ServiceImplementation impl = new ServiceImplementation();
         impl.repository = repo;
@@ -207,8 +240,7 @@
 
         MockHTTPRequest req = new MockHTTPRequest("/org.foo/feed/discussion?package=...", headers);
         FeedServlet fs = new FeedServlet();
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        MockHTTPResponse res = new MockHTTPResponse(out);
+        MockHTTPResponse res = new MockHTTPResponse();
         fs.doGet(req, res);
         assertEquals(HttpServletResponse.SC_UNAUTHORIZED, res.errorCode);
 
@@ -219,6 +251,7 @@
         };
 
 
+        midentity.setAllowLogin(true);
         req = new MockHTTPRequest("/org.foo/feed/discussion", headers, new HashMap<String, String>() {
             {
                 put("package", "testDiscussionFeed");
@@ -226,39 +259,16 @@
             }
         });
         fs = new FeedServlet();
-        out = new ByteArrayOutputStream();
-        res = new MockHTTPResponse(out);
+        res = new MockHTTPResponse();
         fs.doGet(req, res);
 
-        String r = new String(out.toByteArray());
+        String r = res.extractContent();
         assertNotNull(r);
         assertTrue(r.indexOf("This is a comment") > -1);
         assertTrue(r.indexOf("This is another comment") > r.indexOf("This is a comment"));
         System.err.println(r);
 
-
-
+        Lifecycle.endApplication();
     }
-
-
-    class MockFeedServlet extends FeedServlet {
-        boolean throwAuthException = false;
-        @Override
-        void checkCategoryPermission(String cat) {
-            if (throwAuthException) throw new AuthorizationException("NO");
-            super.checkCategoryPermission(cat);    //To change body of overridden methods use File | Settings | File Templates.
-        }
-
-        @Override
-        void checkPackageReadPermission(String packageName) {
-            if (throwAuthException) throw new AuthorizationException("NO");
-            super.checkPackageReadPermission(packageName);    //To change body of overridden methods use File | Settings | File Templates.
-        }
-    }
     
-    @After
-    public void tearDown() throws Exception {
-    	TestEnvironmentSessionHelper.shutdown();
-    }
-    
 }



More information about the jboss-svn-commits mailing list