Forum Discussion

Gepir's avatar
Gepir
Occasional Contributor
8 years ago

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

 

  • 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)

     

     

     

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    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?
    • Gepir's avatar
      Gepir
      Occasional Contributor

      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. 

      • Gepir's avatar
        Gepir
        Occasional Contributor

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

        thanks