• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-module-webservices.rest / 22095623804

17 Feb 2026 10:52AM UTC coverage: 68.572% (+0.03%) from 68.541%
22095623804

push

github

web-flow
RESTWS-1014: Implemented ConceptTreeResource and DrugsByConceptSearch… (#695)

* RESTWS-1014: Implemented ConceptTreeResource and DrugsByConceptSearchHandler

* RESTWS-1014: Added tests

* RESTWS1014: Throwing exception when concept param is not passed

* RESTWS-1014: Applied new identation, code improvements

* RESTWS-1014: Code review

44 of 53 new or added lines in 2 files covered. (83.02%)

99 existing lines in 4 files now uncovered.

9731 of 14191 relevant lines covered (68.57%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/omod-common/src/main/java/org/openmrs/module/webservices/validation/ValidateUtil.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.module.webservices.validation;
11

12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.util.LinkedHashSet;
15
import java.util.Set;
16

17
import org.apache.commons.lang.StringUtils;
18
import org.openmrs.api.APIException;
19
import org.openmrs.api.AdministrationService;
20
import org.openmrs.api.context.Context;
21
import org.springframework.validation.BindException;
22
import org.springframework.validation.Errors;
23
import org.springframework.validation.FieldError;
24
import org.springframework.validation.ObjectError;
25

26
/**
27
 * This is just a copy of the 1.9.x core ValidateUtil method, *except* that if a validation failure
28
 * occurs, then it returns a ValidationException with the Spring Errors object included within in
29
 * (see RESTWS-422) This change will also been made to core (TRUNK-4296). Once REST-WS only supports
30
 * versions of core that have this change, this class (and ValidationException) can be removed, and
31
 * we will not have to explicitly call the "validate" within the DelegatingCrudResource, as we can
32
 * rely on the underlying API validation to do the right thing, and return a ValidationException
33
 * with Errors which we can trap in the BaseRestController
34
 * 
35
 * @deprecated since 3.2.0, use {@link org.openmrs.validator.ValidateUtil} instead  
36
 * Remove this when this ticket is done: https://openmrs.atlassian.net/browse/RESTWS-1016
37
 */
38
@Deprecated
UNCOV
39
public class ValidateUtil {
×
40
        
41
        /**
42
         * Test the given object against all validators that are registered as compatible with the
43
         * object class
44
         * 
45
         * @param obj the object to validate
46
         * @throws ValidationException thrown if a binding exception occurs <strong>Should</strong>
47
         *             throw ValidationException if errors occur during validation
48
         */
49
        public static void validate(Object obj) throws ValidationException {
50
                
UNCOV
51
                Errors errors = new BindException(obj, "");
×
52
                
53
                AdministrationService administrationService = Context.getAdministrationService();
×
54
                
55
                try {
56
                        // using refection here for compatibility with OpenMRS 1.8,x, which does not have this method
UNCOV
57
                        Method validate = AdministrationService.class.getMethod("validate", Object.class, Errors.class);
×
58
                        validate.invoke(administrationService, obj, errors);
×
59
                }
60
                catch (NoSuchMethodException ex) {
×
61
                        // not running OpenMRS 1.9 or higher, so just skip this validation
UNCOV
62
                        return;
×
63
                }
64
                catch (InvocationTargetException ex) {
×
65
                        throw new APIException("Unable to invoke administrationService.validate(Object, Errors) via reflection", ex);
×
66
                }
67
                catch (IllegalAccessException ex) {
×
68
                        throw new APIException("Unable to invoke administrationService.validate(Object, Errors) via reflection", ex);
×
UNCOV
69
                }
×
70
                
UNCOV
71
                if (errors.hasErrors()) {
×
UNCOV
72
                        throw new ValidationException(generateExceptionMessage(obj, errors), errors);
×
73
                }
74
        }
×
75
        
76
        private static String generateExceptionMessage(Object obj, Errors errors) {
77
                Set<String> uniqueErrorMessages = new LinkedHashSet<String>();
×
78
                for (Object objerr : errors.getAllErrors()) {
×
UNCOV
79
                        ObjectError error = (ObjectError) objerr;
×
80
                        String message = Context.getMessageSourceService().getMessage(error.getCode());
×
81
                        if (error instanceof FieldError) {
×
UNCOV
82
                                message = ((FieldError) error).getField() + ": " + message;
×
83
                        }
84
                        uniqueErrorMessages.add(message);
×
85
                }
×
86
                
UNCOV
87
                String exceptionMessage = "'" + obj + "' failed to validate with reason: ";
×
UNCOV
88
                exceptionMessage += StringUtils.join(uniqueErrorMessages, ", ");
×
UNCOV
89
                return exceptionMessage;
×
90
        }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc