Skip to content

Commit 15eccf7

Browse files
committed
Feature #14738
Change the signature of the callback in CDIResourceEventListener. Take into account the change in the signature of the methods in CDIResourceEventListener in the implementors. Add a mechanism to notify about the removal of a user in some roles he played (either explicitly or through a user group) in some component instances. For doing, two listeners are added: - ProfileInstUpdateEventListener to listen for update in the list of users and groups of a given access right profile of a component instance. - GroupUserUnlinkEventListener to listen for removal of a user from a user group (this group could play some roles in some component instances). Add integration tests covering this new mechanism.
1 parent 86b5c26 commit 15eccf7

File tree

57 files changed

+2692
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2692
-110
lines changed

core-api/src/main/java/org/silverpeas/core/notification/system/AbstractResourceEvent.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828
import javax.xml.bind.annotation.XmlAccessorType;
2929
import javax.xml.bind.annotation.XmlElement;
3030
import java.io.Serializable;
31-
import java.util.Collections;
32-
import java.util.HashMap;
33-
import java.util.Map;
34-
import java.util.Set;
31+
import java.util.*;
3532

3633
/**
3734
* It is an abstract implementation of a notification event on a resource's state change. It defines
@@ -67,6 +64,7 @@ protected AbstractResourceEvent() {
6764
* resource is expected: the first being the resource before the update, the second being the
6865
* resource after the update (the result of the update).
6966
*/
67+
@SafeVarargs
7068
protected AbstractResourceEvent(Type type, @NotNull T... resource) {
7169
this.type = type;
7270
if (type == Type.CREATION) {
@@ -128,16 +126,11 @@ public boolean equals(final Object o) {
128126
}
129127

130128
final AbstractResourceEvent<?> that = (AbstractResourceEvent<?>) o;
131-
if (!transition.equals(that.transition)) {
132-
return false;
133-
}
134-
return type == that.type;
129+
return transition.equals(that.transition) && type == that.type;
135130
}
136131

137132
@Override
138133
public int hashCode() {
139-
int result = type.hashCode();
140-
result = 31 * result + transition.hashCode();
141-
return result;
134+
return Objects.hash(this.type, this.transition);
142135
}
143136
}

