diff --git a/src/main/java/com/rbkmoney/orgmanager/repository/OrganizationRepository.java b/src/main/java/com/rbkmoney/orgmanager/repository/OrganizationRepository.java index 7649e7c..56226e2 100644 --- a/src/main/java/com/rbkmoney/orgmanager/repository/OrganizationRepository.java +++ b/src/main/java/com/rbkmoney/orgmanager/repository/OrganizationRepository.java @@ -10,13 +10,28 @@ import java.util.List; @Repository public interface OrganizationRepository extends JpaRepository { - @Query(value = "SELECT * FROM org_manager.organization AS o WHERE o.id IN " + - "(SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 ) ORDER BY o.id DESC", + @Query(value = + " SELECT * FROM org_manager.organization AS o " + + " WHERE o.id IN " + + " ( " + + " SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 " + + " UNION " + + " SELECT id FROM org_manager.organization WHERE owner = ?1 " + + " ) " + + " ORDER BY o.id DESC", nativeQuery = true) List findAllByMember(String userId); - @Query(value = "SELECT * FROM org_manager.organization AS o WHERE o.id IN " + - "(SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 ) AND o.id < ?2 ORDER BY o.id DESC", + @Query(value = + " SELECT * FROM org_manager.organization AS o " + + " WHERE o.id IN " + + " ( " + + " SELECT mo.organization_id FROM org_manager.member_to_organization AS mo WHERE mo.member_id = ?1 " + + " UNION " + + " SELECT id FROM org_manager.organization WHERE owner = ?1 " + + " ) " + + " AND o.id < ?2 " + + " ORDER BY o.id DESC", nativeQuery = true) List findAllByMember(String userId, String continuationId); diff --git a/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java b/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java index 6e9c827..1071b3b 100644 --- a/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java +++ b/src/test/java/com/rbkmoney/orgmanager/controller/UserControllerTest.java @@ -11,13 +11,8 @@ import com.rbkmoney.orgmanager.repository.InvitationRepository; import com.rbkmoney.orgmanager.repository.InvitationRepositoryTest; import com.rbkmoney.orgmanager.repository.MemberRepository; import com.rbkmoney.orgmanager.repository.OrganizationRepository; -import com.rbkmoney.orgmanager.service.OrganizationService; import com.rbkmoney.orgmanager.service.ResourceAccessService; -import com.rbkmoney.swag.organizations.model.InvitationStatusName; -import com.rbkmoney.swag.organizations.model.OrganizationJoinRequest; -import com.rbkmoney.swag.organizations.model.OrganizationMembership; -import com.rbkmoney.swag.organizations.model.OrganizationSearchResult; -import com.rbkmoney.swag.organizations.model.RoleId; +import com.rbkmoney.swag.organizations.model.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,17 +30,13 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.Set; -import static com.rbkmoney.orgmanager.TestObjectFactory.buildInvitation; -import static com.rbkmoney.orgmanager.TestObjectFactory.buildOrganization; -import static com.rbkmoney.orgmanager.TestObjectFactory.randomString; -import static com.rbkmoney.orgmanager.TestObjectFactory.testMemberEntity; +import static com.rbkmoney.orgmanager.TestObjectFactory.*; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.doThrow; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -77,9 +68,6 @@ public class UserControllerTest extends AbstractControllerTest { @Autowired private KeycloakOpenIdStub keycloakOpenIdStub; - @SpyBean - private OrganizationService organizationService; - @SpyBean private ResourceAccessService resourceAccessService; @@ -187,18 +175,40 @@ public class UserControllerTest extends AbstractControllerTest { .andExpect(jsonPath("$.member.id", equalTo(userId))); } + @Test + public void listOrgMembershipWithoutLimitTest() throws Exception { + String jwtToken = generateRBKadminJwt(); + String userId = getUserFromToken(); + MemberEntity targetMember = memberRepository.save(testMemberEntity(userId)); + Set targetEntities = buildOrganization(targetMember, 7); + organizationRepository.saveAll(targetEntities); + + MvcResult mvcResultFirst = mockMvc.perform(get("/user/membership") + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + jwtToken) + .header("X-Request-ID", "testRequestId") + ).andExpect(status().isOk()).andReturn(); + + OrganizationSearchResult organizationSearchResult = objectMapper.readValue( + mvcResultFirst.getResponse().getContentAsString(), OrganizationSearchResult.class); + Assert.assertEquals(7, organizationSearchResult.getResult().size()); + } + @Test public void listOrgMembershipTest() throws Exception { String jwtToken = generateRBKadminJwt(); String userId = getUserFromToken(); MemberEntity targetMember = memberRepository.save(testMemberEntity(userId)); - Set targetEntities = buildOrganization(targetMember, 8); + Set targetEntities = buildOrganization(targetMember, 9); OrganizationEntity anotherOrganization = buildOrganization(); - targetEntities.add(anotherOrganization); + OrganizationEntity organizationWithOwner = buildOrganization(); + organizationWithOwner.setOwner(userId); + targetEntities.addAll(List.of(anotherOrganization, organizationWithOwner)); organizationRepository.saveAll(targetEntities); + String limit = "4"; MvcResult mvcResultFirst = mockMvc.perform(get("/user/membership") - .queryParam("limit", "3") + .queryParam("limit", limit) .accept(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + jwtToken) .header("X-Request-ID", "testRequestId") @@ -206,10 +216,10 @@ public class UserControllerTest extends AbstractControllerTest { OrganizationSearchResult organizationSearchResultFirst = objectMapper.readValue( mvcResultFirst.getResponse().getContentAsString(), OrganizationSearchResult.class); - Assert.assertEquals(3, organizationSearchResultFirst.getResult().size()); + Assert.assertEquals(4, organizationSearchResultFirst.getResult().size()); MvcResult mvcResultSecond = mockMvc.perform(get("/user/membership") - .queryParam("limit", "3") + .queryParam("limit", limit) .queryParam("continuationToken", organizationSearchResultFirst.getContinuationToken()) .accept(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + jwtToken) @@ -218,10 +228,10 @@ public class UserControllerTest extends AbstractControllerTest { OrganizationSearchResult organizationSearchResultSecond = objectMapper.readValue( mvcResultSecond.getResponse().getContentAsString(), OrganizationSearchResult.class); - Assert.assertEquals(3, organizationSearchResultSecond.getResult().size()); + Assert.assertEquals(4, organizationSearchResultSecond.getResult().size()); MvcResult mvcResultThird = mockMvc.perform(get("/user/membership") - .queryParam("limit", "3") + .queryParam("limit", limit) .queryParam("continuationToken", organizationSearchResultSecond.getContinuationToken()) .accept(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + jwtToken)