From bd1913a9791382c90245079e59e6c87b4ce4af16 Mon Sep 17 00:00:00 2001 From: mherman22 Date: Wed, 20 Nov 2024 09:07:18 +0300 Subject: [PATCH] Add Dictionary Category Service Tests --- .../daoimpl/DictionaryCategoryDAOImpl.java | 18 ++-- .../DictionaryCategoryServiceTest.java | 92 +++++++++++++++++++ .../service/DictionaryServiceTest.java | 33 +++---- 3 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 src/test/java/org/openelisglobal/dictionary/service/DictionaryCategoryServiceTest.java diff --git a/src/main/java/org/openelisglobal/dictionarycategory/daoimpl/DictionaryCategoryDAOImpl.java b/src/main/java/org/openelisglobal/dictionarycategory/daoimpl/DictionaryCategoryDAOImpl.java index b7d37d0b73..cda9740e72 100644 --- a/src/main/java/org/openelisglobal/dictionarycategory/daoimpl/DictionaryCategoryDAOImpl.java +++ b/src/main/java/org/openelisglobal/dictionarycategory/daoimpl/DictionaryCategoryDAOImpl.java @@ -45,16 +45,16 @@ public DictionaryCategoryDAOImpl() { public boolean duplicateDictionaryCategoryExists(DictionaryCategory dictionaryCategory) throws LIMSRuntimeException { try { - List list = new ArrayList<>(); // not case sensitive hemolysis and Hemolysis are considered // duplicates // only one of each name, description, local abbrev can exist in entire table String sql = "from DictionaryCategory t where " - + "((trim(lower(t.categoryName)) = :param and t.id != :param3) " + "or " - + "(trim(lower(t.description)) = :param2 and t.id != :param3) " + "or " - + "(trim(lower(t.localAbbreviation)) = :param4 and t.id != :param3)) "; + + "((trim(lower(t.categoryName)) = :param and CAST(t.id AS TEXT) != :param3) or " + + "(trim(lower(t.description)) = :param2 and CAST(t.id AS TEXT) != :param3) or " + + "(trim(lower(t.localAbbreviation)) = :param4 and CAST(t.id AS TEXT) != :param3)) "; + Query query = entityManager.unwrap(Session.class).createQuery(sql, DictionaryCategory.class); query.setParameter("param", dictionaryCategory.getCategoryName().toLowerCase().trim()); @@ -70,15 +70,9 @@ public boolean duplicateDictionaryCategoryExists(DictionaryCategory dictionaryCa query.setParameter("param3", dictId); list = query.list(); - - if (list.size() > 0) { - return true; - } else { - return false; - } + return !list.isEmpty(); } catch (RuntimeException e) { - // bugzilla 2154 LogEvent.logError(e); throw new LIMSRuntimeException("Error in duplicateDictionaryExists()", e); } @@ -99,7 +93,7 @@ public DictionaryCategory getDictionaryCategoryByName(String name) throws LIMSRu return categoryList.get(0); } } catch (RuntimeException e) { - handleException(e, "getDictonaryCategoryByName"); + handleException(e, "getDictionaryCategoryByName"); } return null; diff --git a/src/test/java/org/openelisglobal/dictionary/service/DictionaryCategoryServiceTest.java b/src/test/java/org/openelisglobal/dictionary/service/DictionaryCategoryServiceTest.java new file mode 100644 index 0000000000..b63e762e46 --- /dev/null +++ b/src/test/java/org/openelisglobal/dictionary/service/DictionaryCategoryServiceTest.java @@ -0,0 +1,92 @@ +package org.openelisglobal.dictionary.service; + +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openelisglobal.BaseWebContextSensitiveTest; +import org.openelisglobal.dictionarycategory.service.DictionaryCategoryService; +import org.openelisglobal.dictionarycategory.valueholder.DictionaryCategory; +import org.springframework.beans.factory.annotation.Autowired; + +public class DictionaryCategoryServiceTest extends BaseWebContextSensitiveTest { + + @Autowired + DictionaryCategoryService dictionaryCategoryService; + + @Before + public void setup() throws Exception { + executeDataSetWithStateManagement("testdata/dictionary.xml"); + } + + @Test + public void insert_shouldInsertNewDictionaryCategoryRecord() throws ParseException { + DictionaryCategory dictionaryCategory = new DictionaryCategory(); + dictionaryCategory.setCategoryName("Category Description insert"); + dictionaryCategory.setDescription("For testing insert"); + dictionaryCategory.setLocalAbbreviation("TXT1"); + dictionaryCategory + .setLastupdated(new Timestamp(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/1992").getTime())); + + String dictionaryCategoryId = dictionaryCategoryService.insert(dictionaryCategory); + dictionaryCategory = dictionaryCategoryService.get(dictionaryCategoryId); + + Assert.assertEquals("443", dictionaryCategory.getId()); + Assert.assertEquals("Category Description insert", dictionaryCategory.getCategoryName()); + Assert.assertEquals("TXT1", dictionaryCategory.getLocalAbbreviation()); + Assert.assertEquals("For testing insert", dictionaryCategory.getDescription()); + } + + @Test + public void save_shouldSaveNewDictionaryCategoryRecord() throws ParseException { + DictionaryCategory dictionaryCategory = new DictionaryCategory(); + dictionaryCategory.setCategoryName("Category Description save"); + dictionaryCategory.setDescription("For testing save"); + dictionaryCategory.setLocalAbbreviation("TXT11"); + dictionaryCategory + .setLastupdated(new Timestamp(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/1992").getTime())); + + dictionaryCategory = dictionaryCategoryService.save(dictionaryCategory); + + Assert.assertEquals("442", dictionaryCategory.getId()); + Assert.assertEquals("Category Description save", dictionaryCategory.getCategoryName()); + Assert.assertEquals("TXT11", dictionaryCategory.getLocalAbbreviation()); + Assert.assertEquals("For testing save", dictionaryCategory.getDescription()); + } + + @Test + public void update_shouldUpdateExistingDictionaryCategoryRecord() { + DictionaryCategory dictionaryCategory = dictionaryCategoryService.get("2"); + Assert.assertNotNull(dictionaryCategory); + + dictionaryCategory.setCategoryName("Updated for testing"); + dictionaryCategory = dictionaryCategoryService.update(dictionaryCategory); + + Assert.assertEquals("Updated for testing", dictionaryCategory.getCategoryName()); + Assert.assertEquals("Category Description 2", dictionaryCategory.getDescription()); + Assert.assertEquals("CA2", dictionaryCategory.getLocalAbbreviation()); + Assert.assertEquals("2", dictionaryCategory.getId()); + } + + @Test + public void getDictionaryCategoryByName_shouldGetDictionaryCategoryByName() { + DictionaryCategory dictionaryCategory = dictionaryCategoryService + .getDictionaryCategoryByName("Category Name 2"); + Assert.assertNotNull(dictionaryCategory); + Assert.assertEquals("Category Description 2", dictionaryCategory.getDescription()); + Assert.assertEquals("CA2", dictionaryCategory.getLocalAbbreviation()); + Assert.assertEquals("2", dictionaryCategory.getId()); + } + + private static DictionaryCategory getDictionaryCategory() throws ParseException { + DictionaryCategory dictionaryCategory = new DictionaryCategory(); + dictionaryCategory.setCategoryName("Category Descriptionz"); + dictionaryCategory.setDescription("For testing insert"); + dictionaryCategory.setLocalAbbreviation("TXT1"); + dictionaryCategory + .setLastupdated(new Timestamp(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/1992").getTime())); + return dictionaryCategory; + } +} diff --git a/src/test/java/org/openelisglobal/dictionary/service/DictionaryServiceTest.java b/src/test/java/org/openelisglobal/dictionary/service/DictionaryServiceTest.java index 61becfe578..f7846d6f5e 100644 --- a/src/test/java/org/openelisglobal/dictionary/service/DictionaryServiceTest.java +++ b/src/test/java/org/openelisglobal/dictionary/service/DictionaryServiceTest.java @@ -3,11 +3,11 @@ import java.util.List; import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.openelisglobal.BaseWebContextSensitiveTest; import org.openelisglobal.dictionary.valueholder.Dictionary; import org.openelisglobal.dictionarycategory.service.DictionaryCategoryService; -import org.openelisglobal.dictionarycategory.valueholder.DictionaryCategory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; @@ -25,18 +25,6 @@ public void setup() throws Exception { executeDataSetWithStateManagement("testdata/dictionary.xml"); } - @Test - public void verifyTestData() { - List categories = dictionaryCategoryService.getAll(); - System.out.println("Dictionary Categories: " + categories.size()); - categories.forEach(cat -> System.out - .println(cat.getCategoryName() + " - " + cat.getLocalAbbreviation() + " - " + cat.getDescription())); - - List dictionaries = dictionaryService.getAll(); - System.out.println("Dictionaries: " + dictionaries.size()); - dictionaries.forEach(dict -> System.out.println(dict.getDictEntry() + " - " + dict.getIsActive())); - } - @Test public void delete_shouldDeleteDictionary() { Dictionary dictionaryToDelete = dictionaryService.get("1"); @@ -91,16 +79,17 @@ public void getDictionaryById_shouldReturnDictionaryWhenGivenDictionaryId() { Assert.assertEquals("Y", dictionary.getIsActive()); } -// @Test + @Ignore // This fails with java.lang.AssertionError: Values should be different. Actual: 0 -// public void getDictionaryEntrysByCategoryAbbreviation_shouldGetDictEntrysByCategoryAbbreviation() { -// List dictionaries = dictionaryService.getDictionaryEntrysByCategoryAbbreviation("Dictionary", "CA2"); -// Assert.assertNotEquals(0, dictionaries.size()); -// -// Assert.assertEquals("Dictionary Entry 2", dictionaries.get(0).getDictEntry()); -// Assert.assertEquals("N", dictionaries.get(0).getIsActive()); -// Assert.assertEquals("DE2", dictionaries.get(0).getLocalAbbreviation()); -// } + public void getDictionaryEntrysByCategoryAbbreviation_shouldGetDictEntrysByCategoryAbbreviation() { + List dictionaries = dictionaryService.getDictionaryEntrysByCategoryAbbreviation("Dictionary", + "CA2"); + Assert.assertNotEquals(0, dictionaries.size()); + + Assert.assertEquals("Dictionary Entry 2", dictionaries.get(0).getDictEntry()); + Assert.assertEquals("N", dictionaries.get(0).getIsActive()); + Assert.assertEquals("DE2", dictionaries.get(0).getLocalAbbreviation()); + } @Test public void getDictionaryEntrysByNameAndCategoryDescription_shouldGetDictionaryEntrysByNameAndCategoryDescription() {