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

Add panel service tests #1225

Closed
Closed
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
3 changes: 2 additions & 1 deletion src/test/java/org/openelisglobal/AppTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"org.openelisglobal.systemusermodule.service", "org.openelisglobal.rolemodule.service",
"org.openelisglobal.systemusermodule.daoimpl", "org.openelisglobal.systemusermodule.service",
"org.openelisglobal.login.service", "org.openelisglobal.view", "org.openelisglobal.search.service",
"org.openelisglobal.sample.daoimpl", }, excludeFilters = {
"org.openelisglobal.sample.daoimpl", "org.openelisglobal.menu.MenuServiceTest",
"org.openelisglobal.menu", }, excludeFilters = {
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.patient.controller.*"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.dictionary.controller.*.java"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.config.*"),
Expand Down
183 changes: 183 additions & 0 deletions src/test/java/org/openelisglobal/menu/MenuServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package org.openelisglobal.menu;

import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openelisglobal.BaseWebContextSensitiveTest;
import org.openelisglobal.menu.form.AdminMenuForm;
import org.openelisglobal.menu.service.MenuService;
import org.openelisglobal.menu.util.MenuItem;
import org.openelisglobal.menu.valueholder.AdminMenuItem;
import org.openelisglobal.menu.valueholder.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class MenuServiceTest extends BaseWebContextSensitiveTest {
@Autowired
private MenuService menuService;

@Before
public void setUp() {
}

@After
public void tearDown() {
}

@Test
@Transactional
public void saveSingleMenuItem_shouldSaveAndReturnMenuItem() {
MenuItem menuItem = new MenuItem();
Menu menu = new Menu();
menu.setElementId("testElement");
menuItem.setMenu(menu);

MenuItem savedItem = menuService.save(menuItem);

Assert.assertNotNull(savedItem);
}

@Test
@Transactional
public void saveMultipleMenuItems_shouldSaveAndReturnMenuItems() {
MenuItem menuItem1 = new MenuItem();
Menu menu1 = new Menu();
menu1.setElementId("testElement1");
menuItem1.setMenu(menu1);

MenuItem menuItem2 = new MenuItem();
Menu menu2 = new Menu();
menu2.setElementId("testElement2");
menuItem2.setMenu(menu2);

List<MenuItem> menuItems = List.of(menuItem1, menuItem2);

List<MenuItem> savedItems = menuService.save(menuItems);

Assert.assertNotNull(savedItems);
Assert.assertEquals(2, savedItems.size());
}

@Test
@Transactional
public void getAllActiveMenus_shouldReturnOnlyActiveMenus() {
MenuItem activeItem = new MenuItem();
Menu activeMenu = new Menu();
activeMenu.setElementId("activeElement");
activeMenu.setIsActive(true);
activeItem.setMenu(activeMenu);
menuService.save(activeItem);

MenuItem inactiveItem = new MenuItem();
Menu inactiveMenu = new Menu();
inactiveMenu.setElementId("inactiveElement");
inactiveMenu.setIsActive(false);
inactiveItem.setMenu(inactiveMenu);
menuService.save(inactiveItem);

List<Menu> activeMenus = menuService.getAllActiveMenus();

Assert.assertNotNull(activeMenus);
Assert.assertFalse(activeMenus.isEmpty());
Assert.assertTrue(activeMenus.stream().allMatch(Menu::getIsActive));
}

@Test
public void getAdminMenuItems_shouldReturnAdminMenuItems() {
AdminMenuForm form = new AdminMenuForm();

List<AdminMenuItem> adminMenuItems = new ArrayList<>();
AdminMenuItem item1 = new AdminMenuItem();
item1.setPath("/path1");
AdminMenuItem item2 = new AdminMenuItem();
item2.setPath("/path2");
adminMenuItems.add(item1);
adminMenuItems.add(item2);

form.setAdminMenuItems(adminMenuItems);

List<AdminMenuItem> result = form.getAdminMenuItems();
Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
Assert.assertEquals("/path1", result.get(0).getPath());
Assert.assertEquals("/path2", result.get(1).getPath());
}

@Test
public void setTotalRecordCount_shouldSetAndReturnTotalRecordCount() {
AdminMenuForm form = new AdminMenuForm();
form.setTotalRecordCount("100");
Assert.assertEquals("100", form.getTotalRecordCount());
}

@Test
public void setClickAction_shouldSetAndReturnClickAction() {
Menu menu = new Menu();
menu.setClickAction("clickAction");
Assert.assertEquals("clickAction", menu.getClickAction());
}

@Test
public void setDisplayKey_shouldSetAndReturnDisplayKey() {
Menu menu = new Menu();
menu.setDisplayKey("displayKey");
Assert.assertEquals("displayKey", menu.getDisplayKey());
}

@Test
public void setToolTipKey_shouldSetAndReturnToolTipKey() {
Menu menu = new Menu();
menu.setToolTipKey("toolTipKey");
Assert.assertEquals("toolTipKey", menu.getToolTipKey());
}

@Test
public void setParent_shouldSetAndReturnParent() {
Menu parentMenu = new Menu();
Menu childMenu = new Menu();
childMenu.setParent(parentMenu);
Assert.assertEquals(parentMenu, childMenu.getParent());
}

@Test
public void getLocalizedTitle_shouldReturnLocalizedTitle() {
Menu menu = new Menu();
menu.setDisplayKey("testDisplayKey");

String title = menu.getLocalizedTitle();
Assert.assertNotNull(title);
}

@Test
public void getLocalizedTooltip_shouldReturnLocalizedTooltip() {
Menu menu = new Menu();
menu.setToolTipKey("testToolTipKey");

String tooltip = menu.getLocalizedTooltip();
Assert.assertNotNull(tooltip);
}

@Test
public void setOpenInNewWindow_shouldSetAndReturnOpenInNewWindow() {
Menu menu = new Menu();
menu.setOpenInNewWindow(true);
Assert.assertTrue(menu.isOpenInNewWindow());
}

@Test
public void setIsActive_shouldSetAndReturnIsActive() {
Menu menu = new Menu();
menu.setIsActive(true);
Assert.assertTrue(menu.getIsActive());
}

@Test
public void setHideInOldUI_shouldSetAndReturnHideInOldUI() {
Menu menu = new Menu();
menu.setHideInOldUI(true);
Assert.assertTrue(menu.isHideInOldUI());
}
}
202 changes: 202 additions & 0 deletions src/test/java/org/openelisglobal/panel/PanelServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package org.openelisglobal.panel;

import java.util.List;
import java.util.Random;
import javax.transaction.Transactional;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openelisglobal.BaseWebContextSensitiveTest;
import org.openelisglobal.common.exception.LIMSDuplicateRecordException;
import org.openelisglobal.localization.valueholder.Localization;
import org.openelisglobal.panel.service.PanelService;
import org.openelisglobal.panel.valueholder.Panel;
import org.springframework.beans.factory.annotation.Autowired;

public class PanelServiceTest extends BaseWebContextSensitiveTest {

@Autowired
PanelService panelService;

@Before
public void init() {
// Initialize resources before each test
}

@After
public void tearDown() {
// Clean up resources after each test
}

// Method to create a Panel object
private Panel createPanel(String panelName, String description) {
Panel panel = new Panel();
panel.setPanelName(panelName);
panel.setDescription(description);
panel.setId(String.valueOf(new Random().nextInt(1000))); // Ensure the panel has a numeric ID as String
return panel;
}

@Test
@Transactional
public void insert_shouldCreateNewPanel() throws Exception {
String panelName = "Test Panel";
String description = "This is a test panel.";

Panel panel = createPanel(panelName, description);
String panelId = panelService.insert(panel);
Panel savedPanel = panelService.getPanelById(panelId);

Assert.assertNotNull(savedPanel);
Assert.assertEquals(panelName, savedPanel.getPanelName());
Assert.assertEquals(description, savedPanel.getDescription());
}

@Test(expected = LIMSDuplicateRecordException.class)
@Transactional
public void insert_shouldThrowExceptionForDuplicatePanelName() throws Exception {
String panelName = "Duplicate Panel";
String description = "This is a duplicate panel.";

// Insert the first panel
Panel panel1 = createPanel(panelName, description);
panelService.insert(panel1);

// Attempt to insert a duplicate panel
Panel panel2 = createPanel(panelName, "Another description");
panelService.insert(panel2); // Should throw LIMSDuplicateRecordException
}

@Test(expected = LIMSDuplicateRecordException.class)
@Transactional
public void insert_shouldThrowExceptionForDuplicatePanelDescription() throws Exception {
String panelName1 = "Panel 1";
String description = "Duplicate description";

// Insert the first panel
Panel panel1 = createPanel(panelName1, description);
panelService.insert(panel1);

// Attempt to insert a panel with a duplicate description
Panel panel2 = createPanel("Panel 2", description);
panelService.insert(panel2); // Should throw LIMSDuplicateRecordException
}

@Test
@Transactional
public void update_shouldUpdatePanelInformation() throws Exception {
String panelName = "Panel to Update";
String description = "Initial description.";

Panel panel = createPanel(panelName, description);
String panelId = panelService.insert(panel);
Panel savedPanel = panelService.getPanelById(panelId);

savedPanel.setDescription("Updated description.");
Panel updatedPanel = panelService.update(savedPanel);

Assert.assertEquals("Updated description.", updatedPanel.getDescription());
}

@Test(expected = LIMSDuplicateRecordException.class)
@Transactional
public void update_shouldThrowExceptionForDuplicatePanel() throws Exception {
String panelName1 = "Panel 1";
String description1 = "Description 1";

// Insert first panel
Panel panel1 = createPanel(panelName1, description1);
panelService.insert(panel1);

String panelName2 = "Panel 2";
String description2 = "Description 2";

// Insert second panel
Panel panel2 = createPanel(panelName2, description2);
panelService.insert(panel2);

// Attempt to update panel2 with panel1's name
panel2.setPanelName(panelName1);
panelService.update(panel2); // Should throw LIMSDuplicateRecordException
}

@Test
@Transactional
public void delete_shouldRemovePanelFromDatabase() throws Exception {
String panelName = "Panel to Delete";
String description = "This panel will be deleted.";

Panel panel = createPanel(panelName, description);
String panelId = panelService.insert(panel);

panelService.delete(panelService.getPanelById(panelId));

Panel deletedPanel = panelService.getPanelById(panelId);

Assert.assertNull(deletedPanel);
}

@Test
@Transactional
public void getPanelByName_shouldReturnCorrectPanel() throws Exception {
String panelName = "Panel by Name";
String description = "Panel description.";

Panel panel = createPanel(panelName, description);
panelService.insert(panel);

Panel retrievedPanel = panelService.getPanelByName(panelName);

Assert.assertNotNull(retrievedPanel);
Assert.assertEquals(panelName, retrievedPanel.getPanelName());
Assert.assertEquals(description, retrievedPanel.getDescription());
}

@Test
@Transactional
public void getTotalPanelCount_shouldReturnCorrectCount() throws Exception {
String panelName1 = "Panel 1";
String panelName2 = "Panel 2";

Panel panel1 = createPanel(panelName1, "Description for panel 1.");
Panel panel2 = createPanel(panelName2, "Description for panel 2.");

panelService.insert(panel1);
panelService.insert(panel2);

Integer totalPanelCount = panelService.getTotalPanelCount();

Assert.assertEquals(Integer.valueOf(2), totalPanelCount);
}

@Test
@Transactional
public void getAllActivePanels_shouldReturnActivePanels() throws Exception {
String panelName = "Active Panel";
String description = "This panel is active.";

Panel panel = createPanel(panelName, description);
panelService.insert(panel);

List<Panel> activePanels = panelService.getAllActivePanels();

Assert.assertFalse(activePanels.isEmpty());
Assert.assertTrue(activePanels.stream().anyMatch(p -> p.getPanelName().equals(panelName)));
}

@Test
@Transactional
public void getLocalizationForPanel_shouldReturnLocalization() throws Exception {
String panelName = "Localized Panel";
String description = "This panel has localization.";

Panel panel = createPanel(panelName, description);
panelService.insert(panel);

Localization localization = panelService.getLocalizationForPanel(panel.getId());

Assert.assertNotNull(localization);
// Additional assertions depending on the structure of Localization
}
}
Loading