Java tutorial
/* FailDeviceOrientationProfileTestCase.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.message.DConnectMessage.ErrorCode; import org.deviceconnect.profile.AuthorizationProfileConstants; import org.deviceconnect.profile.DConnectProfileConstants; import org.deviceconnect.profile.DeviceOrientationProfileConstants; import org.deviceconnect.utils.URIBuilder; import org.json.JSONException; import org.json.JSONObject; /** * DeviceOrientation?. * @author NTT DOCOMO, INC. */ public class FailDeviceOrientationProfileTestCase extends RESTfulDConnectTestCase { /** * . * * @param tag */ public FailDeviceOrientationProfileTestCase(final String tag) { super(tag); } /** * deviceId???ondeviceorientation???. * <pre> * ?HTTP * Method: PUT * Path: /deviceorientation/ondeviceorientation * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testPutOnDeviceOrientation001() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpPut(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.EMPTY_DEVICE_ID.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * deviceId??ondeviceorientation???. * <pre> * ?HTTP * Method: PUT * Path: /deviceorientation/ondeviceorientation?deviceId= * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testPutOnDeviceOrientation002() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, ""); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpPut(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * ???deviceId?ondeviceorientation???. * <pre> * ?HTTP * Method: PUT * Path: /deviceorientation/ondeviceorientation?deviceId=123456789 * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testPutOnDeviceOrientation003() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "123456789"); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpPut(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * ?????ondeviceorientation???. * <pre> * ?HTTP * Method: PUT * Path: /deviceorientation/ondeviceorientation?deviceId=xxxxx&abc=abc * </pre> * <pre> * ?? * ??????? * result?0??????? * </pre> */ public void testPutOnDeviceOrientation004() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter("abc", "abc"); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpPut(builder.toString()); JSONObject root = sendRequest(request); assertResultOK(root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * deviceId2????ondeviceorientation???. * <pre> * ?HTTP * Method: PUT * Path: /deviceorientation/ondeviceorientation?deviceId=123456789&deviceId=xxx * </pre> * <pre> * ?? * ??????? * result?1??????? * </pre> */ public void testPutOnDeviceOrientation005() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "123456789"); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpPut(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * deviceId???ondeviceorientation???. * <pre> * ?HTTP * Method: DELETE * Path: /deviceorientation/ondeviceorientation * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testDeleteOnDeviceOrientation001() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpDelete(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.EMPTY_DEVICE_ID.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * deviceId??ondeviceorientation???. * <pre> * ?HTTP * Method: DELETE * Path: /deviceorientation/ondeviceorientation?deviceId= * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testDeleteOnDeviceOrientation002() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, ""); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpDelete(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * ???deviceId?ondeviceorientation???. * <pre> * ?HTTP * Method: DELETE * Path: /deviceorientation/ondeviceorientation?deviceId=123456789 * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testDeleteOnDeviceOrientation003() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "123456789"); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpDelete(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * ?????ondeviceorientation???. * <pre> * ?HTTP * Method: DELETE * Path: /deviceorientation/ondeviceorientation?deviceId=xxxxx&abc=abc * </pre> * <pre> * ?? * ??????? * result?0??????? * </pre> */ public void testDeleteOnDeviceOrientation004() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter("abc", "abc"); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpDelete(builder.toString()); JSONObject root = sendRequest(request); assertResultOK(root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * deviceId2????ondeviceorientation???. * <pre> * ?HTTP * Method: DELETE * Path: /deviceorientation/ondeviceorientation?deviceId=123456789&deviceId=xxx * </pre> * <pre> * ?? * ??????? * result?1??????? * </pre> */ public void testDeleteOnDeviceOrientation005() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "123456789"); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); try { HttpUriRequest request = new HttpDelete(builder.toString()); JSONObject root = sendRequest(request); assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } } /** * GET???/deviceorientation/ondeviceorientation???. * <pre> * ?HTTP * Method: GET * Path: /deviceorientation/ondeviceorientation?deviceId=xxxx&sessionKey=xxxx * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testOnDeviceOrientationInvalidMethodGet() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); 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???/deviceorientation/ondeviceorientation???. * <pre> * ?HTTP * Method: POST * Path: /deviceorientation/ondeviceorientation?deviceId=xxxx&sessionKey=xxxx * </pre> * <pre> * ?? * result?1??????? * </pre> */ public void testOnDeviceOrientationInvalidMethodPost() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId()); builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken()); 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()); } } }