This is an automated email from the ASF dual-hosted git repository.

pgil pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 3cc30333d0 Manage groovy DSL error message retrieval (OFBIZ-10692)
3cc30333d0 is described below

commit 3cc30333d09422ffb9299e8ef88c9906cedb05bf
Author: Leila <leila.mek...@nereide.fr>
AuthorDate: Thu Oct 25 17:13:03 2018 +0200

    Manage groovy DSL error message retrieval (OFBIZ-10692)
    
    Add integration test to validate the good retrieval of the success /
    error message.
    
    Ajout de 4 services groovy pour tester le fonctionnement du moteur de 
service en groovy couplé a des appels de type DSL
---
 .../test/TestServices.groovy}                      | 33 +++++------
 framework/service/servicedef/services_test_se.xml  | 19 +++++-
 .../ofbiz/service/ExecutionServiceException.java   |  2 +-
 .../apache/ofbiz/service/engine/GroovyEngine.java  | 20 +++++--
 .../service/test/GroovyDslServiceEngineTests.java  | 69 ++++++++++++++++++++++
 framework/service/testdef/servicetests.xml         |  2 +
 .../ofbiz/webapp/event/ServiceEventHandler.java    |  5 ++
 7 files changed, 125 insertions(+), 25 deletions(-)

diff --git 
a/framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
 b/framework/service/groovyScripts/test/TestServices.groovy
similarity index 66%
copy from 
framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
copy to framework/service/groovyScripts/test/TestServices.groovy
index 8a8dca5c1c..633f5fcb1c 100644
--- 
a/framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
+++ b/framework/service/groovyScripts/test/TestServices.groovy
@@ -16,24 +16,23 @@
  * specific language governing permissions and limitations
  * under the License.
  
*******************************************************************************/
-package org.apache.ofbiz.service;
 
