0

In my project, I was trying to convert a Flux object to a different one and to save as a list of Flux. createPostTagsReactor() method uses convertToPost for some business logic.

    @Test
public void creatPostTagsReactor(){
    Map<String, Tag> nameMapTag = tagRepository.findAll().collectList().block()
            .parallelStream()
            .collect(Collectors.toMap(t-> t.getTagName(), t-> t));
    Flux<PostTags> postTagsFlux =  postRepository.findAll()
            .flatMap(p-> convertToPostTags(p, nameMapTag));
    postTagsRepository.saveAll(postTagsFlux).subscribe();
}

Flux<PostTags> convertToPostTags(Post post, Map<String, Tag> nameMapTag){
    List<PostTags> postTags = new ArrayList<>();
    return TopicsModelingUtils
            .extractTagsReactive(post.getTags())
            .map(t-> PostTags.builder().postId(post.getId()).tagId(nameMapTag.get(t).getId()).build());
}

Method extractTagsReactive is borrowed from an Util class and the method body is:

 public static Flux<String> extractTagsReactive(String tags){
    tags = tags.replaceAll("<","");
    tags = tags.replaceAll(">",",");
    List<String> tagList = new LinkedList<>(StringUtils.commaDelimitedListToSet(tags));
    tagList.remove(tagList.size()-1);
    return Flux.fromIterable(tagList);
}

Though the test is passed, but no data is saved. I tried debugging and found convertToPostTags is called once for the first Post object, then nothing happens.

I also tried to move the logic in a service class and used @Transactional annotation, but no success.

  • 1
    are you sure your test doesn't simply exit early due to the `.subscribe()` (that is non-blocking)? inside a test, you could try `.blockLast()` instead. – Simon Baslé Jun 03 '21 at 08:16
  • @SimonBaslé, you r right, my test was not executing as I was using .subscribe(), when I used .blockLast() it worked. – Monjur Morshed Jun 03 '21 at 14:54

0 Answers0