Add party id to organization (#36)

* Add party id to organization

* create party with partyId/orgId

* add constraint to party field

* add unique partyId

Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
Gregory 2022-09-08 16:02:59 +03:00 committed by GitHub
parent ae9bd5093c
commit 79ed676b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 66 additions and 69 deletions

View File

@ -1,7 +1,10 @@
package dev.vality.orgmanager.converter;
import dev.vality.bouncer.base.Entity;
import dev.vality.bouncer.context.v1.*;
import dev.vality.bouncer.context.v1.OrgRole;
import dev.vality.bouncer.context.v1.OrgRoleScope;
import dev.vality.bouncer.context.v1.Organization;
import dev.vality.bouncer.context.v1.User;
import dev.vality.orgmanager.entity.MemberEntity;
import dev.vality.orgmanager.entity.MemberRoleEntity;
import dev.vality.orgmanager.entity.OrganizationEntity;
@ -34,7 +37,7 @@ public class BouncerContextConverter {
return new Organization()
.setId(entity.getId())
.setOwner(new Entity().setId(entity.getOwner()))
.setParty(new Entity().setId(entity.getId()))
.setParty(new Entity().setId(entity.getParty()))
.setRoles(CollectionUtils.isEmpty(roles) ? null :
roles.stream()
.filter(memberRoleEntity -> memberRoleEntity.getOrganizationId().equals(entity.getId()))

View File

@ -18,11 +18,14 @@ public class OrganizationConverter {
private final JsonMapper jsonMapper;
public OrganizationEntity toEntity(Organization organization, String ownerId) {
String orgId = UUID.randomUUID().toString();
String partyId = UUID.randomUUID().toString();
return OrganizationEntity.builder()
.id(UUID.randomUUID().toString())
.id(orgId)
.createdAt(LocalDateTime.now())
.name(organization.getName())
.owner(ownerId)
.party(partyId)
.metadata(jsonMapper.toJson(organization.getMetadata()))
.build();
}
@ -33,6 +36,7 @@ public class OrganizationConverter {
.createdAt(OffsetDateTime.of(entity.getCreatedAt(), ZoneOffset.UTC))
.name(entity.getName())
.owner(entity.getOwner())
.party(entity.getParty())
.metadata(entity.getMetadata() != null ? jsonMapper.toMap(entity.getMetadata()) : null);
}

View File

@ -5,7 +5,6 @@ import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashSet;
@ -41,5 +40,6 @@ public class OrganizationEntity implements Serializable {
private LocalDateTime createdAt;
private String name;
private String owner;
private String party;
private String metadata;
}

View File

@ -56,13 +56,8 @@ public class OrganizationService {
String keycloakUserId = token.getSubject();
OrganizationEntity entity = organizationConverter.toEntity(organization, keycloakUserId);
OrganizationEntity savedEntity = organizationRepository.save(entity);
// TODO [v.hramov]: when org-manager will be fully operational party_id != keycloak_user_id
// most likely we will use organization_id as party_id
partyManagementService.createParty(keycloakUserId, keycloakUserId, token.getEmail());
Organization savedOrganization = organizationConverter.toDomain(savedEntity);
savedOrganization.setParty(keycloakUserId);
return savedOrganization;
partyManagementService.createParty(savedEntity.getParty(), keycloakUserId, token.getEmail());
return organizationConverter.toDomain(savedEntity);
}
@Transactional

View File

@ -0,0 +1,3 @@
-- organization
ALTER TABLE org_manager.organization
ADD COLUMN party CHARACTER VARYING NOT NULL DEFAULT 'default';

View File

@ -1,16 +1,13 @@
package dev.vality.orgmanager;
import dev.vality.bouncer.base.Entity;
import dev.vality.bouncer.context.v1.OrgRole;
import dev.vality.bouncer.context.v1.OrgRoleScope;
import dev.vality.bouncer.context.v1.Organization;
import dev.vality.bouncer.context.v1.*;
import dev.vality.orgmanager.entity.InvitationEntity;
import dev.vality.orgmanager.entity.MemberEntity;
import dev.vality.orgmanager.entity.MemberRoleEntity;
import dev.vality.orgmanager.entity.OrganizationEntity;
import dev.vality.orgmanager.entity.OrganizationRoleEntity;
import dev.vality.bouncer.context.v1.User;
import dev.vality.orgmanager.entity.*;
import dev.vality.orgmanager.service.dto.BouncerContextDto;
import dev.vality.orgmanager.service.dto.RoleDto;
import dev.vality.swag.organizations.model.Invitee;
import dev.vality.swag.organizations.model.*;
import org.keycloak.representations.AccessToken;
@ -119,6 +116,8 @@ public abstract class TestObjectFactory {
entity.setCreatedAt(LocalDateTime.now());
entity.setName(randomString());
entity.setOwner(randomString());
entity.setParty(randomString());
entity.setMetadata("{\"a\":\"b\"}");
return entity;
}
@ -128,6 +127,7 @@ public abstract class TestObjectFactory {
.createdAt(LocalDateTime.now())
.name(randomString())
.owner(randomString())
.party(randomString())
.members(Set.of(memberEntity))
.build();
}
@ -138,6 +138,7 @@ public abstract class TestObjectFactory {
.createdAt(LocalDateTime.now())
.name(randomString())
.owner(randomString())
.party(randomString())
.members(entities)
.build();
}

View File

@ -80,7 +80,7 @@ class BouncerContextConverterTest {
assertEquals(organizationEntity.getId(), organization.getId());
assertEquals(organizationEntity.getOwner(), organization.getOwner().getId());
assertEquals(organizationEntity.getId(), organization.getParty().getId());
assertEquals(organizationEntity.getParty(), organization.getParty().getId());
assertTrue(organization.getRoles().isEmpty());
}
@ -94,7 +94,7 @@ class BouncerContextConverterTest {
assertEquals(organizationEntity.getId(), organization.getId());
assertEquals(organizationEntity.getOwner(), organization.getOwner().getId());
assertEquals(organizationEntity.getId(), organization.getParty().getId());
assertEquals(organizationEntity.getParty(), organization.getParty().getId());
assertEquals(memberRoleEntity.getRoleId(), organization.getRoles().iterator().next().getId());
}
}

View File

@ -17,7 +17,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MemberConverterTest {
class MemberConverterTest {
private MemberConverter converter;
@ -46,7 +46,8 @@ public class MemberConverterTest {
.userEmail("email")
.roles(List.of(new MemberRole()));
assertThat(member).isEqualToComparingFieldByField(expected);
assertThat(member).usingRecursiveComparison()
.isEqualTo(expected);
}
private MemberEntity buildMemberEntity() {

View File

@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MemberRoleConverterTest {
class MemberRoleConverterTest {
private MemberRoleConverter converter;
@ -36,7 +36,9 @@ public class MemberRoleConverterTest {
.build();
assertThat(entity.getId()).isNotEmpty();
assertThat(entity).isEqualToIgnoringNullFields(expected);
assertThat(entity).usingRecursiveComparison()
.ignoringExpectedNullFields()
.isEqualTo(expected);
}
@Test
@ -61,7 +63,10 @@ public class MemberRoleConverterTest {
.build();
assertThat(entity.getId()).isNotEmpty();
assertThat(entity).isEqualToIgnoringNullFields(expected);
assertThat(entity)
.usingRecursiveComparison()
.ignoringExpectedNullFields()
.isEqualTo(expected);
}
@Test
@ -81,7 +86,7 @@ public class MemberRoleConverterTest {
.id(entity.getId())
.roleId(RoleId.ADMINISTRATOR);
assertThat(role).isEqualToComparingFieldByField(expected);
assertThat(role).usingRecursiveComparison().isEqualTo(expected);
}
@Test
@ -106,6 +111,6 @@ public class MemberRoleConverterTest {
.resourceId("resource")
.id(ResourceScopeId.SHOP));
assertThat(role).isEqualToComparingFieldByField(expected);
assertThat(role).usingRecursiveComparison().isEqualTo(expected);
}
}

View File

@ -1,21 +1,19 @@
package dev.vality.orgmanager.converter;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.vality.orgmanager.TestObjectFactory;
import dev.vality.orgmanager.entity.OrganizationEntity;
import dev.vality.orgmanager.entity.OrganizationRoleEntity;
import dev.vality.orgmanager.util.JsonMapper;
import dev.vality.swag.organizations.model.Organization;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class OrganizationConverterTest {
class OrganizationConverterTest {
private OrganizationConverter converter;
@ -30,51 +28,36 @@ public class OrganizationConverterTest {
void shouldConvertToEntity() {
// Given
Organization organization = new Organization()
.name("org")
.name(TestObjectFactory.randomString())
.metadata(Map.of("a", "b"));
// When
OrganizationEntity entity = converter.toEntity(organization, "testOwnerId");
String testOwnerId = "testOwnerId";
OrganizationEntity entity = converter.toEntity(organization, testOwnerId);
// Then
OrganizationEntity expected = OrganizationEntity.builder()
.name("org")
.owner("testOwnerId")
.metadata("{\"a\":\"b\"}")
.build();
assertThat(entity.getId()).isNotEmpty();
assertThat(entity.getParty()).isNotEmpty();
assertThat(entity.getCreatedAt()).isNotNull();
assertThat(entity).isEqualToIgnoringNullFields(expected);
assertEquals(organization.getName(), entity.getName());
assertEquals("{\"a\":\"b\"}", entity.getMetadata());
assertEquals(testOwnerId, entity.getOwner());
}
@Test
void shouldConvertToDomain() {
// Given
OrganizationEntity entity = buildOrganizationEntity();
OrganizationEntity entity = TestObjectFactory.buildOrganization();
// When
Organization organization = converter.toDomain(entity);
// Then
Organization expected = new Organization()
.id("id")
.createdAt(OffsetDateTime.parse("2019-08-24T14:15:22Z"))
.name("org")
.owner("own")
.metadata(Map.of("a", "b"));
assertThat(organization).isEqualToComparingFieldByField(expected);
}
private OrganizationEntity buildOrganizationEntity() {
return OrganizationEntity.builder()
.id("id")
.createdAt(LocalDateTime.parse("2019-08-24T14:15:22"))
.name("org")
.owner("own")
.metadata("{\"a\":\"b\"}")
.roles(Set.of(new OrganizationRoleEntity()))
.build();
assertThat(organization.getId()).isNotEmpty();
assertThat(organization.getParty()).isNotEmpty();
assertThat(organization.getCreatedAt()).isNotNull();
assertEquals(entity.getName(), organization.getName());
assertEquals(Map.of("a", "b"), organization.getMetadata());
assertEquals(entity.getOwner(), organization.getOwner());
}
}

View File

@ -13,7 +13,7 @@ import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
public class OrganizationRoleConverterTest {
class OrganizationRoleConverterTest {
private OrganizationRoleConverter converter;
@ -44,6 +44,6 @@ public class OrganizationRoleConverterTest {
.name("name")
.scopes(List.of(ResourceScopeId.SHOP));
assertThat(role).isEqualToComparingFieldByField(expected);
assertThat(role).usingRecursiveComparison().isEqualTo(expected);
}
}

View File

@ -30,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class OrganizationServiceTest {
class OrganizationServiceTest {
@Mock
private OrganizationConverter organizationConverter;
@ -53,7 +53,7 @@ public class OrganizationServiceTest {
void shouldThrowPartyManagementExceptionOnCreate() {
Organization organization = new Organization();
OrganizationEntity entity = new OrganizationEntity();
OrganizationEntity savedEntity = new OrganizationEntity();
OrganizationEntity savedEntity = TestObjectFactory.buildOrganization();
when(organizationConverter.toEntity(organization, OWNER_ID))
.thenReturn(entity);
@ -70,7 +70,7 @@ public class OrganizationServiceTest {
verify(organizationRepository, times(1))
.save(entity);
verify(partyManagementService, times(1))
.createParty(OWNER_ID, OWNER_ID, EMAIL);
.createParty(savedEntity.getParty(), OWNER_ID, EMAIL);
verify(organizationConverter, times(0))
.toDomain(any(OrganizationEntity.class));
}
@ -79,8 +79,9 @@ public class OrganizationServiceTest {
void shouldCreate() {
Organization organization = new Organization();
OrganizationEntity entity = new OrganizationEntity();
OrganizationEntity savedEntity = new OrganizationEntity();
OrganizationEntity savedEntity = TestObjectFactory.buildOrganization();
Organization savedOrganization = new Organization();
savedOrganization.setParty(TestObjectFactory.randomString());
when(organizationConverter.toEntity(organization, OWNER_ID))
.thenReturn(entity);
@ -96,13 +97,13 @@ public class OrganizationServiceTest {
verify(organizationRepository, times(1))
.save(entity);
verify(partyManagementService, times(1))
.createParty(OWNER_ID, OWNER_ID, EMAIL);
.createParty(savedEntity.getParty(), OWNER_ID, EMAIL);
verify(organizationConverter, times(1))
.toDomain(savedEntity);
assertThat(response)
.isEqualTo(savedOrganization);
assertThat(response.getParty())
.isEqualTo(OWNER_ID);
.isEqualTo(savedOrganization.getParty());
}
@Test

View File

@ -35,6 +35,7 @@ public class TestData {
.createdAt(LocalDateTime.now())
.name("name")
.owner("owner")
.party("party")
.members(Set.of(member))
.build();
}