Search in sources :

Example 41 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class BlueUrlTokenizer method parseCurrentRequest.

/**
 * Parse the {@link Stapler#getCurrentRequest() current Stapler request} and return a {@link BlueUrlTokenizer} instance
 * iff the URL is a Blue Ocean UI URL.
 *
 * @return A {@link BlueUrlTokenizer} instance iff the URL is a Blue Ocean UI URL, otherwise {@code null}.
 * @throws IllegalStateException Called outside the scope of an active {@link StaplerRequest}.
 */
@CheckForNull
public static BlueUrlTokenizer parseCurrentRequest() throws IllegalStateException {
    StaplerRequest currentRequest = Stapler.getCurrentRequest();
    if (currentRequest == null) {
        throw new IllegalStateException("Illegal call to BlueoceanUrl.parseCurrentRequest outside the scope of an active StaplerRequest.");
    }
    String path = currentRequest.getOriginalRequestURI();
    String contextPath = currentRequest.getContextPath();
    path = path.substring(contextPath.length());
    return parse(path);
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) CheckForNull(javax.annotation.CheckForNull)

Example 42 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenQueryStringIsNullMustThrowException.

@Test
public void whenQueryStringIsNullMustThrowException() throws Exception {
    // Prepare the SUT
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn(null);
    GogsWebHook gogsWebHook = new GogsWebHook();
    try {
        gogsWebHook.doIndex(staplerRequest, staplerResponse);
    } catch (NullPointerException e) {
        String expectedErrMsg = "The queryString in the request is null";
        assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
        log.info("call failed as expected.");
        return;
    }
    fail("The call should have failed.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) Test(org.junit.Test)

Example 43 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method callDoIndexWithNullResponseMessageMustThrowException.

@Test
public void callDoIndexWithNullResponseMessageMustThrowException() throws IOException {
    GogsWebHook gogsWebHook = new GogsWebHook();
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    try {
        gogsWebHook.doIndex(staplerRequest, null);
    } catch (NullPointerException e) {
        String expectedErrMsg = "Null reply submitted to doIndex method";
        assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
        log.info("call failed as expected.");
        return;
    }
    fail("The call should have failed.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) Test(org.junit.Test)

Example 44 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenUriDoesNotContainUrlNameMustReturnError.

@Test
public void whenUriDoesNotContainUrlNameMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn("job=myJob");
    MockServletInputStream inputStream = new MockServletInputStream("body");
    when(staplerRequest.getInputStream()).thenReturn(inputStream);
    when(staplerRequest.getRequestURI()).thenReturn("/badUri/aaa");
    // perform the testÎ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(404);
    String expectedOutput = "No payload or URI contains invalid entries.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 45 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenEmptyJob2InQueryStringMustReturnError.

@Test
public void whenEmptyJob2InQueryStringMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn("job=&foo=bar");
    // perform the testÎ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(404);
    String expectedOutput = "No value assigned to parameter 'job'";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Aggregations

StaplerRequest (org.kohsuke.stapler.StaplerRequest)79 Test (org.junit.Test)45 MultiBranchProject (jenkins.branch.MultiBranchProject)28 JSONObject (net.sf.json.JSONObject)20 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 ServiceException (io.jenkins.blueocean.commons.ServiceException)17 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 User (hudson.model.User)14 Mailer (hudson.tasks.Mailer)13 StaplerResponse (org.kohsuke.stapler.StaplerResponse)8 File (java.io.File)7 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Nonnull (javax.annotation.Nonnull)5 Item (hudson.model.Item)4 ArrayList (java.util.ArrayList)4