Skip to content
Snippets Groups Projects
Commit 0e3aa412 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#691 - fixed category importer (update icon if an existing icon is found)

parent 82416d25
Branches
Tags
No related merge requests found
...@@ -54,6 +54,7 @@ public class CategoryImporter extends ItemImporter<Category> ...@@ -54,6 +54,7 @@ public class CategoryImporter extends ItemImporter<Category>
{ {
//category already exists //category already exists
newCategoryID = existingCategory.getID(); newCategoryID = existingCategory.getID();
category.setIconReference(existingCategory.getIconReference());
LOGGER.debug(MessageFormat.format("Found matching category with ID: {0} for category \"{1}\".", newCategoryID, category.getName())); LOGGER.debug(MessageFormat.format("Found matching category with ID: {0} for category \"{1}\".", newCategoryID, category.getName()));
} }
return newCategoryID; return newCategoryID;
......
...@@ -4,6 +4,8 @@ import de.deadlocker8.budgetmaster.categories.Category; ...@@ -4,6 +4,8 @@ import de.deadlocker8.budgetmaster.categories.Category;
import de.deadlocker8.budgetmaster.categories.CategoryRepository; import de.deadlocker8.budgetmaster.categories.CategoryRepository;
import de.deadlocker8.budgetmaster.categories.CategoryType; import de.deadlocker8.budgetmaster.categories.CategoryType;
import de.deadlocker8.budgetmaster.database.importer.CategoryImporter; import de.deadlocker8.budgetmaster.database.importer.CategoryImporter;
import de.deadlocker8.budgetmaster.icon.Icon;
import de.deadlocker8.budgetmaster.icon.IconRepository;
import de.deadlocker8.budgetmaster.services.EntityType; import de.deadlocker8.budgetmaster.services.EntityType;
import de.deadlocker8.budgetmaster.services.ImportResultItem; import de.deadlocker8.budgetmaster.services.ImportResultItem;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -25,6 +27,9 @@ class CategoryImporterTest ...@@ -25,6 +27,9 @@ class CategoryImporterTest
@Autowired @Autowired
private CategoryRepository categoryRepository; private CategoryRepository categoryRepository;
@Autowired
private IconRepository iconRepository;
@Test @Test
void test_importCategories() void test_importCategories()
{ {
...@@ -78,18 +83,48 @@ class CategoryImporterTest ...@@ -78,18 +83,48 @@ class CategoryImporterTest
@Test @Test
void test_importCategories_skipExisting_custom() void test_importCategories_skipExisting_custom()
{ {
final Category category = new Category("Category1", "#ff0000", CategoryType.CUSTOM); final Category categoryExisting= new Category("Category1", "#ff0000", CategoryType.CUSTOM);
category.setID(3); categoryExisting.setID(3);
final Category categoryToImport = new Category("Category1", "#ff0000", CategoryType.CUSTOM);
categoryToImport.setID(15);
categoryRepository.save(new Category("existing category 1", "#ffffff", CategoryType.CUSTOM)); categoryRepository.save(new Category("existing category 1", "#ffffff", CategoryType.CUSTOM));
categoryRepository.save(new Category("existing category 2", "#ffffff", CategoryType.CUSTOM)); categoryRepository.save(new Category("existing category 2", "#ffffff", CategoryType.CUSTOM));
categoryRepository.save(category); categoryRepository.save(categoryExisting);
final CategoryImporter importer = new CategoryImporter(categoryRepository); final CategoryImporter importer = new CategoryImporter(categoryRepository);
final ImportResultItem resultItem = importer.importItems(List.of(category)); final ImportResultItem resultItem = importer.importItems(List.of(categoryToImport));
final ImportResultItem expected = new ImportResultItem(EntityType.CATEGORY, 1, 1, List.of());
assertThat(resultItem).isEqualTo(expected);
assertThat(categoryToImport).hasFieldOrPropertyWithValue("ID", 3);
}
@Test
void test_importCategories_existingCategory_takeExistingIcon()
{
final Icon icon = new Icon("fas fa-icons");
icon.setID(1);
iconRepository.save(icon);
final Category categoryExisting = new Category("Category1", "#ff0000", CategoryType.CUSTOM);
categoryExisting.setIconReference(icon);
categoryExisting.setID(3);
final Category categoryToImport = new Category("Category1", "#ff0000", CategoryType.CUSTOM);
categoryToImport.setID(15);
categoryRepository.save(categoryExisting);
final CategoryImporter importer = new CategoryImporter(categoryRepository);
final ImportResultItem resultItem = importer.importItems(List.of(categoryToImport));
final ImportResultItem expected = new ImportResultItem(EntityType.CATEGORY, 1, 1, List.of()); final ImportResultItem expected = new ImportResultItem(EntityType.CATEGORY, 1, 1, List.of());
assertThat(resultItem).isEqualTo(expected); assertThat(resultItem).isEqualTo(expected);
assertThat(category).hasFieldOrPropertyWithValue("ID", 3); assertThat(categoryToImport)
.hasFieldOrPropertyWithValue("ID", 1)
.hasFieldOrPropertyWithValue("iconReference", icon);
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment