io.personium.test.jersey.cell.ctl.AccountTest.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.test.jersey.cell.ctl.AccountTest.java

Source

    /**
     * personium.io
     * Copyright 2014 FUJITSU LIMITED
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package io.personium.test.jersey.cell.ctl;

    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertNull;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Calendar;

    import org.apache.http.HttpHeaders;
    import org.apache.http.HttpStatus;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.junit.experimental.categories.Category;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import io.personium.common.auth.token.AbstractOAuth2Token.TokenDsigException;
    import io.personium.common.auth.token.AbstractOAuth2Token.TokenParseException;
    import io.personium.common.auth.token.AbstractOAuth2Token.TokenRootCrtException;
    import io.personium.common.auth.token.TransCellAccessToken;
    import io.personium.core.model.Box;
    import io.personium.test.categories.Integration;
    import io.personium.test.categories.Regression;
    import io.personium.test.categories.Unit;
    import io.personium.test.jersey.AbstractCase;
    import io.personium.test.jersey.ODataCommon;
    import io.personium.test.jersey.PersoniumRequest;
    import io.personium.test.jersey.PersoniumResponse;
    import io.personium.test.unit.core.UrlUtils;
    import io.personium.test.utils.AccountUtils;
    import io.personium.test.utils.Http;
    import io.personium.test.utils.ResourceUtils;
    import io.personium.test.utils.RoleUtils;
    import io.personium.test.utils.TResponse;

    /**
     * Account???IT.
     */
@Category({Unit.class, Integration.class, Regression.class })
public class AccountTest extends ODataCommon {
    /**
     * .
     */
    private static Logger log = LoggerFactory.getLogger(AccountTest.class);

    static String cellName = "cellname";

    /**
     * . ?super?????
     */
    public AccountTest() {
        super("io.personium.core.rs");
    }

    /**
     * ????????.
     */
    @BeforeClass
    public static void beforeClass() {
        // DB???????????Cell????409????????????????Cell???
        cellName = cellName + Long.toString(Calendar.getInstance().getTimeInMillis());
        log.debug(AccountTest.class.toString() + " BeforeClass:cellName=" + cellName);
    }

    /**
     * ???????.
     */
    @Before
    public void before() {
        Http.request("cell-create.txt")
                .with("token", AbstractCase.BEARER_MASTER_TOKEN)
                .with("cellPath", cellName)
                .returns();
        super.setResponse(null);
    }

    /**
     * ???????.
     */
    @After
    public void after() {
    }

