Search in sources :

Example 86 with BindingResult

use of org.springframework.validation.BindingResult in project ORCID-Source by ORCID.

the class ClaimControllerTest method testResendEmailFailIfTheProfileIsAlreadyClaimed.

@Test
public void testResendEmailFailIfTheProfileIsAlreadyClaimed() {
    BindingResult bindingResult = mock(BindingResult.class);
    when(bindingResult.hasErrors()).thenReturn(false);
    when(emailManager.emailExists("billie@holiday.com")).thenReturn(true);
    when(emailManager.findOrcidIdByEmail("billie@holiday.com")).thenReturn("0000-0000-0000-0000");
    when(profileEntityCacheManager.retrieve(Mockito.anyString())).thenReturn(getProfileEntityToTestClaimResend(true));
    EmailRequest emailRequest = new EmailRequest();
    emailRequest.setEmail("billie@holiday.com");
    emailRequest = claimController.resendClaimEmail(emailRequest);
    assertNotNull(emailRequest);
    assertNull(emailRequest.getSuccessMessage());
    assertNotNull(emailRequest.getErrors());
    assertFalse(emailRequest.getErrors().isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) EmailRequest(org.orcid.pojo.EmailRequest) Test(org.junit.Test)

Example 87 with BindingResult

use of org.springframework.validation.BindingResult in project ORCID-Source by ORCID.

the class PasswordResetControllerTest method testSubmitConsolidatedPasswordReset.

@Test
public void testSubmitConsolidatedPasswordReset() throws Exception {
    RedirectAttributes redirectAttributes = mock(RedirectAttributes.class);
    BindingResult bindingResult = mock(BindingResult.class);
    OneTimeResetPasswordForm oneTimeResetPasswordForm = new OneTimeResetPasswordForm();
    oneTimeResetPasswordForm.setEncryptedEmail("encrypted string not expired");
    MockHttpSession session = new MockHttpSession();
    when(servletRequest.getSession()).thenReturn(session);
    when(encryptionManager.decryptForExternalUse(any(String.class))).thenReturn("email=any@orcid.org&issueDate=2070-05-29T17:04:27");
    when(bindingResult.hasErrors()).thenReturn(true);
    when(mockEmailManagerReadOnly.findOrcidIdByEmail("any@orcid.org")).thenReturn("0000-0000-0000-0000");
    oneTimeResetPasswordForm = passwordResetController.submitPasswordReset(servletRequest, servletResponse, oneTimeResetPasswordForm);
    assertFalse(oneTimeResetPasswordForm.getErrors().isEmpty());
    oneTimeResetPasswordForm.setPassword("Password#123");
    when(bindingResult.hasErrors()).thenReturn(false);
    when(orcidProfileManager.retrieveOrcidProfileByEmail(eq("any@orcid.org"), Matchers.<LoadOptions>any())).thenReturn(orcidWithSecurityQuestion());
    oneTimeResetPasswordForm = passwordResetController.submitPasswordReset(servletRequest, servletResponse, oneTimeResetPasswordForm);
    assertTrue(oneTimeResetPasswordForm.getSuccessRedirectLocation().equals("https://testserver.orcid.org/my-orcid") || oneTimeResetPasswordForm.getSuccessRedirectLocation().equals("https://localhost:8443/orcid-web/my-orcid"));
    verify(redirectAttributes, never()).addFlashAttribute("passwordResetLinkExpired", true);
    when(encryptionManager.decryptForExternalUse(any(String.class))).thenReturn("email=any@orcid.org&issueDate=1970-05-29T17:04:27");
    oneTimeResetPasswordForm = passwordResetController.submitPasswordReset(servletRequest, servletResponse, oneTimeResetPasswordForm);
    assertFalse(oneTimeResetPasswordForm.getErrors().isEmpty());
}
Also used : RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) BindingResult(org.springframework.validation.BindingResult) MockHttpSession(org.springframework.mock.web.MockHttpSession) OneTimeResetPasswordForm(org.orcid.frontend.web.forms.OneTimeResetPasswordForm) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 88 with BindingResult

use of org.springframework.validation.BindingResult in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleMethodArgumentNotValid.