-@SuppressWarnings("serial")
-public class ExecutionServiceException extends 
org.apache.ofbiz.base.util.GeneralException {
-
-    public ExecutionServiceException() {
-        super();
-    }
-
-    public ExecutionServiceException(String str) {
-        super(str);
-    }
+def testPingSuccess() {
+    Map returnMap = success('Service result success')
+    if (parameters.ping) returnMap.pong = parameters.ping
+    return returnMap
+}
 
-    public ExecutionServiceException(String str, Throwable nested) {
-        super(str, nested);
-    }
+def testPingError() {
+    Map returnMap = error('Service result error')
+    if (parameters.ping) returnMap.pong = parameters.ping
+    return returnMap
+}
 
-    public ExecutionServiceException(Throwable nested) {
-        super(nested);
-    }
+def testPingSuccessWithDSLCall() {
+    run service: 'testGroovyPingSuccess', with: parameters
 }
+
+def testPingErrorWithDSLCall() {
+    run service: 'testGroovyPingError', with: parameters
+}
\ No newline at end of file
diff --git a/framework/service/servicedef/services_test_se.xml 
b/framework/service/servicedef/services_test_se.xml
index d267a871b6..fc2a8c2505 100644
--- a/framework/service/servicedef/services_test_se.xml
+++ b/framework/service/servicedef/services_test_se.xml
@@ -211,5 +211,22 @@ under the License.
         <attribute name="testingId" type="String" mode="IN"/>
     </service>
 
-
+    <!--call groovy engine service -->
+    <service name="testGroovyPingSuccess" engine="groovy"
+             
location="component://service/groovyScripts/test/TestServices.groovy" 
invoke="testPingSuccess">
+        <attribute name="ping" mode="IN" type="String"/>
+        <attribute name="pong" mode="OUT" type="String"/>
+    </service>
+    <service name="testGroovyPingError" engine="groovy" 
require-new-transaction="true"
+             
location="component://service/groovyScripts/test/TestServices.groovy" 
invoke="testPingError">
+        <implements service="testGroovyPingSuccess"/>
+    </service>
+    <service name="testGroovyPingSuccessWithDSLCall" engine="groovy"
+             
location="component://service/groovyScripts/test/TestServices.groovy" 
invoke="testPingSuccessWithDSLCall">
+        <implements service="testGroovyPingSuccess"/>
+    </service>
+    <service name="testGroovyPingErrorWithDSLCall" engine="groovy"
+             
location="component://service/groovyScripts/test/TestServices.groovy" 
invoke="testPingErrorWithDSLCall">
+        <implements service="testGroovyPingSuccess"/>
+    </service>
 </services>
diff --git 
a/framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
 
b/framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
index 8a8dca5c1c..9356ad0f69 100644
--- 
a/framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
+++ 
b/framework/service/src/main/java/org/apache/ofbiz/service/ExecutionServiceException.java
@@ -19,7 +19,7 @@
 package org.apache.ofbiz.service;
 
 @SuppressWarnings("serial")
-public class ExecutionServiceException extends 
org.apache.ofbiz.base.util.GeneralException {
+public class ExecutionServiceException extends 
org.apache.ofbiz.service.GenericServiceException {
 
     public ExecutionServiceException() {
         super();
diff --git 
a/framework/service/src/main/java/org/apache/ofbiz/service/engine/GroovyEngine.java
 
b/framework/service/src/main/java/org/apache/ofbiz/service/engine/GroovyEngine.java
index a3d6be3237..35936a0dcd 100644
--- 
a/framework/service/src/main/java/org/apache/ofbiz/service/engine/GroovyEngine.java
+++ 
b/framework/service/src/main/java/org/apache/ofbiz/service/engine/GroovyEngine.java
@@ -28,12 +28,12 @@ import java.util.Set;
 
 import javax.script.ScriptContext;
 
-import org.apache.ofbiz.base.util.GeneralException;
 import org.apache.ofbiz.base.util.GroovyUtil;
 import org.apache.ofbiz.base.util.ScriptHelper;
 import org.apache.ofbiz.base.util.ScriptUtil;
 import org.apache.ofbiz.base.util.UtilValidate;
 import org.apache.ofbiz.service.DispatchContext;
+import org.apache.ofbiz.service.ExecutionServiceException;
 import org.apache.ofbiz.service.GenericServiceException;
 import org.apache.ofbiz.service.ModelService;
 import org.apache.ofbiz.service.ServiceDispatcher;
@@ -120,12 +120,20 @@ public final class GroovyEngine extends 
GenericAsyncEngine {
                     scriptContext.getBindings(ScriptContext.ENGINE_SCOPE),
                     ModelService.OUT_PARAM));
             return result;
-        } catch (GeneralException ge) {
-            throw new GenericServiceException(ge);
         } catch (Exception e) {
-            // detailMessage can be null.  If it is null, the exception won't 
be properly returned and logged,
-            // and that will make spotting problems very difficult.
-            // Disabling this for now in favor of returning a proper exception.
+            // When throwing ExecutionServiceException in Groovy DSL run 
Service method
+            // since we are dependent on Groovy MetaClassImpl that throws 
InvokerInvocationException
+            // we need to check nested exception to return the embedded 
service error message.
+            Throwable nested = e.getCause();
+            if (nested instanceof ExecutionServiceException) {
+                return ServiceUtil.returnError(nested.getMessage());
+            }
+            if (UtilValidate.isEmpty(modelService.getInvoke())) {
+                throw new GenericServiceException("Error running Groovy script 
file ["
+                        + modelService.getLocation() + "]: ", e);
+            }
+            // detailMessage can be null.  If it is null, the exception won't 
be properly returned and logged, and that will
+            // make spotting problems very difficult.  Disabling this for now 
in favor of returning a proper exception.
             throw new GenericServiceException("Error running Groovy method [" 
+ modelService.getInvoke() + "]"
                     + " in Groovy file [" + modelService.getLocation() + "]: 
", e);
         }
diff --git 
a/framework/service/src/main/java/org/apache/ofbiz/service/test/GroovyDslServiceEngineTests.java
 
b/framework/service/src/main/java/org/apache/ofbiz/service/test/GroovyDslServiceEngineTests.java
new file mode 100644
index 0000000000..ab3ba0c34a
--- /dev/null
+++ 
b/framework/service/src/main/java/org/apache/ofbiz/service/test/GroovyDslServiceEngineTests.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
*******************************************************************************/
+package org.apache.ofbiz.service.test;
+
+import org.apache.ofbiz.base.util.UtilMisc;
+import org.apache.ofbiz.service.ModelService;
+import org.apache.ofbiz.service.ServiceUtil;
+import org.apache.ofbiz.service.testtools.OFBizTestCase;
+
+import java.util.Map;
+
+public class GroovyDslServiceEngineTests extends OFBizTestCase {
+
+    public GroovyDslServiceEngineTests(String name) {
+        super(name);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+    }
+
+    public final void testGroovyServices() throws Exception {
+        String pingMsg = "Unit Test";
+        Map<String, Object> pingMap = UtilMisc.toMap("ping", pingMsg);
+
+        //test success
+        Map<String, Object> result = 
getDispatcher().runSync("testGroovyPingSuccess", pingMap);
+        assertTrue(ServiceUtil.isSuccess(result));
+        assertEquals("Service result success", 
result.get(ModelService.SUCCESS_MESSAGE));
+        assertEquals(pingMsg, result.get("pong"));
+
+        //test error
+        result = getDispatcher().runSync("testGroovyPingError", pingMap);
+        assertTrue(ServiceUtil.isError(result));
+        assertEquals("Service result error", 
ServiceUtil.getErrorMessage(result));
+        assertEquals(pingMsg, result.get("pong"));
+
+        //test success with DSL
+        result = getDispatcher().runSync("testGroovyPingSuccessWithDSLCall", 
pingMap);
+        assertTrue(ServiceUtil.isSuccess(result));
+        assertEquals("Service result success", 
result.get(ModelService.SUCCESS_MESSAGE));
+        assertEquals(pingMsg, result.get("pong"));
+
+        //test error with DSL (no out param test since DSL do not support out 
param yet when error)
+        result = getDispatcher().runSync("testGroovyPingErrorWithDSLCall", 
pingMap, 60, true);
+        assertTrue(ServiceUtil.isError(result));
+        assertEquals("Service result error", 
result.get(ModelService.ERROR_MESSAGE));
+    }
+}
diff --git a/framework/service/testdef/servicetests.xml 
b/framework/service/testdef/servicetests.xml
index e393706f3d..630085844e 100644
--- a/framework/service/testdef/servicetests.xml
+++ b/framework/service/testdef/servicetests.xml
@@ -22,6 +22,8 @@ under the License.
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
         
xsi:noNamespaceSchemaLocation="https://ofbiz.apache.org/dtds/test-suite.xsd";>
     <test-case case-name="service-tests"><junit-test-suite 
class-name="org.apache.ofbiz.service.test.ServiceEngineTests"/></test-case>
+    <test-case case-name="service-groovy-DSL-tests"><junit-test-suite
+            
class-name="org.apache.ofbiz.service.test.GroovyDslServiceEngineTests"/></test-case>
     <!-- <test-case case-name="service-soap-tests"><junit-test-suite 
class-name="org.apache.ofbiz.service.test.ServiceSOAPTests"/></test-case> -->
     <test-case case-name="service-entity-auto-tests"><junit-test-suite 
class-name="org.apache.ofbiz.service.test.ServiceEntityAutoTests"/></test-case>
 
diff --git 
a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
 
b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
index be48684143..240e84f11f 100644
--- 
a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
+++ 
b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
@@ -36,6 +36,7 @@ import org.apache.ofbiz.base.util.UtilHttp;
 import org.apache.ofbiz.base.util.UtilValidate;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.service.DispatchContext;
+import org.apache.ofbiz.service.ExecutionServiceException;
 import org.apache.ofbiz.service.GenericServiceException;
 import org.apache.ofbiz.service.LocalDispatcher;
 import org.apache.ofbiz.service.ModelParam;
@@ -265,6 +266,10 @@ public class ServiceEventHandler implements EventHandler {
                 request.setAttribute("_ERROR_MESSAGE_", 
e.getNonNestedMessage());
             }
             return "error";
+        } catch (ExecutionServiceException e) {
+            Debug.logError(e, MODULE);
+            request.setAttribute("_ERROR_MESSAGE_", e.getNonNestedMessage());
+            return "error";
         } catch (GenericServiceException e) {
             Debug.logError(e, "Service invocation error", MODULE);
             throw new EventHandlerException("Service invocation error", 
e.getNested());

Reply via email to