1
0
-1

Hi,

I'm finally getting my feet wet with Fluent scripting, and finding it difficult to debug.  I'm trying to update the list of organisations a user has access to.  I'm running this in the Groovy Console.

Background - user "fred.smith" exists and is associated with another organisation.  I want to remove him from that organisation and add him to the "Test" organisation.

import com.avoka.tm.svc.*
import com.avoka.tm.vo.*
import com.avoka.tm.test.MockRequest


def orgList = ['Test']
def orgNames = orgList.toSet()

def request = new MockRequest()

User user = new UserBuilder(request)
    .setLoginName("fred.smith")
    .setOrgNames(orgNames)
    .update()


And getting the following error:

java.lang.StringIndexOutOfBoundsException: String index out of range: 27
at java.lang.String.substring(String.java:1963)
at com.avoka.fc.core.util.PortalUtils.getPortalContext(PortalUtils.java:1725)
at com.avoka.fc.core.util.PortalUtils.getPortal(PortalUtils.java:130)
at com.avoka.tm.svc.UserBuilder.update(UserBuilder.java:578)
at Script1.invoke(script15063013345061574376595.groovy:9)
at Script1.call(script15063013345061574376595.groovy:20)
at com.avoka.core.groovy.runtime.SecureStaticConsoleGroovyTask.call(SecureStaticConsoleGroovyTask.java:100)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)


I suspect I'm missing something related to running scripts like this from the console?  

    CommentAdd your comment...

    3 answers

    1.  
      2
      1
      0

      Hi Doug,

      You would be better to develop your code as a fluent service rather than in the Groovy console.  Services come with support for unit tests out of the box

      In a live service, the request object is provided as a parameter, so you would just use that, but while mocking, you need to provide sufficient properties on the mock object to allow it to execute your code.

      You might find this page helpful: Unit Testing Groovy Services

      1. Doug Oldmeadow

        Thanks Bill - I guess I do need to change my workflow.  I tend to use the console to experiment and once I understand that I have a solution, I write the service.  Point taken though, so thanks.

      2. Doug Oldmeadow

        Hi Bill,

        Well I tried that, and the result is still the same.  The unit test passes a null request (see below).  If I create a mockrequest to pass, I get the error I was getting from the console.   So we're back to figuring out what attributes need to be set on a mock request for this unit test to work ?   The error message gives no hint at all (java.lang.StringIndexOutOfBoundsException: String index out of range: 27).  I read the Unit Testing link, but there is no relevant info for what is needed to be set in the request.

        Doug O


        class UnitTest {

        /*
        * Perform service unit test
        *
        * throws exception if unit test fails
        */
        void invoke(SvcDef svcDef, Map testParams) throws Exception {

        Map args = [
        "formId": 23
        ]

        Map params = [
        "svcDef": svcDef,
        "request": null,
        "user": null,
        "params": args
        ]

        def result = new ServiceInvoker(svcDef).invoke(params)

        // Check result
        logger.info result

        assert "groovy result - 23" == result
        }
        }

      3. Bill Frost

        Hi Doug,

        I was wrong about setContextPath before, you will need this instead:

            request.setRequestURL('https://... your url details')

        That should get you to the next stage.

      4. Doug Oldmeadow

        Hi Bill

        That got me through to the next stage, where I'm getting an error complaining that I sent a null password.  I found someone has a similar issue see (Why do I need to provide a new password when updating a User with Fluent?).

        Having to change a user password, when updating their groups is baffling to me:  and compromises our whole user management process.

        I haven't been able to find any way to programatically change a users organisation (which also entails changes to groups).  Should I log a ticket for this?


        Doug O

      5. Bill Frost

        Hi Doug,

        I've logged an issue for this and will discuss with the team today.  I'll keep you informed of progress.

      CommentAdd your comment...
    2.  
      2
      1
      0

      Hi,

      You will need to set properties on your MockRequest object.

      The one that caused the error can be provided by request.setContextPath(xxx).

        CommentAdd your comment...
      1.  
        1
        0
        -1

        Thanks Bill.  I'm not clear on what context I would need to provide for a script running in the console, versus how I intend to deploy this as a servlet to be called from another system?   Is this documented anywhere?


        Doug O

          CommentAdd your comment...