    /**
     * ????. Account=>?=>. Role=>?=>. AccountRole??=>?=>. ??=>Role=>Account.
     * @throws TokenParseException TokenParseException
     * @throws TokenRootCrtException TokenRootCrtException
     * @throws TokenDsigException TokenDsigException
     */
    @Test
    public final void crud() throws TokenParseException, TokenDsigException, TokenRootCrtException {
        String testAccountName = "test_account";
        String testAccountPass = "password";
        String accUrl = this.createAccount(testAccountName, testAccountPass);
        log.debug(accUrl);

        String testRoleName = "testRole";
        String roleUrl = this.createRole(testRoleName);
        log.debug(roleUrl);

        // ???C
        Http.request("link-account-role.txt")
                .with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName)
                .with("username", testAccountName)
                .with("roleUrl", roleUrl)
                .returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);

        // DcRequest req = DcRequest.post(UrlUtils.accountLinks(cellName, testAccountName));
        // req.header(HttpHeaders.AUTHORIZATION, MASTER_TOKEN).addJsonBody("uri", roleUrl);
        // DcResponse res = request(req);
        // assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode());

        // ????R(?)
        // TODO ??????
        log.debug("koooooooo");

        PersoniumRequest req = PersoniumRequest.get(UrlUtils.accountLinks(cellName, testAccountName))
                .header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).header(HttpHeaders.ACCEPT, "application/json");
        PersoniumResponse res = request(req);
        assertEquals(HttpStatus.SC_OK, res.getStatusCode());
        JSONObject linkJson = res.bodyAsJson();
        JSONArray linksResults = getResultsFromEntitysResponse(linkJson);
        JSONObject linkJson2 = (JSONObject) linksResults.get(0);
        assertEquals(roleUrl, linkJson2.get("uri"));

        // ????U
        // TODO UPDATE??????

        // BOX?
        String boxName = "testbox";
        String boxSchema = "https://example.com/hogecell/";
        createBox2(boxName, boxSchema);

        // ??
        // grant_type=password&username={username}&password={password}
        req = PersoniumRequest.post(UrlUtils.auth(cellName));
        String authBody =
                String.format("grant_type=password&username=%s&password=%s", testAccountName, testAccountPass);
        req.header(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").addStringBody(authBody);
        res = request(req);
        assertEquals(HttpStatus.SC_OK, res.getStatusCode());
        JSONObject token = res.bodyAsJson();
        assertNotNull(token);

        // ??
        req = PersoniumRequest.post(UrlUtils.auth(cellName));
        authBody = String.format("grant_type=password&username=%s&password=%s&p_target=%s",
                testAccountName, testAccountPass, "https://example.com/testcell");
        req.header(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").addStringBody(authBody);
        res = request(req);
        assertEquals(HttpStatus.SC_OK, res.getStatusCode());
        token = res.bodyAsJson();
        assertNotNull(token);
        String accessToken = null;
        accessToken = (String) token.get("access_token");
        assertNotNull(accessToken);
        TransCellAccessToken tcat = TransCellAccessToken.parse(accessToken);
        String url = UrlUtils.cellRoot(cellName);
        assertEquals(testRoleName, tcat.getRoles().get(0).getName());
        assertEquals(url + "__role/__/" + testRoleName, tcat.getRoles().get(0).schemeCreateUrl(url));
        assertEquals(url, tcat.getIssuer());
        assertEquals(url + "#" + testAccountName, tcat.getSubject());

        // ????D
        String roleId = roleUrl.substring(roleUrl.indexOf("'") + 1);
        roleId = roleId.substring(0, roleId.indexOf("'"));
        req = PersoniumRequest.delete(UrlUtils.accountLink(cellName, testAccountName, roleId))
                .header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN);
        res = request(req);
        assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode());

        // Role?D
        req = PersoniumRequest.delete(roleUrl)
                .header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN)
                .header(HttpHeaders.IF_MATCH, "*");
        res = request(req);
        assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode());

        // Account?D
        req = PersoniumRequest.delete(accUrl)
                .header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN)
                .header(HttpHeaders.IF_MATCH, "*");
        res = request(req);
        assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode());
    }

    /**
     * Role?Link???Account??response?409????.
     */
    @Test
    public final void Role?Link???Account??response?409????() {
        String token = AbstractCase.MASTER_TOKEN_NAME;
        String accountName = "account";
        String pass = "password";
        String roleName = "role";
        String boxName = null;
        try {
            // ?????
            AccountUtils.create(token, cellName, accountName, pass, HttpStatus.SC_CREATED);
            RoleUtils.create(cellName, token, roleName, boxName, HttpStatus.SC_CREATED);
            ResourceUtils.linkAccountRole(cellName, token, accountName, boxName, roleName, HttpStatus.SC_NO_CONTENT);

            // ?????????
            AccountUtils.delete(cellName, token, accountName, HttpStatus.SC_CONFLICT);

            // ????????????
            ResourceUtils.linkAccountRollDelete(cellName, token, accountName, boxName, roleName);
            AccountUtils.delete(cellName, token, accountName, HttpStatus.SC_NO_CONTENT);
        } finally {
            ResourceUtils.linkAccountRollDelete(cellName, token, accountName, boxName, roleName);
            RoleUtils.delete(accountName, token, roleName, boxName, -1);
            AccountUtils.delete(cellName, token, accountName, -1);
        }

    }

    /**
     * ?.
     * @param name ??
     * @param pass 
     * @return ???Account?URL
     */
    String createAccount(final String name, final String pass) {
        // Account?C
        TResponse res = Http.request("account-create.txt")
                .with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName)
                .with("username", name)
                .with("password", pass)
                .returns();

        String accLocHeader = res.getLocationHeader();
        String etag = res.getHeader(HttpHeaders.ETAG);

        // Account?R
        res = Http.request("account-retrieve.txt")
                .with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName)
                .with("username", name)
                .returns()
                .statusCode(HttpStatus.SC_OK)
                .contentType("application/json");

        JSONObject accountJson = res.bodyAsJson();
        accountJson = getResultsFromEntityResponse(accountJson);
        assertEquals(name, accountJson.get("Name"));
        assertNotNull(accountJson.get("__published"));
        assertNotNull(accountJson.get("__updated"));
        assertEquals(etag, ((JSONObject) accountJson.get("__metadata")).get("etag"));

        // Account?U
        // TODO UPDATE??????

        return accLocHeader;
    }

    /**
     * ??.
     * @param testRoleName
     * @return ???Role?Id
     */
    @SuppressWarnings("unchecked")
    String createRole(final String testRoleName) {
        // Role?C
        JSONObject body = new JSONObject();
        body.put("Name", testRoleName);
        body.put("_Box.Name", null);

        TResponse res = Http.request("role-create.txt")
                .with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName)
                .with("body", body.toString())
                .returns()
                .statusCode(HttpStatus.SC_CREATED)
                .inspect(new TResponse.Inspector() {
                    public void inspect(TResponse resp) {
                    }
                });

        String roleLocHeader = res.getLocationHeader();

        assertNotNull(roleLocHeader);

        // Role?R
        res = Http.request("role-retrieve.txt")
                .with("cellPath", cellName)
                .with("rolename", testRoleName)
                .with("boxname", "null")
                .with("token", AbstractCase.MASTER_TOKEN_NAME)
                .returns()
                .statusCode(HttpStatus.SC_OK)
                .contentType("application/json");

        JSONObject roleJson = res.bodyAsJson();
        roleJson = getResultsFromEntityResponse(roleJson);
        assertEquals(testRoleName, roleJson.get("Name"));
        assertNull(roleJson.get("_Box.Name"));
        assertNotNull(roleJson.get("__published"));
        assertNotNull(roleJson.get("__updated"));
        // assertEquals(etag, ((JSONObject) roleJson.get("__metadata")).get("etag"));

        // Role?U
        // TODO UPDATE??????
        return roleLocHeader;
    }

    /**
     * BOX?.
     * @param boxName ??
     * @param boxSchema 
     * @return boxURL
     */
    protected String createBox2(final String boxName, final String boxSchema) {
        // BOX?C
        PersoniumRequest req = PersoniumRequest.post(UrlUtils.cellCtl(cellName, Box.EDM_TYPE_NAME));
        req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", boxName);
        if (boxSchema != null) {
            req.addJsonBody("Schema", boxSchema);
        }
        PersoniumResponse res = request(req);
        assertEquals(HttpStatus.SC_CREATED, res.getStatusCode());
        String url = res.getFirstHeader(HttpHeaders.LOCATION);
        assertNotNull(url);
        return url;
    }

    /**
     * ?????.
     * @param inputStream ?
     * @return ??
     */
    public byte[] readAll(InputStream inputStream) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            while (true) {
                int len = inputStream.read(buffer);
                if (len < 0) {
                    break;
                }
                bout.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bout.toByteArray();
    }

    /**
     * @param body
     * @return results??JSON
     */
    private JSONObject getResultsFromEntityResponse(JSONObject body) {
        assertNotNull(body);
        JSONObject d = (JSONObject) body.get("d");
        assertNotNull(d);
        JSONObject results = (JSONObject) d.get("results");
        assertNotNull(results);
        return results;
    }

    /**
     * @param body
     * @return results??JSON
     */
    private JSONArray getResultsFromEntitysResponse(JSONObject body) {
        assertNotNull(body);
        JSONObject d = (JSONObject) body.get("d");
        assertNotNull(d);
        JSONArray results = (JSONArray) d.get("results");
        assertNotNull(results);
        return results;
    }
}