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

openmrs / openmrs-core / 43502855768

13 Nov 2026 08:33PM UTC coverage: 67.328% (+0.4%) from 66.969%
43502855768

push

github

ibacher
TRUNK-6616: Count obs version history statements without the privilege check

getObsVersionHistory_shouldLoadTheWholeChainInTwoQueries counted the
statements of the service call, which include its privilege check. When
test setup has cleared the role privilege cache in the same transaction,
that check reads roles from the database, so the count depended on test
order. Measure the DAO call instead.

26896 of 39948 relevant lines covered (67.33%)

0.67 hits per line

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

88.89
/api/src/main/java/org/openmrs/validator/RoleValidator.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.validator;
11

12
import org.openmrs.Role;
13
import org.openmrs.annotation.Handler;
14
import org.openmrs.api.context.Context;
15
import org.openmrs.util.PrivilegeConstants;
16
import org.springframework.validation.Errors;
17
import org.springframework.validation.ValidationUtils;
18
import org.springframework.validation.Validator;
19

20
/**
21
 * Validates attributes on the {@link Role} object
22
 * 
23
 * @since 1.5
24
 */
25
@Handler(supports = { Role.class }, order = 50)
26
public class RoleValidator implements Validator {
1 ✔
27
        
28
        /**
29
         * Determines if the command object being submitted is a valid type
30
         * 
31
         * @see org.springframework.validation.Validator#supports(java.lang.Class)
32
         */
33
        @Override
34
        public boolean supports(Class<?> c) {
35
                return c.equals(Role.class);
1 ✔
36
        }
37
        
38
        /**
39
         * Checks the form object for any inconsistencies/errors
40
         * 
41
         * @see org.springframework.validation.Validator#validate(java.lang.Object,
42
         *      org.springframework.validation.Errors)
43
         * <strong>Should</strong> throw NullPointerException if role is null
44
         * <strong>Should</strong> fail validation if role is empty or whitespace
45
         * <strong>Should</strong> pass validation if description is null or empty or whitespace
46
         * <strong>Should</strong> fail validation if role has leading or trailing space
47
         * <strong>Should</strong> fail validation if another role's name differs only in case
48
         * <strong>Should</strong> pass validation if all required fields have proper values
49
         * <strong>Should</strong> pass validation if field lengths are correct
50
         * <strong>Should</strong> fail validation if field lengths are not correct
51
         */
52
        @Override
53
        public void validate(Object obj, Errors errors) {
54
                Role role = (Role) obj;
1 ✔
55
                if (role == null) {
1 ✔
56
                        errors.rejectValue("role", "error.general");
×
57
                } else {
58
                        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "role", "error.role");
1 ✔
59
                        
60
                        // reject any role that has a leading or trailing space
61
                        if (!role.getRole().equals(role.getRole().trim())) {
1 ✔
62
                                errors.rejectValue("role", "error.trailingSpaces");
1 ✔
63
                        }
64

65
                        // Names differing only in case are one role on some databases and two on others, and privilege
66
                        // checks treat superuser role names case-insensitively, so each role needs a distinct name.
67
                        if (hasNameDifferingOnlyInCase(role)) {
1 ✔
68
                                errors.rejectValue("role", "Role.duplicateName");
1 ✔
69
                        }
70
                }
71
                if (obj == null) {
1 ✔
72
                        throw new IllegalArgumentException("validated role object should not be null");
×
73
                } else {
74
                        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "role", "description");
1 ✔
75
                }
76
        }
1 ✔
77

78
        private static boolean hasNameDifferingOnlyInCase(Role role) {
79
                Context.addProxyPrivilege(PrivilegeConstants.GET_ROLES);
1 ✔
80
                try {
81
                        return Context.getUserService().getAllRoles().stream().map(Role::getRole)
1 ✔
82
                                .anyMatch(name -> !name.equals(role.getRole()) && name.trim().equalsIgnoreCase(role.getRole()));
1 ✔
83
                } finally {
84
                        Context.removeProxyPrivilege(PrivilegeConstants.GET_ROLES);
1 ✔
85
                }
86
        }
87

88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc