Skip to content

Commit 5d17ceb

Browse files
author
jwnasambu
committed
BED-23: Bed Tags should be uniquely identified by name
1 parent 0268061 commit 5d17ceb

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.module.bedmanagement.entity.BedTag;
13+
import org.openmrs.module.bedmanagement.service.BedManagementService;
14+
import org.springframework.validation.Errors;
15+
import org.springframework.validation.Validator;
16+
17+
import java.util.List;
18+
19+
public class BedTagValidator implements Validator {
20+
21+
private final BedManagementService bedManagementService;
22+
23+
public BedTagValidator(BedManagementService bedManagementService) {
24+
this.bedManagementService = bedManagementService;
25+
}
26+
27+
@Override
28+
public boolean supports(Class<?> clazz) {
29+
return BedTag.class.isAssignableFrom(clazz);
30+
}
31+
32+
@Override
33+
public void validate(Object target, Errors errors) {
34+
if (!(target instanceof BedTag)) {
35+
errors.reject("bedtag.invalid", "Object is not a BedTag");
36+
return;
37+
}
38+
39+
BedTag tag = (BedTag) target;
40+
41+
if (tag.getName() == null || tag.getName().trim().isEmpty()) {
42+
errors.rejectValue("name", "bedtag.name.required", "Name is required");
43+
}
44+
45+
if (tag.getName() != null && tag.getName().length() > 50) {
46+
errors.rejectValue("name", "bedtag.name.length", "Name is too long");
47+
}
48+
49+
List<BedTag> existingTags = bedManagementService.getAllBedTags();
50+
for (BedTag existingTag : existingTags) {
51+
boolean isVoidedOrExpired = existingTag.isVoided() || existingTag.getDateVoided() != null;
52+
if (!isVoidedOrExpired && existingTag.getName().equals(tag.getName())) {
53+
errors.rejectValue("name", "bedtag.name.exists", "Name already in use");
54+
}
55+
}
56+
}
57+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.junit.Before;
13+
import org.junit.Test;
14+
import org.openmrs.module.bedmanagement.entity.BedTag;
15+
import org.openmrs.module.bedmanagement.service.BedManagementService;
16+
import org.springframework.validation.BindException;
17+
import org.springframework.validation.Errors;
18+
19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.Date;
22+
23+
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertTrue;
25+
import static org.mockito.Mockito.*;
26+
27+
public class BedTagValidatorTest {
28+
29+
private BedTagValidator validator;
30+
31+
private BedManagementService bedManagementService;
32+
33+
private BedTag activeTag;
34+
35+
private BedTag expiredTag;
36+
37+
@Before
38+
public void setup() {
39+
bedManagementService = mock(BedManagementService.class);
40+
validator = new BedTagValidator(bedManagementService);
41+
42+
activeTag = new BedTag();
43+
activeTag.setName("ICU-1");
44+
activeTag.setUuid("active-uuid");
45+
46+
expiredTag = new BedTag();
47+
expiredTag.setName("ExpiredTag");
48+
expiredTag.setUuid("expired-uuid");
49+
expiredTag.setDateVoided(new Date());
50+
}
51+
52+
@Test
53+
public void validate_shouldFailIfNameIsNullOrEmptyOrWhitespace() {
54+
BedTag tag = new BedTag();
55+
56+
tag.setName(null);
57+
Errors errors = new BindException(tag, "tag");
58+
validator.validate(tag, errors);
59+
assertTrue(errors.hasFieldErrors("name"));
60+
61+
tag.setName("");
62+
errors = new BindException(tag, "tag");
63+
validator.validate(tag, errors);
64+
assertTrue(errors.hasFieldErrors("name"));
65+
66+
tag.setName(" ");
67+
errors = new BindException(tag, "tag");
68+
validator.validate(tag, errors);
69+
assertTrue(errors.hasFieldErrors("name"));
70+
}
71+
72+
@Test
73+
public void validate_shouldFailIfNonExpiredNameAlreadyInUse() {
74+
when(bedManagementService.getAllBedTags()).thenReturn(Arrays.asList(activeTag));
75+
76+
BedTag tag = new BedTag();
77+
tag.setName("ICU-1");
78+
79+
Errors errors = new BindException(tag, "tag");
80+
validator.validate(tag, errors);
81+
82+
assertTrue(errors.hasFieldErrors("name"));
83+
}
84+
85+
@Test
86+
public void validate_shouldPassIfExistingNameIsExpired() {
87+
when(bedManagementService.getAllBedTags()).thenReturn(Arrays.asList(expiredTag));
88+
89+
BedTag tag = new BedTag();
90+
tag.setName("ExpiredTag");
91+
92+
Errors errors = new BindException(tag, "tag");
93+
validator.validate(tag, errors);
94+
95+
assertFalse(errors.hasFieldErrors("name"));
96+
}
97+
98+
@Test
99+
public void validate_shouldPassIfAllRequiredFieldsAreValid() {
100+
when(bedManagementService.getAllBedTags()).thenReturn(Collections.emptyList());
101+
102+
BedTag tag = new BedTag();
103+
tag.setName("NewTag");
104+
105+
Errors errors = new BindException(tag, "tag");
106+
validator.validate(tag, errors);
107+
108+
assertFalse(errors.hasErrors());
109+
}
110+
111+
@Test
112+
public void validate_shouldFailIfFieldLengthsAreInvalid() {
113+
BedTag tag = new BedTag();
114+
tag.setName("ThisNameIsWayTooLongForABedTagAndShouldFailValidationAccordingToFieldLengthRules");
115+
116+
Errors errors = new BindException(tag, "tag");
117+
validator.validate(tag, errors);
118+
119+
assertTrue(errors.hasFieldErrors("name"));
120+
}
121+
122+
@Test
123+
public void validate_shouldRejectNonBedTagObjects() {
124+
Object notABedTag = new Object();
125+
Errors errors = new BindException(notABedTag, "notABedTag");
126+
validator.validate(notABedTag, errors);
127+
128+
assertTrue(errors.hasGlobalErrors());
129+
}
130+
}

0 commit comments

Comments
 (0)