Skip to content

Commit 31e077f

Browse files
vlo-rtefreddidierRTE
authored andcommitted
Do not erase entity/group membership when updating an entity/group via API (#7997)
Signed-off-by: vlo-rte <[email protected]>
1 parent 1a4e2c6 commit 31e077f

File tree

6 files changed

+128
-27
lines changed

6 files changed

+128
-27
lines changed

services/users/src/main/java/org/opfab/users/model/Entity.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2024, RTE (http://www.rte-france.com)
1+
/* Copyright (c) 2018-2025, RTE (http://www.rte-france.com)
22
* See AUTHORS.txt
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -142,12 +142,17 @@ public void setRoles(List<RoleEnum> roles){
142142
}
143143

144144
public List<String> getUsers() {
145-
if (users == null)
146-
return Collections.emptyList();
145+
if (users == null) {
146+
// we need to return null in case 'users' field is not in the post query, to differentiate with an empty
147+
// array of users
148+
return null;//NOSONAR
149+
}
147150
return new ArrayList<>(users);
148151
}
149152

150153
public void setUsers(List<String> users) {
151-
this.users = new HashSet<>(users);
154+
if (users != null) {
155+
this.users = new HashSet<>(users);
156+
}
152157
}
153158
}

services/users/src/main/java/org/opfab/users/model/Group.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2024, RTE (http://www.rte-france.com)
1+
/* Copyright (c) 2018-2025, RTE (http://www.rte-france.com)
22
* See AUTHORS.txt
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -132,12 +132,17 @@ public void deletePermission(PermissionEnum permission) {
132132
}
133133

134134
public List<String> getUsers() {
135-
if (users == null)
136-
return Collections.emptyList();
135+
if (users == null) {
136+
// we need to return null in case 'users' field is not in the post query, to differentiate with an empty
137+
// array of users
138+
return null;//NOSONAR
139+
}
137140
return new ArrayList<>(users);
138141
}
139142

140143
public void setUsers(List<String> users) {
141-
this.users = new HashSet<>(users);
144+
if (users != null) {
145+
this.users = new HashSet<>(users);
146+
}
142147
}
143148
}

services/users/src/main/java/org/opfab/users/services/EntitiesService.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022-2024, RTE (http://www.rte-france.com)
1+
/* Copyright (c) 2022-2025, RTE (http://www.rte-france.com)
22
* See AUTHORS.txt
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -65,7 +65,11 @@ public OperationResult<EntityCreationReport<Entity>> createEntity(Entity entity)
6565
}
6666
boolean isAlreadyExisting = entityRepository.findById(entity.getId()).isPresent();
6767
Entity newEntity = entityRepository.save(entity);
68-
updateEntityUsers(newEntity.getId(), entity.getUsers());
68+
69+
if (entity.getUsers() != null) {
70+
updateEntityUsers(newEntity.getId(), entity.getUsers());
71+
}
72+
6973
notificationService.publishUpdatedConfigMessage();
7074
EntityCreationReport<Entity> report = new EntityCreationReport<>(isAlreadyExisting, newEntity);
7175
return new OperationResult<>(report, true, null, null);

services/users/src/main/java/org/opfab/users/services/GroupsService.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022-2024, RTE (http://www.rte-france.com)
1+
/* Copyright (c) 2022-2025, RTE (http://www.rte-france.com)
22
* See AUTHORS.txt
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -71,7 +71,10 @@ public OperationResult<EntityCreationReport<Group>> createGroup(Group group) {
7171
if (foundPerimetersResult.isSuccess()) {
7272
boolean isAlreadyExisting = groupRepository.findById(group.getId()).isPresent();
7373
Group newGroup = groupRepository.save(group);
74-
updateGroupUsers(newGroup.getId(), group.getUsers());
74+
75+
if (group.getUsers() != null) {
76+
updateGroupUsers(newGroup.getId(), group.getUsers());
77+
}
7578
notificationService.publishUpdatedGroupMessage(group.getId());
7679
EntityCreationReport<Group> report = new EntityCreationReport<>(isAlreadyExisting, newGroup);
7780
return new OperationResult<>(report, true, null, null);

services/users/src/test/java/org/opfab/users/services/EntitiesServiceShould.java

+51-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022-2024, RTE (http://www.rte-france.com)
1+
/* Copyright (c) 2022-2025, RTE (http://www.rte-france.com)
22
* See AUTHORS.txt
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -11,14 +11,7 @@
1111

1212
import static org.assertj.core.api.Assertions.assertThat;
1313

14-
import java.util.ArrayList;
15-
import java.util.Arrays;
16-
import java.util.HashSet;
17-
import java.util.List;
18-
import java.util.Optional;
19-
import java.util.Set;
20-
import java.util.SortedSet;
21-
import java.util.TreeSet;
14+
import java.util.*;
2215

2316
import org.junit.jupiter.api.BeforeEach;
2417
import org.junit.jupiter.api.DisplayName;
@@ -131,6 +124,55 @@ void GIVEN_A_Valid_Entity_WHEN_Create_An_Already_Existing_Entity_THEN_Entity_Is_
131124
assertThat(entityRepositoryStub.findById("entity1").get().getName()).isEqualTo("newEntityName");
132125
}
133126

127+
@Test
128+
void GIVEN_A_Valid_Entity_WHEN_Create_An_Already_Existing_Entity_With_Users_Field_THEN_Entity_Is_Updated_And_Users_Are_Updated() {
129+
Entity entity = new Entity("entity1", "newEntityName", null, null, null, null);
130+
entity.setUsers(Arrays.asList("user2"));
131+
132+
OperationResult<EntityCreationReport<Entity>> result = entitiesService.createEntity(entity);
133+
assertThat(result.isSuccess()).isTrue();
134+
assertThat(result.getResult().isUpdate()).isTrue();
135+
assertThat(result.getResult().getEntity().getId()).isEqualTo("entity1");
136+
assertThat(result.getResult().getEntity().getUsers()).hasSize(1);
137+
assertThat(result.getResult().getEntity().getUsers()).contains("user2");
138+
assertThat(userRepositoryStub.findById("user1").get().getEntities()).hasSize(1);
139+
assertThat(userRepositoryStub.findById("user1").get().getEntities()).contains("entity2");
140+
assertThat(userRepositoryStub.findById("user2").get().getEntities()).hasSize(2);
141+
assertThat(userRepositoryStub.findById("user2").get().getEntities()).contains("entity2", "entity1");
142+
}
143+
144+
@Test
145+
void GIVEN_A_Valid_Entity_WHEN_Create_An_Already_Existing_Entity_Without_Users_Field_THEN_Entity_Is_Updated_And_Users_Are_Not_Deleted() {
146+
Entity entity = new Entity("entity1", "newEntityName", null, null, null, null);
147+
entity.setUsers(null);
148+
149+
OperationResult<EntityCreationReport<Entity>> result = entitiesService.createEntity(entity);
150+
assertThat(result.isSuccess()).isTrue();
151+
assertThat(result.getResult().isUpdate()).isTrue();
152+
assertThat(result.getResult().getEntity().getId()).isEqualTo("entity1");
153+
assertThat(result.getResult().getEntity().getUsers()).isNull();
154+
assertThat(userRepositoryStub.findById("user1").get().getEntities()).hasSize(2);
155+
assertThat(userRepositoryStub.findById("user1").get().getEntities()).contains("entity1", "entity2");
156+
assertThat(userRepositoryStub.findById("user2").get().getEntities()).hasSize(1);
157+
assertThat(userRepositoryStub.findById("user2").get().getEntities()).contains("entity2");
158+
}
159+
160+
@Test
161+
void GIVEN_A_Valid_Entity_WHEN_Create_An_Already_Existing_Entity_With_Users_Field_Empty_THEN_Entity_Is_Updated_And_Users_Are_Empty() {
162+
Entity entity = new Entity("entity1", "newEntityName", null, null, null, null);
163+
entity.setUsers(Collections.emptyList());
164+
165+
OperationResult<EntityCreationReport<Entity>> result = entitiesService.createEntity(entity);
166+
assertThat(result.isSuccess()).isTrue();
167+
assertThat(result.getResult().isUpdate()).isTrue();
168+
assertThat(result.getResult().getEntity().getId()).isEqualTo("entity1");
169+
assertThat(result.getResult().getEntity().getUsers()).isEmpty();
170+
assertThat(userRepositoryStub.findById("user1").get().getEntities()).hasSize(1);
171+
assertThat(userRepositoryStub.findById("user1").get().getEntities()).contains("entity2");
172+
assertThat(userRepositoryStub.findById("user2").get().getEntities()).hasSize(1);
173+
assertThat(userRepositoryStub.findById("user2").get().getEntities()).contains("entity2");
174+
}
175+
134176
@Test
135177
void GIVEN_A_Valid_Entity_WHEN_Update_Description_With_Same_Name_THEN_Entity_Is_Updated() {
136178
Entity entity = new Entity("entity1", "Entity 1", "new description", null, null, null);

services/users/src/test/java/org/opfab/users/services/GroupsServiceShould.java

+48-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022-2024, RTE (http://www.rte-france.com)
1+
/* Copyright (c) 2022-2025, RTE (http://www.rte-france.com)
22
* See AUTHORS.txt
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -11,11 +11,7 @@
1111

1212
import static org.assertj.core.api.Assertions.assertThat;
1313

14-
import java.util.ArrayList;
15-
import java.util.HashSet;
16-
import java.util.List;
17-
import java.util.Optional;
18-
import java.util.Set;
14+
import java.util.*;
1915

2016
import org.junit.jupiter.api.BeforeEach;
2117
import org.junit.jupiter.api.DisplayName;
@@ -151,6 +147,52 @@ void GIVEN_A_Valid_Group_WHEN_Create_An_Already_Existing_Group_THEN_Group_Is_Upd
151147
assertThat(groupRepositoryStub.findById("group1").get().getPermissions().get(0)).isEqualTo(PermissionEnum.READONLY);
152148
}
153149

150+
@Test
151+
void GIVEN_A_Valid_Group_WHEN_Create_An_Already_Existing_Group_With_Users_Field_THEN_Group_Is_Updated_And_Users_Are_Updated() {
152+
Group group = new Group("group1", "newGroupName", null, null, null);
153+
group.setUsers(Arrays.asList("user2"));
154+
155+
OperationResult<EntityCreationReport<Group>> result = groupsService.createGroup(group);
156+
assertThat(result.isSuccess()).isTrue();
157+
assertThat(result.getResult().isUpdate()).isTrue();
158+
assertThat(result.getResult().getEntity().getId()).isEqualTo("group1");
159+
assertThat(result.getResult().getEntity().getUsers()).hasSize(1);
160+
assertThat(result.getResult().getEntity().getUsers()).contains("user2");
161+
assertThat(userRepositoryStub.findById("user1").get().getGroups()).isEmpty();
162+
assertThat(userRepositoryStub.findById("user2").get().getGroups()).hasSize(1);
163+
assertThat(userRepositoryStub.findById("user2").get().getGroups()).contains("group1");
164+
}
165+
166+
@Test
167+
void GIVEN_A_Valid_Group_WHEN_Create_An_Already_Existing_Group_Without_Users_Field_THEN_Group_Is_Updated_And_Users_Are_Not_Deleted() {
168+
Group group = new Group("group1", "newGroupName", null, null, null);
169+
group.setUsers(null);
170+
171+
OperationResult<EntityCreationReport<Group>> result = groupsService.createGroup(group);
172+
assertThat(result.isSuccess()).isTrue();
173+
assertThat(result.getResult().isUpdate()).isTrue();
174+
assertThat(result.getResult().getEntity().getId()).isEqualTo("group1");
175+
assertThat(result.getResult().getEntity().getUsers()).isNull();
176+
assertThat(userRepositoryStub.findById("user1").get().getGroups()).hasSize(1);
177+
assertThat(userRepositoryStub.findById("user1").get().getGroups()).contains("group1");
178+
assertThat(userRepositoryStub.findById("user2").get().getGroups()).hasSize(1);
179+
assertThat(userRepositoryStub.findById("user2").get().getGroups()).contains("group1");
180+
}
181+
182+
@Test
183+
void GIVEN_A_Valid_Group_WHEN_Create_An_Already_Existing_Group_With_Users_Field_Empty_THEN_Group_Is_Updated_And_Users_Are_Empty() {
184+
Group group = new Group("group1", "newGroupName", null, null, null);
185+
group.setUsers(Collections.emptyList());
186+
187+
OperationResult<EntityCreationReport<Group>> result = groupsService.createGroup(group);
188+
assertThat(result.isSuccess()).isTrue();
189+
assertThat(result.getResult().isUpdate()).isTrue();
190+
assertThat(result.getResult().getEntity().getId()).isEqualTo("group1");
191+
assertThat(result.getResult().getEntity().getUsers()).isEmpty();
192+
assertThat(userRepositoryStub.findById("user1").get().getGroups()).isEmpty();
193+
assertThat(userRepositoryStub.findById("user2").get().getGroups()).isEmpty();
194+
}
195+
154196
@Test
155197
void GIVEN_A_Group_With_An_Invalid_Perimeter_WHEN_Create_Group_THEN_Return_BAD_REQUEST() {
156198
Set<String> perimeters = new HashSet<>();

0 commit comments

Comments
 (0)