Junit fixes and review comment fixes for Simulator.
[contrib/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / clientcontroller / SimulatorRemoteResource.java
index 078e999..096e65e 100644 (file)
  * limitations under the License.
  */
 
-/**
- * This file contains a class which has a set of native methods for
- * communicating with a remote resource.
- */
 package org.oic.simulator.clientcontroller;
 
 import java.util.LinkedList;
 import java.util.Map;
 
+import org.oic.simulator.InvalidArgsException;
+import org.oic.simulator.NoSupportException;
+import org.oic.simulator.OperationInProgressException;
+import org.oic.simulator.SimulatorException;
 import org.oic.simulator.SimulatorResourceModel;
+import org.oic.simulator.SimulatorResult;
 
 /**
  * SimulatorRemoteResource represents a Resource running in the remote Simulator
@@ -37,234 +38,400 @@ public class SimulatorRemoteResource {
     }
 
     /**
-     * Method to get the URI for this resource
-     * 
-     * @return resource URI
+     * API to get the URI for this resource.
+     *
+     * @return Resource URI
      */
     public String getUri() {
         return mUri;
     }
 
     /**
-     * Method to get the observe capability of this resource
-     * 
-     * @return true if the resource is observable, otherwise false.
+     * API to get the observe policy of this resource.
+     *
+     * @return True if the resource is observable, otherwise false.
      */
     public boolean getIsObservable() {
         return mIsObservable;
     }
 
     /**
-     * Method to get the connectivity type for this resource
-     * 
+     * API to get the connectivity type for this resource.
+     *
      * @return Connectivity type.
      */
-    public String getConnectivityType() {
-        return mConnType;
+    public SimulatorConnectivityType getConnectivityType() {
+        return SimulatorConnectivityType.getConnectivityType(mConnType);
     }
 
     /**
-     * Method to get the list of resource types
-     * 
-     * @return List of resource types
+     * API to get the list of resource types.
+     *
+     * @return List of resource types.
      */
     public LinkedList<String> getResourceTypes() {
         return mResTypes;
     }
 
     /**
-     * Method to get the list of resource interfaces
-     * 
-     * @return List of resource interface
+     * API to get the list of resource interfaces.
+     *
+     * @return List of resource interfaces.
      */
     public LinkedList<String> getResourceInterfaces() {
         return mResInterfaces;
     }
 
     /**
-     * Method to get a string representation of the resource's server ID. This
-     * is unique per-server independent on how it was discovered.
-     * 
-     * @return server ID
+     * API to get host address and port information of the resource.
+     *
+     * @return Host address.
      */
-    public String getServerId() {
-        return mSid;
+    public String getHost() {
+        return mHost;
     }
 
     /**
-     * Method to get a unique Id of the resource.
-     * 
+     * API to get a unique Id of the resource .
+     *
      * @return Unique ID.
      */
-    public String getUid() {
-        return mUid;
+    public String getId() {
+        return mId;
     }
 
     /**
-     * Method to set observation on the resource
-     * 
+     * API to start observing the resource.
+     *
      * @param observeType
-     *            allows the client to specify how it wants to observe
+     *            Allows the client to specify how it wants to observe.
      * @param queryParamsMap
-     *            map which can have the query parameter name and value
+     *            Map which can have the query parameter names and values.
      * @param onObserveListener
-     *            event handler The handler method will be invoked with a map of
-     *            attribute name and values.
-     * 
+     *            The handler method which will be invoked with a map of
+     *            attribute names and values whenever there is a change in
+     *            resource model of the remote resource.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             values.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
-    public native void observe(SimulatorObserveType observeType,
+    public native void startObserve(SimulatorObserveType observeType,
             Map<String, String> queryParamsMap,
-            IObserveListener onObserveListener);
+            IObserveListener onObserveListener) throws InvalidArgsException,
+            SimulatorException;
 
     /**
-     * Method to cancel the observation on the resource
+     * API to stop observing the resource.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if the native remote resource
+     *             object is unavailable.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
-    public native void cancelObserve();
+    public native void stopObserve() throws InvalidArgsException,
+            SimulatorException;
 
     /**
-     * Method to get the attributes of a resource.
-     * 
+     * API to send GET request to the resource. Response will be notified
+     * asynchronously via callback set for {@link IGetListener}.
+     *
      * @param queryParamsMap
-     *            map which can have the query parameter name and value
+     *            Map which can have the query parameter name and value.
      * @param onGetListener
-     *            The event handler will be invoked with a map of attribute name
-     *            and values. The event handler will also have the result from
-     *            this Get operation This will have error codes
+     *            Event handler which will be invoked with the response for GET
+     *            request with a map of attribute name and values.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             values.
+     * @throws NoSupportException
+     *             This exception will be thrown if we cannot send GET request
+     *             to the remote resource.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
     public void get(Map<String, String> queryParamsMap,
-            IGetListener onGetListener) {
-        this.get(null, queryParamsMap, onGetListener);
+            IGetListener onGetListener) throws InvalidArgsException,
+            NoSupportException, SimulatorException {
+        if (null == onGetListener)
+            throw new InvalidArgsException(
+                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
+                    "Parameter passed in invalid");
+        this.nativeGet(null, queryParamsMap, onGetListener);
     }
 
     /**
-     * Method to get the attributes of a resource.
-     * 
+     * API to send GET request to the resource. Response will be notified
+     * asynchronously via callback set for {@link IGetListener}.
+     *
      * @param resourceInterface
-     *            interface type of the resource to operate on
+     *            Interface type of the resource to operate on.
      * @param queryParamsMap
-     *            map which can have the query parameter name and value
+     *            Map which can have the query parameter name and value.
      * @param onGetListener
-     *            The event handler will be invoked with a map of attribute name
-     *            and values. The event handler will also have the result from
-     *            this Get operation This will have error codes
+     *            Event handler which will be invoked with the response for GET
+     *            request with a map of attribute name and values.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             values.
+     * @throws NoSupportException
+     *             This exception will be thrown if we cannot send GET request
+     *             to the remote resource.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
-    public native void get(String resourceInterface,
-            Map<String, String> queryParamsMap, IGetListener onGetListener);
+    public void get(String resourceInterface,
+            Map<String, String> queryParamsMap, IGetListener onGetListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException {
+        if (null == resourceInterface || resourceInterface.isEmpty() || null == onGetListener)
+            throw new InvalidArgsException(
+                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
+                    "Parameter passed in invalid");
+        this.nativeGet(resourceInterface, queryParamsMap, onGetListener);
+    }
+
+    private native void nativeGet(String resourceInterface,
+            Map<String, String> queryParamsMap, IGetListener onGetListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException;
 
     /**
-     * Method to set the representation of a resource (via PUT)
-     * 
+     * API to send PUT request to the resource. Response will be notified
+     * asynchronously via callback set for {@link IPutListener}.
+     *
      * @param representation
-     *            representation of the resource
+     *            {@link SimulatorResourceModel} holding the representation of
+     *            the resource.
      * @param queryParamsMap
-     *            Map which can have the query parameter name and value
+     *            Map which can have the query parameter name and value.
      * @param onPutListener
-     *            event handler The event handler will be invoked with a map of
-     *            attribute name and values.
+     *            Event handler which will be invoked with the response for PUT
+     *            request with a map of attribute name and values.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             value.
+     * @throws NoSupportException
+     *             This exception will be thrown if we cannot send PUT request
+     *             to the remote resource.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
     public void put(SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPutListener onPutListener) {
-        this.put(null, representation, queryParamsMap, onPutListener);
+            Map<String, String> queryParamsMap, IPutListener onPutListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException {
+        if (null == representation || null == onPutListener)
+            throw new InvalidArgsException(
+                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
+                    "Parameter passed in invalid");
+        this.nativePut(null, representation, queryParamsMap, onPutListener);
     }
 
     /**
-     * Method to set the representation of a resource (via PUT)
-     * 
+     * API to send PUT request to the resource. Response will be notified
+     * asynchronously via callback set for {@link IPutListener}.
+     *
      * @param resourceInterface
-     *            interface type of the resource to operate on
+     *            Interface type of the resource to operate on.
      * @param representation
-     *            representation of the resource
+     *            {@link SimulatorResourceModel} holding the representation of
+     *            the resource.
      * @param queryParamsMap
-     *            Map which can have the query parameter name and value
+     *            Map which can have the query parameter name and value.
      * @param onPutListener
-     *            event handler The event handler will be invoked with a map of
-     *            attribute name and values.
+     *            Event handler which will be invoked with the response for PUT
+     *            request with a map of attribute name and values.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             value.
+     * @throws NoSupportException
+     *             This exception will be thrown if we cannot send PUT request
+     *             to the remote resource.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
-    private native int put(String resourceInterface,
+    public void put(String resourceInterface,
+            SimulatorResourceModel representation,
+            Map<String, String> queryParamsMap, IPutListener onPutListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException {
+        if (null == resourceInterface || resourceInterface.isEmpty() ||
+            null == representation || null == onPutListener)
+            throw new InvalidArgsException(
+                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
+                    "Parameter passed in invalid");
+        this.nativePut(resourceInterface, representation, queryParamsMap, onPutListener);
+    }
+
+    private native void nativePut(String resourceInterface,
             SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPutListener onPutListener);
+            Map<String, String> queryParamsMap, IPutListener onPutListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException;
 
     /**
-     * Method to POST on a resource
-     * 
+     * API to send POST request to the resource. Response will be notified
+     * asynchronously via callback set for {@link IPostListener}.
+     *
      * @param representation
-     *            representation of the resource
+     *            {@link SimulatorResourceModel} holding the representation of
+     *            the resource
      * @param queryParamsMap
      *            Map which can have the query parameter name and value
      * @param onPostListener
-     *            event handler The event handler will be invoked with a map of
-     *            attribute name and values.
+     *            Event handler which will be invoked with the response for POST
+     *            request with a map of attribute name and values.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             value.
+     * @throws NoSupportException
+     *             This exception will be thrown if we cannot send POST request
+     *             on the remote resource.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
     public void post(SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPostListener onPostListener) {
-        this.post(null, representation, queryParamsMap, onPostListener);
+            Map<String, String> queryParamsMap, IPostListener onPostListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException {
+        if (null == representation || null == onPostListener)
+            throw new InvalidArgsException(
+                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
+                    "Parameter passed in invalid");
+        this.nativePost(null, representation, queryParamsMap, onPostListener);
     }
 
     /**
-     * Method to POST on a resource
-     * 
+     * API to send POST request to the resource. Response will be notified
+     * asynchronously via callback set for {@link IPostListener}.
+     *
      * @param resourceInterface
-     *            interface type of the resource to operate on
+     *            Interface type of the resource to operate on.
      * @param representation
-     *            representation of the resource
+     *            {@link SimulatorResourceModel} holding the representation of
+     *            the resource.
      * @param queryParamsMap
-     *            Map which can have the query parameter name and value
+     *            Map which can have the query parameter name and value.
      * @param onPostListener
-     *            event handler The event handler will be invoked with a map of
-     *            attribute name and values.
+     *            Event handler which will be invoked with the response for POST
+     *            request with a map of attribute name and values.
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             value.
+     * @throws NoSupportException
+     *             This exception will be thrown if we cannot send POST request
+     *             on the remote resource.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
-    private native int post(String resourceInterface,
+    public void post(String resourceInterface,
             SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPostListener onPostListener);
+            Map<String, String> queryParamsMap, IPostListener onPostListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException {
+        if (null == resourceInterface || resourceInterface.isEmpty() ||
+            null == representation || null == onPostListener)
+            throw new InvalidArgsException(
+                SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
+                "Parameter passed in invalid");
+        this.nativePost(resourceInterface, representation, queryParamsMap, onPostListener);
+    }
+
+    private native void nativePost(String resourceInterface,
+            SimulatorResourceModel representation,
+            Map<String, String> queryParamsMap, IPostListener onPostListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException;
 
     /**
-     * Method to set the RAML file path from application
-     * 
-     * @param ramlPath
-     *            RAML configuration file path
+     * API to provide remote resource configure information,
+     * which is required for using automation feature.
+     *
+     * @param path
+     *            Path to RAML file.
+     *
+     * @throws InvalidArgsException
+     *             Thrown if the RAML configuration file path is invalid.
+     * @throws SimulatorException
+     *             Thrown for other errors.
      */
-    public native void configureRAMLPath(String ramlPath);
+    public native void setConfigInfo(String path)
+            throws InvalidArgsException, SimulatorException;
 
     /**
-     * Method to start verification of a resource using automation
-     * 
+     * API to send multiple requests for the resource, based on
+     * the configure file provided from {@link setConfigInfo}.
+     * This verifies response received as well.
+     *
      * @param requestType
-     *            request type to verify
-     * 
+     *            Request type to verify.
      * @param onVerifyListener
-     *            event handler The event handler will be invoked with the
-     *            automation ID.
-     * 
+     *            This event handler will be invoked with the current status of
+     *            the automation.
+     *
      * @return Automation ID.
-     * 
+     *
+     * @throws InvalidArgsException
+     *             This exception will be thrown if any parameter has invalid
+     *             value.
+     * @throws NoSupportException
+     *             Thrown either if the resource does not support the request
+     *             type or the resource is not configured with RAML.
+     * @throws OperationInProgressException
+     *             Thrown if another request generation session is already in
+     *             progress.
+     * @throws SimulatorException
+     *             This exception will be thrown for other errors.
      */
-    public native int startVerification(int requestType,
-            IVerificationListener onVerifyListener);
+    public int startVerification(SimulatorVerificationType requestType,
+            IVerificationListener onVerifyListener)
+            throws InvalidArgsException, NoSupportException,
+            OperationInProgressException, SimulatorException {
+        return startVerification(requestType.ordinal(), onVerifyListener);
+    }
 
     /**
-     * Method to stop verification of a resource previously started.
-     * 
+     * API to stop sending requests which has been started using {@link setConfigInfo}.
+     *
      * @param id
      *            Automation ID.
-     * 
+     *
+     * @throws InvalidArgsException
+     *             Thrown if the automation ID is invalid.
+     * @throws NoSupportException
+     *             Thrown if the resource is not configured with RAML.
+     * @throws SimulatorException
+     *             Thrown for other errors.
      */
-    public native void stopVerification(int id);
+    public native void stopVerification(int id) throws InvalidArgsException,
+            NoSupportException, SimulatorException;
+
+    private native int startVerification(int requestType,
+            IVerificationListener onVerifyListener)
+            throws InvalidArgsException, NoSupportException,
+            OperationInProgressException, SimulatorException;
 
     @Override
     protected void finalize() throws Throwable {
-        super.finalize();
-
-        dispose();
+        try {
+            dispose();
+        } catch(Throwable t){
+            throw t;
+        } finally{
+            super.finalize();
+        }
     }
 
     private native void dispose();
 
     private long               nativeHandle;
     private String             mUri;
-    private String             mConnType;
-    private String             mSid;
-    private String             mUid;
-    private boolean            mIsObservable;
+    private int                mConnType;
+    private String             mHost;
+    private String             mId;
     private LinkedList<String> mResTypes;
     private LinkedList<String> mResInterfaces;
+    private boolean            mIsObservable;
 }