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

openmrs / openmrs-core / 12870114886

20 Jan 2025 02:26PM UTC coverage: 63.806% (+0.006%) from 63.8%
12870114886

push

github

dkayiwa
Fix failing CustomDatatypeUtilTest

1 of 1 new or added line in 1 file covered. (100.0%)

22 existing lines in 4 files now uncovered.

22017 of 34506 relevant lines covered (63.81%)

0.64 hits per line

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

77.5
/api/src/main/java/org/openmrs/validator/PersonAddressValidator.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 java.util.Date;
13
import java.util.List;
14

15
import org.apache.commons.beanutils.PropertyUtils;
16
import org.apache.commons.lang.StringEscapeUtils;
17
import org.apache.commons.lang3.StringUtils;
18
import org.openmrs.PersonAddress;
19
import org.openmrs.annotation.Handler;
20
import org.openmrs.api.context.Context;
21
import org.openmrs.layout.address.AddressTemplate;
22
import org.openmrs.util.OpenmrsUtil;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25
import org.springframework.validation.Errors;
26
import org.springframework.validation.Validator;
27

28
/**
29
 * This class validates a PersonAddress object.
30
 *
31
 * @since 1.9
32
 */
33
@Handler(supports = { PersonAddress.class }, order = 50)
34
public class PersonAddressValidator implements Validator {
1✔
35
        
36
        private static final Logger log = LoggerFactory.getLogger(PersonAddressValidator.class);
1✔
37
        
38
        /**
39
         * @see org.springframework.validation.Validator#supports(java.lang.Class)
40
         */
41
        @Override
42
        public boolean supports(Class<?> c) {
43
                return PersonAddress.class.isAssignableFrom(c);
1✔
44
        }
45
        
46
        /**
47
         * @see org.springframework.validation.Validator#validate(java.lang.Object,
48
         *      org.springframework.validation.Errors)
49
         * <strong>Should</strong> pass if all the dates are valid
50
         * <strong>Should</strong> fail if the startDate is in the future
51
         * <strong>Should</strong> fail if the endDate is before the startDate
52
         * <strong>Should</strong> pass if startDate and endDate are both null
53
         * <strong>Should</strong> pass if startDate is null
54
         * <strong>Should</strong> pass if endDate is null
55
         * <strong>Should</strong> fail if required fields are empty
56
         * <strong>Should</strong> pass if required fields are not empty
57
         * <strong>Should</strong> pass validation if field lengths are correct
58
         * <strong>Should</strong> fail validation if field lengths are not correct
59
         */
60
        @Override
61
        public void validate(Object object, Errors errors) {
62
                //TODO Validate other aspects of the personAddress object
63
                log.debug("{}.validate...", this.getClass().getName());
1✔
64
                
65
                if (object == null) {
1✔
UNCOV
66
                        throw new IllegalArgumentException("The personAddress object should not be null");
×
67
                }
68
                
69
                PersonAddress personAddress = (PersonAddress) object;
1✔
70
                
71
                //resolve a shorter name to display along with the error message
72
                String addressString;
73
                if (StringUtils.isNotBlank(personAddress.getAddress1())) {
1✔
74
                        addressString = personAddress.getAddress1();
1✔
75
                } else if (StringUtils.isNotBlank(personAddress.getAddress2())) {
1✔
UNCOV
76
                        addressString = personAddress.getAddress2();
×
77
                } else if (StringUtils.isNotBlank(personAddress.getCityVillage())) {
1✔
78
                        addressString = personAddress.getCityVillage();
1✔
79
                } else {
80
                        addressString = personAddress.toString();
1✔
81
                }
82
                
83
                if (OpenmrsUtil.compareWithNullAsEarliest(personAddress.getStartDate(), new Date()) > 0) {
1✔
84
                        errors.rejectValue("startDate", "PersonAddress.error.startDateInFuture", new Object[] { "'" + addressString
1✔
85
                                + "'" }, "The Start Date for address '" + addressString + "' shouldn't be in the future");
86
                }
87
                
88
                if (personAddress.getStartDate() != null
1✔
89
                        && OpenmrsUtil.compareWithNullAsLatest(personAddress.getStartDate(), personAddress.getEndDate()) > 0) {
1✔
90
                        errors.rejectValue("endDate", "PersonAddress.error.endDateBeforeStartDate", new Object[] { "'" + addressString
1✔
91
                                + "'" }, "The End Date for address '" + addressString + "' shouldn't be earlier than the Start Date");
92
                }
93
                
94
                String xml = Context.getLocationService().getAddressTemplate();
1✔
95
                List<String> requiredElements;
96
                
97
                try {
98
                        AddressTemplate addressTemplate = Context.getSerializationService().getDefaultSerializer().deserialize(StringEscapeUtils.unescapeXml(xml),
1✔
99
                            AddressTemplate.class);
100
                        requiredElements = addressTemplate.getRequiredElements();
1✔
101
                }
102
                catch (Exception e) {
×
103
                        errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error"));
×
UNCOV
104
                        return;
×
105
                }
1✔
106
                
107
                if (requiredElements != null) {
1✔
108
                        for (String fieldName : requiredElements) {
1✔
109
                                try {
110
                                        Object value = PropertyUtils.getProperty(personAddress, fieldName);
1✔
111
                                        if (StringUtils.isBlank((String) value)) {
1✔
112
                                                //required field not found
113
                                                errors.reject(Context.getMessageSourceService().getMessage(
1✔
114
                                                    "AddressTemplate.error.requiredAddressFieldIsBlank", new Object[] { fieldName },
115
                                                    Context.getLocale()));
1✔
116
                                        }
117
                                }
UNCOV
118
                                catch (Exception e) {
×
119
                                        //wrong field declared in template
120
                                        errors
×
UNCOV
121
                                                .reject(Context.getMessageSourceService().getMessage(
×
122
                                                    "AddressTemplate.error.fieldNotDeclaredInTemplate", new Object[] { fieldName },
UNCOV
123
                                                    Context.getLocale()));
×
124
                                }
1✔
125
                        }
1✔
126
                }
127
                
128
                ValidateUtil.validateFieldLengths(errors, object.getClass(), "address1", "address2", "cityVillage", "stateProvince",
1✔
129
                    "postalCode", "country", "latitude", "longitude", "voidReason", "countyDistrict", "address3", "address4",
130
                    "address5", "address6", "address7", "address8", "address9", "address10", "address11", "address12", "address13", 
131
                    "address14", "address15");
132
        }
1✔
133
}
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