cancel
Showing results for 
Search instead for 
Did you mean: 

Webscript unit test

aitbenmouh
Champ in-the-making
Champ in-the-making
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.
3 REPLIES 3

mitpatoliya
Star Collaborator
Star Collaborator
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.

aitbenmouh
Champ in-the-making
Champ in-the-making
I will try thanks.

aitbenmouh
Champ in-the-making
Champ in-the-making
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 :
The attribute value is undefined for the annotation type ContextConfiguration 


even i added my RemoteProject to the project classpath.