diff --git a/api-lib-core/src/main/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileService.java b/api-lib-core/src/main/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileService.java index d9ca9046c64c98064c18c544c2ca2adb6cf60a77..856c1325ec781b8736b709177e3e038a7287f609 100644 --- a/api-lib-core/src/main/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileService.java +++ b/api-lib-core/src/main/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileService.java @@ -1,22 +1,25 @@ 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)); + } } diff --git a/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcGetUserProfileResponseTestFactory.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcGetUserProfileResponseTestFactory.java index 829e0ffef5c3070e3e0d7090858f91e881151617..9531b0254f5295d7209f612773c14875ea76789c 100644 --- a/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcGetUserProfileResponseTestFactory.java +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcGetUserProfileResponseTestFactory.java @@ -1,12 +1,16 @@ 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()); + } + } diff --git a/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileServiceTest.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileServiceTest.java index 5bf4b1ef3115783c70d00a06e8495e282454fe89..9719976a1fbbb223fbd601329195c91a0efe60ef 100644 --- a/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileServiceTest.java +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcOzgCloudUserProfileServiceTest.java @@ -1,40 +1,83 @@ 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()); + } + } + } diff --git a/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcUserProfileTestFactory.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcUserProfileTestFactory.java index f7ab6cd1a49cb82af545edd67ab5542b0f19c384..17811513445da825184b954ea0173fce23edaed7 100644 --- a/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcUserProfileTestFactory.java +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/user/GrpcUserProfileTestFactory.java @@ -1,13 +1,19 @@ 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); + + } + }