This class, to begin with, will have a generic, public httpRequest method:
/** * Provides Jenkins functionality required by the other utility classes. */ //Info: This class helps to reduce the Jenkins plug-in specific code clutter in the utility classes. // All the Jenkins-specific code should be present in this(and more, if required) classes. final class JenkinsUtil { private def script public JenkinsUtil(def script) { this.script = script } /** * * @param link * @param parameters Refer the httpRequest plug-in <a href="https://jenkins.io/doc/pipeline/steps/http_request/">documentation</a> for acceptable parameters. * @return */ public def initiateHttpRequest(String link, Map<String, Object> parameters) { //Remove script.println "Link: ${link}, parameters: ${parameters}" String validationErrorMessage = validateHttpRequestParameters(parameters) if(validationErrorMessage != null && !validationErrorMessage.isEmpty()){ script.println "Validation error in httpRequest ${validationErrorMessage}" return validationErrorMessage } String parametersString = getAppendedParametersForHttpRequest(parameters) script.httpRequest url: link, parametersString } private String validateHttpRequestParameters(Map<String,Object>parameters){ if(parameters == null || parameters.isEmpty()){ return "Parameters for the httpRequest cannot be null/empty." } //TODO:If the parameters contain anything other than the keys mentioned in the official documentation, return //TODO:If the values for any of the parameter keys deviate from what the acceptable values as per the official documentation are, return } private String getAppendedParametersForHttpRequest(Map<String, String> parameters){ StringBuffer parametersSb = new StringBuffer() parameters.each{ key, value -> parametersSb << key+":"+value+"," } parametersSb.deleteCharAt(parametersSb.length()-1) //Remove script.println "parameters are ${parametersSb.toString()}" return parametersSb.toString() } } Suppose, I try to write a unit test for the above class: import spock.lang.Specification class JenkinsUtilTest extends Specification { def "InitiateHttpRequest"() { given: JenkinsUtil jenkinsUtil = new JenkinsUtil(/*How to create a script instance*/) String url = "https://ci.prod-jenkins.com/cjoc/" Map<String,Object>parameters = new HashMap<>() parameters.put("ignoreSslErrors",true) when: def response = jenkinsUtil.initiateHttpRequest(url,parameters) then: response.contains("401") } } } } Questions: 1. How do I create a script instance to pass to the JenkinsUtil(Maybe, a Expando or Script instance) 2. Is there a way(say, by including some Jenkins dependencies in build.gradle) to simulate the 'script.httpRequest' call i.e simulate the Jenkins httpRequest plugin 3. Is it correct to even think of unit-testing such a class 4. Is it correct to even think of creating such a class -- You received this message because you are subscribed to the Google Groups "Jenkins Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/04b3f980-b59a-4b8a-86cd-7b55cf54ccea%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.