mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 08:25:21 +00:00
commit
d4ab0741f4
6
pom.xml
6
pom.xml
@ -193,6 +193,12 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
@ -145,7 +145,8 @@ public class OrgsController implements OrgsApi {
|
||||
String userId) {
|
||||
log.info("Expel member organization: requestId={}, orgId={}, userId={}", xRequestID, orgId, userId);
|
||||
resourceAccessService.checkMemberRights(orgId, userId);
|
||||
return organizationService.expelOrgMember(orgId, userId);
|
||||
organizationService.expelOrgMember(orgId, userId);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -49,8 +49,8 @@ public class UserController implements UserApi {
|
||||
log.info("Join organization: body={}", body);
|
||||
resourceAccessService.checkOrganizationRights(body);
|
||||
AccessToken accessToken = keycloakService.getAccessToken();
|
||||
return organizationService
|
||||
.joinOrganization(body.getInvitation(), accessToken.getSubject(), accessToken.getEmail());
|
||||
return ResponseEntity.ok(organizationService
|
||||
.joinOrganization(body.getInvitation(), accessToken.getSubject(), accessToken.getEmail()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -26,14 +26,15 @@ public class MemberRoleConverter {
|
||||
}
|
||||
|
||||
public MemberRole toDomain(MemberRoleEntity entity) {
|
||||
ResourceScopeId resourceScopeId = Optional.ofNullable(entity.getScopeId())
|
||||
MemberRoleScope memberRoleScope = Optional.ofNullable(entity.getScopeId())
|
||||
.map(ResourceScopeId::fromValue)
|
||||
.map(resourceScopeId -> new MemberRoleScope()
|
||||
.id(resourceScopeId)
|
||||
.resourceId(entity.getResourceId()))
|
||||
.orElse(null);
|
||||
return new MemberRole()
|
||||
.id(entity.getId())
|
||||
.roleId(RoleId.fromValue(entity.getRoleId()))
|
||||
.scope(new MemberRoleScope()
|
||||
.id(resourceScopeId)
|
||||
.resourceId(entity.getResourceId()));
|
||||
.scope(memberRoleScope);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import lombok.*;
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@ -25,7 +26,7 @@ public class MemberEntity implements Serializable {
|
||||
name = "member_to_member_role",
|
||||
joinColumns = @JoinColumn(name = "member_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "member_role_id"))
|
||||
private Set<MemberRoleEntity> roles;
|
||||
private Set<MemberRoleEntity> roles = new HashSet<>();
|
||||
|
||||
@ToString.Exclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
|
@ -8,6 +8,7 @@ import javax.persistence.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@ -23,12 +24,12 @@ public class OrganizationEntity implements Serializable {
|
||||
|
||||
@ToString.Exclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
|
||||
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
|
||||
@JoinTable(
|
||||
name = "member_to_organization",
|
||||
joinColumns = @JoinColumn(name = "organization_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "member_id"))
|
||||
private Set<MemberEntity> members;
|
||||
private Set<MemberEntity> members = new HashSet<>();
|
||||
|
||||
@ToString.Exclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
|
@ -14,7 +14,6 @@ import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -24,7 +23,6 @@ import org.springframework.util.StringUtils;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
@ -43,22 +41,6 @@ public class OrganizationService {
|
||||
private final InvitationRepository invitationRepository;
|
||||
private final MemberRoleService memberRoleService;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<OrganizationEntity> findById(String orgId) {
|
||||
Optional<OrganizationEntity> organizationEntityOptional = organizationRepository.findById(orgId);
|
||||
if (organizationEntityOptional.isPresent()) {
|
||||
OrganizationEntity organizationEntity = organizationEntityOptional.get();
|
||||
Hibernate.initialize(organizationEntity.getMembers());
|
||||
for (MemberEntity member : organizationEntity.getMembers()) {
|
||||
Hibernate.initialize(member.getRoles());
|
||||
}
|
||||
Hibernate.initialize(organizationEntity.getRoles());
|
||||
|
||||
return Optional.of(organizationEntity);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// TODO [a.romanov]: idempotency
|
||||
public ResponseEntity<Organization> create(
|
||||
String ownerId,
|
||||
@ -129,18 +111,22 @@ public class OrganizationService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ResponseEntity<Void> expelOrgMember(String orgId, String userId) {
|
||||
Optional<OrganizationEntity> organizationEntityOptional = organizationRepository.findById(orgId);
|
||||
public void expelOrgMember(String orgId, String userId) {
|
||||
OrganizationEntity organization = organizationRepository.findById(orgId)
|
||||
.orElseThrow(ResourceNotFoundException::new);
|
||||
MemberEntity member = organization.getMembers().stream()
|
||||
.filter(memberEntity -> memberEntity.getId().equals(userId))
|
||||
.findFirst()
|
||||
.orElseThrow(ResourceNotFoundException::new);
|
||||
|
||||
if (organizationEntityOptional.isEmpty()) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.build();
|
||||
}
|
||||
|
||||
organizationEntityOptional.get().getMembers().removeIf(memberEntity -> memberEntity.getId().equals(userId));
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
Optional<MemberRoleEntity> memberRole = member.getRoles().stream()
|
||||
.filter(memberRoleEntity -> memberRoleEntity.getOrganizationId().equals(orgId))
|
||||
.findFirst();
|
||||
organization.getMembers().remove(member);
|
||||
memberRole.ifPresent(memberRoleEntity -> {
|
||||
member.getRoles().remove(memberRoleEntity);
|
||||
memberRoleService.delete(memberRoleEntity.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -264,56 +250,40 @@ public class OrganizationService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ResponseEntity<OrganizationMembership> joinOrganization(String token, String userId, String userEmail) {
|
||||
Optional<InvitationEntity> invitationEntityOptional = invitationRepository.findByAcceptToken(token);
|
||||
|
||||
if (invitationEntityOptional.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
InvitationEntity invitationEntity = invitationEntityOptional.get();
|
||||
|
||||
public OrganizationMembership joinOrganization(String token, String userId, String userEmail) {
|
||||
InvitationEntity invitationEntity = invitationRepository.findByAcceptToken(token)
|
||||
.orElseThrow(ResourceNotFoundException::new);
|
||||
if (invitationEntity.isExpired()) {
|
||||
throw new InviteExpiredException(invitationEntity.getExpiresAt().toString());
|
||||
}
|
||||
|
||||
Optional<OrganizationEntity> organizationEntityOptional =
|
||||
organizationRepository.findById(invitationEntity.getOrganizationId());
|
||||
|
||||
if (organizationEntityOptional.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
|
||||
invitationEntity.setAcceptedAt(LocalDateTime.now());
|
||||
invitationEntity.setAcceptedMemberId(userId);
|
||||
invitationEntity.setStatus(InvitationStatusName.ACCEPTED.getValue());
|
||||
|
||||
OrganizationEntity organizationEntity = organizationEntityOptional.get();
|
||||
|
||||
MemberEntity memberEntity = findOrCreateMember(userId, userEmail, invitationEntity.getInviteeRoles());
|
||||
invitationEntity.getInviteeRoles().addAll(invitationEntity.getInviteeRoles());
|
||||
|
||||
OrganizationEntity organizationEntity =
|
||||
organizationRepository.findById(invitationEntity.getOrganizationId())
|
||||
.orElseThrow(ResourceNotFoundException::new);
|
||||
MemberEntity memberEntity = findOrCreateMember(userId, userEmail);
|
||||
memberEntity.getRoles().addAll(invitationEntity.getInviteeRoles());
|
||||
organizationEntity.getMembers().add(memberEntity);
|
||||
|
||||
acceptInvitation(userId, invitationEntity);
|
||||
OrganizationMembership organizationMembership = new OrganizationMembership();
|
||||
organizationMembership.setMember(memberConverter.toDomain(memberEntity));
|
||||
organizationMembership.setOrg(organizationConverter.toDomain(organizationEntity));
|
||||
|
||||
return ResponseEntity.ok(organizationMembership);
|
||||
return organizationMembership;
|
||||
}
|
||||
|
||||
private MemberEntity findOrCreateMember(String userId, String userEmail, Set<MemberRoleEntity> inviteeRoles) {
|
||||
Optional<MemberEntity> memberEntityOptional = memberRepository.findById(userId);
|
||||
if (memberEntityOptional.isEmpty()) {
|
||||
return memberRepository.save(
|
||||
MemberEntity.builder()
|
||||
.id(userId)
|
||||
.roles(inviteeRoles)
|
||||
.email(userEmail)
|
||||
.build());
|
||||
private MemberEntity findOrCreateMember(String userId, String userEmail) {
|
||||
return memberRepository.findById(userId)
|
||||
.orElseGet(() -> {
|
||||
MemberEntity entity = new MemberEntity();
|
||||
entity.setId(userId);
|
||||
entity.setEmail(userEmail);
|
||||
return entity;
|
||||
});
|
||||
|
||||
}
|
||||
return memberEntityOptional.get();
|
||||
|
||||
private void acceptInvitation(String userId, InvitationEntity invitationEntity) {
|
||||
invitationEntity.setAcceptedAt(LocalDateTime.now());
|
||||
invitationEntity.setAcceptedMemberId(userId);
|
||||
invitationEntity.setStatus(InvitationStatusName.ACCEPTED.getValue());
|
||||
}
|
||||
|
||||
public String getOrgIdByInvitationToken(String token) {
|
||||
|
@ -1,23 +1,15 @@
|
||||
package com.rbkmoney.orgmanager;
|
||||
|
||||
import com.rbkmoney.bouncer.context.v1.Entity;
|
||||
import com.rbkmoney.bouncer.context.v1.OrgRole;
|
||||
import com.rbkmoney.bouncer.context.v1.OrgRoleScope;
|
||||
import com.rbkmoney.bouncer.context.v1.Organization;
|
||||
import com.rbkmoney.bouncer.context.v1.User;
|
||||
import com.rbkmoney.bouncer.context.v1.*;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.service.dto.BouncerContextDto;
|
||||
import com.rbkmoney.orgmanager.service.dto.RoleDto;
|
||||
import com.rbkmoney.swag.organizations.model.InvitationRequest;
|
||||
import com.rbkmoney.swag.organizations.model.Invitee;
|
||||
import com.rbkmoney.swag.organizations.model.InviteeContact;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRole;
|
||||
import com.rbkmoney.swag.organizations.model.OrganizationJoinRequest;
|
||||
import com.rbkmoney.swag.organizations.model.ResourceScopeId;
|
||||
import com.rbkmoney.swag.organizations.model.RoleId;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import org.keycloak.representations.AccessToken;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@ -106,17 +98,11 @@ public abstract class TestObjectFactory {
|
||||
}
|
||||
|
||||
public static OrganizationEntity buildOrganization() {
|
||||
MemberEntity member = MemberEntity.builder()
|
||||
.id(randomString())
|
||||
.email("email")
|
||||
.build();
|
||||
|
||||
return OrganizationEntity.builder()
|
||||
.id(randomString())
|
||||
.createdAt(LocalDateTime.now())
|
||||
.name(randomString())
|
||||
.owner(randomString())
|
||||
.members(Set.of(member))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -1,83 +1,30 @@
|
||||
package com.rbkmoney.orgmanager.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRoleRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.AbstractRepositoryTest;
|
||||
import com.rbkmoney.orgmanager.service.ResourceAccessService;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Import(KeycloakTestConfig.class)
|
||||
public abstract class AbstractControllerTest {
|
||||
|
||||
@Autowired
|
||||
protected InvitationRepository invitationRepository;
|
||||
|
||||
@Autowired
|
||||
protected OrganizationRepository organizationRepository;
|
||||
|
||||
@Autowired
|
||||
protected MemberRepository memberRepository;
|
||||
|
||||
@Autowired
|
||||
protected MemberRoleRepository memberRoleRepository;
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
@TestPropertySource(locations = "classpath:wiremock.properties")
|
||||
public abstract class AbstractControllerTest extends AbstractRepositoryTest {
|
||||
|
||||
@SpyBean
|
||||
protected ResourceAccessService resourceAccessService;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
keycloakOpenIdStub.givenStub();
|
||||
withTransaction(() -> {
|
||||
invitationRepository.deleteAll();
|
||||
organizationRepository.deleteAll();
|
||||
memberRepository.deleteAll();
|
||||
memberRoleRepository.deleteAll();
|
||||
});
|
||||
}
|
||||
|
||||
@ClassRule
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:9.6")
|
||||
.withStartupTimeout(Duration.ofMinutes(5));
|
||||
|
||||
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
TestPropertyValues.of(
|
||||
"spring.datasource.url=" + postgres.getJdbcUrl(),
|
||||
"spring.datasource.username=" + postgres.getUsername(),
|
||||
"spring.datasource.password=" + postgres.getPassword(),
|
||||
"spring.flyway.url=" + postgres.getJdbcUrl(),
|
||||
"spring.flyway.user=" + postgres.getUsername(),
|
||||
"spring.flyway.password=" + postgres.getPassword())
|
||||
.and(configurableApplicationContext.getEnvironment().getActiveProfiles())
|
||||
.applyTo(configurableApplicationContext);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@ -101,19 +48,4 @@ public abstract class AbstractControllerTest {
|
||||
return keycloakOpenIdStub.getUserId();
|
||||
}
|
||||
|
||||
protected void withTransaction(Runnable runnable){
|
||||
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||
runnable.run();
|
||||
return;
|
||||
}
|
||||
transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
runnable.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,17 @@
|
||||
package com.rbkmoney.orgmanager.controller;
|
||||
|
||||
import com.rbkmoney.orgmanager.OrgManagerApplication;
|
||||
import com.rbkmoney.orgmanager.TestObjectFactory;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepositoryTest;
|
||||
import com.rbkmoney.orgmanager.util.TestData;
|
||||
import com.rbkmoney.swag.organizations.model.InvitationRequest;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRole;
|
||||
import com.rbkmoney.swag.organizations.model.RoleId;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
@ -33,13 +25,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = {OrgManagerApplication.class, UserController.class})
|
||||
@ContextConfiguration(initializers = InvitationRepositoryTest.Initializer.class)
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
@TestPropertySource(locations = "classpath:wiremock.properties")
|
||||
public class OrgsControllerTest extends AbstractControllerTest {
|
||||
|
||||
public static final String ORGANIZATION_ID = "3Kf21K54ldE3";
|
||||
@ -103,19 +88,26 @@ public class OrgsControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
@Transactional
|
||||
void expelOrgMemberTest() throws Exception {
|
||||
MemberEntity savedMember = memberRepository.save(TestObjectFactory.testMemberEntity(TestObjectFactory.randomString()));
|
||||
OrganizationEntity savedOrganization = organizationRepository.save(TestObjectFactory.buildOrganization(savedMember));
|
||||
MemberEntity member = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
|
||||
OrganizationEntity organization = TestObjectFactory.buildOrganization(member);
|
||||
MemberRoleEntity savedMemberRole =
|
||||
memberRoleRepository.save(TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId()));
|
||||
member.setRoles(Set.of(savedMemberRole));
|
||||
MemberEntity savedMember = memberRepository.save(member);
|
||||
OrganizationEntity savedOrganization = organizationRepository.save(organization);
|
||||
|
||||
|
||||
mockMvc.perform(delete(String.format("/orgs/%s/members/%s", savedOrganization.getId(), savedMember.getId()))
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "Bearer " + generateRBKadminJwt())
|
||||
.header("X-Request-ID", "testRequestId"))
|
||||
.andExpect(status().isOk());
|
||||
.andExpect(status().isNoContent());
|
||||
|
||||
Optional<OrganizationEntity> organizationEntityOptional = organizationRepository.findById(savedOrganization.getId());
|
||||
assertTrue(organizationEntityOptional.isPresent());
|
||||
assertFalse(organizationEntityOptional.get().getMembers().stream()
|
||||
.anyMatch(memberEntity -> memberEntity.getId().equals(MEMBER_ID)));
|
||||
OrganizationEntity organizationEntity = organizationRepository.findById(savedOrganization.getId()).get();
|
||||
assertTrue(organizationEntity.getMembers().stream().noneMatch(m -> m.getId().equals(savedMember.getId())));
|
||||
MemberEntity memberEntity = memberRepository.findById(savedMember.getId()).get();
|
||||
assertTrue(memberEntity.getRoles().isEmpty());
|
||||
assertTrue(memberRoleRepository.findAll().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,25 +1,20 @@
|
||||
package com.rbkmoney.orgmanager.controller;
|
||||
|
||||
import com.rbkmoney.orgmanager.OrgManagerApplication;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
|
||||
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepositoryTest;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.rbkmoney.orgmanager.TestObjectFactory.*;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@ -29,13 +24,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = {OrgManagerApplication.class, UserController.class})
|
||||
@ContextConfiguration(initializers = InvitationRepositoryTest.Initializer.class)
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
@TestPropertySource(locations = "classpath:wiremock.properties")
|
||||
public class UserControllerTest extends AbstractControllerTest {
|
||||
|
||||
@Test
|
||||
@ -69,7 +57,7 @@ public class UserControllerTest extends AbstractControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void joinOrgTest() throws Exception {
|
||||
void joinOrgNewMemberTest() throws Exception {
|
||||
String jwtToken = generateRBKadminJwt();
|
||||
String userId = getUserFromToken();
|
||||
OrganizationEntity savedOrg = organizationRepository.save(buildOrganization());
|
||||
@ -89,10 +77,44 @@ public class UserControllerTest extends AbstractControllerTest {
|
||||
mvcResult.getResponse().getContentAsString(), OrganizationMembership.class);
|
||||
assertEquals(savedOrg.getId(), organizationMembership.getOrg().getId());
|
||||
assertEquals(userId, organizationMembership.getMember().getId());
|
||||
assertTrue(organizationMembership.getMember().getRoles().stream()
|
||||
.anyMatch(memberRole -> memberRole.getRoleId() == RoleId.ADMINISTRATOR));
|
||||
assertTrue(organizationMembership.getMember().getRoles().stream()
|
||||
.anyMatch(memberRole -> memberRole.getRoleId() == RoleId.ACCOUNTANT));
|
||||
List<String> actualRoles =
|
||||
organizationMembership.getMember().getRoles().stream().map(MemberRole::getRoleId).map(RoleId::getValue)
|
||||
.collect(Collectors.toList());
|
||||
List<String> expectedRoles = savedInvitation.getInviteeRoles().stream().map(MemberRoleEntity::getRoleId)
|
||||
.collect(Collectors.toList());
|
||||
assertIterableEquals(expectedRoles, actualRoles);
|
||||
InvitationEntity invitationEntity = invitationRepository.findById(savedInvitation.getId()).get();
|
||||
assertEquals(invitationEntity.getStatus(), InvitationStatusName.ACCEPTED.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void joinOrgExistMemberTest() throws Exception {
|
||||
String jwtToken = generateRBKadminJwt();
|
||||
String userId = getUserFromToken();
|
||||
memberRepository.save(testMemberEntity(userId));
|
||||
OrganizationEntity savedOrg = organizationRepository.save(buildOrganization());
|
||||
InvitationEntity savedInvitation = invitationRepository.save(buildInvitation(savedOrg.getId()));
|
||||
OrganizationJoinRequest organizationJoinRequest = new OrganizationJoinRequest();
|
||||
organizationJoinRequest.setInvitation(savedInvitation.getAcceptToken());
|
||||
|
||||
MvcResult mvcResult = mockMvc.perform(post("/user/membership")
|
||||
.contentType("application/json")
|
||||
.content(objectMapper.writeValueAsString(organizationJoinRequest))
|
||||
.header("Authorization", "Bearer " + jwtToken)
|
||||
.header("X-Request-ID", "testRequestId"))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
OrganizationMembership organizationMembership = objectMapper.readValue(
|
||||
mvcResult.getResponse().getContentAsString(), OrganizationMembership.class);
|
||||
assertEquals(savedOrg.getId(), organizationMembership.getOrg().getId());
|
||||
assertEquals(userId, organizationMembership.getMember().getId());
|
||||
List<String> actualRoles =
|
||||
organizationMembership.getMember().getRoles().stream().map(MemberRole::getRoleId).map(RoleId::getValue)
|
||||
.collect(Collectors.toList());
|
||||
List<String> expectedRoles = savedInvitation.getInviteeRoles().stream().map(MemberRoleEntity::getRoleId)
|
||||
.collect(Collectors.toList());
|
||||
assertIterableEquals(expectedRoles, actualRoles);
|
||||
|
||||
InvitationEntity invitationEntity = invitationRepository.findById(savedInvitation.getId()).get();
|
||||
assertEquals(invitationEntity.getStatus(), InvitationStatusName.ACCEPTED.getValue());
|
||||
|
@ -6,8 +6,8 @@ import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.util.JsonMapper;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
@ -25,7 +25,7 @@ public class InvitationConverterTest {
|
||||
|
||||
private InvitationConverter converter;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MemberRoleConverter memberRoleConverter = mock(MemberRoleConverter.class);
|
||||
when(memberRoleConverter.toDomain(any(MemberRoleEntity.class)))
|
||||
@ -43,7 +43,7 @@ public class InvitationConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToEntity() {
|
||||
void shouldConvertToEntity() {
|
||||
// Given
|
||||
InvitationRequest invitation = new InvitationRequest()
|
||||
.invitee(new Invitee()
|
||||
@ -73,7 +73,7 @@ public class InvitationConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
void shouldConvertToDomain() {
|
||||
// Given
|
||||
InvitationEntity entity = InvitationEntity.builder()
|
||||
.id("id")
|
||||
|
@ -6,14 +6,14 @@ import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.swag.organizations.model.Member;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRole;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -23,7 +23,7 @@ public class MemberConverterTest {
|
||||
|
||||
private MemberConverter converter;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MemberRoleConverter memberRoleConverter = mock(MemberRoleConverter.class);
|
||||
when(memberRoleConverter.toDomain(any(MemberRoleEntity.class)))
|
||||
@ -39,7 +39,7 @@ public class MemberConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
void shouldConvertToDomain() {
|
||||
// Given
|
||||
MemberEntity entity = buildMemberEntity();
|
||||
|
||||
@ -56,12 +56,12 @@ public class MemberConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToThrift() {
|
||||
void shouldConvertToThrift() {
|
||||
MemberEntity entity = buildMemberEntity();
|
||||
User user = converter.toThrift(entity);
|
||||
Assert.assertEquals(entity.getId(), user.getId());
|
||||
Assert.assertEquals(entity.getEmail(), user.getEmail());
|
||||
Assert.assertEquals(entity.getOrganizations().size(), user.getOrgs().size());
|
||||
assertEquals(entity.getId(), user.getId());
|
||||
assertEquals(entity.getEmail(), user.getEmail());
|
||||
assertEquals(entity.getOrganizations().size(), user.getOrgs().size());
|
||||
}
|
||||
|
||||
private MemberEntity buildMemberEntity() {
|
||||
|
@ -5,8 +5,8 @@ import com.rbkmoney.swag.organizations.model.MemberRole;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRoleScope;
|
||||
import com.rbkmoney.swag.organizations.model.ResourceScopeId;
|
||||
import com.rbkmoney.swag.organizations.model.RoleId;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -14,13 +14,13 @@ public class MemberRoleConverterTest {
|
||||
|
||||
private MemberRoleConverter converter;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
converter = new MemberRoleConverter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToEntityWithoutScope() {
|
||||
void shouldConvertToEntityWithoutScope() {
|
||||
// Given
|
||||
MemberRole role = new MemberRole()
|
||||
.roleId(RoleId.ADMINISTRATOR);
|
||||
@ -39,7 +39,7 @@ public class MemberRoleConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToEntity() {
|
||||
void shouldConvertToEntity() {
|
||||
// Given
|
||||
MemberRole role = new MemberRole()
|
||||
.roleId(RoleId.ADMINISTRATOR)
|
||||
@ -63,7 +63,7 @@ public class MemberRoleConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomainWithoutScope() {
|
||||
void shouldConvertToDomainWithoutScope() {
|
||||
// Given
|
||||
MemberRoleEntity entity = MemberRoleEntity.builder()
|
||||
.id("id")
|
||||
@ -77,14 +77,13 @@ public class MemberRoleConverterTest {
|
||||
// Then
|
||||
MemberRole expected = new MemberRole()
|
||||
.id(entity.getId())
|
||||
.roleId(RoleId.ADMINISTRATOR)
|
||||
.scope(new MemberRoleScope());
|
||||
.roleId(RoleId.ADMINISTRATOR);
|
||||
|
||||
assertThat(role).isEqualToComparingFieldByField(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
void shouldConvertToDomain() {
|
||||
// Given
|
||||
MemberRoleEntity entity = MemberRoleEntity.builder()
|
||||
.id("id")
|
||||
|
@ -5,8 +5,8 @@ import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationRoleEntity;
|
||||
import com.rbkmoney.orgmanager.util.JsonMapper;
|
||||
import com.rbkmoney.swag.organizations.model.Organization;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
@ -14,13 +14,13 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class OrganizationConverterTest {
|
||||
|
||||
private OrganizationConverter converter;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
converter = new OrganizationConverter(
|
||||
new JsonMapper(
|
||||
@ -28,7 +28,7 @@ public class OrganizationConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToEntity() {
|
||||
void shouldConvertToEntity() {
|
||||
// Given
|
||||
Organization organization = new Organization()
|
||||
.name("org")
|
||||
@ -50,7 +50,7 @@ public class OrganizationConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
void shouldConvertToDomain() {
|
||||
// Given
|
||||
OrganizationEntity entity = buildOrganizationEntity();
|
||||
|
||||
@ -69,7 +69,7 @@ public class OrganizationConverterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToThrift(){
|
||||
void shouldConvertToThrift() {
|
||||
OrganizationEntity organizationEntity = buildOrganizationEntity();
|
||||
var organization = converter.toThrift(organizationEntity);
|
||||
assertEquals(organizationEntity.getId(), organization.getId());
|
||||
|
@ -5,8 +5,8 @@ import com.rbkmoney.orgmanager.entity.ScopeEntity;
|
||||
import com.rbkmoney.swag.organizations.model.ResourceScopeId;
|
||||
import com.rbkmoney.swag.organizations.model.Role;
|
||||
import com.rbkmoney.swag.organizations.model.RoleId;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -17,13 +17,13 @@ public class OrganizationRoleConverterTest {
|
||||
|
||||
private OrganizationRoleConverter converter;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
converter = new OrganizationRoleConverter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
void shouldConvertToDomain() {
|
||||
// Given
|
||||
OrganizationRoleEntity entity = OrganizationRoleEntity.builder()
|
||||
.id("id")
|
||||
|
@ -1,15 +1,62 @@
|
||||
package com.rbkmoney.orgmanager.repository;
|
||||
|
||||
import com.rbkmoney.orgmanager.OrgManagerApplication;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = {OrgManagerApplication.class})
|
||||
@ContextConfiguration(initializers = InvitationRepositoryTest.Initializer.class)
|
||||
public abstract class AbstractRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
protected InvitationRepository invitationRepository;
|
||||
|
||||
@Autowired
|
||||
protected OrganizationRepository organizationRepository;
|
||||
|
||||
@Autowired
|
||||
protected MemberRepository memberRepository;
|
||||
|
||||
@Autowired
|
||||
protected MemberRoleRepository memberRoleRepository;
|
||||
|
||||
@Autowired
|
||||
protected OrganizationRoleRepository organizationRoleRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
withTransaction(() -> {
|
||||
invitationRepository.deleteAll();
|
||||
organizationRepository.deleteAll();
|
||||
memberRepository.deleteAll();
|
||||
memberRoleRepository.deleteAll();
|
||||
organizationRoleRepository.deleteAll();
|
||||
});
|
||||
}
|
||||
|
||||
@ClassRule
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:9.6")
|
||||
@ -30,4 +77,19 @@ public abstract class AbstractRepositoryTest {
|
||||
.applyTo(configurableApplicationContext);
|
||||
}
|
||||
}
|
||||
|
||||
protected void withTransaction(Runnable runnable) {
|
||||
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||
runnable.run();
|
||||
return;
|
||||
}
|
||||
transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
runnable.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,8 @@
|
||||
package com.rbkmoney.orgmanager.repository;
|
||||
|
||||
import com.rbkmoney.orgmanager.OrgManagerApplication;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@ -17,24 +10,14 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@DirtiesContext
|
||||
@SpringBootTest(classes = OrgManagerApplication.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(initializers = InvitationRepositoryTest.Initializer.class)
|
||||
public class InvitationRepositoryTest extends AbstractRepositoryTest {
|
||||
|
||||
private static final String ORGANIZATION_ID = "orgId";
|
||||
|
||||
@Autowired
|
||||
private InvitationRepository invitationRepository;
|
||||
|
||||
@Autowired
|
||||
private MemberRoleRepository memberRoleRepository;
|
||||
|
||||
@Test
|
||||
public void shouldSaveInvitationWithRoles() {
|
||||
void shouldSaveInvitationWithRoles() {
|
||||
// Given
|
||||
String invitationId = "invitationId";
|
||||
|
||||
|
@ -1,58 +1,32 @@
|
||||
package com.rbkmoney.orgmanager.repository;
|
||||
|
||||
import com.rbkmoney.orgmanager.OrgManagerApplication;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.ScopeEntity;
|
||||
import com.rbkmoney.orgmanager.service.OrganizationService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@DirtiesContext
|
||||
@SpringBootTest(classes = OrgManagerApplication.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(initializers = InvitationRepositoryTest.Initializer.class)
|
||||
@Transactional
|
||||
public class OrganizationRepositoryTest extends AbstractRepositoryTest {
|
||||
|
||||
private static final String ORGANIZATION_ID = "orgId";
|
||||
|
||||
@Autowired
|
||||
private OrganizationRepository organizationRepository;
|
||||
|
||||
@Autowired
|
||||
private OrganizationService organizationService;
|
||||
|
||||
@Autowired
|
||||
private MemberRepository memberRepository;
|
||||
|
||||
@Autowired
|
||||
private OrganizationRoleRepository organizationRoleRepository;
|
||||
|
||||
@Autowired
|
||||
private OrganizationRoleRepository scopeRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
organizationRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldModifyOrganization() {
|
||||
void shouldModifyOrganization() {
|
||||
// Given
|
||||
MemberEntity member = MemberEntity.builder()
|
||||
.id("memberId")
|
||||
@ -72,13 +46,13 @@ public class OrganizationRepositoryTest extends AbstractRepositoryTest {
|
||||
String modifyOrgName = "testOrgName";
|
||||
organizationService.modify(ORGANIZATION_ID, modifyOrgName);
|
||||
|
||||
Optional<OrganizationEntity> organizationEntityOptional = organizationService.findById(ORGANIZATION_ID);
|
||||
Assert.assertTrue(organizationEntityOptional.isPresent());
|
||||
Assert.assertEquals(modifyOrgName, organizationEntityOptional.get().getName());
|
||||
Optional<OrganizationEntity> organizationEntityOptional = organizationRepository.findById(ORGANIZATION_ID);
|
||||
assertTrue(organizationEntityOptional.isPresent());
|
||||
assertEquals(modifyOrgName, organizationEntityOptional.get().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveOrganizationWithMembers() {
|
||||
void shouldSaveOrganizationWithMembers() {
|
||||
// Given
|
||||
MemberEntity member = MemberEntity.builder()
|
||||
.id("memberId")
|
||||
@ -96,7 +70,7 @@ public class OrganizationRepositoryTest extends AbstractRepositoryTest {
|
||||
organizationRepository.save(organization);
|
||||
|
||||
// Then
|
||||
Optional<OrganizationEntity> savedOrganization = organizationService.findById(ORGANIZATION_ID);
|
||||
Optional<OrganizationEntity> savedOrganization = organizationRepository.findById(ORGANIZATION_ID);
|
||||
assertTrue(savedOrganization.isPresent());
|
||||
assertThat(savedOrganization.get().getMembers()).hasSize(1);
|
||||
|
||||
@ -105,7 +79,7 @@ public class OrganizationRepositoryTest extends AbstractRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveOrganizationWithRoles() {
|
||||
void shouldSaveOrganizationWithRoles() {
|
||||
// Given
|
||||
ScopeEntity scope = ScopeEntity.builder()
|
||||
.id("Shop")
|
||||
@ -131,7 +105,7 @@ public class OrganizationRepositoryTest extends AbstractRepositoryTest {
|
||||
organizationRepository.save(organization);
|
||||
|
||||
// Then
|
||||
Optional<OrganizationEntity> savedOrganizationOptional = organizationService.findById(ORGANIZATION_ID);
|
||||
Optional<OrganizationEntity> savedOrganizationOptional = organizationRepository.findById(ORGANIZATION_ID);
|
||||
assertTrue(savedOrganizationOptional.isPresent());
|
||||
assertThat(savedOrganizationOptional.get().getRoles()).hasSize(1);
|
||||
savedOrganizationOptional.get().getRoles().forEach(
|
||||
|
@ -1,33 +1,20 @@
|
||||
package com.rbkmoney.orgmanager.service;
|
||||
|
||||
import com.rbkmoney.orgmanager.OrgManagerApplication;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.repository.AbstractRepositoryTest;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepositoryTest;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.orgmanager.util.JsonMapper;
|
||||
import com.rbkmoney.orgmanager.util.TestData;
|
||||
import com.rbkmoney.swag.organizations.model.InvitationListResult;
|
||||
import com.rbkmoney.swag.organizations.model.InvitationStatusName;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@DirtiesContext
|
||||
@SpringBootTest(classes = OrgManagerApplication.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(initializers = InvitationRepositoryTest.Initializer.class)
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class InvitationServiceIntegrationTest extends AbstractRepositoryTest {
|
||||
|
||||
private static final String INVITATION_ID = "invitationId";
|
||||
@ -36,30 +23,21 @@ public class InvitationServiceIntegrationTest extends AbstractRepositoryTest {
|
||||
|
||||
private static final String MEMBER_ID = "memberId";
|
||||
|
||||
@Autowired
|
||||
private OrganizationRepository organizationRepository;
|
||||
|
||||
@Autowired
|
||||
private InvitationRepository invitationRepository;
|
||||
|
||||
@Autowired
|
||||
private InvitationService invitationService;
|
||||
|
||||
@Autowired
|
||||
private JsonMapper jsonMapper;
|
||||
|
||||
@Test
|
||||
public void checkOnInvitationStatus() {
|
||||
void checkOnInvitationStatus() {
|
||||
InvitationEntity expiredInvite =
|
||||
TestData.buildInvitation(ORGANIZATION_ID, INVITATION_ID, LocalDateTime.now().minusDays(1));
|
||||
invitationRepository.save(expiredInvite);
|
||||
invitationService.checkAndModifyExpiredStatus();
|
||||
expiredInvite = invitationRepository.findById(INVITATION_ID).get();
|
||||
Assert.assertEquals(InvitationStatusName.EXPIRED.getValue(), expiredInvite.getStatus());
|
||||
assertEquals(InvitationStatusName.EXPIRED.getValue(), expiredInvite.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkExpiredInviteWithPendingStatus() {
|
||||
void checkExpiredInviteWithPendingStatus() {
|
||||
OrganizationEntity organizationEntity = TestData.buildOrganization(ORGANIZATION_ID, MEMBER_ID);
|
||||
organizationRepository.save(organizationEntity);
|
||||
|
||||
@ -71,7 +49,7 @@ public class InvitationServiceIntegrationTest extends AbstractRepositoryTest {
|
||||
|
||||
ResponseEntity<InvitationListResult> responseEntity =
|
||||
invitationService.list(ORGANIZATION_ID, InvitationStatusName.PENDING);
|
||||
Assert.assertEquals(1, responseEntity.getBody().getResult().size());
|
||||
assertEquals(1, responseEntity.getBody().getResult().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,11 +6,11 @@ import com.rbkmoney.orgmanager.repository.InvitationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import org.apache.thrift.TException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@ -20,7 +20,7 @@ import java.util.Optional;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class InvitationServiceTest {
|
||||
|
||||
@Mock private InvitationConverter invitationConverter;
|
||||
@ -33,7 +33,7 @@ public class InvitationServiceTest {
|
||||
private InvitationService service;
|
||||
|
||||
@Test
|
||||
public void shouldCreate() throws TException {
|
||||
void shouldCreate() throws TException {
|
||||
// Given
|
||||
InvitationRequest invitation = new InvitationRequest();
|
||||
InvitationEntity entity = new InvitationEntity();
|
||||
@ -62,7 +62,7 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet() {
|
||||
void shouldGet() {
|
||||
// Given
|
||||
String invitationId = "invitationId";
|
||||
InvitationEntity entity = new InvitationEntity();
|
||||
@ -84,7 +84,7 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFound() {
|
||||
void shouldReturnNotFound() {
|
||||
// Given
|
||||
String invitationId = "invitationId";
|
||||
|
||||
@ -102,7 +102,7 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindByOrganizationIdAndStatus() {
|
||||
void shouldFindByOrganizationIdAndStatus() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
|
||||
@ -129,7 +129,7 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfOrganizationDoesNotExist() {
|
||||
void shouldReturnNotFoundIfOrganizationDoesNotExist() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
when(organizationRepository.existsById(orgId))
|
||||
@ -146,7 +146,7 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRevoke() {
|
||||
void shouldRevoke() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
String invitationId = "invitationId";
|
||||
@ -172,7 +172,7 @@ public class InvitationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfInvitationDoesNotExist() {
|
||||
void shouldReturnNotFoundIfInvitationDoesNotExist() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
String invitationId = "invitationId";
|
||||
|
@ -8,11 +8,11 @@ import com.rbkmoney.orgmanager.repository.OrganizationRoleRepository;
|
||||
import com.rbkmoney.swag.organizations.model.Role;
|
||||
import com.rbkmoney.swag.organizations.model.RoleAvailableListResult;
|
||||
import com.rbkmoney.swag.organizations.model.RoleId;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@ -22,7 +22,7 @@ import java.util.Set;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class OrganizationRoleServiceTest {
|
||||
|
||||
@Mock private OrganizationRepository organizationRepository;
|
||||
@ -33,7 +33,7 @@ public class OrganizationRoleServiceTest {
|
||||
private OrganizationRoleService service;
|
||||
|
||||
@Test
|
||||
public void shouldListRoles() {
|
||||
void shouldListRoles() {
|
||||
// Given
|
||||
OrganizationRoleEntity organizationRoleEntity = new OrganizationRoleEntity();
|
||||
Role role = new Role();
|
||||
@ -61,7 +61,7 @@ public class OrganizationRoleServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfNoOrganizationExistForRolesList() {
|
||||
void shouldReturnNotFoundIfNoOrganizationExistForRolesList() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
|
||||
@ -79,7 +79,7 @@ public class OrganizationRoleServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindRoleById() {
|
||||
void shouldFindRoleById() {
|
||||
// Given
|
||||
OrganizationRoleEntity organizationRoleEntity = new OrganizationRoleEntity();
|
||||
Role role = new Role();
|
||||
@ -105,7 +105,7 @@ public class OrganizationRoleServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfOrganizationDoesNotExist() {
|
||||
void shouldReturnNotFoundIfOrganizationDoesNotExist() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
RoleId roleId = RoleId.ADMINISTRATOR;
|
||||
|
@ -9,11 +9,11 @@ import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.swag.organizations.model.Member;
|
||||
import com.rbkmoney.swag.organizations.model.MemberOrgListResult;
|
||||
import com.rbkmoney.swag.organizations.model.Organization;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@ -23,7 +23,7 @@ import java.util.Set;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class OrganizationServiceTest {
|
||||
|
||||
@Mock private OrganizationConverter organizationConverter;
|
||||
@ -35,7 +35,7 @@ public class OrganizationServiceTest {
|
||||
private OrganizationService service;
|
||||
|
||||
@Test
|
||||
public void shouldCreate() {
|
||||
void shouldCreate() {
|
||||
// Given
|
||||
Organization organization = new Organization();
|
||||
OrganizationEntity entity = new OrganizationEntity();
|
||||
@ -62,7 +62,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet() {
|
||||
void shouldGet() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
OrganizationEntity entity = new OrganizationEntity();
|
||||
@ -84,7 +84,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFound() {
|
||||
void shouldReturnNotFound() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
|
||||
@ -102,7 +102,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldListMembers() {
|
||||
void shouldListMembers() {
|
||||
// Given
|
||||
MemberEntity memberEntity = new MemberEntity();
|
||||
Member member = new Member();
|
||||
@ -130,7 +130,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfNoOrganizationExistForMembersList() {
|
||||
void shouldReturnNotFoundIfNoOrganizationExistForMembersList() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
|
||||
@ -148,7 +148,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindMemberById() {
|
||||
void shouldFindMemberById() {
|
||||
// Given
|
||||
MemberEntity memberEntity = new MemberEntity();
|
||||
Member member = new Member();
|
||||
@ -171,7 +171,7 @@ public class OrganizationServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfMemberDoesNotExist() {
|
||||
void shouldReturnNotFoundIfMemberDoesNotExist() {
|
||||
// Given
|
||||
String userId = "userId";
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user