• 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

94.44
/api/src/main/java/org/openmrs/api/cache/RolePrivileges.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.api.cache;
11

12
import java.io.Serializable;
13
import java.util.Collections;
14
import java.util.HashSet;
15
import java.util.Locale;
16
import java.util.Objects;
17
import java.util.Set;
18

19
/**
20
 * Immutable, pre-computed view of everything a single role grants: the flattened set of privilege
21
 * names of the role and all of its transitively inherited roles, the names of those roles, and
22
 * whether that closure confers superuser status. It is the cached value used by
23
 * {@link RolePrivilegeCache} to answer privilege and role checks without re-expanding the role
24
 * graph on every call. Role names, like privilege names, are compared case-insensitively.
25
 * <p>
26
 * Privilege comparison in OpenMRS is case-insensitive (see
27
 * {@link org.openmrs.Role#hasPrivilege(String)}), so {@link #containsPrivilege(String)} and
28
 * {@link #equals(Object)}/{@link #hashCode()} compare {@link #normalize(String) normalized} names.
29
 * <p>
30
 * {@link #getPrivilegeNames()} nonetheless reports the names in the casing they were supplied in,
31
 * not normalized, since they name privileges in messages, such as those listing the privileges a
32
 * user lacks.
33
 *
34
 * @since 3.0.0, 2.9.0, 2.8.9
35
 */
36
public final class RolePrivileges implements Serializable {
37

38
        // bumped when normalizedNames (3.0.0) and then roleNames were added to the serialized form
39
        private static final long serialVersionUID = 3L;
40

41
        /** Names as supplied, for display; never used for matching. */
42
        private final Set<String> privilegeNames;
43

44
        /** The same names {@link #normalize(String) normalized}; what every comparison here uses. */
45
        private final Set<String> normalizedNames;
46

47
        private final boolean grantsSuperuser;
48

49
        /** Names of the role and its inherited roles as supplied */
50
        private final Set<String> roleNames;
51

52
        /** The same names {@link #normalize(String) normalized}, for matching */
53
        private final Set<String> normalizedRoleNames;
54

55
        /**
56
         * @param privilegeNames privilege names granted by the role and its inherited closure; stored as
57
         *            supplied and additionally normalized for matching, null elements ignored
58
         * @param grantsSuperuser whether the role or any inherited role confers superuser status
59
         */
60
        public RolePrivileges(Set<String> privilegeNames, boolean grantsSuperuser) {
61
                this(privilegeNames, grantsSuperuser, Collections.emptySet());
1 ✔
62
        }
1 ✔
63

64
        /**
65
         * @param privilegeNames privilege names granted by the role and its inherited closure; stored as
66
         *            supplied and additionally normalized for matching, null elements ignored
67
         * @param grantsSuperuser whether the role or any inherited role confers superuser status
68
         * @param roleNames names of the role and every role it inherits, null elements ignored
69
         * @since 2.8.11
70
         */
71
        public RolePrivileges(Set<String> privilegeNames, boolean grantsSuperuser, Set<String> roleNames) {
1 ✔
72
                Objects.requireNonNull(privilegeNames, "privilegeNames must not be null");
1 ✔
73
                Objects.requireNonNull(roleNames, "roleNames must not be null");
1 ✔
74
                this.privilegeNames = withoutNulls(privilegeNames);
1 ✔
75
                this.normalizedNames = normalizeAll(this.privilegeNames);
1 ✔
76
                this.grantsSuperuser = grantsSuperuser;
1 ✔
77
                this.roleNames = withoutNulls(roleNames);
1 ✔
78
                this.normalizedRoleNames = normalizeAll(this.roleNames);
1 ✔
79
        }
1 ✔
80

81
        private static Set<String> withoutNulls(Set<String> names) {
82
                Set<String> copy = new HashSet<>();
1 ✔
83
                for (String name : names) {
1 ✔
84
                        if (name != null) {
1 ✔
85
                                copy.add(name);
1 ✔
86
                        }
87
                }
1 ✔
88
                return Collections.unmodifiableSet(copy);
1 ✔
89
        }
90

91
        private static Set<String> normalizeAll(Set<String> names) {
92
                Set<String> normalized = new HashSet<>();
1 ✔
93
                for (String name : names) {
1 ✔
94
                        normalized.add(normalize(name));
1 ✔
95
                }
1 ✔
96
                return Collections.unmodifiableSet(normalized);
1 ✔
97
        }
98

99
        /**
100
         * @return true if this role's closure confers superuser status
101
         */
102
        public boolean grantsSuperuser() {
103
                return grantsSuperuser;
1 ✔
104
        }
105

106
        /**
107
         * @param privilege the privilege name to test (compared case-insensitively)
108
         * @return true if the role's closure grants the given privilege
109
         */
110
        public boolean containsPrivilege(String privilege) {
111
                return privilege != null && normalizedNames.contains(normalize(privilege));
1 ✔
112
        }
113

114
        /**
115
         * @return an unmodifiable view of the privilege names in this closure, in their original casing
116
         *         (see this class's javadoc for why matching does not use them)
117
         */
118
        public Set<String> getPrivilegeNames() {
119
                return privilegeNames;
1 ✔
120
        }
121

122
        /**
123
         * @param role a role name (compared case-insensitively)
124
         * @return true if the role is this role or one it inherits
125
         * @since 2.8.11
126
         */
127
        public boolean includesRole(String role) {
128
                return role != null && normalizedRoleNames.contains(normalize(role));
1 ✔
129
        }
130

131
        /**
132
         * @return an unmodifiable view of the names of this role and the roles it inherits, as supplied
133
         * @since 2.8.11
134
         */
135
        public Set<String> getRoleNames() {
136
                return roleNames;
×
137
        }
138

139
        /**
140
         * Case-normalizes a privilege name for lookup. Uses {@link Locale#ROOT} so the normalization is
141
         * stable across server locales.
142
         *
143
         * @param privilege the privilege name to normalize; must not be null
144
         * @return the lower-cased privilege name
145
         * @throws NullPointerException if <code>privilege</code> is null
146
         */
147
        public static String normalize(String privilege) {
148
                return privilege.toLowerCase(Locale.ROOT);
1 ✔
149
        }
150

151
        @Override
152
        public boolean equals(Object o) {
153
                if (this == o) {
1 ✔
154
                        return true;
1 ✔
155
                }
156
                if (!(o instanceof RolePrivileges)) {
1 ✔
157
                        return false;
×
158
                }
159
                RolePrivileges other = (RolePrivileges) o;
1 ✔
160
                return grantsSuperuser == other.grantsSuperuser && normalizedNames.equals(other.normalizedNames)
1 ✔
161
                        && normalizedRoleNames.equals(other.normalizedRoleNames);
1 ✔
162
        }
163

164
        @Override
165
        public int hashCode() {
166
                return Objects.hash(normalizedNames, grantsSuperuser, normalizedRoleNames);
1 ✔
167
        }
168
}
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