Check Out the Latest News About SmartBear Connect
JOIN OUR WEEKLY CHALLENGE
API_Bloom_banner

API Bloom is a weekly prize-winning challenge in the Ready! API Community. Each week, we assign a task that you need to accomplish to become a winner. See the rules here.

THIS WEEK TASK
Get the biggest number of Kudos for your replies on the Ready! API forums
Top 3 Kudoed members will get $50, $35, and $15 gift cards!

LAST WEEK WINNERS
@nmrao, @msiadak, @Debz, @richie, @Emil91, @PramodYadav, @jkrier

Reply
Occasional Contributor
Posts: 5
Registered: ‎04-21-2017
Accepted Solution

Call Java class with variable in groovy

Hi all... I need to call java class and send variable in to. I know how to call class.

 

my code:

 

 

def oliNum="1234567890"
Ticket newTicket = TicketTestUtil.generateTicket();

log.info newTicket;

 

 

And I need send -> oliNum in to the java class. Could you help please?

 

Thanks

 

Community Hero
Posts: 4,364
Registered: ‎05-17-2013

Re: Call Java class with variable in groovy

You need to know the API of that Java class. There should an appropriate method which accepts the argument of same type.
Do you have that full class?
Occasional Contributor
Posts: 5
Registered: ‎04-21-2017

Re: Call Java class with variable in groovy

Yes I have source code whole java class. 

 

public final class TicketTestUtil {

    private static final Gson gson = JsonUtils.createGson(false);
    private static final String HMAC_PASSWORD_IN_BASE64 = "xxxxxxxxx";

    private TicketTestUtil() {
        // empty on purpose for utility class
    }

    public static String generateTicketJson() {
        Ticket ticket = generateTicket();
        return gson.toJson(ticket);
    }

    public static Ticket generateTicket() {

        RandomStringGenerator randomStringGenerator = new SecuredRandomStringGenerator(SecuredRandomStringGenerator.Alphabet.ALPHANUMERIC_CHARS);

        Ticket ticket = new Ticket();
        ticket.setChannel(1l);
        ticket.setAlgorithmType(1l);
        ticket.setMethodType(1l);
        ticket.setTicketNonce(randomStringGenerator.nextString(20).toString());
        ticket.setOliNumber("4778077007");
        ticket.setTicketTimeStamp(new LocalDateTime(DateTimeZone.UTC).toDateTime(DateTimeZone.UTC).getMillis());
        ticket.setTicketVersion(1l);
        ticket.setClientThreadId(1l);

        ByteSource hmacPassword;
        try {
            hmacPassword = ByteSourceFactory.create(new Base64Codec().decode(HMAC_PASSWORD_IN_BASE64.getBytes(StandardCharsets.UTF_8)));
        } catch (CodecException e) {
            return null;
        }

        HMACHashFunction hmacHash = new HMACHashFunction(HMAC_SHA256, hmacPassword);
        String hmac = "";
        try {
            hmac = hmacHash.computeHash(ByteSourceFactory.create(ticket.getOliNumber() +
                    ticket.getChannel() +
                    ticket.getTicketNonce() +
                    ticket.getTicketTimeStamp() +
                    String.valueOf(ticket.getMethodType()))).toBase64();

        } catch (HashException e) {
            // error not logged, we don"t care
        }
        ticket.setHmac(hmac);

        return ticket;
    }

}

In the static generateTicket() is setOliNumber and there I need to set variable waht will I send from groovy in soapUI. 

Occasional Contributor
Posts: 5
Registered: ‎04-21-2017

Re: Call Java class with variable in groovy

Maybe, fastest will be, rewrite this class to the groovy... could someone help? 

thanks

Highlighted
Community Hero
Posts: 4,364
Registered: ‎05-17-2013

Re: Call Java class with variable in groovy

Here are the changes needed in your java class:

 

#1 from

 

public static Ticket generateTicket() {

 

To

public static Ticket generateTicket(String oliNumber) {

 

#2 from

ticket.setOliNumber("4778077007");

To

 ticket.setOliNumber(oliNumber);

 

 

 

In the groovy script, you should be able to call

 

Change from 

Ticket newTicket = TicketTestUtil.generateTicket()

To:

Ticket newTicket = TicketTestUtil.generateTicket(oliNumber)

 

 

 

 

Occasional Contributor
Posts: 5
Registered: ‎04-21-2017

Re: Call Java class with variable in groovy

Thanks a lot... I'll try.

 

Regards

Stan

Occasional Contributor
Posts: 5
Registered: ‎04-21-2017

Re: Call Java class with variable in groovy

It works! thanks a lot... :-) 

Regards 

Stan

Let's Meet in Boston