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

Java tutorial

Introduction

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

Source

/*
 FailSystemProfileTestCase.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 org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.deviceconnect.android.test.plugin.profile.TestSystemProfileConstants;
import org.deviceconnect.message.DConnectMessage.ErrorCode;
import org.deviceconnect.profile.DConnectProfileConstants;
import org.deviceconnect.profile.SystemProfileConstants;
import org.deviceconnect.utils.URIBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * System?.
 * @author NTT DOCOMO, INC.
 */
public class FailSystemProfileTestCase extends RESTfulDConnectTestCase {

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

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system?abc=abc
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * version?String????????
     * </pre>
     */
    public void testGetSystemUndefinedParameter() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.addParameter("abc", "abc");
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject resp = sendRequest(request);
            assertResultOK(resp);
            assertEquals(TestSystemProfileConstants.VERSION, resp.getString(SystemProfileConstants.PARAM_VERSION));
            JSONArray support = resp.getJSONArray(SystemProfileConstants.PARAM_SUPPORTS);
            assertNotNull("support is null.", support);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * POST???.
     * <pre>
     * ?HTTP
     * Method: POST
     * Path: /system
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemInvalidMethodPost() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        try {
            HttpUriRequest request = new HttpPost(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * PUT?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /system/device?deviceId=123456789&deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemInvalidMethodPut() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * DELETE?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /system/device?deviceId=123456789&deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemInvalidMethodDelete() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * deviceId???????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceNoDeviceId() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.EMPTY_DEVICE_ID.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * deviceId???????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device?deviceId=
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceEmptyDeviceId() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "");
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ???deviceId??????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device?deviceId=123456789
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceInvalidDeviceId() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "123456789");
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ???????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device?deviceId=xxxx&abc=abc
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceUndefinedParameter() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        builder.addParameter("abc", "abc");
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject resp = sendRequest(request);
            assertResultOK(resp);
            assertEquals("1.0", resp.getString(SystemProfileConstants.PARAM_VERSION));
            JSONObject connect = resp.getJSONObject(SystemProfileConstants.PARAM_CONNECT);
            assertNotNull(connect);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * deviceId2????????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device?deviceId=123456789&deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * ???????
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceDuplicatedDeviceId() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "123456789");
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * POST?????.
     * <pre>
     * ?HTTP
     * Method: POST
     * Path: /system/device?deviceId=123456789&deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceInvalidMethodPost() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPost(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * PUT?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /system/device?deviceId=123456789&deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceInvalidMethodPut() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.UNKNOWN_ATTRIBUTE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * DELETE?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /system/device?deviceId=123456789&deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceInvalidMethodDelete() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_DEVICE);
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.UNKNOWN_ATTRIBUTE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * GET????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device/events?sessionKey=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceEventsInvalidMethodGet() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_EVENTS);
        builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * POST????.
     * <pre>
     * ?HTTP
     * Method: POST
     * Path: /system/device/events?sessionKey=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceEventsInvalidMethodPost() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_EVENTS);
        builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId());
        try {
            HttpUriRequest request = new HttpPost(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * PUT????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /system/device/events?sessionKey=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceEventsInvalidMethodPut() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_EVENTS);
        builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * GET????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device/keyword
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceKeywordInvalidMethodGet() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_KEYWORD);
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * POST????.
     * <pre>
     * ?HTTP
     * Method: POST
     * Path: /system/device/keyword
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceKeywordInvalidMethodPost() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_KEYWORD);
        try {
            HttpUriRequest request = new HttpPost(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * DELETE????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /system/device/keyword
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceKeywordInvalidMethodDelete() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_KEYWORD);
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * GET?????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /system/device/wakeup?pluginId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceWakeupInvalidMethodGet() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_WAKEUP);
        builder.addParameter(SystemProfileConstants.PARAM_PLUGIN_ID, getTestPluginId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.UNKNOWN_ATTRIBUTE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * POST?????.
     * <pre>
     * ?HTTP
     * Method: POST
     * Path: /system/device/wakeup?pluginId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceWakeupInvalidMethodPost() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_WAKEUP);
        builder.addParameter(SystemProfileConstants.PARAM_PLUGIN_ID, getTestPluginId());
        try {
            HttpUriRequest request = new HttpPost(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * DELETE?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /system/device/wakeup?pluginId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    public void testGetSystemDeviceWakeupInvalidMethodDelete() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(SystemProfileConstants.PROFILE_NAME);
        builder.setInterface(SystemProfileConstants.INTERFACE_DEVICE);
        builder.setAttribute(SystemProfileConstants.ATTRIBUTE_WAKEUP);
        builder.addParameter(SystemProfileConstants.PARAM_PLUGIN_ID, getTestPluginId());
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ATTRIBUTE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }
}