Skip to content
Snippets Groups Projects
Commit edb15755 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4606 add callcontext to request to user manager

parent 7e632de7
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.apilib.user;
import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextAttachingInterceptor;
import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider;
import de.ozgcloud.user.grpc.userprofile.UserProfileServiceGrpc.UserProfileServiceBlockingStub;
import de.ozgcloud.user.userprofile.GrpcGetUserProfileRequest;
import lombok.RequiredArgsConstructor;
import net.devh.boot.grpc.client.inject.GrpcClient;
@RequiredArgsConstructor
public class GrpcOzgCloudUserProfileService
implements OzgCloudUserProfileService {
public class GrpcOzgCloudUserProfileService implements OzgCloudUserProfileService {
@GrpcClient("user-manager")
private final UserProfileServiceBlockingStub serviceStub;
private final UserProfileMapper mapper;
private final OzgCloudCallContextProvider contextProvider;
@Override
public OzgCloudUserProfile getById(OzgCloudUserId userId) {
var grpcUserProfileResponse = serviceStub.getById(buildGetUserRequest(userId));
var grpcUserProfileResponse = getUserProfileServiceStub().getById(buildGetUserRequest(userId));
return mapper.mapFromGrpc(grpcUserProfileResponse.getUserProfile());
}
......@@ -26,4 +29,7 @@ public class GrpcOzgCloudUserProfileService
.build();
}
private UserProfileServiceBlockingStub getUserProfileServiceStub() {
return serviceStub.withInterceptors(new OzgCloudCallContextAttachingInterceptor(contextProvider));
}
}
package de.ozgcloud.apilib.user;
import de.ozgcloud.user.userprofile.GrpcGetUserProfileResponse;
public class GrpcGetUserProfileResponseTestFactory {
/*
* public static GrpcGetUserProfileResponse create() { return
* createBuilder().build(); }
*
* public static GrpcGetUserProfileResponse.Builder createBuilder() { return
* GrpcGetUserProfileResponse.newBuilder()
* .setUserProfile(GrpcUserProfileTestFactory.create()); }
*/
public static GrpcGetUserProfileResponse create() {
return createBuilder().build();
}
public static GrpcGetUserProfileResponse.Builder createBuilder() {
return GrpcGetUserProfileResponse.newBuilder()
.setUserProfile(GrpcUserProfileTestFactory.create());
}
}
package de.ozgcloud.apilib.user;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider;
import de.ozgcloud.user.grpc.userprofile.UserProfileServiceGrpc.UserProfileServiceBlockingStub;
import de.ozgcloud.user.userprofile.GrpcGetUserProfileRequest;
class GrpcOzgCloudUserProfileServiceTest {
/*
* @InjectMocks private GrpcOzgCloudUserProfileService service;
*
* @Mock private UserProfileServiceBlockingStub userProfileServiceStub;
*
* @Mock private UserProfileMapper mapper;
*
* @Nested class TestGetById {
*
* @Captor private ArgumentCaptor<GrpcGetUserProfileRequest> requestCaptor;
*
* private OzgCloudUserProfile mappedUserProfile =
* OzgCloudUserProfileTestFactory.create();
*
* @BeforeEach void init() {
* when(userProfileServiceStub.getById(any())).thenReturn(
* GrpcGetUserProfileResponseTestFactory.create());
* when(mapper.mapFromGrpc(any())).thenReturn(mappedUserProfile); }
*
* @Test void shouldCallStub() {
* service.getById(OzgCloudUserProfileTestFactory.ID);
*
* verify(userProfileServiceStub).getById(requestCaptor.capture());
* assertThat(requestCaptor.getValue().getUserId()).isEqualTo(
* OzgCloudUserProfileTestFactory.ID.toString()); }
*
* @Test void shouldCallMapper() {
* service.getById(OzgCloudUserProfileTestFactory.ID);
*
* verify(mapper).mapFromGrpc(any()); }
*
* @Test void shouldReturnResult() { var result =
* service.getById(OzgCloudUserProfileTestFactory.ID);
*
* assertThat(result).isSameAs(mappedUserProfile); } }
*/
@InjectMocks
private GrpcOzgCloudUserProfileService service;
@Mock
private UserProfileServiceBlockingStub userProfileServiceStub;
@Mock
private UserProfileMapper mapper;
@Mock
private OzgCloudCallContextProvider contextProvider;
@BeforeEach
void prepareStubMock() {
when(userProfileServiceStub.withInterceptors(any())).thenReturn(userProfileServiceStub);
}
@Nested
class TestGetById {
@Captor
private ArgumentCaptor<GrpcGetUserProfileRequest> requestCaptor;
private OzgCloudUserProfile mappedUserProfile = OzgCloudUserProfileTestFactory.create();
@BeforeEach
void init() {
when(userProfileServiceStub.getById(any())).thenReturn(
GrpcGetUserProfileResponseTestFactory.create());
when(mapper.mapFromGrpc(any())).thenReturn(mappedUserProfile);
}
@Test
void shouldCallStub() {
service.getById(OzgCloudUserProfileTestFactory.ID);
verify(userProfileServiceStub).getById(requestCaptor.capture());
assertThat(requestCaptor.getValue().getUserId()).isEqualTo(
OzgCloudUserProfileTestFactory.ID.toString());
}
@Test
void shouldCallMapper() {
service.getById(OzgCloudUserProfileTestFactory.ID);
verify(mapper).mapFromGrpc(any());
}
@Test
void shouldReturnResult() {
var result = service.getById(OzgCloudUserProfileTestFactory.ID);
assertThat(result).isSameAs(mappedUserProfile);
}
@Test
void shouldAddInterceptor() {
service.getById(OzgCloudUserProfileTestFactory.ID);
verify(userProfileServiceStub).withInterceptors(notNull());
}
}
}
package de.ozgcloud.apilib.user;
import static de.ozgcloud.apilib.user.OzgCloudUserProfileTestFactory.*;
import de.ozgcloud.user.userprofile.GrpcUserProfile;
public class GrpcUserProfileTestFactory {
/*
* public static GrpcUserProfile create() { return createBuilder().build(); }
*
* public static GrpcUserProfile.Builder createBuilder() { return
* GrpcUserProfile.newBuilder() .setId(ID.toString()) .setFirstName(FIRST_NAME)
* .setLastName(LAST_NAME);
*
* }
*/
public static GrpcUserProfile create() {
return createBuilder().build();
}
public static GrpcUserProfile.Builder createBuilder() {
return GrpcUserProfile.newBuilder().setId(ID.toString()).setFirstName(FIRST_NAME)
.setLastName(LAST_NAME);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment