From 15d2b2133e7e56fe7084b59c21f85ff47bb6e37c Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Thu, 13 Sep 2012 14:48:37 +0200
Subject: [PATCH] OO-302: fix a unit tests of the REST API, update testing
 readme

---
 TESTING.README.LATEST                         | 29 +++++++------------
 .../org/olat/login/FunctionalLoginTest.java   | 16 ++--------
 .../restapi/CoursesContactElementTest.java    |  1 +
 .../org/olat/restapi/CoursesElementsTest.java | 20 ++++++-------
 .../profile/mysql/olat.arquillian.properties  |  3 +-
 .../profile/mysql/olat.eclipse.properties     |  5 ----
 src/test/resources/arquillian.xml             |  9 ++++--
 7 files changed, 30 insertions(+), 53 deletions(-)

diff --git a/TESTING.README.LATEST b/TESTING.README.LATEST
index beca74ea368..ec1fd2c0587 100644
--- a/TESTING.README.LATEST
+++ b/TESTING.README.LATEST
@@ -3,18 +3,12 @@ junit and integration tests in OpenOLAT 8
 
 * Preconditions
 -----------------------------------------
-- Make sure the following ports are not in use (Selenium, Tomcat 1, Tomcat 2, JMS, MBeanServer(s) )
-	4444 / 8181 / 8105 / 8109 / 8282 / 8205 / 8209 / 61616 / 3000 / 3001 / 3002
+- Make sure the following ports are not in use (Selenium, Tomcat )
+	14444 / 8080 / 8009 / 8089 
 
 - Make sure you have a MySQL database version 5.1 or 5.5 with the InnoDB as default engine 
   The server must be at localhost. To change the config see databaseCorecontext.xml
 
-- Edit src/test/profile/selenium/config/defaultcontext.xml and add the browserID line (Firefox 3.x only supported):
-    E.g. Firefox on linux box
-     <entry key="browserId" value="*firefox /usr/lib64/firefox/firefox"/>
-    E.g. Firefox verison on a mac
-     <entry key="browserId" value="*firefox /Applications/Firefox3.5.app/Contents/MacOS/firefox-bin"/>
-
 - Clone a clean project or remove any olat.local.properties you might have on the classpath. This is important!
 	
 - Make sure maven has enough memory. E.g execute the following:
@@ -26,9 +20,6 @@ junit and integration tests in OpenOLAT 8
 
 * Setup (necessary only once)
 -----------------------------------------
-- Install tomcats
-	mvn verify -Pinstall-tomcats
-
 - Setup database users and tables in the pom.xml. The default settings are:
     <test.env.db.name>olattest</test.env.db.name>
     <test.env.db.user>olat</test.env.db.user>
@@ -66,17 +57,17 @@ junit and integration tests in OpenOLAT 8
 
 * Execute selenium functional integration tests
 -----------------------------------------
-- Selenium functional tests on GUI layer that load OpenOLAT in a Tomcat 7.0.27 managed by Arquillian
-	mvn package
+- First build the application without tests (arquillian need the library before mvn give them)
+    mvn -DskipTests=true -Parquillian clean package
+    
+- Run the tests
 	mvn -Parquillian surefire:test
 
 
 * Execute a single selenium functional integration test in Eclipse
 -----------------------------------------
-- Start Eclipse, refresh project and wait until Eclipse is finished with refreshing and compiling
-
-- Run the maven build to fill the libraries (The deployment take all the libraries from the standard build process)
-  mvn clean package
-
-- In Eclipse right-click a selenium testcase and select "run in jUnit"
+- First build the application without tests as before
+    mvn -DskipTests=true -Parquillian clean package
+ 
+- Run single test as JUnit Test in Eclipse
 
diff --git a/src/test/java/org/olat/login/FunctionalLoginTest.java b/src/test/java/org/olat/login/FunctionalLoginTest.java
index 3c67a8f3dba..e6fd7efe385 100644
--- a/src/test/java/org/olat/login/FunctionalLoginTest.java
+++ b/src/test/java/org/olat/login/FunctionalLoginTest.java
@@ -22,7 +22,6 @@ package org.olat.login;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -33,6 +32,7 @@ import javax.ws.rs.core.UriBuilder;
 
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpPut;
+import org.apache.http.util.EntityUtils;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.container.test.api.RunAsClient;
 import org.jboss.arquillian.drone.api.annotation.Drone;
