From 9834efee5d42f9ba8df937fd70e9cd82248ecc07 Mon Sep 17 00:00:00 2001 From: srosse <none@none> Date: Fri, 29 Jun 2012 14:18:20 +0200 Subject: [PATCH] OO-291: make the area working + unit tests too --- .../org/olat/group/area/BGAreaManager.java | 2 + .../olat/group/area/BGAreaManagerImpl.java | 42 +- .../group/ui/area/BGAreaEditController.java | 99 ++-- .../olat/group/test/BGAreaManagerTest.java | 476 +++++++++++++++++- .../test/BusinessGroupServiceImplTest.java | 5 +- 5 files changed, 544 insertions(+), 80 deletions(-) diff --git a/src/main/java/org/olat/group/area/BGAreaManager.java b/src/main/java/org/olat/group/area/BGAreaManager.java index 99bd66830f4..f270e3359ed 100644 --- a/src/main/java/org/olat/group/area/BGAreaManager.java +++ b/src/main/java/org/olat/group/area/BGAreaManager.java @@ -73,6 +73,8 @@ public interface BGAreaManager { * @return The area or null if the area does not exists */ public abstract BGArea findBGArea(String areaName, OLATResource resource); + + public BGArea loadArea(Long key); /** * Update the given area in the database diff --git a/src/main/java/org/olat/group/area/BGAreaManagerImpl.java b/src/main/java/org/olat/group/area/BGAreaManagerImpl.java index 228335e8578..7be7083a228 100644 --- a/src/main/java/org/olat/group/area/BGAreaManagerImpl.java +++ b/src/main/java/org/olat/group/area/BGAreaManagerImpl.java @@ -42,6 +42,7 @@ import org.olat.core.commons.persistence.DBFactory; import org.olat.core.id.Identity; import org.olat.core.logging.OLATRuntimeException; import org.olat.core.manager.BasicManager; +import org.olat.core.util.StringHelper; import org.olat.core.util.coordinate.CoordinatorManager; import org.olat.core.util.coordinate.SyncerCallback; import org.olat.core.util.coordinate.SyncerExecutor; @@ -101,6 +102,11 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { } return areas; } + + @Override + public BGArea loadArea(Long key) { + return dbInstance.getCurrentEntityManager().find(BGAreaImpl.class, key); + } /** * @see org.olat.group.area.BGAreaManager#findBGArea(java.lang.String, @@ -219,8 +225,8 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { if(areas == null || areas.isEmpty()) return Collections.emptyList(); StringBuilder sb = new StringBuilder(); - sb.append("select bgarel.businessGroup from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ") - .append(" where bgarel.groupArea.key in (:areakeys)"); + sb.append("select distinct bgarel.businessGroup from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ") + .append(" where bgarel.groupArea.key in (:areaKeys)"); List<Long> areaKeys = new ArrayList<Long>(); for(BGArea area:areas) { @@ -241,17 +247,29 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { public List<BusinessGroup> findBusinessGroupsOfAreaAttendedBy(Identity identity, String areaName, OLATResource resource) { StringBuilder sb = new StringBuilder(); sb.append("select bgi from ").append(BusinessGroupImpl.class.getName()).append(" as bgi ") - .append(", org.olat.basesecurity.SecurityGroupMembershipImpl as sgmi , org.olat.group.area.BGtoAreaRelationImpl as bgarel") + .append(", org.olat.basesecurity.SecurityGroupMembershipImpl as sgmi") + .append(", org.olat.group.area.BGtoAreaRelationImpl as bgarel") .append(", org.olat.group.area.BGAreaImpl as area") - .append(" where area.name=:name and bgarel.businessGroup=bgi") - .append(" and bgarel.groupArea=area and bgi.partipiciantGroup=sgmi.securityGroup and sgmi.identity.key=:identityKey") - .append(" and area.resource.key=:resourceKey"); + .append(" where bgarel.groupArea=area and bgi.partipiciantGroup=sgmi.securityGroup and bgarel.businessGroup=bgi") + .append(" and sgmi.identity.key=:identityKey"); - List<BusinessGroup> groups = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), BusinessGroup.class) - .setParameter("identityKey", identity.getKey()) - .setParameter("resourceKey", resource.getKey()) - .getResultList(); + if(StringHelper.containsNonWhitespace(areaName)) { + sb.append(" and area.name=:name"); + } + if(resource != null) { + sb.append(" and area.resource.key=:resourceKey"); + } + TypedQuery<BusinessGroup> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), BusinessGroup.class) + .setParameter("identityKey", identity.getKey()); + if(StringHelper.containsNonWhitespace(areaName)) { + query.setParameter("name", areaName); + } + if(resource != null) { + query.setParameter("resourceKey", resource.getKey()); + } + + List<BusinessGroup> groups = query.getResultList(); return groups; } @@ -267,7 +285,7 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { if(groups == null || groups.isEmpty()) return Collections.emptyList(); StringBuilder sb = new StringBuilder(); - sb.append("select bgarel.groupArea from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ") + sb.append("select distinct bgarel.groupArea from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ") .append("where bgarel.businessGroup.key in (:groupKeys)"); TypedQuery<BGArea> areaQuery = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), BGArea.class); @@ -371,7 +389,7 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { */ private void removeBGFromArea(Long businessGroupKey, Long bgAreaKey) { StringBuilder sb = new StringBuilder(); - sb.append("delete from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel where bgarel.groupArea.key=:areaKey and bgarel.businessGroup=:groupKey"); + sb.append("delete from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel where bgarel.groupArea.key=:areaKey and bgarel.businessGroup.key=:groupKey"); dbInstance.getCurrentEntityManager().createQuery(sb.toString()) .setParameter("areaKey", bgAreaKey) diff --git a/src/main/java/org/olat/group/ui/area/BGAreaEditController.java b/src/main/java/org/olat/group/ui/area/BGAreaEditController.java index c895f8711b9..66a194d06a6 100644 --- a/src/main/java/org/olat/group/ui/area/BGAreaEditController.java +++ b/src/main/java/org/olat/group/ui/area/BGAreaEditController.java @@ -25,7 +25,6 @@ package org.olat.group.ui.area; -import java.util.Iterator; import java.util.List; import org.apache.commons.lang.StringEscapeUtils; @@ -39,10 +38,7 @@ import org.olat.core.gui.control.Controller; import org.olat.core.gui.control.Event; import org.olat.core.gui.control.WindowControl; import org.olat.core.gui.control.controller.BasicController; -import org.olat.core.gui.translator.PackageTranslator; -import org.olat.core.gui.translator.Translator; import org.olat.core.logging.activity.ThreadLocalUserActivityLogger; -import org.olat.core.util.Util; import org.olat.group.BusinessGroup; import org.olat.group.BusinessGroupService; import org.olat.group.GroupLoggingAction; @@ -61,12 +57,7 @@ import org.olat.util.logging.activity.LoggingResourceable; * @author gnaegi */ public class BGAreaEditController extends BasicController { - private static final String PACKAGE = Util.getPackageName(BGAreaEditController.class); - private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(PACKAGE); - // helpers - - private Translator trans; // GUI components private TabbedPane tabbedPane; private VelocityContainer editVC, detailsTabVC, groupsTabVC; @@ -76,7 +67,7 @@ public class BGAreaEditController extends BasicController { // area, context and group references private BGArea area; private OLATResource resource; - private List allGroups, inAreaGroups; + private List<BusinessGroup> allGroups, inAreaGroups; // managers private final BGAreaManager areaManager; private final BusinessGroupService businessGroupService; @@ -91,7 +82,6 @@ public class BGAreaEditController extends BasicController { public BGAreaEditController(UserRequest ureq, WindowControl wControl, BGArea area) { super(ureq, wControl); - this.trans = new PackageTranslator(PACKAGE, ureq.getLocale()); this.area = area; areaManager = CoreSpringFactory.getImpl(BGAreaManager.class); resource = area.getResource(); @@ -113,44 +103,41 @@ public class BGAreaEditController extends BasicController { * initialize the main velocity wrapper container */ private void initEditVC() { - editVC = new VelocityContainer("edit", VELOCITY_ROOT + "/edit.html", trans, this); + editVC = createVelocityContainer("edit"); editVC.put("tabbedpane", tabbedPane); - editVC.contextPut("title", trans.translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(this.area.getName()).toString() })); + editVC.contextPut("title", translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(area.getName()).toString() })); } /** * initialize the area details tab */ private void initAndAddDetailsTab(UserRequest ureq, WindowControl wControl) { - this.detailsTabVC = new VelocityContainer("detailstab", VELOCITY_ROOT + "/detailstab.html", this.trans, this); - //TODO:pb: refactor BGControllerFactory.create..AreaController to be - //usefull here - if (this.areaController != null) { - removeAsListenerAndDispose(this.areaController); - } - this.areaController = new BGAreaFormController(ureq, wControl, this.area, false); - listenTo(this.areaController); - this.detailsTabVC.put("areaForm", this.areaController.getInitialComponent()); - this.tabbedPane.addTab(this.trans.translate("tab.details"), this.detailsTabVC); + detailsTabVC = createVelocityContainer("detailstab"); + + removeAsListenerAndDispose(areaController); + areaController = new BGAreaFormController(ureq, wControl, area, false); + listenTo(areaController); + detailsTabVC.put("areaForm", areaController.getInitialComponent()); + tabbedPane.addTab(translate("tab.details"), detailsTabVC); } /** * initalize the group to area association tab */ private void initAndAddGroupsTab() { - groupsTabVC = new VelocityContainer("groupstab", VELOCITY_ROOT + "/groupstab.html", trans, this); - tabbedPane.addTab(trans.translate("tab.groups"), groupsTabVC); + groupsTabVC = createVelocityContainer("groupstab"); + tabbedPane.addTab(translate("tab.groups"), groupsTabVC); - this.allGroups = businessGroupService.findBusinessGroups(null, null, false, false, resource, 0, -1); - this.inAreaGroups = areaManager.findBusinessGroupsOfArea(this.area); - this.groupsDataModel = new GroupsToAreaDataModel(this.allGroups, this.inAreaGroups); + allGroups = businessGroupService.findBusinessGroups(null, null, false, false, resource, 0, -1); + inAreaGroups = areaManager.findBusinessGroupsOfArea(area); + groupsDataModel = new GroupsToAreaDataModel(allGroups, inAreaGroups); - groupsChoice = new Choice("groupsChoice", trans); + groupsChoice = new Choice("groupsChoice", getTranslator()); groupsChoice.setSubmitKey("submit"); groupsChoice.setCancelKey("cancel"); groupsChoice.setTableDataModel(groupsDataModel); groupsChoice.addListener(this); - groupsTabVC.put(groupsChoice); + groupsTabVC.put("groupsChoice", groupsChoice); groupsTabVC.contextPut("noGroupsFound", (allGroups.size() > 0 ? Boolean.FALSE : Boolean.TRUE)); } @@ -160,15 +147,14 @@ public class BGAreaEditController extends BasicController { */ @Override public void event(UserRequest ureq, Component source, Event event) { - if (source == this.groupsChoice) { + if (source == groupsChoice) { if (event == Choice.EVNT_VALIDATION_OK) { doUpdateGroupAreaRelations(); // do logging - if (this.inAreaGroups.size()==0) { + if (inAreaGroups.isEmpty()) { ThreadLocalUserActivityLogger.log(GroupLoggingAction.BGAREA_UPDATED_NOW_EMPTY, getClass()); } else { - for (Iterator it = inAreaGroups.iterator(); it.hasNext();) { - BusinessGroup aGroup = (BusinessGroup) it.next(); + for (BusinessGroup aGroup : inAreaGroups) { ThreadLocalUserActivityLogger.log(GroupLoggingAction.BGAREA_UPDATED_MEMBER_GROUP, getClass(), LoggingResourceable.wrap(aGroup)); } @@ -183,24 +169,19 @@ public class BGAreaEditController extends BasicController { if (event == Event.DONE_EVENT) { BGArea updatedArea = doAreaUpdate(); if (updatedArea == null) { - this.areaController.resetAreaName(); - getWindowControl().setWarning(this.trans.translate("error.area.name.exists")); + areaController.resetAreaName(); + getWindowControl().setWarning(translate("error.area.name.exists")); } else { - this.area = updatedArea; - this.editVC.contextPut("title", this.trans.translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(this.area.getName()).toString() })); + area = updatedArea; + editVC.contextPut("title", translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(area.getName()).toString() })); } } else if (event == Event.CANCELLED_EVENT) { // area might have been changed, reload from db - this.area = this.areaManager.reloadArea(this.area); - //TODO:pb: refactor BGControllerFactory.create..AreaController to be - //usefull here - - if (this.areaController != null) { - removeAsListenerAndDispose(this.areaController); - } - this.areaController = new BGAreaFormController(ureq, getWindowControl(), this.area, false); - listenTo(this.areaController); - this.detailsTabVC.put("areaForm", this.areaController.getInitialComponent()); + area = areaManager.reloadArea(area); + removeAsListenerAndDispose(areaController); + areaController = new BGAreaFormController(ureq, getWindowControl(), area, false); + listenTo(areaController); + detailsTabVC.put("areaForm", areaController.getInitialComponent()); } } } @@ -211,9 +192,9 @@ public class BGAreaEditController extends BasicController { * @return the updated area */ public BGArea doAreaUpdate() { - this.area.setName(this.areaController.getAreaName()); - this.area.setDescription(this.areaController.getAreaDescription()); - return this.areaManager.updateBGArea(this.area); + area.setName(areaController.getAreaName()); + area.setDescription(areaController.getAreaDescription()); + return areaManager.updateBGArea(area); } /** @@ -221,25 +202,21 @@ public class BGAreaEditController extends BasicController { */ public void doUpdateGroupAreaRelations() { // 1) add groups to area - List addedGroups = groupsChoice.getAddedRows(); - Iterator iterator = addedGroups.iterator(); - while (iterator.hasNext()) { - Integer position = (Integer) iterator.next(); + List<Integer> addedGroups = groupsChoice.getAddedRows(); + for (Integer position:addedGroups) { BusinessGroup group = groupsDataModel.getGroup(position.intValue()); // refresh group to prevent stale object exception and context proxy // issues group = CoreSpringFactory.getImpl(BusinessGroupService.class).loadBusinessGroup(group); // refresh group also in table model - this.allGroups.set(position.intValue(), group); + allGroups.set(position.intValue(), group); // add group now to area and update in area group list areaManager.addBGToBGArea(group, area); - this.inAreaGroups.add(group); + inAreaGroups.add(group); } // 2) remove groups from area - List removedGroups = groupsChoice.getRemovedRows(); - iterator = removedGroups.iterator(); - while (iterator.hasNext()) { - Integer position = (Integer) iterator.next(); + List<Integer> removedGroups = groupsChoice.getRemovedRows(); + for (Integer position:removedGroups) { BusinessGroup group = groupsDataModel.getGroup(position.intValue()); areaManager.removeBGFromArea(group, area); this.inAreaGroups.remove(group); diff --git a/src/test/java/org/olat/group/test/BGAreaManagerTest.java b/src/test/java/org/olat/group/test/BGAreaManagerTest.java index bb827ea3090..d959f35fd01 100644 --- a/src/test/java/org/olat/group/test/BGAreaManagerTest.java +++ b/src/test/java/org/olat/group/test/BGAreaManagerTest.java @@ -33,7 +33,9 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -43,9 +45,14 @@ import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.olat.basesecurity.BaseSecurity; +import org.olat.core.commons.persistence.DB; import org.olat.core.commons.persistence.DBFactory; +import org.olat.core.id.Identity; import org.olat.core.logging.OLog; import org.olat.core.logging.Tracing; +import org.olat.group.BusinessGroup; +import org.olat.group.BusinessGroupService; import org.olat.group.area.BGArea; import org.olat.group.area.BGAreaManager; import org.olat.resource.OLATResource; @@ -55,7 +62,7 @@ import org.springframework.beans.factory.annotation.Autowired; /** * - * @author Christian Guretzki + * @author Christian Guretzki, srosse */ public class BGAreaManagerTest extends OlatTestCase { @@ -63,8 +70,14 @@ public class BGAreaManagerTest extends OlatTestCase { private OLATResource c1, c2; + @Autowired + private DB dbInstance; @Autowired private BGAreaManager areaManager; + @Autowired + private BusinessGroupService businessGroupService; + @Autowired + private BaseSecurity securityManager; @Before public void setUp() { @@ -92,10 +105,464 @@ public class BGAreaManagerTest extends OlatTestCase { @Test public void testCreateBGArea() { + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + String description = "description:" + areaName; + BGArea area = areaManager.createAndPersistBGAreaIfNotExists(areaName, description, resource); + Assert.assertNotNull(area); + dbInstance.commitAndCloseSession(); + + //check by reloading the area + BGArea reloadedArea = areaManager.reloadArea(area); + Assert.assertNotNull(reloadedArea); + Assert.assertNotNull(reloadedArea.getCreationDate()); + Assert.assertNotNull(reloadedArea.getResource()); + Assert.assertEquals(areaName, reloadedArea.getName()); + Assert.assertEquals(description, reloadedArea.getDescription()); + Assert.assertEquals(resource, reloadedArea.getResource()); + } + + @Test + public void findBGArea() { + //create a resource with areas + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("find-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("find-2-" + areaName, "description:" + areaName, resource); + + dbInstance.commitAndCloseSession(); + + BGArea fArea1 = areaManager.findBGArea("find-1-" + areaName, resource); + Assert.assertNotNull(fArea1); + Assert.assertEquals(area1, fArea1); + + BGArea fArea2 = areaManager.findBGArea("find-2-" + areaName, resource); + Assert.assertNotNull(fArea2); + Assert.assertEquals(area2, fArea2); + } + + @Test + public void updateBGArea() { + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area = areaManager.createAndPersistBGAreaIfNotExists("upd-1-" + areaName, "description:" + areaName, resource); + + dbInstance.commitAndCloseSession(); + + //update the area + area.setName("Hello world"); + area.setDescription("The world is big"); + BGArea updatedArea = areaManager.updateBGArea(area); + //check output + Assert.assertNotNull(updatedArea); + Assert.assertEquals("Hello world", updatedArea.getName()); + Assert.assertEquals("The world is big", updatedArea.getDescription()); + + dbInstance.commitAndCloseSession(); + + BGArea reloadedArea = areaManager.loadArea(area.getKey()); + Assert.assertNotNull(reloadedArea); + Assert.assertEquals("Hello world", reloadedArea.getName()); + Assert.assertEquals("The world is big", reloadedArea.getDescription()); + } + + @Test + public void addBGToBGArea() { + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area = areaManager.createAndPersistBGAreaIfNotExists("area-" + areaName, "description:" + areaName, resource); + BusinessGroup group = businessGroupService.createBusinessGroup(null, "area-group", "area-group-desc", 0, -1, false, false, resource); + + dbInstance.commitAndCloseSession(); + + //add the relation + areaManager.addBGToBGArea(group, area); + } + + @Test + public void deleteBGArea() { + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + + //add the relations + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area1); + areaManager.addBGToBGArea(group2, area2); + dbInstance.commitAndCloseSession(); + + //delete area 1 + areaManager.deleteBGArea(area1); + dbInstance.commitAndCloseSession(); + + //check that it's really deleted + BGArea deletedArea = areaManager.loadArea(area1.getKey()); + Assert.assertNull(deletedArea); + //but not all areas are deleted + BGArea reloadedArea2 = areaManager.loadArea(area2.getKey()); + Assert.assertNotNull(reloadedArea2); + } + + + @Test + public void addAndFindByResource() { + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + BGArea area3 = areaManager.createAndPersistBGAreaIfNotExists("area-3-" + areaName, "description:" + areaName, resource); + + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + + //add the relation + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area2); + areaManager.addBGToBGArea(group1, area3); + areaManager.addBGToBGArea(group2, area3); + dbInstance.commitAndCloseSession(); + + //count + int numOfAreas = areaManager.countBGAreasOfBGContext(resource); + Assert.assertEquals(3, numOfAreas); + + //find areas + List<BGArea> areas = areaManager.findBGAreasOfBGContext(resource); + Assert.assertNotNull(areas); + Assert.assertEquals(3, areas.size()); + Assert.assertTrue(areas.contains(area1)); + Assert.assertTrue(areas.contains(area2)); + Assert.assertTrue(areas.contains(area3)); + } + + @Test + public void addAndFindByGroup() { + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group3 = businessGroupService.createBusinessGroup(null, "area-3-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + + //add the relation + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area2); + areaManager.addBGToBGArea(group3, area1); + areaManager.addBGToBGArea(group3, area2); + dbInstance.commitAndCloseSession(); + + //check find group 1 + List<BGArea> areasGroup1 = areaManager.findBGAreasOfBusinessGroup(group1); + Assert.assertNotNull(areasGroup1); + Assert.assertEquals(1, areasGroup1.size()); + Assert.assertTrue(areasGroup1.contains(area1)); + + //check find group 3 + List<BGArea> areasGroup3 = areaManager.findBGAreasOfBusinessGroup(group3); + Assert.assertNotNull(areasGroup3); + Assert.assertEquals(2, areasGroup3.size()); + Assert.assertTrue(areasGroup3.contains(area1)); + Assert.assertTrue(areasGroup3.contains(area2)); + } + + @Test + public void addAndFindByGroups() { + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group3 = businessGroupService.createBusinessGroup(null, "area-3-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group4 = businessGroupService.createBusinessGroup(null, "area-4-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + + //add the relation + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area2); + areaManager.addBGToBGArea(group3, area1); + areaManager.addBGToBGArea(group3, area2); + areaManager.addBGToBGArea(group4, area2); + dbInstance.commitAndCloseSession(); + + //check find group 1 + List<BGArea> areasGroup1 = areaManager.findBGAreasOfBusinessGroups(Collections.singletonList(group1)); + Assert.assertNotNull(areasGroup1); + Assert.assertEquals(1, areasGroup1.size()); + Assert.assertTrue(areasGroup1.contains(area1)); + + //check find group 2 and 4 -> only area 2 + List<BusinessGroup> groups2_4 = new ArrayList<BusinessGroup>(); + groups2_4.add(group2); + groups2_4.add(group4); + List<BGArea> areasGroup2_4 = areaManager.findBGAreasOfBusinessGroups(groups2_4); + Assert.assertNotNull(areasGroup2_4); + Assert.assertEquals(1, areasGroup2_4.size()); + Assert.assertTrue(areasGroup2_4.contains(area2)); + + //check find all groups + List<BusinessGroup> allGroups = new ArrayList<BusinessGroup>(); + allGroups.add(group1); + allGroups.add(group2); + allGroups.add(group3); + allGroups.add(group4); + List<BGArea> areasAllGroups = areaManager.findBGAreasOfBusinessGroups(allGroups); + Assert.assertNotNull(areasAllGroups); + Assert.assertEquals(2, areasAllGroups.size()); + Assert.assertTrue(areasAllGroups.contains(area1)); + Assert.assertTrue(areasAllGroups.contains(area2)); + + //check empty list + List<BGArea> areasEmpty = areaManager.findBGAreasOfBusinessGroups(Collections.<BusinessGroup>emptyList()); + Assert.assertNotNull(areasEmpty); + Assert.assertEquals(0, areasEmpty.size()); + } + + @Test + public void addFindAndDeleteRelation() { + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); String areaName = UUID.randomUUID().toString(); - BGArea bgArea = areaManager.createAndPersistBGAreaIfNotExists(areaName, "description:" + areaName, c1); - Assert.assertNotNull(bgArea); - DBFactory.getInstance().commit(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + //create 2 groups + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + //add the relations + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area1); + dbInstance.commitAndCloseSession(); + + //check find groups + List<BusinessGroup> groups = areaManager.findBusinessGroupsOfArea(area1); + Assert.assertNotNull(groups); + Assert.assertEquals(2, groups.size()); + Assert.assertTrue(groups.contains(group1)); + Assert.assertTrue(groups.contains(group2)); + + dbInstance.commitAndCloseSession(); + //remove relation to group1 + areaManager.removeBGFromArea(group2, area1); + dbInstance.commitAndCloseSession(); + + //check find groups + List<BusinessGroup> diminushedGroups = areaManager.findBusinessGroupsOfArea(area1); + Assert.assertNotNull(diminushedGroups); + Assert.assertEquals(1, diminushedGroups.size()); + Assert.assertTrue(diminushedGroups.contains(group1)); + } + + @Test + public void findGroupsByAreas() { + //create a resource, 3 area and 2 group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + BGArea area3 = areaManager.createAndPersistBGAreaIfNotExists("area-3-" + areaName, "description:" + areaName, resource); + //create 2 groups + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + //add the relations + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group1, area2); + areaManager.addBGToBGArea(group1, area3); + areaManager.addBGToBGArea(group2, area1); + dbInstance.commitAndCloseSession(); + + //check with empty list + List<BusinessGroup> groupEmpty = areaManager.findBusinessGroupsOfAreas(Collections.<BGArea>emptyList()); + Assert.assertNotNull(groupEmpty); + Assert.assertEquals(0, groupEmpty.size()); + + //check find area 3 -> only group 1 + List<BusinessGroup> groupArea3 = areaManager.findBusinessGroupsOfAreas(Collections.singletonList(area3)); + Assert.assertNotNull(groupArea3); + Assert.assertEquals(1, groupArea3.size()); + Assert.assertTrue(groupArea3.contains(group1)); + + //check find area 1,2 and 3 -> only group 1 and 2 + List<BGArea> allAreas = new ArrayList<BGArea>(); + allAreas.add(area1); + allAreas.add(area2); + allAreas.add(area3); + List<BusinessGroup> groupAllAreas = areaManager.findBusinessGroupsOfAreas(allAreas); + Assert.assertNotNull(groupAllAreas); + Assert.assertEquals(2, groupAllAreas.size()); + Assert.assertTrue(groupAllAreas.contains(group1)); + Assert.assertTrue(groupAllAreas.contains(group2)); + } + + @Test + public void addAndDeleteRelations() { + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + //create 2 groups + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group3 = businessGroupService.createBusinessGroup(null, "area-3-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + //add the relations + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area1); + areaManager.addBGToBGArea(group2, area2); + areaManager.addBGToBGArea(group3, area1); + dbInstance.commitAndCloseSession(); + + //check with find groups + List<BGArea> areaGroup2 = areaManager.findBGAreasOfBusinessGroup(group2); + Assert.assertNotNull(areaGroup2); + Assert.assertEquals(2, areaGroup2.size()); + Assert.assertTrue(areaGroup2.contains(area1)); + Assert.assertTrue(areaGroup2.contains(area2)); + + //remove relation to group2 + areaManager.deleteBGtoAreaRelations(group2); + dbInstance.commitAndCloseSession(); + + //check find groups + List<BGArea> areaGroup2After = areaManager.findBGAreasOfBusinessGroup(group2); + Assert.assertNotNull(areaGroup2After); + Assert.assertEquals(0, areaGroup2After.size()); + } + + @Test + public void checkIfOneOrMoreNameExistsInContext() { + //create a resource, an area, a group + OLATResource resource1 = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource1); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource1); + //create 2 groups + dbInstance.commitAndCloseSession(); + + //check empty list + boolean emptyTest = areaManager.checkIfOneOrMoreNameExistsInContext(Collections.<String>emptySet(), resource1); + Assert.assertFalse(emptyTest); + + //check names + Set<String> name1 = new HashSet<String>(); + name1.add("Hello OpenOLAT"); + name1.add(area1.getName()); + boolean test1 = areaManager.checkIfOneOrMoreNameExistsInContext(name1, resource1); + Assert.assertTrue(test1); + + //check more names + Set<String> name2 = new HashSet<String>(); + name2.add("Hello OpenOLAT"); + name2.add(area1.getName()); + name2.add(area2.getName()); + boolean test2 = areaManager.checkIfOneOrMoreNameExistsInContext(name2, resource1); + Assert.assertTrue(test2); + + //check wrong names + Set<String> name3 = new HashSet<String>(); + name3.add("Hello OpenOLAT"); + name3.add("area-1-"); + name3.add("area-2-"); + boolean test3 = areaManager.checkIfOneOrMoreNameExistsInContext(name3, resource1); + Assert.assertFalse(test3); + } + + @Test + public void findBusinessGroupsOfAreaAttendedBy() { + Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("attendee-1-" + UUID.randomUUID().toString()); + Identity id2 = JunitTestHelper.createAndPersistIdentityAsUser("attendee-2-" + UUID.randomUUID().toString()); + Identity id3 = JunitTestHelper.createAndPersistIdentityAsUser("attendee-3-" + UUID.randomUUID().toString()); + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + //create 2 groups + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group3 = businessGroupService.createBusinessGroup(null, "area-3-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + //add the relations + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area1); + areaManager.addBGToBGArea(group2, area2); + areaManager.addBGToBGArea(group3, area1); + dbInstance.commitAndCloseSession(); + //add attendee + securityManager.addIdentityToSecurityGroup(id1, group1.getPartipiciantGroup()); + securityManager.addIdentityToSecurityGroup(id2, group2.getPartipiciantGroup()); + securityManager.addIdentityToSecurityGroup(id2, group3.getPartipiciantGroup()); + securityManager.addIdentityToSecurityGroup(id3, group3.getPartipiciantGroup()); + dbInstance.commitAndCloseSession(); + + //find with resource + List<BusinessGroup> groupId1 = areaManager.findBusinessGroupsOfAreaAttendedBy(id1, null, resource); + Assert.assertNotNull(groupId1); + Assert.assertEquals(1, groupId1.size()); + Assert.assertTrue(groupId1.contains(group1)); + + //find nothing with name and resource + List<BusinessGroup> groupId1Area2 = areaManager.findBusinessGroupsOfAreaAttendedBy(id1, "area-2-" + areaName, resource); + Assert.assertNotNull(groupId1Area2); + Assert.assertEquals(0, groupId1Area2.size()); + + //find groups id 2 with name and resource + List<BusinessGroup> groupId2Area1 = areaManager.findBusinessGroupsOfAreaAttendedBy(id2, "area-1-" + areaName, resource); + Assert.assertNotNull(groupId2Area1); + Assert.assertEquals(2, groupId2Area1.size()); + Assert.assertTrue(groupId2Area1.contains(group2)); + Assert.assertTrue(groupId2Area1.contains(group3)); + } + + @Test + public void isIdentityInBGArea() { + Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("attendee-1-" + UUID.randomUUID().toString()); + //create a resource, an area, a group + OLATResource resource = JunitTestHelper.createRandomResource(); + String areaName = UUID.randomUUID().toString(); + BGArea area1 = areaManager.createAndPersistBGAreaIfNotExists("area-1-" + areaName, "description:" + areaName, resource); + BGArea area2 = areaManager.createAndPersistBGAreaIfNotExists("area-2-" + areaName, "description:" + areaName, resource); + BGArea area3 = areaManager.createAndPersistBGAreaIfNotExists("area-3-" + areaName, "description:" + areaName, resource); + //create 2 groups + BusinessGroup group1 = businessGroupService.createBusinessGroup(null, "area-1-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "area-2-group", "area-group-desc", 0, -1, false, false, resource); + BusinessGroup group3 = businessGroupService.createBusinessGroup(null, "area-3-group", "area-group-desc", 0, -1, false, false, resource); + dbInstance.commitAndCloseSession(); + //add the relations + areaManager.addBGToBGArea(group1, area1); + areaManager.addBGToBGArea(group2, area1); + areaManager.addBGToBGArea(group2, area2); + areaManager.addBGToBGArea(group3, area3); + dbInstance.commitAndCloseSession(); + //add attendee + securityManager.addIdentityToSecurityGroup(id1, group1.getPartipiciantGroup()); + securityManager.addIdentityToSecurityGroup(id1, group2.getOwnerGroup()); + dbInstance.commitAndCloseSession(); + + //check in area 1 + boolean testArea1 = areaManager.isIdentityInBGArea(id1, "area-1-" + areaName, resource); + Assert.assertTrue(testArea1); + //check in area 1 + boolean testArea2 = areaManager.isIdentityInBGArea(id1, "area-2-" + areaName, resource); + Assert.assertTrue(testArea2); + //check in area 1 + boolean testArea3 = areaManager.isIdentityInBGArea(id1, "area-3-" + areaName, resource); + Assert.assertFalse(testArea3); } /** @@ -135,7 +602,6 @@ public class BGAreaManagerTest extends OlatTestCase { assertEquals("Not all threads has finished", 0, finfishCount.getCount()); } - /** * thread 1 : try to create - sleep - delete sleep diff --git a/src/test/java/org/olat/group/test/BusinessGroupServiceImplTest.java b/src/test/java/org/olat/group/test/BusinessGroupServiceImplTest.java index 53efbf2e268..56262fc22b9 100644 --- a/src/test/java/org/olat/group/test/BusinessGroupServiceImplTest.java +++ b/src/test/java/org/olat/group/test/BusinessGroupServiceImplTest.java @@ -37,7 +37,6 @@ import java.util.List; import java.util.Set; import java.util.UUID; -import org.apache.log4j.Logger; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -51,6 +50,8 @@ import org.olat.collaboration.CollaborationToolsFactory; import org.olat.core.commons.persistence.DBFactory; import org.olat.core.id.Identity; import org.olat.core.id.User; +import org.olat.core.logging.OLog; +import org.olat.core.logging.Tracing; import org.olat.core.util.Encoder; import org.olat.group.BusinessGroup; import org.olat.group.BusinessGroupService; @@ -70,7 +71,7 @@ import org.springframework.beans.factory.annotation.Autowired; public class BusinessGroupServiceImplTest extends OlatTestCase { // - private static Logger log = Logger.getLogger(BusinessGroupServiceImplTest.class.getName()); + private static OLog log = Tracing.createLoggerFor(BusinessGroupServiceImplTest.class); /* * ::Test Setup:: */ -- GitLab