[jboss-svn-commits] JBL Code SVN: r36022 - 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:18:30 EST 2010


Author: tsurdilovic
Date: 2010-11-22 12:18:30 -0500 (Mon, 22 Nov 2010)
New Revision: 36022

Modified:
   labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/RestAPIServletTest.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/RestAPIServletTest.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/RestAPIServletTest.java	2010-11-22 17:17:03 UTC (rev 36021)
+++ labs/jbossrules/soa_branches/BRMS-5.1.x/drools-guvnor/src/test/java/org/drools/guvnor/server/files/RestAPIServletTest.java	2010-11-22 17:18:30 UTC (rev 36022)
@@ -22,43 +22,22 @@
 import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
-import javax.jcr.Session;
 
 import junit.framework.TestCase;
 
 import org.apache.util.Base64;
+import org.drools.guvnor.server.security.MockIdentity;
 import org.drools.guvnor.server.util.TestEnvironmentSessionHelper;
 import org.drools.repository.AssetItem;
 import org.drools.repository.AssetItemIterator;
 import org.drools.repository.PackageItem;
 import org.drools.repository.RulesRepository;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.contexts.Lifecycle;
 
 public class RestAPIServletTest extends TestCase {
 
-
-	public void testUnpack() {
-		String b42 = "BASIC " + new String( Base64.encode("user:pass".getBytes()) );
-		RestAPIServlet serv = new RestAPIServlet();
-		String[] d = serv.unpack(b42);
-		assertEquals("user", d[0]);
-		assertEquals("pass", d[1]);
-
-	}
-
-	public void testAllowUser() {
-		RestAPIServlet serv = new RestAPIServlet();
-		assertFalse(serv.allowUser(null));
-		assertFalse(serv.allowUser(""));
-		assertFalse(serv.allowUser("bgoo"));
-		String b42 = "BASIC " + new String( Base64.encode("user:pass".getBytes()) );
-		assertFalse(serv.allowUser(b42));
-		b42 = "BASIC " + new String( Base64.encode("test:password".getBytes()) );
-		assertTrue(serv.allowUser(b42));
-	}
-
-
-
-	public void testGet() throws Exception {
+	public void testGet() throws Exception {        
 		RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession( true ) );
 		PackageItem pkg = repo.createPackage("testGetRestServlet", "");
 		AssetItem ass = pkg.addAsset("asset1", "");
@@ -66,8 +45,21 @@
 		ass.updateContent("some content");
 		ass.checkin("hey ho");
 
-
-
+        //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);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
+        Contexts.getSessionContext().set( "repository", repo );
+        
+        
 		RestAPIServlet serv = new RestAPIServlet();
 		assertNotNull(serv.getAPI());
 		Map<String, String> headers = new HashMap<String, String>() {
@@ -78,7 +70,7 @@
 		String uri = "http://loser/api/packages/testGetRestServlet/asset1.drl";
 		MockHTTPRequest req = new MockHTTPRequest(uri, headers);
 
-		MockHTTPResponse res = new MockHTTPResponse(new ByteArrayOutputStream());
+		MockHTTPResponse res = new MockHTTPResponse();
 
 		//try with no password
 		serv.doGet(req, res);
@@ -93,64 +85,80 @@
 			}
 		};
 		req = new MockHTTPRequest(uri, headers);
-		res = new MockHTTPResponse(new ByteArrayOutputStream());
+		res = new MockHTTPResponse();
 		serv.doGet(req, res);
 		assertEquals(HttpServletResponse.SC_UNAUTHORIZED, res.errorCode);
 		assertTrue(res.headers.containsKey("WWW-Authenticate"));
 
 		//finally, making it work
+        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())));
 			}
 		};
 
-		ByteArrayOutputStream out = new ByteArrayOutputStream();
 		req = new MockHTTPRequest(uri, headers);
-		res = new MockHTTPResponse(out);
+		res = new MockHTTPResponse();
 		serv.doGet(req, res);
 
 		assertEquals(0, res.errorCode);
-		String data = out.toString();
+		String data = res.extractContent();
 		assertEquals("some content", data);
 
-		assertEquals("application/x-download", res.contentType);
+		assertEquals("application/x-download", res.getContentType());
 		assertEquals(true, res.containsHeader("Content-Disposition"));
 
 		//now try getting some version listings
-		out = new ByteArrayOutputStream();
 		req = new MockHTTPRequest(uri, headers);
 		req.queryString = "version=all";
-		res = new MockHTTPResponse(out);
+		res = new MockHTTPResponse();
 		serv.doGet(req, res);
 
 		assertEquals(0, res.errorCode);
-		data = out.toString();
+		data = res.extractContent();
 		assertFalse("some content".equals(data));
 		assertNotNull(data);
 
+        Lifecycle.endApplication();
 	}
 
 	public void testPost() throws Exception {
 		RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession() );
 		PackageItem pkg = repo.createPackage("testPostRestServlet", "");
 
