private ModelAndView getModelAndView(MvcResult mvcResult) { ModelAndView mav = mvcResult.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } return mav; }
/** * Assert the selected view name with the given Hamcrest {@link Matcher}. */ public ResultMatcher name(final Matcher<? super String> matcher) { return result -> { ModelAndView mav = result.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } assertThat("View name", mav.getViewName(), matcher); }; }
/** * Assert the selected view name. */ public ResultMatcher name(final String expectedViewName) { return result -> { ModelAndView mav = result.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } assertEquals("View name", expectedViewName, mav.getViewName()); }; }
/** * Print {@link MvcResult} details. */ @Override public final void handle(MvcResult result) throws Exception { this.printer.printHeading("MockHttpServletRequest"); printRequest(result.getRequest()); this.printer.printHeading("Handler"); printHandler(result.getHandler(), result.getInterceptors()); this.printer.printHeading("Async"); printAsyncResult(result); this.printer.printHeading("Resolved Exception"); printResolvedException(result.getResolvedException()); this.printer.printHeading("ModelAndView"); printModelAndView(result.getModelAndView()); this.printer.printHeading("FlashMap"); printFlashMap(RequestContextUtils.getOutputFlashMap(result.getRequest())); this.printer.printHeading("MockHttpServletResponse"); printResponse(result.getResponse()); }
@Test public void testQRDoesNotChangeDuringOneSession() throws Exception { redirectToMFARegistration(); assertFalse(userGoogleMfaCredentialsProvisioning.activeUserCredentialExists(user.getId(), mfaProvider.getId())); MvcResult res = performGetMfaRegister().andExpect(view().name("mfa/qr_code")).andReturn(); String qrUrl = (String) res.getModelAndView().getModel().get("qrurl"); performGetMfaRegister() .andExpect(view().name("mfa/qr_code")) .andExpect(model().attribute("qrurl", qrUrl)); }
private ModelAndView getModelAndView(MvcResult mvcResult) { ModelAndView mav = mvcResult.getModelAndView(); assertTrue("No ModelAndView found", mav != null); return mav; }
private ModelAndView getModelAndView(MvcResult mvcResult) { ModelAndView mav = mvcResult.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } return mav; }
@Override public void match(MvcResult result) throws Exception { ModelAndView mav = result.getModelAndView(); assertTrue("No ModelAndView found", mav != null); assertThat("View name", mav.getViewName(), matcher); } };
/** * Assert the selected view name with the given Hamcrest {@link Matcher}. */ public ResultMatcher name(final Matcher<? super String> matcher) { return result -> { ModelAndView mav = result.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } assertThat("View name", mav.getViewName(), matcher); }; }
@Override public void match(MvcResult result) throws Exception { ModelAndView mav = result.getModelAndView(); assertTrue("No ModelAndView found", mav != null); assertEquals("View name", expectedViewName, mav.getViewName()); } };
/** * Assert the selected view name. */ public ResultMatcher name(final String expectedViewName) { return result -> { ModelAndView mav = result.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } assertEquals("View name", expectedViewName, mav.getViewName()); }; }
@Test @SuppressWarnings("unchecked") public void shouldReturnAllJobDefinitionsAsHtml() throws Exception { // given final JobDefinition fooJobDef = jobDefinition("FooJob", "Foo"); final JobDefinition barJobDef = notTriggerableDefinition("BarJob", "Bar"); when(jobDefinitionService.getJobDefinitions()).thenReturn(asList(fooJobDef, barJobDef)); // when mockMvc.perform( get(MANAGEMENT_CONTEXT + "/jobdefinitions/") .accept("text/html") ) .andExpect(status().is(200)) .andDo(result -> { Map<String, Object> model = result.getModelAndView().getModel(); List<Map<String, Object>> jobDefinitions = (List<Map<String, Object>>) model.get("jobdefinitions"); assertThat(jobDefinitions.size(), is(2)); assertThat(jobDefinitions.get(0).get("frequency"), is("Every 60 Minutes")); assertThat(jobDefinitions.get(0).get("isDisabled"), is(false)); assertThat(jobDefinitions.get(0).get("comment"), is("")); assertThat(jobDefinitions.get(1).get("frequency"), is("Never")); assertThat(jobDefinitions.get(1).get("isDisabled"), is(false)); assertThat(jobDefinitions.get(1).get("comment"), is("")); }); }
@Test @SuppressWarnings("unchecked") public void shouldConvertToSecondsIfSecondsIsLessThan60() throws Exception { // Given final JobDefinition jobDef = jobDefinition("TheJob", "Job", ofSeconds(59)); when(jobDefinitionService.getJobDefinitions()).thenReturn(singletonList(jobDef)); // when mockMvc.perform( get(MANAGEMENT_CONTEXT + "/jobdefinitions/") .accept("text/html") ) .andExpect(status().is(200)) .andDo(result -> { List<Map<String, Object>> jobDefinitions = (List<Map<String, Object>>) result.getModelAndView().getModel().get("jobdefinitions"); assertThat(jobDefinitions.size(), is(1)); assertThat(jobDefinitions.get(0).get("frequency"), is("Every 59 Seconds")); }); }
@Test @SuppressWarnings("unchecked") public void shouldConvertToMinutesIfSecondsIsNotLessThan60() throws Exception { // Given final JobDefinition jobDef = jobDefinition("TheJob", "Job", ofSeconds(60)); when(jobDefinitionService.getJobDefinitions()).thenReturn(singletonList(jobDef)); // when mockMvc.perform( get(MANAGEMENT_CONTEXT + "/jobdefinitions/") .accept("text/html") ) .andExpect(status().is(200)) .andDo(result -> { List<Map<String, Object>> jobDefinitions = (List<Map<String, Object>>) result.getModelAndView().getModel().get("jobdefinitions"); assertThat(jobDefinitions.size(), is(1)); assertThat(jobDefinitions.get(0).get("frequency"), is("Every 1 Minutes")); }); }
@Test public void loginPageTest() throws Exception { MvcResult mvcResult = mockMvc.perform(get(SecurityConstants.AUTH_LOGIN_PATH)).andExpect(status().isOk()).andReturn(); Assert.assertNotNull(mvcResult.getModelAndView().getViewName()); }
@Test public void getSoapPort() throws Exception { final SoapProject soapProject = SoapProjectGenerator.generateSoapProject(); final SoapPort soapPort = SoapPortGenerator.generateSoapPort(); final SoapOperation soapOperation = SoapOperationGenerator.generateSoapOperation(); final List<SoapOperation> operations = new ArrayList<SoapOperation>(); operations.add(soapOperation); soapPort.setOperations(operations); when(serviceProcessor.process(any(ReadSoapPortInput.class))).thenReturn(ReadSoapPortOutput.builder() .port(soapPort) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + soapProject.getId() + SLASH + PORT + SLASH + soapPort.getId() + SLASH); ResultActions result = mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(4 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(SOAP_PORT, soapPort)); SoapPort soapPortResponse = (SoapPort) result.andReturn().getModelAndView().getModel().get(SOAP_PORT); }
/** * Print {@link MvcResult} details. */ @Override public final void handle(MvcResult result) throws Exception { this.printer.printHeading("MockHttpServletRequest"); printRequest(result.getRequest()); this.printer.printHeading("Handler"); printHandler(result.getHandler(), result.getInterceptors()); this.printer.printHeading("Async"); printAsyncResult(result); this.printer.printHeading("Resolved Exception"); printResolvedException(result.getResolvedException()); this.printer.printHeading("ModelAndView"); printModelAndView(result.getModelAndView()); this.printer.printHeading("FlashMap"); printFlashMap(RequestContextUtils.getOutputFlashMap(result.getRequest())); this.printer.printHeading("MockHttpServletResponse"); printResponse(result.getResponse()); }
/** * Print {@link MvcResult} details. */ @Override public final void handle(MvcResult result) throws Exception { this.printer.printHeading("MockHttpServletRequest"); printRequest(result.getRequest()); this.printer.printHeading("Handler"); printHandler(result.getHandler(), result.getInterceptors()); this.printer.printHeading("Async"); printAsyncResult(result); this.printer.printHeading("Resolved Exception"); printResolvedException(result.getResolvedException()); this.printer.printHeading("ModelAndView"); printModelAndView(result.getModelAndView()); this.printer.printHeading("FlashMap"); printFlashMap(RequestContextUtils.getOutputFlashMap(result.getRequest())); this.printer.printHeading("MockHttpServletResponse"); printResponse(result.getResponse()); }
@Test public void testDefaultPage() throws Exception { final SoapProject soapProject = SoapProjectGenerator.generateSoapProject(); final SoapPort soapPort = SoapPortGenerator.generateSoapPort(); final SoapOperation soapOperation = SoapOperationGenerator.generateSoapOperation(); when(serviceProcessor.process(isA(ReadSoapPortInput.class))).thenReturn(ReadSoapPortOutput.builder() .port(soapPort) .build()); when(serviceProcessor.process(isA(ReadSoapOperationInput.class))).thenReturn(ReadSoapOperationOutput.builder() .operation(soapOperation) .build()); when(serviceProcessor.process(isA(ReadSoapEventsByOperationIdInput.class))).thenReturn(ReadSoapEventsByOperationIdOutput.builder() .soapEvents(new ArrayList<SoapEvent>()) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + soapProject.getId() + SLASH + PORT + SLASH + soapPort.getId() + SLASH + OPERATION + SLASH + soapOperation.getId() + SLASH); ResultActions result = mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(6 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(SOAP_PROJECT_ID, soapProject.getId())) .andExpect(MockMvcResultMatchers.model().attribute(SOAP_PORT_ID, soapPort.getId())) .andExpect(MockMvcResultMatchers.model().attribute(SOAP_OPERATION, soapOperation)); SoapOperation SoapOperationResponse = (SoapOperation) result.andReturn().getModelAndView().getModel().get(SOAP_OPERATION); String hostAddress = serviceController.getHostAddress(); Assert.assertEquals(HTTP + hostAddress + COLON + DEFAULT_PORT + CONTEXT + SLASH + MOCK + SLASH + SOAP + SLASH + PROJECT + SLASH + soapProject.getId() + SLASH + soapPort.getUri(), soapOperation.getInvokeAddress()); }
@Test public void testGetServiceValid() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); when(serviceProcessor.process(isA(ReadRestResourceInput.class))).thenReturn(ReadRestResourceOutput.builder().restResource(restResource).build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId()); ResultActions result = mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(5 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, restProject.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATION_ID, restApplication.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_RESOURCE, restResource)); RestResource restResourceResponse = (RestResource) result.andReturn().getModelAndView().getModel().get(REST_RESOURCE); String hostAddress = restResourceController.getHostAddress(); }