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; 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.grpc.userprofile.UserProfileServiceGrpc.UserProfileServiceBlockingStub;
import de.ozgcloud.user.userprofile.GrpcGetUserProfileRequest; import de.ozgcloud.user.userprofile.GrpcGetUserProfileRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.devh.boot.grpc.client.inject.GrpcClient; import net.devh.boot.grpc.client.inject.GrpcClient;
@RequiredArgsConstructor @RequiredArgsConstructor
public class GrpcOzgCloudUserProfileService public class GrpcOzgCloudUserProfileService implements OzgCloudUserProfileService {
implements OzgCloudUserProfileService {
@GrpcClient("user-manager") @GrpcClient("user-manager")
private final UserProfileServiceBlockingStub serviceStub; private final UserProfileServiceBlockingStub serviceStub;
private final UserProfileMapper mapper; private final UserProfileMapper mapper;
private final OzgCloudCallContextProvider contextProvider;
@Override @Override
public OzgCloudUserProfile getById(OzgCloudUserId userId) { public OzgCloudUserProfile getById(OzgCloudUserId userId) {
var grpcUserProfileResponse = serviceStub.getById(buildGetUserRequest(userId)); var grpcUserProfileResponse = getUserProfileServiceStub().getById(buildGetUserRequest(userId));
return mapper.mapFromGrpc(grpcUserProfileResponse.getUserProfile()); return mapper.mapFromGrpc(grpcUserProfileResponse.getUserProfile());
} }
...@@ -26,4 +29,7 @@ public class GrpcOzgCloudUserProfileService ...@@ -26,4 +29,7 @@ public class GrpcOzgCloudUserProfileService
.build(); .build();
} }
private UserProfileServiceBlockingStub getUserProfileServiceStub() {
return serviceStub.withInterceptors(new OzgCloudCallContextAttachingInterceptor(contextProvider));
}
} }
package de.ozgcloud.apilib.user; package de.ozgcloud.apilib.user;
import de.ozgcloud.user.userprofile.GrpcGetUserProfileResponse;
public class GrpcGetUserProfileResponseTestFactory { public class GrpcGetUserProfileResponseTestFactory {
/*
* public static GrpcGetUserProfileResponse create() { return public static GrpcGetUserProfileResponse create() {
* createBuilder().build(); } return createBuilder().build();
* }
* public static GrpcGetUserProfileResponse.Builder createBuilder() { return
* GrpcGetUserProfileResponse.newBuilder() public static GrpcGetUserProfileResponse.Builder createBuilder() {
* .setUserProfile(GrpcUserProfileTestFactory.create()); } return GrpcGetUserProfileResponse.newBuilder()
*/ .setUserProfile(GrpcUserProfileTestFactory.create());
}
} }
package de.ozgcloud.apilib.user; 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 { class GrpcOzgCloudUserProfileServiceTest {
/*
* @InjectMocks private GrpcOzgCloudUserProfileService service; @InjectMocks
* private GrpcOzgCloudUserProfileService service;
* @Mock private UserProfileServiceBlockingStub userProfileServiceStub;
* @Mock
* @Mock private UserProfileMapper mapper; private UserProfileServiceBlockingStub userProfileServiceStub;
* @Mock
* @Nested class TestGetById { private UserProfileMapper mapper;
*
* @Captor private ArgumentCaptor<GrpcGetUserProfileRequest> requestCaptor; @Mock
* private OzgCloudCallContextProvider contextProvider;
* private OzgCloudUserProfile mappedUserProfile =
* OzgCloudUserProfileTestFactory.create(); @BeforeEach
* void prepareStubMock() {
* @BeforeEach void init() { when(userProfileServiceStub.withInterceptors(any())).thenReturn(userProfileServiceStub);
* when(userProfileServiceStub.getById(any())).thenReturn( }
* GrpcGetUserProfileResponseTestFactory.create());
* when(mapper.mapFromGrpc(any())).thenReturn(mappedUserProfile); } @Nested
* class TestGetById {
* @Test void shouldCallStub() {
* service.getById(OzgCloudUserProfileTestFactory.ID); @Captor
* private ArgumentCaptor<GrpcGetUserProfileRequest> requestCaptor;
* verify(userProfileServiceStub).getById(requestCaptor.capture());
* assertThat(requestCaptor.getValue().getUserId()).isEqualTo( private OzgCloudUserProfile mappedUserProfile = OzgCloudUserProfileTestFactory.create();
* OzgCloudUserProfileTestFactory.ID.toString()); }
* @BeforeEach
* @Test void shouldCallMapper() { void init() {
* service.getById(OzgCloudUserProfileTestFactory.ID); when(userProfileServiceStub.getById(any())).thenReturn(
* GrpcGetUserProfileResponseTestFactory.create());
* verify(mapper).mapFromGrpc(any()); } when(mapper.mapFromGrpc(any())).thenReturn(mappedUserProfile);
* }
* @Test void shouldReturnResult() { var result =
* service.getById(OzgCloudUserProfileTestFactory.ID); @Test
* void shouldCallStub() {
* assertThat(result).isSameAs(mappedUserProfile); } } 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; package de.ozgcloud.apilib.user;
import static de.ozgcloud.apilib.user.OzgCloudUserProfileTestFactory.*;
import de.ozgcloud.user.userprofile.GrpcUserProfile;
public class GrpcUserProfileTestFactory { public class GrpcUserProfileTestFactory {
/*
* public static GrpcUserProfile create() { return createBuilder().build(); } 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.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