-		HashMap<String, String> headers = new HashMap<String, String>() {
+        //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);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
+        Contexts.getSessionContext().set( "repository", repo );
+        
+        
+		Map<String, String> headers = new HashMap<String, String>() {
 			{
 				put("Authorization", "BASIC " + new String(Base64.encode("test:password".getBytes())));
 			}
-		};
+		};		
 
 		ByteArrayInputStream in = new ByteArrayInputStream("some new content".getBytes());
 		RestAPIServlet serv = new RestAPIServlet();
 		MockHTTPRequest req = new MockHTTPRequest("http://foo/api/packages/testPostRestServlet/asset1.drl", headers, in);
 
-		MockHTTPResponse res = new MockHTTPResponse(null);
+		MockHTTPResponse res = new MockHTTPResponse();
 		serv.doPost(req, res);
 
-		assertEquals("OK", res.stringWriter.toString());
+		assertEquals("OK", res.extractContent());
 
-		AssetItemIterator it = pkg.listAssetsByFormat(new String[] {"drl"});
+		AssetItemIterator it = pkg.listAssetsByFormat("drl");
 		AssetItem ass = it.next();
 		assertEquals("asset1", ass.getName());
 		assertEquals("drl", ass.getFormat());
@@ -160,11 +168,10 @@
 
 		in = new ByteArrayInputStream("more content".getBytes());
 		req = new MockHTTPRequest("http://foo/api/packages/testPostRestServlet/asset2.xls", headers, in);
-		res = new MockHTTPResponse(null);
+		res = new MockHTTPResponse();
 		serv.doPost(req, res);
-		assertEquals("OK", res.stringWriter.toString());
-       
-		pkg.getNode().refresh(false);
+		assertEquals("OK", res.extractContent());
+
 		AssetItem ass2 = pkg.loadAsset("asset2");
 		assertEquals("xls", ass2.getFormat());
 		assertTrue(ass2.isBinary());
@@ -173,21 +180,36 @@
 		String out = new String(ass2.getBinaryContentAsBytes());
 		assertEquals("more content", out);
 
-		
         repo.logout();
 
+        Lifecycle.endApplication();
 	}
 
 	public void testPut() throws Exception {
-
 		RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession() );
 		PackageItem pkg = repo.createPackage("testPutRestServlet", "");
 		AssetItem ass = pkg.addAsset("asset1", "abc");
 		ass.updateFormat("drl");
 		ass.checkin("");
 		long ver = ass.getVersionNumber();
+		
+		
+        //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);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
+        Contexts.getSessionContext().set( "repository", repo );
 
-		HashMap<String, String> headers = new HashMap<String, String>() {
+        
+		Map<String, String> headers = new HashMap<String, String>() {
 			{
 				put("Authorization", "BASIC " + new String(Base64.encode("test:password".getBytes())));
 				put("Checkin-Comment", "hey ho");
@@ -199,18 +221,19 @@
 		MockHTTPRequest req = new MockHTTPRequest("http://foo/api/packages/testPutRestServlet/asset1.drl", headers, in);
 
 
-		MockHTTPResponse res = new MockHTTPResponse(null);
+		MockHTTPResponse res = new MockHTTPResponse();
 		serv.doPut(req, res);
 
-		assertEquals("OK", res.stringWriter.toString());
+		assertEquals("OK", res.extractContent());
 
 		ass = pkg.loadAsset("asset1");
-		pkg.getNode().refresh(false);
 		assertEquals("some new content", ass.getContent());
 		assertEquals(ver + 1, ass.getVersionNumber());
 		assertEquals("hey ho", ass.getCheckinComment());
 
         repo.logout();
+
+        Lifecycle.endApplication();        
 	}
 
 
@@ -220,8 +243,23 @@
 		AssetItem ass = pkg.addAsset("asset1", "abc");
 		ass.updateFormat("drl");
 		ass.checkin("");
-
-		HashMap<String, String> headers = new HashMap<String, String>() {
+		
+        //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);
+        Contexts.getSessionContext().set( "org.jboss.seam.security.identity",
+                                          midentity );        
+        FileManagerUtils manager = new FileManagerUtils();
+        manager.setRepository(repo);
+        Contexts.getSessionContext().set( "fileManager", manager );
+        Contexts.getSessionContext().set( "repository", repo );
+        
+        
+		Map<String, String> headers = new HashMap<String, String>() {
 			{
 				put("Authorization", "BASIC " + new String(Base64.encode("test:password".getBytes())));
 			}
@@ -231,10 +269,10 @@
 		RestAPIServlet serv = new RestAPIServlet();
 		MockHTTPRequest req = new MockHTTPRequest("http://foo/api/packages/testDeleteRestServlet/asset1.drl", headers, in);
 
-		MockHTTPResponse res = new MockHTTPResponse(null);
+		MockHTTPResponse res = new MockHTTPResponse();
 		serv.doDelete(req, res);
 
-		assertEquals("OK", res.stringWriter.toString());
+		assertEquals("OK", res.extractContent());
 
 
 
@@ -243,5 +281,6 @@
 
         repo.logout();
 
+        Lifecycle.endApplication();
 	}
 }



More information about the jboss-svn-commits mailing list