@Test public void whenEmptyHeaderTypeMustReturnError() 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); //perform the test performDoIndexTest(staplerRequest, staplerResponse, uniqueFile); //validate that everything was done as planed verify(staplerResponse).setStatus(403); String expectedOutput = "Only push event can be accepted."; isExpectedOutput(uniqueFile, expectedOutput); log.info("Test succeeded."); }
@Test public void whenWrongHeaderTypeMustReturnError() 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("junk"); //perform the testÎ performDoIndexTest(staplerRequest, staplerResponse, uniqueFile); //validate that everything was done as planed verify(staplerResponse).setStatus(403); String expectedOutput = "Only push event can be accepted."; isExpectedOutput(uniqueFile, expectedOutput); log.info("Test succeeded."); }
@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."); }
@Test public void whenNoJobInQueryStringMustReturnError() 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("foo=bar&blaah=blaah"); //perform the testÎ performDoIndexTest(staplerRequest, staplerResponse, uniqueFile); //validate that everything was done as planed verify(staplerResponse).setStatus(404); String expectedOutput = "Parameter 'job' is missing."; isExpectedOutput(uniqueFile, expectedOutput); log.info("Test succeeded."); }
@Test public void whenEmptyJobInQueryStringMustReturnError() 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."); }
@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."); }
@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."); }
@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."); }
@SuppressWarnings("unchecked") private static void setupReq() { req = Mockito.mock(RequestImpl.class); given(req.bindJSON(any(Class.class), any(JSONObject.class))).willCallRealMethod(); given(req.bindJSON(any(Class.class), any(Class.class), any(JSONObject.class))).willCallRealMethod(); given(req.setBindListener(any(BindInterceptor.class))).willCallRealMethod(); req.setBindListener(BindInterceptor.NOOP); }
/** * Constructs this extension of {@link RequestImpl} under the assumption that {@link RequestImpl} is also the * underlying type of the {@link StaplerRequest}. * * @param request the request submitted the {@link TemplateDrivenMultiBranchProject} */ TemplateStaplerRequestWrapper(StaplerRequest request) { /* * Ugly casts to RequestImpl... but should be ok since it will throw * errors, which we want anyway if it's not that type. */ super(request.getStapler(), request, ((RequestImpl) request).ancestors, ((RequestImpl) request).tokens); }
/** * A request to use with {@link Stapler} that pretends the given * json block is submitted form data, to allow it to be sent through * the object creation process. */ @VisibleForTesting StaplerRequest getRequest(Stapler stapler, final JSONObject json) { return new RequestImpl(stapler, new FakeHttpServletRequest() { @Override public String getRequestURI() { return "/"; } }, Collections.EMPTY_LIST, null) { @Override public JSONObject getSubmittedForm() { return json; } @Override public String getParameter(String name) { Object o = getSubmittedForm().opt(name); if (o instanceof JSONObject) { return ((JSONObject) o).optString("value"); } else if (o != null) { return o.toString(); } return null; } }; }