/**
 * <p>
 *   Errors thrown in Controllers methods with arguments annotated with JAX-RS @Valid.
 * </p>
 *
 * @param ex
 * @return
 */
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    ErrorDTO dto = ErrorDTO.of(fieldErrors, ErrorCodes.VALIDATION_FAILURE.getMessage(), ex.getLocalizedMessage());
    return generateResponseEntity(dto, HttpStatus.BAD_REQUEST);
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) FieldError(org.springframework.validation.FieldError)

Example 89 with BindingResult

use of org.springframework.validation.BindingResult in project openmrs-module-coreapps by openmrs.

the class QuickVisitFragmentController method update.

public FragmentActionResult update(@SpringBean("visitService") VisitService visitService, @RequestParam("patientId") Patient patient, @RequestParam("visitId") Visit visit, @RequestParam(value = "selectedTypeId", required = false) VisitType selectedType, UiUtils uiUtils, HttpServletRequest request) {
    BindingResult bindingResult = null;
    if (selectedType != null) {
        visit.setVisitType(selectedType);
    }
    if (request.getParameterNames() != null && request.getParameterNames().hasMoreElements()) {
        List<VisitAttributeType> attributeTypes = visitService.getAllVisitAttributeTypes();
        WebAttributeUtil.handleSubmittedAttributesForType(visit, bindingResult, VisitAttribute.class, request, attributeTypes);
    }
    try {
        visitService.saveVisit(visit);
    } catch (APIException e) {
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Visit.save.error");
    }
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, uiUtils.message("coreapps.visit.updateVisit.successMessage", uiUtils.format(patient)));
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
    SimpleObject result = SimpleObject.create("success", true, "search", "?patientId=" + visit.getPatient().getId() + "&visitId=" + visit.getId());
    return new ObjectResult(result);
}
Also used : BindingResult(org.springframework.validation.BindingResult) APIException(org.openmrs.api.APIException) SimpleObject(org.openmrs.ui.framework.SimpleObject) VisitAttributeType(org.openmrs.VisitAttributeType) ObjectResult(org.openmrs.ui.framework.fragment.action.ObjectResult)

Example 90 with BindingResult

use of org.springframework.validation.BindingResult in project openmrs-module-coreapps by openmrs.

the class QuickVisitFragmentController method create.

@Transactional
public FragmentActionResult create(@SpringBean("adtService") AdtService adtService, @SpringBean("visitService") VisitService visitService, @RequestParam("patientId") Patient patient, @RequestParam("locationId") Location location, UiUtils uiUtils, @RequestParam(value = "selectedTypeId", required = false) VisitType selectedType, UiSessionContext emrContext, HttpServletRequest request) {
    VisitDomainWrapper activeVisit = adtService.getActiveVisit(patient, location);
    BindingResult bindingResult = null;
    if (activeVisit != null) {
        return new FailureResult(uiUtils.message("coreapps.activeVisits.alreadyExists"));
    }
    Visit visit = adtService.ensureVisit(patient, new Date(), location);
    if (selectedType != null) {
        // set visit type
        visit.setVisitType(selectedType);
        // manually handle the attribute parameters
        List<VisitAttributeType> attributeTypes = visitService.getAllVisitAttributeTypes();
        WebAttributeUtil.handleSubmittedAttributesForType(visit, bindingResult, VisitAttribute.class, request, attributeTypes);
        try {
            visitService.saveVisit(visit);
        } catch (APIException e) {
            return new FailureResult((uiUtils.message("Visit.save.error")));
        }
    }
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, uiUtils.message("coreapps.visit.createQuickVisit.successMessage", uiUtils.encodeHtml(uiUtils.format(patient))));
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
    SimpleObject result = SimpleObject.create("id", visit.getId().toString(), "uuid", visit.getUuid());
    return new ObjectResult(result);
}
Also used : BindingResult(org.springframework.validation.BindingResult) APIException(org.openmrs.api.APIException) SimpleObject(org.openmrs.ui.framework.SimpleObject) FailureResult(org.openmrs.ui.framework.fragment.action.FailureResult) Visit(org.openmrs.Visit) VisitAttributeType(org.openmrs.VisitAttributeType) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) ObjectResult(org.openmrs.ui.framework.fragment.action.ObjectResult) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BindingResult (org.springframework.validation.BindingResult)143 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)28 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)24 List (java.util.List)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)21 ResponseEntity (org.springframework.http.ResponseEntity)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)18 ObjectError (org.springframework.validation.ObjectError)17 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13