if (v != null) { if (v instanceof Describable) { o.accumulate(param.getName(), staplerJsonForDescr((Describable)v)); } else if (v instanceof List && !((List) v).isEmpty()) { JSONArray a = new JSONArray(); for (Object obj : (List) v) { if (obj instanceof Describable) { a.add(staplerJsonForDescr((Describable) obj)); } else if (obj instanceof Number) { a.add(obj.toString());
/** * Tests a form submitting part of the generator. * * @param desc * The describable we'll translate to JSON. * @param responseText * Expected directive snippet to be generated */ private void assertGenerateDirective(@Nonnull AbstractDirective desc, @Nonnull String responseText) throws Exception { // First, make sure the expected response text actually matches the toGroovy for the directive. assertEquals(desc.toGroovy(true), responseText); // Then submit the form with the appropriate JSON (we generate it from the directive, but it matches the form JSON exactly) JenkinsRule.WebClient wc = r.createWebClient(); WebRequest wrs = new WebRequest(new URL(r.getURL(), DirectiveGenerator.GENERATE_URL), HttpMethod.POST); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new NameValuePair("json", staplerJsonForDescr(desc).toString())); // WebClient.addCrumb *replaces* rather than *adds*: params.add(new NameValuePair(r.jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(), r.jenkins.getCrumbIssuer().getCrumb(null))); wrs.setRequestParameters(params); WebResponse response = wc.getPage(wrs).getWebResponse(); assertEquals("text/plain", response.getContentType()); assertEquals(responseText, response.getContentAsString().trim()); }