Advertisement
Guest User

HibernateFormDaoTest

a guest
Jan 13th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  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.api.db.hibernate;
  15.  
  16. import static org.junit.Assert.assertNotNull;
  17.  
  18. import org.junit.Assert;
  19. import org.junit.Before;
  20. import org.junit.BeforeClass;
  21. import org.junit.Test;
  22. import org.openmrs.Concept;
  23. import org.openmrs.EncounterType;
  24. import org.openmrs.Field;
  25. import org.openmrs.Form;
  26. import org.openmrs.FormField;
  27. import org.openmrs.api.context.Context;
  28. import org.openmrs.api.db.DAOException;
  29. import org.openmrs.test.BaseContextSensitiveTest;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.HashSet;
  34. import java.util.List;
  35. import java.util.Set;
  36.  
  37. public class HibernateFormDAOTest extends BaseContextSensitiveTest {
  38.  
  39. private HibernateFormDAO dao = null;
  40. protected static final String INITIAL_FIELDS_XML = "org/openmrs/api/include/FormServiceTest-initialFieldTypes.xml";
  41.  
  42. /**
  43. * Run this before each unit test in this class.
  44. *
  45. * @throws Exception
  46. */
  47. @Before
  48. public void runBeforeEachTest() throws Exception {
  49.  
  50. if (dao == null)
  51. // fetch the dao from the spring application context
  52. dao = (HibernateFormDAO) applicationContext.getBean("formDAO");
  53. }
  54.  
  55. @Test
  56. public void getFormCriteria_shouldReturnOneFormIfContainingAnyFormFieldIsEmpty()throws Exception{
  57. String partialName = null;
  58. Boolean published = null;
  59. Boolean retired = null;
  60. Collection<EncounterType> encounterTypes = new ArrayList<EncounterType>();
  61. Collection<FormField> containingAnyFormField = new ArrayList<FormField>();
  62. Collection<FormField> containingAllFormFields = new ArrayList<FormField>();
  63. Collection<Field> fields = new ArrayList<Field>();
  64.  
  65. List<Form> formsReturned = dao.getForms(partialName, published, encounterTypes, retired, containingAnyFormField,
  66. containingAllFormFields, fields);
  67.  
  68. Assert.assertEquals(1, formsReturned.size());
  69. }
  70.  
  71. @Test
  72. public void getFormCriteria_shouldReturnValuesWithMatchingFormFields()throws Exception{
  73. String partialName = "";
  74. Boolean published = false;
  75. Boolean retired = false;
  76. Collection<EncounterType> encounterTypes = new ArrayList<EncounterType>();
  77. Collection<FormField> containingAnyFormField = new ArrayList<FormField>();
  78. Collection<FormField> containingAllFormFields = new ArrayList<FormField>();
  79. Collection<Field> fields = new ArrayList<Field>();
  80.  
  81. executeDataSet(INITIAL_FIELDS_XML);
  82. executeDataSet("org/openmrs/api/include/FormServiceTest-formFields.xml");
  83. containingAnyFormField.add(new FormField(3));
  84.  
  85. try {
  86. List<Form> formsReturned = dao.getForms(partialName, published, encounterTypes, retired, containingAnyFormField,
  87. containingAllFormFields, fields);
  88. ArrayList<Integer> formFieldIds = new ArrayList<Integer>();
  89.  
  90. if (containingAnyFormField.isEmpty()) {
  91. Assert.assertEquals(1, formsReturned.size());
  92. }
  93. else {
  94. for (FormField formField : containingAnyFormField) {
  95. formFieldIds.add(formField.getId());
  96. }
  97. for (Form form : formsReturned) {
  98. Assert.assertEquals(true, formFieldIds.contains(form.getFormId()));
  99. }
  100. }
  101. }
  102. catch (DAOException ex) {
  103. System.out.println(ex.getMessage());
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement