mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 00:15:23 +00:00
Added imp;
This commit is contained in:
parent
7bb623537e
commit
230c5e3b7b
5
pom.xml
5
pom.xml
@ -54,6 +54,11 @@
|
||||
<artifactId>swag-organizations</artifactId>
|
||||
<version>1.8-1de59d5-epic-server</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>org-management-proto</artifactId>
|
||||
<version>1.2-06c5c84</version>
|
||||
</dependency>
|
||||
|
||||
<!--spring-->
|
||||
<dependency>
|
||||
|
@ -0,0 +1,8 @@
|
||||
package com.rbkmoney.orgmanager.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
}
|
@ -1,10 +1,14 @@
|
||||
package com.rbkmoney.orgmanager.converter;
|
||||
|
||||
import com.rbkmoney.bouncer.context.v1.*;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.swag.organizations.model.Member;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
@Service
|
||||
@ -12,6 +16,7 @@ import static java.util.stream.Collectors.toSet;
|
||||
public class MemberConverter {
|
||||
|
||||
private final MemberRoleConverter memberRoleConverter;
|
||||
private final OrganizationConverter organizationConverter;
|
||||
|
||||
public Member toDomain(MemberEntity entity) {
|
||||
return new Member()
|
||||
@ -21,4 +26,16 @@ public class MemberConverter {
|
||||
.map(memberRoleConverter::toDomain)
|
||||
.collect(toSet()));
|
||||
}
|
||||
|
||||
public User toThrift(MemberEntity member) {
|
||||
return new User()
|
||||
.setId(member.getId())
|
||||
.setRealm(new Entity())
|
||||
.setEmail(member.getEmail())
|
||||
.setOrgs(member.getOrganizations() == null ? null :
|
||||
member.getOrganizations()
|
||||
.stream()
|
||||
.map(organizationConverter::toThrift)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.rbkmoney.orgmanager.converter;
|
||||
|
||||
import com.rbkmoney.bouncer.context.v1.Entity;
|
||||
import com.rbkmoney.bouncer.context.v1.OrgRole;
|
||||
import com.rbkmoney.bouncer.context.v1.OrgRoleScope;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.util.JsonMapper;
|
||||
import com.rbkmoney.swag.organizations.model.Organization;
|
||||
@ -10,6 +13,7 @@ import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ -35,4 +39,17 @@ public class OrganizationConverter {
|
||||
.owner(entity.getOwner())
|
||||
.metadata(jsonMapper.toMap(entity.getMetadata()));
|
||||
}
|
||||
|
||||
public com.rbkmoney.bouncer.context.v1.Organization toThrift(OrganizationEntity e) {
|
||||
return new com.rbkmoney.bouncer.context.v1.Organization()
|
||||
.setId(e.getId())
|
||||
.setOwner(new Entity())
|
||||
.setRoles(e.getRoles() == null ? null :
|
||||
e.getRoles().stream()
|
||||
.map(r -> new OrgRole()
|
||||
.setId(r.getId())
|
||||
.setScope(new OrgRoleScope()
|
||||
.setShop(new Entity())))
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
|
@ -26,5 +26,14 @@ public class MemberEntity implements Serializable {
|
||||
inverseJoinColumns = @JoinColumn(name = "member_role_id"))
|
||||
private Set<MemberRoleEntity> roles;
|
||||
|
||||
@ToString.Exclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
@JoinTable(
|
||||
name = "member_to_organization",
|
||||
joinColumns = @JoinColumn(name = "member_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "organization_id"))
|
||||
private Set<OrganizationEntity> organizations;
|
||||
|
||||
private String email;
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
package com.rbkmoney.orgmanager.service;
|
||||
|
||||
import com.rbkmoney.bouncer.ctx.ContextFragment;
|
||||
import com.rbkmoney.bouncer.ctx.ContextFragmentType;
|
||||
import com.rbkmoney.orgmanagement.AuthContextProviderSrv;
|
||||
import com.rbkmoney.orgmanagement.UserNotFound;
|
||||
import com.rbkmoney.orgmanager.converter.MemberConverter;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.TSerializer;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AuthContextService implements AuthContextProviderSrv.Iface {
|
||||
|
||||
private final MemberRepository memberRepository;
|
||||
private final MemberConverter memberConverter;
|
||||
|
||||
@Override
|
||||
public ContextFragment getUserContext(String id) throws UserNotFound, TException {
|
||||
|
||||
Optional<MemberEntity> entity = memberRepository.findById(id);
|
||||
if (entity.isEmpty()) {
|
||||
throw new UserNotFound();
|
||||
}
|
||||
MemberEntity member = entity.get();
|
||||
TSerializer tSerializer = new TSerializer();
|
||||
return new ContextFragment()
|
||||
.setType(ContextFragmentType.v1_thrift_binary)
|
||||
.setContent(tSerializer.serialize(memberConverter.toThrift(member)));
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.rbkmoney.orgmanager.servlet;
|
||||
|
||||
import com.rbkmoney.orgmanagement.AuthContextProviderSrv;
|
||||
import com.rbkmoney.woody.thrift.impl.http.THServiceBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet("/auth-context")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AuthContextProviderServlet extends GenericServlet {
|
||||
|
||||
private Servlet thriftServlet;
|
||||
|
||||
private final AuthContextProviderSrv.Iface authContextProvider;
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
thriftServlet = new THServiceBuilder()
|
||||
.build(AuthContextProviderSrv.Iface.class, authContextProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
|
||||
thriftServlet.service(req, res);
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
package com.rbkmoney.orgmanager.converter;
|
||||
|
||||
import com.rbkmoney.bouncer.context.v1.User;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -26,17 +29,17 @@ public class MemberConverterTest {
|
||||
when(memberRoleConverter.toEntity(any(MemberRole.class), anyString()))
|
||||
.thenReturn(new MemberRoleEntity());
|
||||
|
||||
converter = new MemberConverter(memberRoleConverter);
|
||||
OrganizationConverter organizationConverter = mock(OrganizationConverter.class);
|
||||
when(organizationConverter.toThrift(any(OrganizationEntity.class)))
|
||||
.thenReturn(new com.rbkmoney.bouncer.context.v1.Organization());
|
||||
|
||||
converter = new MemberConverter(memberRoleConverter, organizationConverter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
// Given
|
||||
MemberEntity entity = MemberEntity.builder()
|
||||
.id("id")
|
||||
.email("email")
|
||||
.roles(Set.of(new MemberRoleEntity()))
|
||||
.build();
|
||||
MemberEntity entity = buildMemberEntity();
|
||||
|
||||
// When
|
||||
Member member = converter.toDomain(entity);
|
||||
@ -49,4 +52,23 @@ public class MemberConverterTest {
|
||||
|
||||
assertThat(member).isEqualToComparingFieldByField(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public 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());
|
||||
}
|
||||
|
||||
private MemberEntity buildMemberEntity() {
|
||||
return MemberEntity.builder()
|
||||
.id("id")
|
||||
.email("email")
|
||||
.roles(Set.of(new MemberRoleEntity()))
|
||||
.organizations(Set.of(new OrganizationEntity()))
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,7 @@ package com.rbkmoney.orgmanager.converter;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;
|
||||
@ -10,8 +11,10 @@ import org.junit.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.Assert.assertEquals;
|
||||
|
||||
public class OrganizationConverterTest {
|
||||
|
||||
@ -50,13 +53,7 @@ public class OrganizationConverterTest {
|
||||
@Test
|
||||
public void shouldConvertToDomain() {
|
||||
// Given
|
||||
OrganizationEntity entity = OrganizationEntity.builder()
|
||||
.id("id")
|
||||
.createdAt(LocalDateTime.parse("2019-08-24T14:15:22"))
|
||||
.name("org")
|
||||
.owner("own")
|
||||
.metadata("{\"a\":\"b\"}")
|
||||
.build();
|
||||
OrganizationEntity entity = buildOrganizationEntity();
|
||||
|
||||
// When
|
||||
Organization organization = converter.toDomain(entity);
|
||||
@ -71,4 +68,23 @@ public class OrganizationConverterTest {
|
||||
|
||||
assertThat(organization).isEqualToComparingFieldByField(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertToThrift(){
|
||||
OrganizationEntity organizationEntity = buildOrganizationEntity();
|
||||
var organization = converter.toThrift(organizationEntity);
|
||||
assertEquals(organizationEntity.getId(), organization.getId());
|
||||
assertEquals(organizationEntity.getRoles().size(), organization.getRoles().size());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.rbkmoney.orgmanager.service;
|
||||
|
||||
import com.rbkmoney.bouncer.context.v1.User;
|
||||
import com.rbkmoney.bouncer.ctx.ContextFragment;
|
||||
import com.rbkmoney.bouncer.ctx.ContextFragmentType;
|
||||
import com.rbkmoney.orgmanagement.UserNotFound;
|
||||
import com.rbkmoney.orgmanager.converter.MemberConverter;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import org.apache.thrift.TException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AuthContextServiceTest {
|
||||
|
||||
@Mock
|
||||
private MemberRepository memberRepository;
|
||||
@Mock
|
||||
private MemberConverter memberConverter;
|
||||
|
||||
@InjectMocks
|
||||
private AuthContextService service;
|
||||
|
||||
@Test
|
||||
public void testUserContext() throws TException {
|
||||
String id = "1";
|
||||
when(memberRepository.findById(id)).thenReturn(Optional.of(new MemberEntity()));
|
||||
when(memberConverter.toThrift(any())).thenReturn(new User());
|
||||
|
||||
ContextFragment userContext = service.getUserContext(id);
|
||||
|
||||
verify(memberRepository, times(1)).findById(id);
|
||||
verify(memberConverter, times(1)).toThrift(any());
|
||||
assertEquals(ContextFragmentType.v1_thrift_binary, userContext.getType());
|
||||
}
|
||||
|
||||
@Test(expected = UserNotFound.class)
|
||||
public void testUserNotFound() throws TException {
|
||||
String id = "1";
|
||||
when(memberRepository.findById(id)).thenReturn(Optional.empty() );
|
||||
service.getUserContext(id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user