|
| 1 | +/** |
| 2 | + * The contents of this file are subject to the OpenMRS Public License |
| 3 | + * Version 1.0 (the "License"); you may not use this file except in |
| 4 | + * compliance with the License. You may obtain a copy of the License at |
| 5 | + * http://license.openmrs.org |
| 6 | + * |
| 7 | + * Software distributed under the License is distributed on an "AS IS" |
| 8 | + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 9 | + * License for the specific language governing rights and limitations |
| 10 | + * under the License. |
| 11 | + * |
| 12 | + * Copyright (C) OpenMRS, LLC. All Rights Reserved. |
| 13 | + */ |
| 14 | +package org.openmrs.validator; |
| 15 | + |
| 16 | +import org.junit.Assert; |
| 17 | +import org.junit.Test; |
| 18 | +import org.openmrs.Concept; |
| 19 | +import org.openmrs.OrderFrequency; |
| 20 | +import org.openmrs.api.context.Context; |
| 21 | +import org.openmrs.test.BaseContextSensitiveTest; |
| 22 | +import org.openmrs.test.Verifies; |
| 23 | +import org.springframework.validation.BindException; |
| 24 | +import org.springframework.validation.Errors; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests methods on the {@link OrderFrequencyValidator} class. |
| 28 | + */ |
| 29 | +public class OrderFrequencyValidatorTest extends BaseContextSensitiveTest { |
| 30 | + |
| 31 | + /** |
| 32 | + * @see {@link OrderFrequencyValidator#validate(Object,Errors)} |
| 33 | + */ |
| 34 | + @Test |
| 35 | + @Verifies(value = "should fail if orderFrequency is null", method = "validate(Object,Errors)") |
| 36 | + public void validate_shouldFailIfOrderFrequencyIsNull() throws Exception { |
| 37 | + Errors errors = new BindException(new OrderFrequency(), "orderFrequency"); |
| 38 | + new OrderFrequencyValidator().validate(null, errors); |
| 39 | + |
| 40 | + Assert.assertTrue(errors.hasErrors()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @see {@link OrderFrequencyValidator#validate(Object,Errors)} |
| 45 | + */ |
| 46 | + @Test |
| 47 | + @Verifies(value = "should fail if concept is null", method = "validate(Object,Errors)") |
| 48 | + public void validate_shouldFailIfConceptIsNull() throws Exception { |
| 49 | + OrderFrequency orderFrequency = new OrderFrequency(); |
| 50 | + |
| 51 | + Errors errors = new BindException(orderFrequency, "orderFrequency"); |
| 52 | + new OrderFrequencyValidator().validate(orderFrequency, errors); |
| 53 | + |
| 54 | + Assert.assertTrue(errors.hasFieldErrors("concept")); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @see {@link OrderFrequencyValidator#validate(Object,Errors)} |
| 59 | + */ |
| 60 | + @Test |
| 61 | + @Verifies(value = "should fail if the concept is not of class frequency", method = "validate(Object,Errors)") |
| 62 | + public void validate_shouldFailIfConceptIsNotOfClassFrequency() throws Exception { |
| 63 | + OrderFrequency orderFrequency = new OrderFrequency(); |
| 64 | + orderFrequency.setConcept(Context.getConceptService().getConcept(88)); |
| 65 | + Errors errors = new BindException(orderFrequency, "orderFrequency"); |
| 66 | + new OrderFrequencyValidator().validate(orderFrequency, errors); |
| 67 | + |
| 68 | + Assert.assertTrue(errors.hasFieldErrors("concept")); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @see {@link OrderFrequencyValidator#validate(Object,Errors)} |
| 73 | + */ |
| 74 | + @Test |
| 75 | + @Verifies(value = "should fail if concept is used by another frequency", method = "validate(Object,Errors)") |
| 76 | + public void validate_shouldFailIfConceptIsUsedByAnotherFrequency() throws Exception { |
| 77 | + OrderFrequency orderFrequency = new OrderFrequency(); |
| 78 | + orderFrequency.setConcept(Context.getConceptService().getConcept(113)); |
| 79 | + Errors errors = new BindException(orderFrequency, "orderFrequency"); |
| 80 | + new OrderFrequencyValidator().validate(orderFrequency, errors); |
| 81 | + |
| 82 | + Assert.assertTrue(errors.hasFieldErrors("concept")); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @see {@link OrderFrequencyValidator#validate(Object,Errors)} |
| 87 | + */ |
| 88 | + @Test |
| 89 | + @Verifies(value = "should pass if all fields are correct", method = "validate(Object,Errors)") |
| 90 | + public void validate_shouldPassIfAllFieldsAreCorrect() throws Exception { |
| 91 | + Concept concept = new Concept(1); |
| 92 | + concept.setConceptClass(Context.getConceptService().getConceptClass(19)); |
| 93 | + |
| 94 | + OrderFrequency orderFrequency = new OrderFrequency(); |
| 95 | + orderFrequency.setConcept(concept); |
| 96 | + Errors errors = new BindException(orderFrequency, "orderFrequency"); |
| 97 | + new OrderFrequencyValidator().validate(orderFrequency, errors); |
| 98 | + |
| 99 | + Assert.assertFalse(errors.hasErrors()); |
| 100 | + } |
| 101 | +} |