From 9d203b6f32d7c6c29adb1f3ce29f621cf565c919 Mon Sep 17 00:00:00 2001 From: ggmaleva Date: Thu, 9 Dec 2021 16:05:51 +0300 Subject: [PATCH] fix context for owner user --- pom.xml | 2 +- .../entity/MemberContextEntity.java | 5 +- .../exception/ResourceNotFoundException.java | 7 ++ .../repository/MemberContextRepository.java | 2 +- .../repository/MemberRepository.java | 2 + .../service/OrganizationService.java | 21 ++-- .../migration/V7__change_member_context.sql | 1 + .../controller/UserControllerTest.java | 104 ++++++++++++------ 8 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 src/main/resources/db/migration/V7__change_member_context.sql diff --git a/pom.xml b/pom.xml index e9d9ff3..4446270 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ 0.0.1-SNAPSHOT jar - Org manager + org-manager UTF-8 diff --git a/src/main/java/com/rbkmoney/orgmanager/entity/MemberContextEntity.java b/src/main/java/com/rbkmoney/orgmanager/entity/MemberContextEntity.java index 8c60602..6711c8c 100644 --- a/src/main/java/com/rbkmoney/orgmanager/entity/MemberContextEntity.java +++ b/src/main/java/com/rbkmoney/orgmanager/entity/MemberContextEntity.java @@ -6,6 +6,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; + import java.io.Serializable; @Entity @@ -24,8 +25,6 @@ public class MemberContextEntity implements Serializable { @JoinColumn(name = "organization_id", referencedColumnName = "id") private OrganizationEntity organizationEntity; - @OneToOne - @JoinColumn(name = "member_id", referencedColumnName = "id") - private MemberEntity memberEntity; + private String memberId; } diff --git a/src/main/java/com/rbkmoney/orgmanager/exception/ResourceNotFoundException.java b/src/main/java/com/rbkmoney/orgmanager/exception/ResourceNotFoundException.java index 3ff565e..5c524a5 100644 --- a/src/main/java/com/rbkmoney/orgmanager/exception/ResourceNotFoundException.java +++ b/src/main/java/com/rbkmoney/orgmanager/exception/ResourceNotFoundException.java @@ -1,4 +1,11 @@ package com.rbkmoney.orgmanager.exception; public class ResourceNotFoundException extends RuntimeException { + + public ResourceNotFoundException() { + } + + public ResourceNotFoundException(String message) { + super(message); + } } diff --git a/src/main/java/com/rbkmoney/orgmanager/repository/MemberContextRepository.java b/src/main/java/com/rbkmoney/orgmanager/repository/MemberContextRepository.java index 325c65f..0896759 100644 --- a/src/main/java/com/rbkmoney/orgmanager/repository/MemberContextRepository.java +++ b/src/main/java/com/rbkmoney/orgmanager/repository/MemberContextRepository.java @@ -9,6 +9,6 @@ import java.util.Optional; @Repository public interface MemberContextRepository extends JpaRepository { - Optional findByMemberEntityId(String memberId); + Optional findByMemberId(String memberId); } diff --git a/src/main/java/com/rbkmoney/orgmanager/repository/MemberRepository.java b/src/main/java/com/rbkmoney/orgmanager/repository/MemberRepository.java index b937d73..d388598 100644 --- a/src/main/java/com/rbkmoney/orgmanager/repository/MemberRepository.java +++ b/src/main/java/com/rbkmoney/orgmanager/repository/MemberRepository.java @@ -32,5 +32,7 @@ public interface MemberRepository extends JpaRepository { " AND m.id = mtmr.member_id ", nativeQuery = true) List getOrgMemberList(String orgId); + boolean existsById(String id); + } diff --git a/src/main/java/com/rbkmoney/orgmanager/service/OrganizationService.java b/src/main/java/com/rbkmoney/orgmanager/service/OrganizationService.java index f9eed4e..a823d14 100644 --- a/src/main/java/com/rbkmoney/orgmanager/service/OrganizationService.java +++ b/src/main/java/com/rbkmoney/orgmanager/service/OrganizationService.java @@ -18,6 +18,7 @@ import org.keycloak.representations.AccessToken; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.time.LocalDateTime; @@ -263,28 +264,32 @@ public class OrganizationService { OrganizationEntity organizationEntity = organizationRepository.findById(organizationId) .orElseThrow(ResourceNotFoundException::new); Optional memberContextEntityOptional = - memberContextRepository.findByMemberEntityId(userId); + memberContextRepository.findByMemberId(userId); if (memberContextEntityOptional.isPresent()) { MemberContextEntity memberContextEntity = memberContextEntityOptional.get(); memberContextEntity.setOrganizationEntity(organizationEntity); memberContextRepository.save(memberContextEntity); } else { - MemberEntity memberEntity = memberRepository.findById(userId) - .orElseThrow(() -> new IllegalArgumentException("Can't find member. Unknown userId=" + userId)); + if (isNotExistUser(userId)) { + throw new ResourceNotFoundException("Can't find member. Unknown userId=" + userId); + } MemberContextEntity memberContextEntity = new MemberContextEntity(); memberContextEntity.setOrganizationEntity(organizationEntity); - memberContextEntity.setMemberEntity(memberEntity); + memberContextEntity.setMemberId(userId); memberContextRepository.save(memberContextEntity); } } - public MemberContext findMemberContext(String userId) { - MemberContextEntity memberContextEntity = memberContextRepository.findByMemberEntityId(userId) - .orElseThrow(ResourceNotFoundException::new); + private boolean isNotExistUser(String userId) { + return !memberRepository.existsById(userId) & + CollectionUtils.isEmpty(organizationRepository.findAllByOwner(userId)); + } + public MemberContext findMemberContext(String userId) { + MemberContextEntity memberContextEntity = memberContextRepository.findByMemberId(userId) + .orElseThrow(ResourceNotFoundException::new); MemberContext memberContext = new MemberContext(); memberContext.setOrganizationId(memberContextEntity.getOrganizationEntity().getId()); - return memberContext; } diff --git a/src/main/resources/db/migration/V7__change_member_context.sql b/src/main/resources/db/migration/V7__change_member_context.sql new file mode 100644 index 0000000..f40a71a --- /dev/null +++ b/src/main/resources/db/migration/V7__change_member_context.sql @@ -0,0 +1 @@ +ALTER TABLE org_manager.member_context DROP CONSTRAINT member_context_to_member_fkey; diff --git a/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java b/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java index 6755763..886a940 100644 --- a/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java +++ b/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java @@ -1,6 +1,5 @@ package com.rbkmoney.orgmanager.controller; -import com.fasterxml.jackson.core.JsonProcessingException; import com.rbkmoney.orgmanager.entity.*; import com.rbkmoney.orgmanager.exception.AccessDeniedException; import com.rbkmoney.orgmanager.exception.ResourceNotFoundException; @@ -311,6 +310,23 @@ public class UserControllerTest extends AbstractControllerTest { assertNull(organizationSearchResultThird.getContinuationToken()); } + @Test + void switchOrganizationWithNotExistMember() throws Exception { + OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization()); + OrganizationSwitchRequest organizationSwitchRequest = new OrganizationSwitchRequest(); + organizationSwitchRequest.setOrganizationId(organizationEntity.getId()); + String jwtToken = generateRbkAdminJwt(); + + mockMvc.perform(put("/user/context") + .accept(MediaType.APPLICATION_JSON) + .contentType("application/json") + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + .content(objectMapper.writeValueAsString(organizationSwitchRequest)) + ) + .andExpect(status().isNotFound()); + } + @Test void switchOrganizationWithNewContextCreation() throws Exception { String userId = getUserFromToken(); @@ -321,18 +337,44 @@ public class UserControllerTest extends AbstractControllerTest { organizationSwitchRequest.setOrganizationId(organizationEntity.getId()); mockMvc.perform(put("/user/context") - .accept(MediaType.APPLICATION_JSON) - .contentType("application/json") - .header("Authorization", "Bearer " + jwtToken) - .header("X-Request-ID", "testRequestId") - .content(objectMapper.writeValueAsString(organizationSwitchRequest)) - ) + .accept(MediaType.APPLICATION_JSON) + .contentType("application/json") + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + .content(objectMapper.writeValueAsString(organizationSwitchRequest)) + ) .andExpect(status().isNoContent()); Optional memberContextEntityOptional = - memberContextRepository.findByMemberEntityId(userId); + memberContextRepository.findByMemberId(userId); assertTrue(memberContextEntityOptional.isPresent()); - assertEquals(userId, memberContextEntityOptional.get().getMemberEntity().getId()); + assertEquals(userId, memberContextEntityOptional.get().getMemberId()); + assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId()); + } + + @Test + void switchOrganizationWithOwnerUser() throws Exception { + String userId = getUserFromToken(); + OrganizationEntity organization = buildOrganization(); + organization.setOwner(userId); + OrganizationEntity organizationEntity = organizationRepository.save(organization); + String jwtToken = generateRbkAdminJwt(); + OrganizationSwitchRequest organizationSwitchRequest = new OrganizationSwitchRequest(); + organizationSwitchRequest.setOrganizationId(organizationEntity.getId()); + + mockMvc.perform(put("/user/context") + .accept(MediaType.APPLICATION_JSON) + .contentType("application/json") + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + .content(objectMapper.writeValueAsString(organizationSwitchRequest)) + ) + .andExpect(status().isNoContent()); + Optional memberContextEntityOptional = + memberContextRepository.findByMemberId(userId); + + assertTrue(memberContextEntityOptional.isPresent()); + assertEquals(userId, memberContextEntityOptional.get().getMemberId()); assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId()); } @@ -345,12 +387,12 @@ public class UserControllerTest extends AbstractControllerTest { organizationSwitchRequest.setOrganizationId("testOrgId"); mockMvc.perform(put("/user/context") - .accept(MediaType.APPLICATION_JSON) - .contentType("application/json") - .header("Authorization", "Bearer " + jwtToken) - .header("X-Request-ID", "testRequestId") - .content(objectMapper.writeValueAsString(organizationSwitchRequest)) - ) + .accept(MediaType.APPLICATION_JSON) + .contentType("application/json") + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + .content(objectMapper.writeValueAsString(organizationSwitchRequest)) + ) .andExpect(status().isNotFound()); } @@ -361,7 +403,7 @@ public class UserControllerTest extends AbstractControllerTest { OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization()); MemberContextEntity memberContextEntity = memberContextRepository.save( MemberContextEntity.builder() - .memberEntity(memberEntity) + .memberId(userId) .organizationEntity(organizationEntity) .build() ); @@ -371,15 +413,15 @@ public class UserControllerTest extends AbstractControllerTest { organizationSwitchRequest.setOrganizationId(newOrganizationEntity.getId()); mockMvc.perform(put("/user/context") - .accept(MediaType.APPLICATION_JSON) - .contentType("application/json") - .header("Authorization", "Bearer " + jwtToken) - .header("X-Request-ID", "testRequestId") - .content(objectMapper.writeValueAsString(organizationSwitchRequest)) - ) + .accept(MediaType.APPLICATION_JSON) + .contentType("application/json") + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + .content(objectMapper.writeValueAsString(organizationSwitchRequest)) + ) .andExpect(status().isNoContent()); Optional memberContextEntityOptional = - memberContextRepository.findByMemberEntityId(userId); + memberContextRepository.findByMemberId(userId); assertTrue(memberContextEntityOptional.isPresent()); assertEquals(newOrganizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId()); @@ -392,21 +434,19 @@ public class UserControllerTest extends AbstractControllerTest { OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization()); MemberContextEntity memberContextEntity = memberContextRepository.save( MemberContextEntity.builder() - .memberEntity(memberEntity) + .memberId(userId) .organizationEntity(organizationEntity) .build() ); String jwtToken = generateRbkAdminJwt(); - MvcResult mvcResult = mockMvc.perform(get("/user/context") - .accept(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + jwtToken) - .header("X-Request-ID", "testRequestId") - ) + mockMvc.perform(get("/user/context") + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + ) .andExpect(status().isOk()) - .andReturn(); - - System.out.println(mvcResult); + .andExpect(jsonPath("$.organizationId", equalTo(organizationEntity.getId()))); } }