From fa503b2832e35888cc29361ca50fdf9969129d16 Mon Sep 17 00:00:00 2001 From: "spurthi.segu" Date: Tue, 6 Oct 2015 20:34:31 +0530 Subject: [PATCH] Junit testing fixes for Simulator APIs. Change-Id: Id55390d0476be0349b2d2bbaa84adbbd36e4a8dd Signed-off-by: spurthi.segu Reviewed-on: https://gerrit.iotivity.org/gerrit/3629 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../clientcontroller/manager/ResourceManager.java | 11 +++++ .../serviceprovider/manager/ResourceManager.java | 25 ++++++++++- .../java/jni/simulator_remote_resource_jni.cpp | 6 +-- .../java/jni/simulator_remote_resource_jni.h | 6 +-- .../java/jni/simulator_resource_server_jni.cpp | 16 ++++++- .../src/org/oic/simulator/SimulatorManager.java | 8 ++++ .../clientcontroller/SimulatorRemoteResource.java | 51 ++++++++++++++++++++-- .../serviceprovider/SimulatorResourceServer.java | 2 +- .../src/common/simulator_resource_model.cpp | 5 +++ .../resource_update_automation_mngr.cpp | 5 +++ 10 files changed, 121 insertions(+), 14 deletions(-) diff --git a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java index 565a9ae..9b8bdb4 100644 --- a/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java +++ b/service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java @@ -47,6 +47,7 @@ import oic.simulator.clientcontroller.utils.Utility; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import org.oic.simulator.ILogger.Level; +import org.oic.simulator.InvalidArgsException; import org.oic.simulator.ResourceAttribute; import org.oic.simulator.ResourceAttribute.Range; import org.oic.simulator.ResourceAttribute.Type; @@ -1482,6 +1483,16 @@ public class ResourceManager { } try { resourceN.stopVerification(autoId); + } catch (InvalidArgsException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "[" + e.getClass().getSimpleName() + "]" + + e.code().toString() + "-" + + e.message()); + return; } catch (SimulatorException e) { Activator .getDefault() diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java index febfbb6..ae89c0d 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java @@ -44,6 +44,7 @@ import oic.simulator.serviceprovider.utils.Utility; import org.eclipse.swt.graphics.Image; import org.oic.simulator.IAutomation; import org.oic.simulator.ILogger.Level; +import org.oic.simulator.InvalidArgsException; import org.oic.simulator.ResourceAttribute; import org.oic.simulator.ResourceAttribute.Range; import org.oic.simulator.ResourceAttribute.Type; @@ -403,7 +404,7 @@ public class ResourceManager { orderedResourceUriMap.remove(resourceType); } } - } else if (null != resourceURI) { + } else if (null != resourceType) { orderedResourceUriMap.remove(resourceType); } else { orderedResourceUriMap.clear(); @@ -1383,7 +1384,17 @@ public class ResourceManager { if (null != resourceServerN) { try { resourceServerN.stopAutomation(autoId); - } catch (SimulatorException e) { + } catch (InvalidArgsException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "[" + e.getClass().getSimpleName() + "]" + + e.code().toString() + "-" + + e.message()); + return; + }catch (SimulatorException e) { Activator .getDefault() .getLogManager() @@ -1522,6 +1533,16 @@ public class ResourceManager { // Call native method try { resourceServer.stopAutomation(autoId); + } catch (InvalidArgsException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "[" + e.getClass().getSimpleName() + "]" + + e.code().toString() + "-" + + e.message()); + return false; } catch (SimulatorException e) { Activator .getDefault() diff --git a/service/simulator/java/jni/simulator_remote_resource_jni.cpp b/service/simulator/java/jni/simulator_remote_resource_jni.cpp index 0b41841..3b62939 100644 --- a/service/simulator/java/jni/simulator_remote_resource_jni.cpp +++ b/service/simulator/java/jni/simulator_remote_resource_jni.cpp @@ -515,7 +515,7 @@ Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_stopObserve } JNIEXPORT void JNICALL -Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_get +Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_nativeGet (JNIEnv *env, jobject thiz, jstring jResourceInterface, jobject jQueryParamsMap, jobject jListener) { @@ -585,7 +585,7 @@ Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_get } JNIEXPORT void JNICALL -Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_put +Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_nativePut (JNIEnv *env, jobject thiz, jstring jResourceInterface, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener) { @@ -659,7 +659,7 @@ Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_put } JNIEXPORT void JNICALL -Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_post +Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_nativePost (JNIEnv *env, jobject thiz, jstring jResourceInterface, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener) { diff --git a/service/simulator/java/jni/simulator_remote_resource_jni.h b/service/simulator/java/jni/simulator_remote_resource_jni.h index 2fd681a..50befef 100644 --- a/service/simulator/java/jni/simulator_remote_resource_jni.h +++ b/service/simulator/java/jni/simulator_remote_resource_jni.h @@ -47,17 +47,17 @@ Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_stopObserve (JNIEnv *env, jobject thiz); JNIEXPORT void JNICALL -Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_get +Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_nativeGet (JNIEnv *env, jobject thiz, jstring jResourceInterface, jobject jQueryParamsMap, jobject jListener); JNIEXPORT void JNICALL -Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_put +Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_nativePut (JNIEnv *env, jobject thiz, jstring jResourceInterface, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener); JNIEXPORT void JNICALL -Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_post +Java_org_oic_simulator_clientcontroller_SimulatorRemoteResource_nativePost (JNIEnv *env, jobject thiz, jstring jResourceInterface, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener); diff --git a/service/simulator/java/jni/simulator_resource_server_jni.cpp b/service/simulator/java/jni/simulator_resource_server_jni.cpp index b9cadf2..50affa6 100644 --- a/service/simulator/java/jni/simulator_resource_server_jni.cpp +++ b/service/simulator/java/jni/simulator_resource_server_jni.cpp @@ -603,8 +603,20 @@ Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!"); return; } - - resource->stopUpdateAutomation(automationId); + + try + { + resource->stopUpdateAutomation(automationId); + } + catch (InvalidArgsException &e) + { + throwInvalidArgsException(env, e.code(), e.what()); + } + catch (...) + { + throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception"); + } + SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.") } diff --git a/service/simulator/java/sdk/src/org/oic/simulator/SimulatorManager.java b/service/simulator/java/sdk/src/org/oic/simulator/SimulatorManager.java index d9f502c..a16ce67 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/SimulatorManager.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/SimulatorManager.java @@ -47,6 +47,10 @@ public class SimulatorManager { public static SimulatorResourceServer createResource(String configPath, IResourceModelChangedListener listener) throws InvalidArgsException, SimulatorException { + if (configPath.isEmpty() || null == listener) + throw new InvalidArgsException( + SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(), + "Parameter passed in invalid"); SimulatorResourceServer simulatorResourceServerObj; simulatorResourceServerObj = SimulatorManagerNativeInterface .createResource(configPath, listener); @@ -76,6 +80,10 @@ public class SimulatorManager { public static SimulatorResourceServer[] createResource(String configPath, int count, IResourceModelChangedListener listener) throws InvalidArgsException, SimulatorException { + if (configPath.isEmpty() || count < 0 || null == listener) + throw new InvalidArgsException( + SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(), + "Parameter passed in invalid"); SimulatorResourceServer[] simulatorResourceServers; simulatorResourceServers = SimulatorManagerNativeInterface .createResources(configPath, count, listener); diff --git a/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java b/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java index c35e1fd..daed3b7 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java @@ -24,6 +24,7 @@ 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 @@ -156,6 +157,10 @@ public class SimulatorRemoteResource { public void get(Map queryParamsMap, IGetListener onGetListener) throws InvalidArgsException, NoSupportException, SimulatorException { + if (null == onGetListener) + throw new InvalidArgsException( + SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(), + "Parameter passed in invalid"); this.get(null, queryParamsMap, onGetListener); } @@ -180,7 +185,17 @@ public class SimulatorRemoteResource { * @throws SimulatorException * This exception will be thrown for other errors. */ - public native void get(String resourceInterface, + public void get(String resourceInterface, + Map queryParamsMap, IGetListener onGetListener) + throws InvalidArgsException, NoSupportException, SimulatorException { + if (resourceInterface.isEmpty() || null == onGetListener) + throw new InvalidArgsException( + SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(), + "Parameter passed in invalid"); + this.get(resourceInterface, queryParamsMap, onGetListener); + } + + private native void nativeGet(String resourceInterface, Map queryParamsMap, IGetListener onGetListener) throws InvalidArgsException, NoSupportException, SimulatorException; @@ -209,6 +224,10 @@ public class SimulatorRemoteResource { public void put(SimulatorResourceModel representation, Map 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.put(null, representation, queryParamsMap, onPutListener); } @@ -236,7 +255,18 @@ public class SimulatorRemoteResource { * @throws SimulatorException * This exception will be thrown for other errors. */ - private native void put(String resourceInterface, + public void put(String resourceInterface, + SimulatorResourceModel representation, + Map queryParamsMap, IPutListener onPutListener) + throws InvalidArgsException, NoSupportException, SimulatorException { + if (resourceInterface.isEmpty() || null == representation || null == onPutListener) + throw new InvalidArgsException( + SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(), + "Parameter passed in invalid"); + this.put(resourceInterface, representation, queryParamsMap, onPutListener); + } + + private native void nativePut(String resourceInterface, SimulatorResourceModel representation, Map queryParamsMap, IPutListener onPutListener) throws InvalidArgsException, NoSupportException, SimulatorException; @@ -266,6 +296,10 @@ public class SimulatorRemoteResource { public void post(SimulatorResourceModel representation, Map 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.post(null, representation, queryParamsMap, onPostListener); } @@ -293,7 +327,18 @@ public class SimulatorRemoteResource { * @throws SimulatorException * This exception will be thrown for other errors. */ - public native void post(String resourceInterface, + public void post(String resourceInterface, + SimulatorResourceModel representation, + Map queryParamsMap, IPostListener onPostListener) + throws InvalidArgsException, NoSupportException, SimulatorException { + if (resourceInterface.isEmpty() || null == representation || null == onPostListener) + throw new InvalidArgsException( + SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(), + "Parameter passed in invalid"); + this.post(resourceInterface, representation, queryParamsMap, onPostListener); + } + + private native void nativePost(String resourceInterface, SimulatorResourceModel representation, Map queryParamsMap, IPostListener onPostListener) throws InvalidArgsException, NoSupportException, SimulatorException; diff --git a/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java b/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java index 0d9accd..a42a12f 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java @@ -389,7 +389,7 @@ public class SimulatorResourceServer { * This exception will be thrown for other errors. */ public native void stopAutomation(int automationId) - throws SimulatorException; + throws InvalidArgsException, SimulatorException; /** * API to remove an attribute from the simulated resource. diff --git a/service/simulator/src/common/simulator_resource_model.cpp b/service/simulator/src/common/simulator_resource_model.cpp index 417bb84..dcd7988 100644 --- a/service/simulator/src/common/simulator_resource_model.cpp +++ b/service/simulator/src/common/simulator_resource_model.cpp @@ -311,6 +311,11 @@ void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string void SimulatorResourceModel::removeAttribute(const std::string &attrName) { + if (m_attributes.end() == m_attributes.find(attrName)) + { + return; + } + m_attributes.erase(attrName); return; } diff --git a/service/simulator/src/service-provider/resource_update_automation_mngr.cpp b/service/simulator/src/service-provider/resource_update_automation_mngr.cpp index 1567f4f..c5ec75e 100644 --- a/service/simulator/src/service-provider/resource_update_automation_mngr.cpp +++ b/service/simulator/src/service-provider/resource_update_automation_mngr.cpp @@ -105,12 +105,17 @@ void UpdateAutomationMngr::stop(int id) { m_resourceUpdationList[id]->stop(); m_resourceUpdationList.erase(m_resourceUpdationList.find(id)); + return; } else if (m_attrUpdationList.end() != m_attrUpdationList.find(id)) { m_attrUpdationList[id]->stop(); m_attrUpdationList.erase(m_attrUpdationList.find(id)); + return; } + + //Throw InvalidArgsException + throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid Automation Id!"); } void UpdateAutomationMngr::stopAll() -- 2.7.4