Junit fixes and review comment fixes for Simulator.
[contrib/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / clientcontroller / SimulatorRemoteResource.java
index 583c273..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.List;
+import java.util.LinkedList;
 import java.util.Map;
 
-import org.oic.simulator.serviceprovider.SimulatorResourceModel;
+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
@@ -33,184 +34,404 @@ import org.oic.simulator.serviceprovider.SimulatorResourceModel;
 public class SimulatorRemoteResource {
 
     private SimulatorRemoteResource(long nativeHandle) {
-        this.mNativeHandle = nativeHandle;
+        this.nativeHandle = nativeHandle;
+    }
+
+    /**
+     * API to get the URI for this resource.
+     *
+     * @return Resource URI
+     */
+    public String getUri() {
+        return mUri;
+    }
+
+    /**
+     * API to get the observe policy of this resource.
+     *
+     * @return True if the resource is observable, otherwise false.
+     */
+    public boolean getIsObservable() {
+        return mIsObservable;
+    }
+
+    /**
+     * API to get the connectivity type for this resource.
+     *
+     * @return Connectivity type.
+     */
+    public SimulatorConnectivityType getConnectivityType() {
+        return SimulatorConnectivityType.getConnectivityType(mConnType);
     }
 
     /**
-     * Method to get the URI for this resource
+     * API to get the list of resource types.
      *
-     * @return resource URI
+     * @return List of resource types.
      */
-    public native String getUri();
+    public LinkedList<String> getResourceTypes() {
+        return mResTypes;
+    }
 
     /**
-     * Method to get the list of resource types
+     * API to get the list of resource interfaces.
      *
-     * @return List of resource types
+     * @return List of resource interfaces.
      */
-    public native List<String> getResourceTypes();
+    public LinkedList<String> getResourceInterfaces() {
+        return mResInterfaces;
+    }
 
     /**
-     * Method to get the list of resource interfaces
+     * API to get host address and port information of the resource.
      *
-     * @return List of resource interface
+     * @return Host address.
      */
-    public native List<String> getResourceInterfaces();
+    public String getHost() {
+        return mHost;
+    }
 
     /**
-     * Method to get a string representation of the resource's server ID. This
-     * is unique per-server independent on how it was discovered.
+     * API to get a unique Id of the resource .
      *
-     * @return server ID
+     * @return Unique ID.
      */
-    public native String getServerId();
+    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 native void get(Map<String, String> queryParamsMap,
-            IGetListener onGetListener);
+    public void get(Map<String, String> queryParamsMap,
+            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 resourceType
-     *            resourceType of the resource to operate on
      * @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 void get(String resourceType, String resourceInterface,
-            Map<String, String> queryParamsMap, IGetListener onGetListener) {
-        this.get2(resourceType, resourceInterface, queryParamsMap,
-                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 get2(String resourceType, String resourceInterface,
-            Map<String, String> queryParamsMap, IGetListener 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 native void put(SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPutListener onPutListener);
+    public void put(SimulatorResourceModel representation,
+            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 resourceType
-     *            resource type of the resource to operate on
      * @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.
      */
-    public void put(String resourceType, String resourceInterface,
+    public void put(String resourceInterface,
             SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPutListener onPutListener) {
-        this.put2(resourceType, resourceInterface, representation,
-                queryParamsMap, onPutListener);
+            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 put2(String resourceType, String resourceInterface,
+    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 native void post(SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPostListener onPostListener);
+    public void post(SimulatorResourceModel representation,
+            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 resourceType
-     *            resource type of the resource to operate on
      * @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.
      */
-    public void post(String resourceType, String resourceInterface,
+    public void post(String resourceInterface,
             SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPostListener onPostListener) {
-        this.post2(resourceType, resourceInterface, representation,
-                queryParamsMap, 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 post2(String resourceType, String resourceInterface,
+    private native void nativePost(String resourceInterface,
             SimulatorResourceModel representation,
-            Map<String, String> queryParamsMap, IPostListener onPostListener);
+            Map<String, String> queryParamsMap, IPostListener onPostListener)
+            throws InvalidArgsException, NoSupportException, SimulatorException;
+
+    /**
+     * 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 setConfigInfo(String path)
+            throws InvalidArgsException, SimulatorException;
+
+    /**
+     * 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.
+     * @param onVerifyListener
+     *            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 int startVerification(SimulatorVerificationType requestType,
+            IVerificationListener onVerifyListener)
+            throws InvalidArgsException, NoSupportException,
+            OperationInProgressException, SimulatorException {
+        return startVerification(requestType.ordinal(), onVerifyListener);
+    }
+
+    /**
+     * 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) 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 mNativeHandle;
+    private long               nativeHandle;
+    private String             mUri;
+    private int                mConnType;
+    private String             mHost;
+    private String             mId;
+    private LinkedList<String> mResTypes;
+    private LinkedList<String> mResInterfaces;
+    private boolean            mIsObservable;
 }