Код: Выделить всё
@NonNull
@Override
public Mono createAsset(@NonNull final CreateFileAssetInput input) {
return serviceStub.flatMap(stub -> Mono.create(sink -> {
stub.migrateFileAsset(
assetToProtoAssetMapper.toProtoMigrateFileAssetRequest(input),
new MigrateFileAssetResponseStreamObserver(sink, assetToProtoAssetMapper));
})
.doFirst(() -> log.atInfo()
.addKeyValue("name", input.name())
.addKeyValue("createdBy", input.createdBy())
.log("migrating asset"))
.doOnSuccess(asset -> log.atInfo().addKeyValue("id", asset.id()).log("Migrated File Asset"))
.doOnError(throwable -> log.atError()
.addKeyValue("name", input.name())
.addKeyValue("createdBy", input.createdBy())
.setCause(throwable)
.log("failed to migrate file asset")));
}
Код: Выделить всё
private Mono serviceStubMono;
@Mock
private ContentManagementRespositoryServiceStub serviceStub;
@Mock
private AssetToProtoAssetMapper assetToProtoAssetMapper;
@private GrpcContentRepositoryService grpcContentRepositoryService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
serviceStubMono = Mono.just(serviceStub);
grpcContentRepositoryService = new GrpcContentRepositoryService(serviceStubMono, assetToProtoAssetMapper);
}
@Test
void testCreateAsset_hasUuidId() {
// Mock Input Data
CreateFileAssetInput input = CreateFileAssetInput.builder()
.name("example-asset")
.path("/example/path")
.type(AssetType.DOCUMENT)
.content("example-data".getBytes())
.createdBy("test-user")
.build();
// Mock UUID Response from gRPC service
UUID fakeUuid = UUID.randomUUID();
when(assetToProtoAssetMapper.toProtoMigrateFileAssetRequest(input))
.thenReturn(MigrateFileAssetRequest.getDefaultInstance());
Asset expectedAsset = Asset.builder()
.id(fakeUuid)
.name("example-asset")
.path("/example/path")
.type(AssetType.DOCUMENT)
.createdBy("test-user")
.build();
when(assetToProtoAssetMapper.toModel(any()))
.thenReturn(expectedAsset);
doAnswer(invocation -> {
MigrateFileAssetResponse response = MigrateFileAssetResponse.newBuilder()
.setAsset(com.example.AssetProto.newBuilder()
.setId(fakeUuid.toString())
.setName("example-asset")
.build())
.build();
StreamObserver observer = invocation.getArgument(1);
observer.onNext(response);
observer.onCompleted();
return null;
})
.when(serviceStub)
.migrateFileAsset(any(), any());
Mono resultMono = grpcContentRepositoryService.createAsset(input);
StepVerifier.create(resultMono)
.assertNext(asset -> {
assertNotNull(asset, "Asset should not be null");
assertEquals(fakeUuid, asset.id(), "Asset ID should match the fake UUID");
assertEquals("example-asset", asset.name(), "Asset name should match");
assertEquals("/example/path", asset.path(), "Asset path should match");
})
.verifyComplete();
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... ing-really
Мобильная версия