|
| 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