Webscript unit test
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2013 11:30 AM
Hello everybody,
Im trying to do some unit test of my webscripts but not success yet, if someone have already done an exemple with SDK in eclipse please give us steps thanks.
I'm developping my controller with Java backend and so i need to do dome tests with JUNIT before deploying. I read smothing about mock server in Alfresco wiki but it's not clair.
Im trying to do some unit test of my webscripts but not success yet, if someone have already done an exemple with SDK in eclipse please give us steps thanks.
I'm developping my controller with Java backend and so i need to do dome tests with JUNIT before deploying. I read smothing about mock server in Alfresco wiki but it's not clair.
Labels:
- Labels:
-
Archive
3 REPLIES 3
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2013 07:46 AM
Have you checked this?
https://forums.alfresco.com/forum/developer-discussions/web-scripts/unittest-upload-file-webscript-0...
There is a link given for sample test class for unit testing of webscript.
https://forums.alfresco.com/forum/developer-discussions/web-scripts/unittest-upload-file-webscript-0...
There is a link given for sample test class for unit testing of webscript.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2013 05:44 AM
I will try thanks.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2013 07:00 AM
Hello,
This what i wrote in my classe Test :
<blockcode>
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.util.GUID;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.runner.RunWith;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:system-test-context.xml")
public class HelloWorld extends BaseWebScriptTest {
private static final String URL_SITES = "/api/sites";
private static final String URL_MEMBERSHIPS = "/memberships";
private static final String USER_TWO = "UserTwo";
private static final String USER_ONE = "UserOne";
private static final String USER_ADMIN = "admin";
private SiteService siteService;
private AuthenticationComponent authenticationComponent;
public void setUp() throws Exception {
super.setUp();
this.siteService = (SiteService) getServer().getApplicationContext()
.getBean("SiteService");
this.authenticationComponent = (AuthenticationComponent) getServer()
.getApplicationContext().getBean("authenticationComponent");
// Authenticate as user
this.authenticationComponent.setCurrentUser(USER_ADMIN);
}
public void testPostMemberships() throws Exception {
// Create a site
String shortName = GUID.generate();
siteService.createSite("myPreset", shortName, "myTitle",
"myDescription", true);
// Build the JSON membership object
JSONObject membership = new JSONObject();
membership.put("role", SiteModel.SITE_CONSUMER);
JSONObject person = new JSONObject();
person.put("userName", USER_TWO);
membership.put("person", person);
// Post the memebership
Response response = sendRequest(new PostRequest(URL_SITES + "/"
+ shortName + URL_MEMBERSHIPS, membership.toString(),
"application/json"), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// Check the result
assertEquals(SiteModel.SITE_CONSUMER, membership.get("role"));
assertEquals(USER_TWO, membership.getJSONObject("person").get(
"userName"));
// Get the membership list
response = sendRequest(new GetRequest(URL_SITES + "/" + shortName
+ URL_MEMBERSHIPS), 200);
JSONArray result2 = new JSONArray(response.getContentAsString());
assertNotNull(result2);
assertEquals(2, result2.length());
}
public void testGetMembership() throws Exception {
// Create a site
String shortName = GUID.generate();
siteService.createSite("myPreset", shortName, "myTitle",
"myDescription", true);
// Test error conditions
sendRequest(new GetRequest(URL_SITES + "/badsite" + URL_MEMBERSHIPS
+ "/" + USER_ONE), 404);
sendRequest(new GetRequest(URL_SITES + "/" + shortName
+ URL_MEMBERSHIPS + "/baduser"), 404);
sendRequest(new GetRequest(URL_SITES + "/" + shortName
+ URL_MEMBERSHIPS + "/" + USER_TWO), 404);
// Test GET Membership
Response response = sendRequest(new GetRequest(URL_SITES + "/"
+ shortName + URL_MEMBERSHIPS + "/" + USER_ONE), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// Check the result
assertEquals(SiteModel.SITE_MANAGER, result.get("role"));
assertEquals(USER_ONE, result.getJSONObject("person").get("userName"));
}
}</blockcode>
But it can't find the second annotation :
even i added my RemoteProject to the project classpath.
This what i wrote in my classe Test :
<blockcode>
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.util.GUID;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.runner.RunWith;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:system-test-context.xml")
public class HelloWorld extends BaseWebScriptTest {
private static final String URL_SITES = "/api/sites";
private static final String URL_MEMBERSHIPS = "/memberships";
private static final String USER_TWO = "UserTwo";
private static final String USER_ONE = "UserOne";
private static final String USER_ADMIN = "admin";
private SiteService siteService;
private AuthenticationComponent authenticationComponent;
public void setUp() throws Exception {
super.setUp();
this.siteService = (SiteService) getServer().getApplicationContext()
.getBean("SiteService");
this.authenticationComponent = (AuthenticationComponent) getServer()
.getApplicationContext().getBean("authenticationComponent");
// Authenticate as user
this.authenticationComponent.setCurrentUser(USER_ADMIN);
}
public void testPostMemberships() throws Exception {
// Create a site
String shortName = GUID.generate();
siteService.createSite("myPreset", shortName, "myTitle",
"myDescription", true);
// Build the JSON membership object
JSONObject membership = new JSONObject();
membership.put("role", SiteModel.SITE_CONSUMER);
JSONObject person = new JSONObject();
person.put("userName", USER_TWO);
membership.put("person", person);
// Post the memebership
Response response = sendRequest(new PostRequest(URL_SITES + "/"
+ shortName + URL_MEMBERSHIPS, membership.toString(),
"application/json"), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// Check the result
assertEquals(SiteModel.SITE_CONSUMER, membership.get("role"));
assertEquals(USER_TWO, membership.getJSONObject("person").get(
"userName"));
// Get the membership list
response = sendRequest(new GetRequest(URL_SITES + "/" + shortName
+ URL_MEMBERSHIPS), 200);
JSONArray result2 = new JSONArray(response.getContentAsString());
assertNotNull(result2);
assertEquals(2, result2.length());
}
public void testGetMembership() throws Exception {
// Create a site
String shortName = GUID.generate();
siteService.createSite("myPreset", shortName, "myTitle",
"myDescription", true);
// Test error conditions
sendRequest(new GetRequest(URL_SITES + "/badsite" + URL_MEMBERSHIPS
+ "/" + USER_ONE), 404);
sendRequest(new GetRequest(URL_SITES + "/" + shortName
+ URL_MEMBERSHIPS + "/baduser"), 404);
sendRequest(new GetRequest(URL_SITES + "/" + shortName
+ URL_MEMBERSHIPS + "/" + USER_TWO), 404);
// Test GET Membership
Response response = sendRequest(new GetRequest(URL_SITES + "/"
+ shortName + URL_MEMBERSHIPS + "/" + USER_ONE), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// Check the result
assertEquals(SiteModel.SITE_MANAGER, result.get("role"));
assertEquals(USER_ONE, result.getJSONObject("person").get("userName"));
}
}</blockcode>
But it can't find the second annotation :
The attribute value is undefined for the annotation type ContextConfiguration
even i added my RemoteProject to the project classpath.
