Forum Discussion

aFrigge's avatar
aFrigge
Frequent Visitor
7 years ago

Data Generation SoapUI - Swedish organisation number

Hi!

 

I'm very new to groovy (and SoapUI) scripting and I have no experience with Java.

 

I'm trying to create a script that generates swedish organisation numbers for automated tests.

 

When building a TIN you use the luhn algorithm (wiki), but I have a hard time figuring out how to make it in groovy.


I've found some examples of how to do it in Java (link), but I don't know how to translate it to groovy and make it work in SoapUI.

 

The format I want is 999999-9999 or 9999999999.

 

Thank you!

 

[EDIT]

I tried generating the first 9 digits from DataGen, but I still don't understand how I can calculate and append the property with the check digit.

  • JHunt's avatar
    JHunt
    Community Hero

    The Java code that you linked to works just fine as Groovy for me. I only made the syntax a bit more Groovy below, but I didn't need to in order to run it.

     

    You have asked in the wrong forum for help with a DataGen (that would need to go to the SoapUI Pro forum), but we can do this with a Groovy script too:

     

    Closure<Boolean> luhn = { String num ->
        int sum = 0
        boolean alternate = false
        for (int i = num.length() - 1; i >= 0; i--) {
            int n = Integer.parseInt(num.substring(i, i + 1))
            if (alternate) {
                n *= 2;
                if (n > 9) {
                    n = (n % 10) + 1
                }
            }
            sum += n
            alternate = !alternate
        }
        return (sum % 10 == 0)
    }
    
    int next (Closure c, int x) {
        final int MAX = 9999999999
        for (i = x + 1; i <= MAX; i++) {
            if (c(i.toString())) return i
        }
        throw new Exception ("No more in range")
    }
    
    int tin = 1234567890
    (1..5).each {
        tin = next (luhn, tin)
        context.tin = tin
        testRunner.runTestStepByName("SOAP Request")
    }
    return null

    This is finding the next 5 numbers starting after 1234567890 and sending a test request for each of them. See the screenshot for how you might set up the test case. We are using the context to hold a value to be used in another step.