Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(test) Add Dictionary Category Service Tests #1318

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public DictionaryCategoryDAOImpl() {
public boolean duplicateDictionaryCategoryExists(DictionaryCategory dictionaryCategory)
throws LIMSRuntimeException {
try {

List<DictionaryCategory> 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<DictionaryCategory> query = entityManager.unwrap(Session.class).createQuery(sql,
DictionaryCategory.class);
query.setParameter("param", dictionaryCategory.getCategoryName().toLowerCase().trim());
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,18 +25,6 @@ public void setup() throws Exception {
executeDataSetWithStateManagement("testdata/dictionary.xml");
}

// @Test
public void verifyTestData() {
List<DictionaryCategory> categories = dictionaryCategoryService.getAll();
System.out.println("Dictionary Categories: " + categories.size());
categories.forEach(cat -> System.out
.println(cat.getCategoryName() + " - " + cat.getLocalAbbreviation() + " - " + cat.getDescription()));

List<Dictionary> 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");
Expand Down Expand Up @@ -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<Dictionary> 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<Dictionary> 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() {
Expand Down
Loading