@@ -112,10 +112,8 @@ public class FunctionalLoginTest {
 	@Test
 	@RunAsClient
 	public void loginWithRandomUser() throws IOException, URISyntaxException{
-
 		RestConnection restConnection = new RestConnection(deploymentUrl);
 
-
 		Assert.assertTrue(restConnection.login(functionalUtil.getUsername(), functionalUtil.getPassword()));
 
 		UserVO vo = new UserVO();
@@ -132,7 +130,6 @@ public class FunctionalLoginTest {
 		vo.putProperty("gender", "Female");//male or female
 		vo.putProperty("birthDay", "12/12/2009");
 
-
 		URI request = UriBuilder.fromUri(deploymentUrl.toURI()).path("restapi").path("users").build();
 		HttpPut method = restConnection.createPut(request, MediaType.APPLICATION_JSON, true);
 		restConnection.addJsonEntity(method, vo);
@@ -140,20 +137,11 @@ public class FunctionalLoginTest {
 
 		HttpResponse response = restConnection.execute(method);
 		assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
-		InputStream body = response.getEntity().getContent();
+		EntityUtils.consume(response.getEntity());
 
 		functionalUtil.setDeploymentUrl(deploymentUrl.toString());
 		Assert.assertTrue(functionalUtil.login(browser));
 
-
-		//Identity savedIdent = BaseSecurityManager.getInstance().findIdentityByName(username); doesn't work
-
-		//		URI deleteRequest = UriBuilder.fromUri(deploymentUrl.toURI()).path("restapi").path("/users/" + savedIdent.getKey()).build();
-		//		HttpDelete deleteMethod = restConnection.createDelete(deleteRequest, MediaType.APPLICATION_XML, true);
-		//		HttpResponse deleteResponse = restConnection.execute(deleteMethod);
-		//		assertEquals(200, deleteResponse.getStatusLine().getStatusCode());
-
-
 		restConnection.shutdown();
 	}
 }
diff --git a/src/test/java/org/olat/restapi/CoursesContactElementTest.java b/src/test/java/org/olat/restapi/CoursesContactElementTest.java
index 7fa2f00f8d0..9eca11c8a5b 100644
--- a/src/test/java/org/olat/restapi/CoursesContactElementTest.java
+++ b/src/test/java/org/olat/restapi/CoursesContactElementTest.java
@@ -164,6 +164,7 @@ public class CoursesContactElementTest extends OlatJerseyTestCase {
 		assertEquals(config.getBooleanEntry(CONFIG_KEY_EMAILTOCOACHES), true);
 		assertEquals(config.getBooleanEntry(CONFIG_KEY_EMAILTOPARTICIPANTS), true);
 		
+		@SuppressWarnings("unchecked")
 		List<String> tos = (List<String>)config.get(CONFIG_KEY_EMAILTOADRESSES);
 		assertNotNull(tos);
 		assertEquals(2, tos.size());
diff --git a/src/test/java/org/olat/restapi/CoursesElementsTest.java b/src/test/java/org/olat/restapi/CoursesElementsTest.java
index 09ce1ceb94c..d396acbcbfd 100644
--- a/src/test/java/org/olat/restapi/CoursesElementsTest.java
+++ b/src/test/java/org/olat/restapi/CoursesElementsTest.java
@@ -39,6 +39,8 @@ import java.net.URL;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.UriBuilder;
 
+import junit.framework.Assert;
+
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
@@ -147,8 +149,6 @@ public class CoursesElementsTest extends OlatJerseyTestCase {
 		
 		URI newPageUri = getElementsUri(course).path("singlepage").build();
 		HttpPost newPageMethod = conn.createPost(newPageUri, MediaType.APPLICATION_JSON, true);
-		newPageMethod.addHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
-
 		MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
 		entity.addPart("file", new FileBody(page));
 		entity.addPart("filename", new StringBody(page.getName()));
@@ -366,7 +366,6 @@ public class CoursesElementsTest extends OlatJerseyTestCase {
 			.queryParam("longTitle", "Single-Page-long-0")
 			.queryParam("objectives", "Single-Page-objectives-0").build();
 		HttpPut newPageMethod = conn.createPut(newPageUri, MediaType.APPLICATION_JSON, true);
-		newPageMethod.addHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
 		conn.addMultipart(newPageMethod, page.getName(), page);
 		
 		HttpResponse newPageCode = conn.execute(newPageMethod);
@@ -524,22 +523,23 @@ public class CoursesElementsTest extends OlatJerseyTestCase {
 		assertEquals(enNode.getParentId(), course.getEditorRootNodeId());
 		
 		//create a test node
-		URL cpUrl = CoursesElementsTest.class.getResource("qti-demo.zip");
-		assertNotNull(cpUrl);
-		File cp = new File(cpUrl.toURI());
+		URL qtiDemoUrl = CoursesElementsTest.class.getResource("qti-demo.zip");
+		assertNotNull(qtiDemoUrl);
+		File qtiFile = new File(qtiDemoUrl.toURI());
+		Assert.assertEquals(7518, qtiFile.length());
 
 		URI repoEntriesUri = UriBuilder.fromUri(getContextURI()).path("repo/entries").build();
 		HttpPut qtiRepoMethod = conn.createPut(repoEntriesUri, MediaType.APPLICATION_JSON, true);
-		qtiRepoMethod.addHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
 		MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
-		entity.addPart("file", new FileBody(cp));
+		entity.addPart("file", new FileBody(qtiFile));
 		entity.addPart("filename", new StringBody("qti-demo.zip"));
 		entity.addPart("resourcename", new StringBody("QTI demo"));
 		entity.addPart("displayname", new StringBody("QTI demo"));
 		qtiRepoMethod.setEntity(entity);
 		
 		HttpResponse qtiRepoCode = conn.execute(qtiRepoMethod);
-		assertTrue(qtiRepoCode.getStatusLine().getStatusCode() == 200 || qtiRepoCode.getStatusLine().getStatusCode() == 201);
+		int qtiHttpCode = qtiRepoCode.getStatusLine().getStatusCode();
+		assertTrue(qtiHttpCode == 200 || qtiHttpCode == 201);
 		RepositoryEntryVO newTestVO = conn.parse(qtiRepoCode, RepositoryEntryVO.class);
 		assertNotNull(newTestVO);
 		
@@ -604,7 +604,6 @@ public class CoursesElementsTest extends OlatJerseyTestCase {
 
 		URI repoEntriesUri2 = UriBuilder.fromUri(getContextURI()).path("repo").path("entries").build();
 		HttpPut surveyRepoMethod = conn.createPut(repoEntriesUri2, MediaType.APPLICATION_JSON, true);
-		surveyRepoMethod.addHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
 		MultipartEntity surveyEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
 		surveyEntity.addPart("file", new FileBody(surveyFile));
 		surveyEntity.addPart("filename", new StringBody("questionnaire-demo.zip"));
@@ -830,7 +829,6 @@ public class CoursesElementsTest extends OlatJerseyTestCase {
 		//update the root node
 		URI rootUri = getElementsUri(course).path("structure").path(course.getEditorRootNodeId()).build();
 		HttpPost newStructureMethod = conn.createPost(rootUri, MediaType.APPLICATION_JSON, true);
-		newStructureMethod.addHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
 		MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
 		entity.addPart("file", new FileBody(page));
 		entity.addPart("filename", new StringBody(page.getName()));
diff --git a/src/test/profile/mysql/olat.arquillian.properties b/src/test/profile/mysql/olat.arquillian.properties
index 35398f0e0fb..879959dae94 100644
--- a/src/test/profile/mysql/olat.arquillian.properties
+++ b/src/test/profile/mysql/olat.arquillian.properties
@@ -12,6 +12,7 @@ db.show_sql=false
 archive.dir=
 log.dir=
 folder.root=
+restapi.enable=true
 
 instance.id=2
 
@@ -23,8 +24,6 @@ localization.cache=true
 is.translation.server=disabled
 deploy.course.exports=false
 
-restapi.enable=true
-
 # for UserTest
 keepUserEmailAfterDeletion=true
 keepUserLoginAfterDeletion=true
diff --git a/src/test/profile/mysql/olat.eclipse.properties b/src/test/profile/mysql/olat.eclipse.properties
index e8410493bcb..5d4a513a70e 100644
--- a/src/test/profile/mysql/olat.eclipse.properties
+++ b/src/test/profile/mysql/olat.eclipse.properties
@@ -35,12 +35,10 @@ codepoint.jms.broker.url=vm://embedded?broker.persistent=false
 #search.broker.url=failover:(tcp://localhost:${test.env.jms.broker.port}?wireFormat.maxInactivityDuration=0)
 #codepoint.jms.broker.url=failover:(tcp://localhost:${test.env.jms.broker.port}?wireFormat.maxInactivityDuration=0)
 
-
 # enable/disable codepoint/breakpoint framework
 codepoint_server.enabled=true
 smtp.host=testing
 
-#
 #
 # if you like to run your tests against mysql just uncomment the following. Please do not commit it!
 #
@@ -52,6 +50,3 @@ db.host.port=3306
 db.database.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
 db.hibernate.ddl.auto=
 auto.upgrade.database=false
-
-restapi.enable=true
-
diff --git a/src/test/resources/arquillian.xml b/src/test/resources/arquillian.xml
index 81bad42f3f0..0c057af6bf1 100644
--- a/src/test/resources/arquillian.xml
+++ b/src/test/resources/arquillian.xml
@@ -12,14 +12,19 @@
         <configuration>
             <property name="workDir">target/arquillianDeployments</property>
 			<property name="catalinaHome">target/arq-apache-tomcat-7.0.27</property>
-		<!-- <property name="javaVmArguments">-Xmx512m -XX:MaxPermSize=128m -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y</property>  -->
+			<property name="javaVmArguments">-Xmx512m -XX:MaxPermSize=128m</property>
+			<!-- debugging arguments for javaVm: -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y -->
         </configuration>
     </container>
     
 	<extension qualifier="selenium">
 		<property name="browserCapabilities">firefox</property>
+		<!-- Path for Firefox if needed
 		<property name="capabilityWebdriverFirefoxBin">/Applications/Firefox.app/Contents/MacOS/firefox-bin</property>
-        <!-- <property name="browser">*googlechrome</property> -->
+        -->
+        <!-- Not all tests pass with Chrome (issue with file upload)
+        <property name="browser">*googlechrome</property>
+        -->
     </extension>
 
 </arquillian>
\ No newline at end of file
-- 
GitLab