mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-11-10 15:59:57 +00:00
Extend team to support defaultRoles (#2885)
* Extend team to support defaultRoles * Add support for team defaultRoles change description * Add tests
This commit is contained in:
parent
9e4d8d709d
commit
0791c8c266
@ -35,8 +35,8 @@ import org.openmetadata.catalog.util.EntityUtil;
|
|||||||
import org.openmetadata.catalog.util.EntityUtil.Fields;
|
import org.openmetadata.catalog.util.EntityUtil.Fields;
|
||||||
|
|
||||||
public class TeamRepository extends EntityRepository<Team> {
|
public class TeamRepository extends EntityRepository<Team> {
|
||||||
static final Fields TEAM_UPDATE_FIELDS = new Fields(TeamResource.FIELD_LIST, "profile,users");
|
static final Fields TEAM_UPDATE_FIELDS = new Fields(TeamResource.FIELD_LIST, "profile,users,defaultRoles");
|
||||||
static final Fields TEAM_PATCH_FIELDS = new Fields(TeamResource.FIELD_LIST, "profile,users");
|
static final Fields TEAM_PATCH_FIELDS = new Fields(TeamResource.FIELD_LIST, "profile,users,defaultRoles");
|
||||||
|
|
||||||
public TeamRepository(CollectionDAO dao) {
|
public TeamRepository(CollectionDAO dao) {
|
||||||
super(
|
super(
|
||||||
@ -52,23 +52,33 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
false);
|
false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<EntityReference> getUsers(List<UUID> userIds) {
|
public List<EntityReference> getEntityReferences(List<UUID> ids) {
|
||||||
if (userIds == null) {
|
if (ids == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List<EntityReference> users = new ArrayList<>();
|
List<EntityReference> entityReferences = new ArrayList<>();
|
||||||
for (UUID id : userIds) {
|
for (UUID id : ids) {
|
||||||
users.add(new EntityReference().withId(id));
|
entityReferences.add(new EntityReference().withId(id));
|
||||||
}
|
}
|
||||||
return users;
|
return entityReferences;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validateUsers(List<EntityReference> users) throws IOException {
|
public void validateEntityReferences(List<EntityReference> entityReferences, String entityType) throws IOException {
|
||||||
if (users != null) {
|
if (entityReferences != null) {
|
||||||
users.sort(EntityUtil.compareEntityReference);
|
entityReferences.sort(EntityUtil.compareEntityReference);
|
||||||
for (EntityReference user : users) {
|
for (EntityReference entityReference : entityReferences) {
|
||||||
EntityReference ref = daoCollection.userDAO().findEntityReferenceById(user.getId());
|
EntityReference ref;
|
||||||
user.withType(ref.getType()).withName(ref.getName()).withDisplayName(ref.getDisplayName());
|
switch (entityType) {
|
||||||
|
case Entity.USER:
|
||||||
|
ref = daoCollection.userDAO().findEntityReferenceById(entityReference.getId());
|
||||||
|
break;
|
||||||
|
case Entity.ROLE:
|
||||||
|
ref = daoCollection.roleDAO().findEntityReferenceById(entityReference.getId());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unsupported entity reference for validation");
|
||||||
|
}
|
||||||
|
entityReference.withType(ref.getType()).withName(ref.getName()).withDisplayName(ref.getDisplayName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,6 +90,7 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
}
|
}
|
||||||
team.setUsers(fields.contains("users") ? getUsers(team) : null);
|
team.setUsers(fields.contains("users") ? getUsers(team) : null);
|
||||||
team.setOwns(fields.contains("owns") ? getOwns(team) : null);
|
team.setOwns(fields.contains("owns") ? getOwns(team) : null);
|
||||||
|
team.setDefaultRoles(fields.contains("defaultRoles") ? getDefaultRoles(team) : null);
|
||||||
return team;
|
return team;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,21 +107,23 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepare(Team team) throws IOException {
|
public void prepare(Team team) throws IOException {
|
||||||
validateUsers(team.getUsers());
|
validateEntityReferences(team.getUsers(), Entity.USER);
|
||||||
|
validateEntityReferences(team.getDefaultRoles(), Entity.ROLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void storeEntity(Team team, boolean update) throws IOException {
|
public void storeEntity(Team team, boolean update) throws IOException {
|
||||||
// Relationships and fields such as href are derived and not stored as part of json
|
// Relationships and fields such as href are derived and not stored as part of json
|
||||||
List<EntityReference> users = team.getUsers();
|
List<EntityReference> users = team.getUsers();
|
||||||
|
List<EntityReference> defaultRoles = team.getDefaultRoles();
|
||||||
|
|
||||||
// Don't store users, href as JSON. Build it on the fly based on relationships
|
// Don't store users, defaultRoles, href as JSON. Build it on the fly based on relationships
|
||||||
team.withUsers(null).withHref(null);
|
team.withUsers(null).withDefaultRoles(null).withHref(null);
|
||||||
|
|
||||||
store(team.getId(), team, update);
|
store(team.getId(), team, update);
|
||||||
|
|
||||||
// Restore the relationships
|
// Restore the relationships
|
||||||
team.withUsers(users);
|
team.withUsers(users).withDefaultRoles(defaultRoles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -118,6 +131,9 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
for (EntityReference user : Optional.ofNullable(team.getUsers()).orElse(Collections.emptyList())) {
|
for (EntityReference user : Optional.ofNullable(team.getUsers()).orElse(Collections.emptyList())) {
|
||||||
addRelationship(team.getId(), user.getId(), Entity.TEAM, Entity.USER, Relationship.HAS);
|
addRelationship(team.getId(), user.getId(), Entity.TEAM, Entity.USER, Relationship.HAS);
|
||||||
}
|
}
|
||||||
|
for (EntityReference defaultRole : Optional.ofNullable(team.getDefaultRoles()).orElse(Collections.emptyList())) {
|
||||||
|
addRelationship(team.getId(), defaultRole.getId(), Entity.TEAM, Entity.ROLE, Relationship.HAS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -127,11 +143,7 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
|
|
||||||
private List<EntityReference> getUsers(Team team) throws IOException {
|
private List<EntityReference> getUsers(Team team) throws IOException {
|
||||||
List<String> userIds = findTo(team.getId(), Entity.TEAM, Relationship.HAS, Entity.USER, toBoolean(toInclude(team)));
|
List<String> userIds = findTo(team.getId(), Entity.TEAM, Relationship.HAS, Entity.USER, toBoolean(toInclude(team)));
|
||||||
List<EntityReference> users = new ArrayList<>();
|
return populateEntityReferences(userIds, Entity.USER);
|
||||||
for (String userId : userIds) {
|
|
||||||
users.add(daoCollection.userDAO().findEntityReferenceById(UUID.fromString(userId)));
|
|
||||||
}
|
|
||||||
return users;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<EntityReference> getOwns(Team team) throws IOException {
|
private List<EntityReference> getOwns(Team team) throws IOException {
|
||||||
@ -142,6 +154,29 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
.findTo(team.getId().toString(), Entity.TEAM, Relationship.OWNS.ordinal(), toBoolean(toInclude(team))));
|
.findTo(team.getId().toString(), Entity.TEAM, Relationship.OWNS.ordinal(), toBoolean(toInclude(team))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<EntityReference> getDefaultRoles(Team team) throws IOException {
|
||||||
|
List<String> defaultRoleIds =
|
||||||
|
findTo(team.getId(), Entity.TEAM, Relationship.HAS, Entity.ROLE, toBoolean(toInclude(team)));
|
||||||
|
return populateEntityReferences(defaultRoleIds, Entity.ROLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<EntityReference> populateEntityReferences(List<String> ids, String entityType) throws IOException {
|
||||||
|
List<EntityReference> refs = new ArrayList<>();
|
||||||
|
for (String id : ids) {
|
||||||
|
switch (entityType) {
|
||||||
|
case Entity.USER:
|
||||||
|
refs.add(daoCollection.userDAO().findEntityReferenceById(UUID.fromString(id)));
|
||||||
|
break;
|
||||||
|
case Entity.ROLE:
|
||||||
|
refs.add(daoCollection.roleDAO().findEntityReferenceById(UUID.fromString(id)));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unsupported entity type for populating entityReference list");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return refs;
|
||||||
|
}
|
||||||
|
|
||||||
public static class TeamEntityInterface implements EntityInterface<Team> {
|
public static class TeamEntityInterface implements EntityInterface<Team> {
|
||||||
private final Team entity;
|
private final Team entity;
|
||||||
|
|
||||||
@ -273,27 +308,56 @@ public class TeamRepository extends EntityRepository<Team> {
|
|||||||
public void entitySpecificUpdate() throws IOException {
|
public void entitySpecificUpdate() throws IOException {
|
||||||
recordChange("profile", original.getEntity().getProfile(), updated.getEntity().getProfile());
|
recordChange("profile", original.getEntity().getProfile(), updated.getEntity().getProfile());
|
||||||
updateUsers(original.getEntity(), updated.getEntity());
|
updateUsers(original.getEntity(), updated.getEntity());
|
||||||
|
updateDefaultRoles(original.getEntity(), updated.getEntity());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUsers(Team origTeam, Team updatedTeam) throws JsonProcessingException {
|
private void updateUsers(Team origTeam, Team updatedTeam) throws JsonProcessingException {
|
||||||
List<EntityReference> origUsers = Optional.ofNullable(origTeam.getUsers()).orElse(Collections.emptyList());
|
List<EntityReference> origUsers = Optional.ofNullable(origTeam.getUsers()).orElse(Collections.emptyList());
|
||||||
List<EntityReference> updatedUsers = Optional.ofNullable(updatedTeam.getUsers()).orElse(Collections.emptyList());
|
List<EntityReference> updatedUsers = Optional.ofNullable(updatedTeam.getUsers()).orElse(Collections.emptyList());
|
||||||
|
updateEntityRelationships(
|
||||||
|
"users", origTeam.getId(), updatedTeam.getId(), Relationship.HAS, Entity.USER, origUsers, updatedUsers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateDefaultRoles(Team origTeam, Team updatedTeam) throws JsonProcessingException {
|
||||||
|
List<EntityReference> origDefaultRoles =
|
||||||
|
Optional.ofNullable(origTeam.getDefaultRoles()).orElse(Collections.emptyList());
|
||||||
|
List<EntityReference> updatedDefaultRoles =
|
||||||
|
Optional.ofNullable(updatedTeam.getDefaultRoles()).orElse(Collections.emptyList());
|
||||||
|
updateEntityRelationships(
|
||||||
|
"defaultRoles",
|
||||||
|
origTeam.getId(),
|
||||||
|
updatedTeam.getId(),
|
||||||
|
Relationship.HAS,
|
||||||
|
Entity.ROLE,
|
||||||
|
origDefaultRoles,
|
||||||
|
updatedDefaultRoles);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateEntityRelationships(
|
||||||
|
String field,
|
||||||
|
UUID origId,
|
||||||
|
UUID updatedId,
|
||||||
|
Relationship relationshipType,
|
||||||
|
String toEntityType,
|
||||||
|
List<EntityReference> origRefs,
|
||||||
|
List<EntityReference> updatedRefs)
|
||||||
|
throws JsonProcessingException {
|
||||||
List<EntityReference> added = new ArrayList<>();
|
List<EntityReference> added = new ArrayList<>();
|
||||||
List<EntityReference> deleted = new ArrayList<>();
|
List<EntityReference> deleted = new ArrayList<>();
|
||||||
if (recordListChange("users", origUsers, updatedUsers, added, deleted, entityReferenceMatch)) {
|
if (!recordListChange(field, origRefs, updatedRefs, added, deleted, entityReferenceMatch)) {
|
||||||
// Remove users from original and add users from updated
|
// No changes between original and updated.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Remove relationships from original
|
||||||
daoCollection
|
daoCollection
|
||||||
.relationshipDAO()
|
.relationshipDAO()
|
||||||
.deleteFrom(origTeam.getId().toString(), Entity.TEAM, Relationship.HAS.ordinal(), "user");
|
.deleteFrom(origId.toString(), Entity.TEAM, relationshipType.ordinal(), toEntityType);
|
||||||
// Add relationships
|
// Add relationships from updated
|
||||||
for (EntityReference user : updatedUsers) {
|
for (EntityReference ref : updatedRefs) {
|
||||||
addRelationship(updatedTeam.getId(), user.getId(), Entity.TEAM, Entity.USER, Relationship.HAS);
|
addRelationship(updatedId, ref.getId(), Entity.TEAM, toEntityType, relationshipType);
|
||||||
}
|
|
||||||
|
|
||||||
updatedUsers.sort(EntityUtil.compareEntityReference);
|
|
||||||
origUsers.sort(EntityUtil.compareEntityReference);
|
|
||||||
}
|
}
|
||||||
|
updatedRefs.sort(EntityUtil.compareEntityReference);
|
||||||
|
origRefs.sort(EntityUtil.compareEntityReference);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,6 +78,7 @@ public class TeamResource {
|
|||||||
|
|
||||||
public static Team addHref(UriInfo uriInfo, Team team) {
|
public static Team addHref(UriInfo uriInfo, Team team) {
|
||||||
Entity.withHref(uriInfo, team.getUsers());
|
Entity.withHref(uriInfo, team.getUsers());
|
||||||
|
Entity.withHref(uriInfo, team.getDefaultRoles());
|
||||||
Entity.withHref(uriInfo, team.getOwns());
|
Entity.withHref(uriInfo, team.getOwns());
|
||||||
return team;
|
return team;
|
||||||
}
|
}
|
||||||
@ -98,7 +99,7 @@ public class TeamResource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static final String FIELDS = "profile,users,owns";
|
protected static final String FIELDS = "profile,users,owns,defaultRoles";
|
||||||
public static final List<String> FIELD_LIST = Arrays.asList(FIELDS.replace(" ", "").split(","));
|
public static final List<String> FIELD_LIST = Arrays.asList(FIELDS.replace(" ", "").split(","));
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@ -370,6 +371,7 @@ public class TeamResource {
|
|||||||
.withProfile(ct.getProfile())
|
.withProfile(ct.getProfile())
|
||||||
.withUpdatedBy(securityContext.getUserPrincipal().getName())
|
.withUpdatedBy(securityContext.getUserPrincipal().getName())
|
||||||
.withUpdatedAt(System.currentTimeMillis())
|
.withUpdatedAt(System.currentTimeMillis())
|
||||||
.withUsers(dao.getUsers(ct.getUsers()));
|
.withUsers(dao.getEntityReferences(ct.getUsers()))
|
||||||
|
.withDefaultRoles(dao.getEntityReferences(ct.getDefaultRoles()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,25 +4,32 @@
|
|||||||
"title": "CreateTeamRequest",
|
"title": "CreateTeamRequest",
|
||||||
"description": "Team entity",
|
"description": "Team entity",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|
||||||
"properties": {
|
"properties": {
|
||||||
"name": {
|
"name": {
|
||||||
"$ref": "../../entity/teams/team.json#/definitions/teamName"
|
"$ref": "../../entity/teams/team.json#/definitions/teamName"
|
||||||
},
|
},
|
||||||
"displayName": {
|
"displayName": {
|
||||||
"description": "Optional name used for display purposes. Example 'Marketing Team'",
|
"description": "Optional name used for display purposes. Example 'Marketing Team'.",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"description": "Optional description of the team",
|
"description": "Optional description of the team.",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"profile": {
|
"profile": {
|
||||||
"description": "Optional team profile information",
|
"description": "Optional team profile information.",
|
||||||
"$ref": "../../type/profile.json"
|
"$ref": "../../type/profile.json"
|
||||||
},
|
},
|
||||||
"users": {
|
"users": {
|
||||||
"description": "Optional IDs of users that are part of the team",
|
"description": "Optional IDs of users that are part of the team.",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "../../type/basic.json#/definitions/uuid"
|
||||||
|
},
|
||||||
|
"default": null
|
||||||
|
},
|
||||||
|
"defaultRoles": {
|
||||||
|
"description": "Roles to be assigned to all users that are part of this team.",
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
"$ref": "../../type/basic.json#/definitions/uuid"
|
"$ref": "../../type/basic.json#/definitions/uuid"
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
"title": "Team",
|
"title": "Team",
|
||||||
"description": "This schema defines the Team entity. A Team is a group of zero or more users. Teams can own zero or more data assets.",
|
"description": "This schema defines the Team entity. A Team is a group of zero or more users. Teams can own zero or more data assets.",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|
||||||
"definitions": {
|
"definitions": {
|
||||||
"teamName": {
|
"teamName": {
|
||||||
"description": "A unique name of the team typically the team ID from an identity provider. Example - group Id from LDAP.",
|
"description": "A unique name of the team typically the team ID from an identity provider. Example - group Id from LDAP.",
|
||||||
@ -13,7 +12,6 @@
|
|||||||
"maxLength": 128
|
"maxLength": 128
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
"properties": {
|
"properties": {
|
||||||
"id": {
|
"id": {
|
||||||
"$ref": "../../type/basic.json#/definitions/uuid"
|
"$ref": "../../type/basic.json#/definitions/uuid"
|
||||||
@ -66,6 +64,10 @@
|
|||||||
"description": "When `true` indicates the entity has been soft deleted.",
|
"description": "When `true` indicates the entity has been soft deleted.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false
|
"default": false
|
||||||
|
},
|
||||||
|
"defaultRoles": {
|
||||||
|
"description": "Roles to be assigned to all users that are part of this team.",
|
||||||
|
"$ref": "../../type/entityReference.json#/definitions/entityReferenceList"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": ["id", "name", "href"],
|
"required": ["id", "name", "href"],
|
||||||
|
|||||||
@ -36,12 +36,14 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.client.HttpResponseException;
|
import org.apache.http.client.HttpResponseException;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInfo;
|
import org.junit.jupiter.api.TestInfo;
|
||||||
import org.openmetadata.catalog.Entity;
|
import org.openmetadata.catalog.Entity;
|
||||||
import org.openmetadata.catalog.api.teams.CreateTeam;
|
import org.openmetadata.catalog.api.teams.CreateTeam;
|
||||||
|
import org.openmetadata.catalog.entity.teams.Role;
|
||||||
import org.openmetadata.catalog.entity.teams.Team;
|
import org.openmetadata.catalog.entity.teams.Team;
|
||||||
import org.openmetadata.catalog.entity.teams.User;
|
import org.openmetadata.catalog.entity.teams.User;
|
||||||
import org.openmetadata.catalog.exception.CatalogExceptionMessage;
|
import org.openmetadata.catalog.exception.CatalogExceptionMessage;
|
||||||
@ -55,7 +57,6 @@ import org.openmetadata.catalog.type.FieldChange;
|
|||||||
import org.openmetadata.catalog.type.ImageList;
|
import org.openmetadata.catalog.type.ImageList;
|
||||||
import org.openmetadata.catalog.type.Profile;
|
import org.openmetadata.catalog.type.Profile;
|
||||||
import org.openmetadata.catalog.util.EntityInterface;
|
import org.openmetadata.catalog.util.EntityInterface;
|
||||||
import org.openmetadata.catalog.util.EntityUtil;
|
|
||||||
import org.openmetadata.catalog.util.JsonUtils;
|
import org.openmetadata.catalog.util.JsonUtils;
|
||||||
import org.openmetadata.catalog.util.TestUtils;
|
import org.openmetadata.catalog.util.TestUtils;
|
||||||
import org.openmetadata.catalog.util.TestUtils.UpdateType;
|
import org.openmetadata.catalog.util.TestUtils.UpdateType;
|
||||||
@ -88,21 +89,28 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void post_teamWithUsers_200_OK(TestInfo test) throws IOException {
|
void post_teamWithUsersAndDefaultRoles_200_OK(TestInfo test) throws IOException {
|
||||||
// Add team to user relationships while creating a team
|
// Add team to user relationships while creating a team
|
||||||
UserResourceTest userResourceTest = new UserResourceTest();
|
UserResourceTest userResourceTest = new UserResourceTest();
|
||||||
User user1 = userResourceTest.createEntity(userResourceTest.createRequest(test, 1), TEST_AUTH_HEADERS);
|
User user1 = userResourceTest.createEntity(userResourceTest.createRequest(test, 1), TEST_AUTH_HEADERS);
|
||||||
User user2 = userResourceTest.createEntity(userResourceTest.createRequest(test, 2), TEST_AUTH_HEADERS);
|
User user2 = userResourceTest.createEntity(userResourceTest.createRequest(test, 2), TEST_AUTH_HEADERS);
|
||||||
List<UUID> users = Arrays.asList(user1.getId(), user2.getId());
|
List<UUID> users = Arrays.asList(user1.getId(), user2.getId());
|
||||||
|
|
||||||
|
RoleResourceTest roleResourceTest = new RoleResourceTest();
|
||||||
|
Role role1 = roleResourceTest.createEntity(roleResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
|
||||||
|
Role role2 = roleResourceTest.createEntity(roleResourceTest.createRequest(test, 2), ADMIN_AUTH_HEADERS);
|
||||||
|
List<UUID> roles = Arrays.asList(role1.getId(), role2.getId());
|
||||||
|
|
||||||
CreateTeam create =
|
CreateTeam create =
|
||||||
createRequest(test)
|
createRequest(test)
|
||||||
.withDisplayName("displayName")
|
.withDisplayName("displayName")
|
||||||
.withDescription("description")
|
.withDescription("description")
|
||||||
.withProfile(PROFILE)
|
.withProfile(PROFILE)
|
||||||
.withUsers(users);
|
.withUsers(users)
|
||||||
|
.withDefaultRoles(roles);
|
||||||
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
|
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
|
||||||
|
|
||||||
// Make sure the user entity has relationship to the team
|
// Ensure that the user entity has relationship to the team
|
||||||
user1 = userResourceTest.getEntity(user1.getId(), "teams", TEST_AUTH_HEADERS);
|
user1 = userResourceTest.getEntity(user1.getId(), "teams", TEST_AUTH_HEADERS);
|
||||||
assertEquals(team.getId(), user1.getTeams().get(0).getId());
|
assertEquals(team.getId(), user1.getTeams().get(0).getId());
|
||||||
user2 = userResourceTest.getEntity(user2.getId(), "teams", TEST_AUTH_HEADERS);
|
user2 = userResourceTest.getEntity(user2.getId(), "teams", TEST_AUTH_HEADERS);
|
||||||
@ -135,15 +143,26 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
UserResourceTest userResourceTest = new UserResourceTest();
|
UserResourceTest userResourceTest = new UserResourceTest();
|
||||||
User user1 = userResourceTest.createEntity(userResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
|
User user1 = userResourceTest.createEntity(userResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
|
||||||
List<UUID> users = Collections.singletonList(user1.getId());
|
List<UUID> users = Collections.singletonList(user1.getId());
|
||||||
CreateTeam create = createRequest(test).withUsers(users);
|
|
||||||
|
RoleResourceTest roleResourceTest = new RoleResourceTest();
|
||||||
|
Role role1 = roleResourceTest.createEntity(roleResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
|
||||||
|
List<UUID> roles = Collections.singletonList(role1.getId());
|
||||||
|
|
||||||
|
CreateTeam create = createRequest(test).withUsers(users).withDefaultRoles(roles);
|
||||||
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
|
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
|
||||||
|
|
||||||
// Team with users can be deleted - Team -- has --> User relationships are deleted
|
// Team with users and defaultRoles can be deleted
|
||||||
|
// Team -- has --> User relationships are deleted
|
||||||
|
// Team -- has --> Role relationships are deleted
|
||||||
deleteAndCheckEntity(team, ADMIN_AUTH_HEADERS);
|
deleteAndCheckEntity(team, ADMIN_AUTH_HEADERS);
|
||||||
|
|
||||||
// Make sure user does not have relationship to this team
|
// Ensure that the user does not have relationship to this team
|
||||||
User user = userResourceTest.getEntity(user1.getId(), "teams", ADMIN_AUTH_HEADERS);
|
User user = userResourceTest.getEntity(user1.getId(), "teams", ADMIN_AUTH_HEADERS);
|
||||||
assertTrue(user.getTeams().isEmpty());
|
assertTrue(user.getTeams().isEmpty());
|
||||||
|
|
||||||
|
// Ensure that the role is not deleted
|
||||||
|
Role role = roleResourceTest.getEntity(role1.getId(), "", ADMIN_AUTH_HEADERS);
|
||||||
|
assertNotNull(role);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -160,7 +179,7 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void patch_deleteUserFromTeam_200(TestInfo test) throws IOException {
|
void patch_deleteUserAndDefaultRoleFromTeam_200(TestInfo test) throws IOException {
|
||||||
UserResourceTest userResourceTest = new UserResourceTest();
|
UserResourceTest userResourceTest = new UserResourceTest();
|
||||||
final int totalUsers = 20;
|
final int totalUsers = 20;
|
||||||
ArrayList<UUID> users = new ArrayList<>();
|
ArrayList<UUID> users = new ArrayList<>();
|
||||||
@ -168,17 +187,35 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
User user = userResourceTest.createEntity(userResourceTest.createRequest(test, i), ADMIN_AUTH_HEADERS);
|
User user = userResourceTest.createEntity(userResourceTest.createRequest(test, i), ADMIN_AUTH_HEADERS);
|
||||||
users.add(user.getId());
|
users.add(user.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RoleResourceTest roleResourceTest = new RoleResourceTest();
|
||||||
|
roleResourceTest.createRolesAndSetDefault(test, 5, 0);
|
||||||
|
List<Role> roles = roleResourceTest.listEntities(Map.of(), ADMIN_AUTH_HEADERS).getData();
|
||||||
|
List<UUID> rolesIds = roles.stream().map(Role::getId).collect(Collectors.toList());
|
||||||
|
|
||||||
CreateTeam create =
|
CreateTeam create =
|
||||||
createRequest(getEntityName(test), "description", "displayName", null).withProfile(PROFILE).withUsers(users);
|
createRequest(getEntityName(test), "description", "displayName", null)
|
||||||
|
.withProfile(PROFILE)
|
||||||
|
.withUsers(users)
|
||||||
|
.withDefaultRoles(rolesIds);
|
||||||
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
|
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
|
||||||
|
|
||||||
// Remove a user from the team list using patch request
|
// Remove a user from the team using patch request
|
||||||
String json = JsonUtils.pojoToJson(team);
|
String json = JsonUtils.pojoToJson(team);
|
||||||
int removeUserIndex = new Random().nextInt(totalUsers);
|
int removeUserIndex = new Random().nextInt(totalUsers);
|
||||||
EntityReference deletedUser = team.getUsers().get(removeUserIndex).withHref(null);
|
EntityReference deletedUser = team.getUsers().get(removeUserIndex).withHref(null);
|
||||||
team.getUsers().remove(removeUserIndex);
|
team.getUsers().remove(removeUserIndex);
|
||||||
ChangeDescription change = getChangeDescription(team.getVersion());
|
ChangeDescription change = getChangeDescription(team.getVersion());
|
||||||
change.getFieldsDeleted().add(new FieldChange().withName("users").withOldValue(Arrays.asList(deletedUser)));
|
change.getFieldsDeleted().add(new FieldChange().withName("users").withOldValue(Arrays.asList(deletedUser)));
|
||||||
|
team = patchEntityAndCheck(team, json, ADMIN_AUTH_HEADERS, UpdateType.MINOR_UPDATE, change);
|
||||||
|
|
||||||
|
// Remove a default role from the team using patch request
|
||||||
|
json = JsonUtils.pojoToJson(team);
|
||||||
|
int removeDefaultRoleIndex = new Random().nextInt(roles.size());
|
||||||
|
EntityReference deletedRole = team.getDefaultRoles().get(removeDefaultRoleIndex).withHref(null);
|
||||||
|
team.getDefaultRoles().remove(removeDefaultRoleIndex);
|
||||||
|
change = getChangeDescription(team.getVersion());
|
||||||
|
change.getFieldsDeleted().add(new FieldChange().withName("defaultRoles").withOldValue(Arrays.asList(deletedRole)));
|
||||||
patchEntityAndCheck(team, json, ADMIN_AUTH_HEADERS, UpdateType.MINOR_UPDATE, change);
|
patchEntityAndCheck(team, json, ADMIN_AUTH_HEADERS, UpdateType.MINOR_UPDATE, change);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,6 +225,7 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
String expectedDisplayName,
|
String expectedDisplayName,
|
||||||
Profile expectedProfile,
|
Profile expectedProfile,
|
||||||
List<EntityReference> expectedUsers,
|
List<EntityReference> expectedUsers,
|
||||||
|
List<EntityReference> expectedDefaultRoles,
|
||||||
String expectedUpdatedBy) {
|
String expectedUpdatedBy) {
|
||||||
assertListNotNull(team.getId(), team.getHref());
|
assertListNotNull(team.getId(), team.getHref());
|
||||||
assertEquals(expectedDescription, team.getDescription());
|
assertEquals(expectedDescription, team.getDescription());
|
||||||
@ -195,6 +233,7 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
assertEquals(expectedDisplayName, team.getDisplayName());
|
assertEquals(expectedDisplayName, team.getDisplayName());
|
||||||
assertEquals(expectedProfile, team.getProfile());
|
assertEquals(expectedProfile, team.getProfile());
|
||||||
TestUtils.assertEntityReferenceList(expectedUsers, team.getUsers());
|
TestUtils.assertEntityReferenceList(expectedUsers, team.getUsers());
|
||||||
|
TestUtils.assertEntityReferenceList(expectedDefaultRoles, team.getDefaultRoles());
|
||||||
TestUtils.validateEntityReference(team.getOwns());
|
TestUtils.validateEntityReference(team.getOwns());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,18 +253,20 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
expectedTeam.getDisplayName(),
|
expectedTeam.getDisplayName(),
|
||||||
expectedTeam.getProfile(),
|
expectedTeam.getProfile(),
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
updatedBy);
|
updatedBy);
|
||||||
assertNull(getTeam.getOwns());
|
assertNull(getTeam.getOwns());
|
||||||
|
|
||||||
// .../teams?fields=users,owns
|
// .../teams?fields=users,owns,profile,defaultRoles
|
||||||
fields = "users,owns,profile";
|
fields = "users,owns,profile,defaultRoles";
|
||||||
getTeam =
|
getTeam =
|
||||||
byName
|
byName
|
||||||
? getEntityByName(expectedTeam.getName(), fields, ADMIN_AUTH_HEADERS)
|
? getEntityByName(expectedTeam.getName(), fields, ADMIN_AUTH_HEADERS)
|
||||||
: getEntity(expectedTeam.getId(), fields, ADMIN_AUTH_HEADERS);
|
: getEntity(expectedTeam.getId(), fields, ADMIN_AUTH_HEADERS);
|
||||||
assertNotNull(getTeam.getProfile());
|
assertNotNull(getTeam.getProfile());
|
||||||
validateEntityReference(getTeam.getUsers());
|
|
||||||
validateEntityReference(getTeam.getOwns());
|
validateEntityReference(getTeam.getOwns());
|
||||||
|
validateEntityReference(getTeam.getUsers());
|
||||||
|
validateEntityReference(getTeam.getDefaultRoles());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -252,14 +293,21 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
getEntityInterface(team), createRequest.getDescription(), TestUtils.getPrincipal(authHeaders), null);
|
getEntityInterface(team), createRequest.getDescription(), TestUtils.getPrincipal(authHeaders), null);
|
||||||
|
|
||||||
assertEquals(createRequest.getProfile(), team.getProfile());
|
assertEquals(createRequest.getProfile(), team.getProfile());
|
||||||
|
TestUtils.validateEntityReference(team.getOwns());
|
||||||
|
|
||||||
List<EntityReference> expectedUsers = new ArrayList<>();
|
List<EntityReference> expectedUsers = new ArrayList<>();
|
||||||
for (UUID teamId : Optional.ofNullable(createRequest.getUsers()).orElse(Collections.emptyList())) {
|
for (UUID userId : Optional.ofNullable(createRequest.getUsers()).orElse(Collections.emptyList())) {
|
||||||
expectedUsers.add(new EntityReference().withId(teamId).withType(Entity.USER));
|
expectedUsers.add(new EntityReference().withId(userId).withType(Entity.USER));
|
||||||
}
|
}
|
||||||
expectedUsers = expectedUsers.isEmpty() ? null : expectedUsers;
|
expectedUsers = expectedUsers.isEmpty() ? null : expectedUsers;
|
||||||
TestUtils.assertEntityReferenceList(expectedUsers, team.getUsers());
|
TestUtils.assertEntityReferenceList(expectedUsers, team.getUsers());
|
||||||
TestUtils.validateEntityReference(team.getOwns());
|
|
||||||
|
List<EntityReference> expectedDefaultRoles = new ArrayList<>();
|
||||||
|
for (UUID roleId : Optional.ofNullable(createRequest.getDefaultRoles()).orElse(Collections.emptyList())) {
|
||||||
|
expectedDefaultRoles.add(new EntityReference().withId(roleId).withType(Entity.ROLE));
|
||||||
|
}
|
||||||
|
expectedDefaultRoles = expectedDefaultRoles.isEmpty() ? null : expectedDefaultRoles;
|
||||||
|
TestUtils.assertEntityReferenceList(expectedDefaultRoles, team.getDefaultRoles());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -287,15 +335,17 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
|
|
||||||
assertEquals(expected.getDisplayName(), updated.getDisplayName());
|
assertEquals(expected.getDisplayName(), updated.getDisplayName());
|
||||||
assertEquals(expected.getProfile(), updated.getProfile());
|
assertEquals(expected.getProfile(), updated.getProfile());
|
||||||
|
TestUtils.validateEntityReference(updated.getOwns());
|
||||||
|
|
||||||
List<EntityReference> expectedUsers = Optional.ofNullable(expected.getUsers()).orElse(Collections.emptyList());
|
List<EntityReference> expectedUsers = Optional.ofNullable(expected.getUsers()).orElse(Collections.emptyList());
|
||||||
List<EntityReference> actualUsers = Optional.ofNullable(updated.getUsers()).orElse(Collections.emptyList());
|
List<EntityReference> actualUsers = Optional.ofNullable(updated.getUsers()).orElse(Collections.emptyList());
|
||||||
actualUsers.forEach(TestUtils::validateEntityReference);
|
TestUtils.assertEntityReferenceList(expectedUsers, actualUsers);
|
||||||
|
|
||||||
actualUsers.sort(EntityUtil.compareEntityReference);
|
List<EntityReference> expectedDefaultRoles =
|
||||||
expectedUsers.sort(EntityUtil.compareEntityReference);
|
Optional.ofNullable(expected.getDefaultRoles()).orElse(Collections.emptyList());
|
||||||
assertEquals(expectedUsers, actualUsers);
|
List<EntityReference> actualDefaultRoles =
|
||||||
TestUtils.validateEntityReference(updated.getOwns());
|
Optional.ofNullable(updated.getDefaultRoles()).orElse(Collections.emptyList());
|
||||||
|
TestUtils.assertEntityReferenceList(expectedDefaultRoles, actualDefaultRoles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -308,11 +358,11 @@ public class TeamResourceTest extends EntityResourceTest<Team, CreateTeam> {
|
|||||||
if (expected == actual) {
|
if (expected == actual) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (fieldName.equals("users")) {
|
if (fieldName.equals("users") || fieldName.equals("defaultRoles")) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<EntityReference> expectedUsers = (List<EntityReference>) expected;
|
List<EntityReference> expectedRefs = (List<EntityReference>) expected;
|
||||||
List<EntityReference> actualUsers = JsonUtils.readObjects(actual.toString(), EntityReference.class);
|
List<EntityReference> actualRefs = JsonUtils.readObjects(actual.toString(), EntityReference.class);
|
||||||
assertEntityReferencesFieldChange(expectedUsers, actualUsers);
|
assertEntityReferencesFieldChange(expectedRefs, actualRefs);
|
||||||
} else {
|
} else {
|
||||||
assertCommonFieldChange(fieldName, expected, actual);
|
assertCommonFieldChange(fieldName, expected, actual);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,5 +7,5 @@ Provides metadata version information.
|
|||||||
|
|
||||||
from incremental import Version
|
from incremental import Version
|
||||||
|
|
||||||
__version__ = Version("metadata", 0, 9, 0, dev=11)
|
__version__ = Version("metadata", 0, 9, 0, dev=12)
|
||||||
__all__ = ["__version__"]
|
__all__ = ["__version__"]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user