core-api/src/main/java/org/silverpeas/core/notification/system/CDIAfterSuccessfulTransactionResourceEventListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
* some of the following methods to transparently receive the events on which they are interested:
4242
* </p>
4343
* <p>
44-
* If the observation is performed into a transaction, all the events are performed just after a
45-
* successful commit.
44+
* If the observation is performed within a transaction, all the events are then just received
45+
* after a successful commit.
4646
* </p>
4747
* <ul>
4848
* <li>{@code org.silverpeas.core.notification.system.ResourceEventListener#onCreation(ResourceEvent} to
@@ -72,7 +72,7 @@ public abstract class CDIAfterSuccessfulTransactionResourceEventListener<T exten
7272
* @param event an event.
7373
* @throws Exception if the processing of the event fails.
7474
*/
75-
public void onEvent(@Observes(during = AFTER_SUCCESS) T event) throws Exception {
75+
public void onEvent(@Observes(during = AFTER_SUCCESS) T event) {
7676
Transaction.performInNew(() -> {
7777
try {
7878
dispatchEvent(event);

core-api/src/main/java/org/silverpeas/core/notification/system/CDIResourceEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public abstract class CDIResourceEventListener<T extends ResourceEvent<?>>
6464
* @param event an event.
6565
* @throws Exception if the processing of the event fails.
6666
*/
67-
public void onEvent(@Observes T event) throws Exception {
67+
public void onEvent(@Observes T event) {
6868
dispatchEvent(event);
6969
}
7070
}

core-api/src/main/java/org/silverpeas/core/notification/system/ResourceEventListener.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,65 +53,58 @@ public interface ResourceEventListener<T extends ResourceEvent<?>> {
5353
* nonrecoverable. By default, this method does nothing.
5454
*
5555
* @param event the event on the deletion of a resource.
56-
* @throws java.lang.Exception if an error occurs while treating the event.
5756
*/
58-
default void onDeletion(final T event) throws Exception {
57+
default void onDeletion(final T event) {
5958
}
6059

6160
/**
6261
* An event on the removing of a resource has be listened. A removed resource is again existent
6362
* and it is recoverable; it is usually put in a trash. By default, this method does nothing.
6463
*
6564
* @param event the event on the removing of a resource.
66-
* @throws java.lang.Exception if an error occurs while treating the event.
6765
*/
68-
default void onRemoving(final T event) throws Exception {
66+
default void onRemoving(final T event) {
6967
}
7068

7169
/**
7270
* An event on the recovery of a removed resource has be listened. By default, this method does
7371
* nothing.
7472
*
7573
* @param event the event on the removing of a resource.
76-
* @throws java.lang.Exception if an error occurs while treating the event.
7774
*/
78-
default void onRecovery(final T event) throws Exception {
75+
default void onRecovery(final T event) {
7976
}
8077

8178
/**
8279
* An event on the update of a resource has be listened. By default, this method does nothing.
8380
*
8481
* @param event the event on the update of a resource.
85-
* @throws java.lang.Exception if an error occurs while treating the event.
8682
*/
87-
default void onUpdate(final T event) throws Exception {
83+
default void onUpdate(final T event) {
8884
}
8985

9086
/**
9187
* An event on the move of a resource has be listened. By default, this method does nothing.
9288
*
9389
* @param event the event on the move of a resource.
94-
* @throws java.lang.Exception if an error occurs while treating the event.
9590
*/
96-
default void onMove(final T event) throws Exception {
91+
default void onMove(final T event) {
9792
}
9893

9994
/**
10095
* An event on the creation of a resource has be listened. By default, this method does nothing.
10196
*
10297
* @param event the event on the creation of a resource.
103-
* @throws java.lang.Exception if an error occurs while treating the event.
10498
*/
105-
default void onCreation(final T event) throws Exception {
99+
default void onCreation(final T event) {
106100
}
107101

108102
/**
109103
* An event on the unlock of a resource has be listened. By default, this method does nothing.
110104
*
111105
* @param event the event on the unlock of a resource.
112-
* @throws java.lang.Exception if an error occurs while treating the event.
113106
*/
114-
default void onUnlock(final T event) throws Exception {
107+
default void onUnlock(final T event) {
115108
}
116109

117110
/**
@@ -135,9 +128,8 @@ default void onUnlock(final T event) throws Exception {
135128
* </p>
136129
*
137130
* @param event the event to dispatch.
138-
* @throws java.lang.Exception if an error occurs while treating the event.
139131
*/
140-
default void dispatchEvent(final T event) throws Exception {
132+
default void dispatchEvent(final T event) {
141133
if (isEnabled()) {
142134
switch (event.getType()) {
143135
case CREATION:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Copyright (C) 2000 - 2025 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection with Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have received a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "https://www.silverpeas.org/legal/floss_exception.html"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
23+
*/
24+
25+
package org.silverpeas.core.admin.user.notification.role;
26+
27+
import org.jboss.shrinkwrap.api.Archive;
28+
import org.junit.Before;
29+
import org.junit.Rule;
30+
import org.silverpeas.core.admin.service.AdminException;
31+
import org.silverpeas.core.admin.service.Administration;
32+
import org.silverpeas.core.admin.user.ProfileInstManager;
33+
import org.silverpeas.core.admin.user.model.ProfileInst;
34+
import org.silverpeas.core.admin.user.model.User;
35+
import org.silverpeas.core.admin.user.notification.role.test.Validator;
36+
import org.silverpeas.core.cache.service.CacheAccessorProvider;
37+
import org.silverpeas.core.cache.service.SessionCacheAccessor;
38+
import org.silverpeas.core.persistence.Transaction;
39+
import org.silverpeas.core.test.WarBuilder4LibCore;
40+
import org.silverpeas.core.test.integration.rule.DbSetupRule;
41+
import org.silverpeas.kernel.SilverpeasRuntimeException;
42+
43+
import javax.inject.Inject;
44+
import java.util.List;
45+
46+
import static org.junit.Assert.assertEquals;
47+
import static org.junit.Assert.assertTrue;
48+
49+
/**
50+
* Base class of the integration tests on the
51+
* {@link org.silverpeas.core.admin.user.notification.role.UserRoleEvent} event listeners.
52+
*
53+
* @author mmoquillon
54+
*/
55+
public abstract class BaseUserRoleEventListenerTest {
56+
57+
public static final String VALIDATOR_ROLE = "validator";
58+
public static final String READER_ROLE = "reader";
59+
public static final List<String> NO_IDS = List.of();
60+
public static final String INSTANCE_ID = "myComponent1";
61+
62+
@Rule
63+
public DbSetupRule dbSetupRule = DbSetupRule.createTablesFrom("create_database.sql")
64+
.loadInitialDataSetFrom("insert_dataset.sql");
65+
66+
@Inject
67+
private ProfileInstManager profileInstManager;
68+
69+
@Inject
70+
private Administration admin;
71+
72+
public static Archive<?> createTestArchiveFor(Class<? extends BaseUserRoleEventListenerTest> test) {
73+
return WarBuilder4LibCore.onWarForTestClass(test)
74+
.addSilverpeasExceptionBases()
75+
.addAdministrationFeatures()
76+
.addDatabaseToolFeatures()
77+
.addJpaPersistenceFeatures()
78+
.addPublicationTemplateFeatures()
79+
.addAsResource("org/silverpeas/core/admin/user/notification/role")
80+
.testFocusedOn(warBuilder ->
81+
warBuilder.addPackages(true, test.getPackageName()))
82+
.build();
83+
}
84+
85+
@Before
86+
public void setUpCurrentRequester() {
87+
SessionCacheAccessor sessionCacheAccessor =
88+
CacheAccessorProvider.getSessionCacheAccessor();
89+
sessionCacheAccessor.newSessionCache(User.getById("1"));
90+
}
91+
92+
@Before
93+
public void reloadAdminCache() {
94+
admin.reloadCache();
95+
}
96+
97+
public ProfileInst getProfileInst(String roleName) {
98+
String id;
99+
switch (roleName) {
100+
case "admin":
101+
id = "1";
102+
break;
103+
case VALIDATOR_ROLE:
104+
id = "2";
105+
break;
106+
default:
107+
id = "3";
108+
break;
109+
}
110+
return getProfileInstById(id);
111+
}
112+
113+
public ProfileInst getProfileInstById(String id) {
114+
return Transaction.performInOne(() -> {
115+
try {
116+
return profileInstManager.getProfileInst(id, false);
117+
} catch (AdminException e) {
118+
throw new SilverpeasRuntimeException(e);
119+
}
120+
});
121+
}
122+
123+
public void updateProfileInst(ProfileInst profileInst) {
124+
Transaction.performInOne(() -> {
125+
try {
126+
profileInstManager.updateProfileInst(profileInst);
127+
} catch (AdminException e) {
128+
throw new SilverpeasRuntimeException(e);
129+
}
130+
return null;
131+
});
132+
}
133+
134+
private String createProfileInst(ProfileInst profileInst) {
135+
return Transaction.performInOne(() -> {
136+
try {
137+
return profileInstManager.createProfileInst(profileInst,
138+
profileInst.getComponentFatherId());
139+
} catch (AdminException e) {
140+
throw new SilverpeasRuntimeException(e);
141+
}
142+
});
143+
}
144+
145+
public void setUpProfileInstWith(String profileName, List<String> users, List<String> groups) {
146+
ProfileInst initialProfile = getProfileInst(profileName);
147+
initialProfile.getAllUsers().addAll(users);
148+
initialProfile.getAllGroups().addAll(groups);
149+
updateProfileInst(initialProfile);
150+
}
151+
152+
public String setUpInheritedProfileInstWith(String profileName, List<String> users,
153+
List<String> groups) {
154+
ProfileInst initialProfile = new ProfileInst();
155+
initialProfile.setComponentFatherId(1);
156+
initialProfile.setInherited(true);
157+
initialProfile.setName(profileName);
158+
initialProfile.setLabel(profileName);
159+
initialProfile.getAllUsers().addAll(users);
160+
initialProfile.getAllGroups().addAll(groups);
161+
return createProfileInst(initialProfile);
162+
}
163+
164+
public static void assertValidatorsEquality(List<Validator> actualValidators,
165+
List<Validator> expectedValidators) {
166+
assertEquals(expectedValidators.size(), actualValidators.size());
167+
assertTrue(actualValidators.containsAll(expectedValidators));
168+
}
169+
170+
public static void assertValidatorIsRemoved(String validatorId,
171+
List<Validator> actualValidators) {
172+
assertTrue(actualValidators.stream()
173+
.noneMatch(v -> v.getUserId().equals(validatorId)));
174+
}
175+
}
176+

0 commit comments

Comments
 (0)