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

Java tutorial

Introduction

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

Source

/*
 FailServiceDiscoveryProfileTestCase.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 android.support.test.runner.AndroidJUnit4;

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.TestServiceDiscoveryProfileConstants;
import org.deviceconnect.message.DConnectMessage.ErrorCode;
import org.deviceconnect.profile.AuthorizationProfileConstants;
import org.deviceconnect.profile.DConnectProfileConstants;
import org.deviceconnect.profile.ServiceDiscoveryProfileConstants;
import org.deviceconnect.utils.URIBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Network Service Discovery ?.
 * @author NTT DOCOMO, INC.
 */
@RunWith(AndroidJUnit4.class)
public class FailServiceDiscoveryProfileTestCase extends RESTfulDConnectTestCase {
    /**
     * POST???.
     * 
     * <pre>
     * ?HTTP
     * Method: POST
     * Path: /servicediscovery
     * </pre>
     * 
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    @Test
    public void testGetServices001() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);
        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());
        }
    }

    /**
     * PUT???.
     * 
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /servicediscovery
     * </pre>
     * 
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    @Test
    public void testGetServices002() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultError(ErrorCode.NOT_SUPPORT_ATTRIBUTE.getCode(), root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * DELETE???.
     * 
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /servicediscovery
     * </pre>
     * 
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    @Test
    public void testGetServices003() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        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());
        }
    }

    /**
     * deviceid????.
     * 
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /servicediscovery?deviceid=xxxx
     * </pre>
     * 
     * <pre>
     * ??
     * result?0???????
     * services?????1??????
     * services??Test Success Device??name???????
     * </pre>
     */
    @Test
    public void testGetServices004() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);
        builder.addParameter(DConnectProfileConstants.PARAM_SERVICE_ID, getServiceId());
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultOK(root);
            JSONArray services = root.getJSONArray(ServiceDiscoveryProfileConstants.PARAM_SERVICES);
            assertNotNull("services is null.", root);
            assertTrue("services not found.", services.length() > 0);
            boolean isFoundName = false;
            for (int i = 0; i < services.length(); i++) {
                JSONObject service = services.getJSONObject(i);
                String name = service.getString(ServiceDiscoveryProfileConstants.PARAM_NAME);
                String id = service.getString(ServiceDiscoveryProfileConstants.PARAM_ID);
                String type = service.getString(ServiceDiscoveryProfileConstants.PARAM_TYPE);
                assertNotNull("service.name is null", name);
                if (name.equals(TestServiceDiscoveryProfileConstants.DEVICE_NAME)) {
                    isFoundName = true;
                }
                assertNotNull("service.id is null", id);
                assertNotNull("service.type is null", type);
            }
            if (!isFoundName) {
                fail("Not found Test DevicePlugin.");
            }
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * GET??.
     * 
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /servicediscovery/onservicechange
     * </pre>
     * 
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    @Test
    public void testOnServiceChangeInvalidMethodGet() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);
        builder.setAttribute(ServiceDiscoveryProfileConstants.ATTRIBUTE_ON_SERVICE_CHANGE);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        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: /servicediscovery/onservicechange
     * </pre>
     * 
     * <pre>
     * ??
     * result?1???????
     * </pre>
     */
    @Test
    public void testOnServiceChangeInvalidMethodPost() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);
        builder.setAttribute(ServiceDiscoveryProfileConstants.ATTRIBUTE_ON_SERVICE_CHANGE);
        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());
        }
    }

}