org.deviceconnect.android.profile.restful.test.NormalProximityProfileTestCase.java Source code

Java tutorial

Introduction

Here is the source code for org.deviceconnect.android.profile.restful.test.NormalProximityProfileTestCase.java

Source

/*
 NormalProximityProfileTestCase.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
package org.deviceconnect.android.profile.restful.test;

import junit.framework.Assert;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.deviceconnect.android.test.plugin.profile.TestProximityProfileConstants;
import org.deviceconnect.message.DConnectMessage;
import org.deviceconnect.profile.AuthorizationProfileConstants;
import org.deviceconnect.profile.DConnectProfileConstants;
import org.deviceconnect.profile.ProximityProfileConstants;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Proximity?.
 * @author NTT DOCOMO, INC.
 */
public class NormalProximityProfileTestCase extends RESTfulDConnectTestCase {

    /**
     * .
     * @param tag 
     */
    public NormalProximityProfileTestCase(final String tag) {
        super(tag);
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /proximity/ondeviceproximity?deviceid=xxxx&session_key=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testOnDeviceProximity01() {
        try {
            JSONObject event = registerEventCallback(ProximityProfileConstants.ATTRIBUTE_ON_DEVICE_PROXIMITY);
            JSONObject proximity = event.getJSONObject(ProximityProfileConstants.PARAM_PROXIMITY);
            Assert.assertEquals(Double.valueOf(TestProximityProfileConstants.VALUE),
                    proximity.getDouble(ProximityProfileConstants.PARAM_VALUE));
            Assert.assertEquals(Double.valueOf(TestProximityProfileConstants.MIN),
                    proximity.getDouble(ProximityProfileConstants.PARAM_MIN));
            Assert.assertEquals(Double.valueOf(TestProximityProfileConstants.MAX),
                    proximity.getDouble(ProximityProfileConstants.PARAM_MAX));
            Assert.assertEquals(Double.valueOf(TestProximityProfileConstants.THRESHOLD),
                    proximity.getDouble(ProximityProfileConstants.PARAM_THRESHOLD));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /proximity/ondeviceproximity?deviceid=xxxx&session_key=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testOnDeviceProximity02() {
        unregisterEventCallback(ProximityProfileConstants.ATTRIBUTE_ON_DEVICE_PROXIMITY);
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /proximity/onuserproximity?deviceid=xxxx&session_key=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testOnUserProximity01() {
        try {
            JSONObject event = registerEventCallback(ProximityProfileConstants.ATTRIBUTE_ON_USER_PROXIMITY);
            JSONObject proximity = event.getJSONObject(ProximityProfileConstants.PARAM_PROXIMITY);
            Assert.assertEquals(true, proximity.getBoolean(ProximityProfileConstants.PARAM_NEAR));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /proximity/onuserproximity?deviceid=xxxx&session_key=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testOnUserProximity02() {
        unregisterEventCallback(ProximityProfileConstants.ATTRIBUTE_ON_USER_PROXIMITY);
    }

    /**
     * ???.
     * @param attribute ????
     * @return ???
     * @throws JSONException JSON??????
     */
    private JSONObject registerEventCallback(final String attribute) throws JSONException {
        StringBuilder builder = new StringBuilder();
        builder.append(DCONNECT_MANAGER_URI);
        builder.append("/" + ProximityProfileConstants.PROFILE_NAME);
        builder.append("/" + attribute);
        builder.append("?");
        builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
        builder.append("&");
        builder.append(DConnectProfileConstants.PARAM_SESSION_KEY + "=" + getClientId());
        builder.append("&");
        builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
        HttpUriRequest request = new HttpPut(builder.toString());
        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        JSONObject event = waitForEvent();
        Assert.assertNotNull("event is null.", event);
        return event;
    }

    /**
     * ???.
     * @param attribute ????
     */
    private void unregisterEventCallback(final String attribute) {
        StringBuilder builder = new StringBuilder();
        builder.append(DCONNECT_MANAGER_URI);
        builder.append("/" + ProximityProfileConstants.PROFILE_NAME);
        builder.append("/" + attribute);
        builder.append("?");
        builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
        builder.append("&");
        builder.append(DConnectProfileConstants.PARAM_SESSION_KEY + "=" + getClientId());
        builder.append("&");
        builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject root = sendRequest(request);
            Assert.assertNotNull("root is null.", root);
            Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

}