From 8e7991df0cf386cf4801a16ac92d42a584b8298d Mon Sep 17 00:00:00 2001 From: Erling Jahr Date: Mon, 20 Nov 2023 15:59:58 +0100 Subject: [PATCH] added producer for user --- .../member/MemberEntityProducerService.java | 3 +- .../user/UserEntityProducerService.java | 40 +++++++++++++++++++ .../java/no/fintlabs/user/UserService.java | 8 ++-- .../no/fintlabs/user/UserServiceTest.java | 4 +- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/main/java/no/fintlabs/user/UserEntityProducerService.java diff --git a/src/main/java/no/fintlabs/member/MemberEntityProducerService.java b/src/main/java/no/fintlabs/member/MemberEntityProducerService.java index 4b7aa76..075a2a5 100644 --- a/src/main/java/no/fintlabs/member/MemberEntityProducerService.java +++ b/src/main/java/no/fintlabs/member/MemberEntityProducerService.java @@ -19,7 +19,8 @@ public class MemberEntityProducerService { private final EntityTopicService entityTopicService; - public MemberEntityProducerService(EntityProducerFactory entityProducerFactory, EntityTopicService entityTopicService) { + public MemberEntityProducerService(EntityProducerFactory entityProducerFactory, + EntityTopicService entityTopicService) { entityProducer = entityProducerFactory.createProducer(Member.class); this.entityTopicService = entityTopicService; entityTopicNameParameters = EntityTopicNameParameters diff --git a/src/main/java/no/fintlabs/user/UserEntityProducerService.java b/src/main/java/no/fintlabs/user/UserEntityProducerService.java new file mode 100644 index 0000000..6b3f56e --- /dev/null +++ b/src/main/java/no/fintlabs/user/UserEntityProducerService.java @@ -0,0 +1,40 @@ +package no.fintlabs.user; + +import lombok.extern.slf4j.Slf4j; +import no.fintlabs.kafka.entity.EntityProducer; +import no.fintlabs.kafka.entity.EntityProducerFactory; +import no.fintlabs.kafka.entity.EntityProducerRecord; +import no.fintlabs.kafka.entity.topic.EntityTopicNameParameters; +import no.fintlabs.kafka.entity.topic.EntityTopicService; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class UserEntityProducerService { + private final EntityProducer entityProducer; + private final EntityTopicNameParameters entityTopicNameParameters; + + public UserEntityProducerService(EntityProducerFactory entityProducerFactory, + EntityTopicService entityTopicService) { + entityProducer = entityProducerFactory.createProducer(User.class); + entityTopicNameParameters = EntityTopicNameParameters + .builder() + .resource("kontrolluser") + .build(); + entityTopicService.ensureTopic(entityTopicNameParameters,0); + } + + public void publish(User user){ + String key = user.getResourceId(); + entityProducer.send( + EntityProducerRecord.builder() + .topicNameParameters(entityTopicNameParameters) + .key(key) + .value(user) + .build() + ); + } + + + +} diff --git a/src/main/java/no/fintlabs/user/UserService.java b/src/main/java/no/fintlabs/user/UserService.java index 042c06e..d2a625a 100644 --- a/src/main/java/no/fintlabs/user/UserService.java +++ b/src/main/java/no/fintlabs/user/UserService.java @@ -9,23 +9,23 @@ import java.util.Collection; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.function.Consumer; -import java.util.stream.Stream; @Service @Slf4j public class UserService { private final UserRepository userRepository; + private final UserEntityProducerService userEntityProducerService; private final MemberService memberService; private final AuthorizationClient authorizationClient; - public UserService(UserRepository userRepository, MemberService memberService, AuthorizationClient authorizationClient) { + public UserService(UserRepository userRepository, UserEntityProducerService userEntityProducerService, MemberService memberService, AuthorizationClient authorizationClient) { this.userRepository = userRepository; + this.userEntityProducerService = userEntityProducerService; this.memberService = memberService; this.authorizationClient = authorizationClient; } @@ -44,6 +44,7 @@ private Runnable onSaveNewUser(User user) { User newUser = userRepository.save(user); log.info("Create new user: " + user.getId()); memberService.process(memberService.create(newUser)); + userEntityProducerService.publish(newUser); }; } @@ -53,6 +54,7 @@ private Consumer onSaveExistingUser(User user) { log.info("Update user: " + user.getId()); memberService.process(memberService.create(user)); userRepository.save(user); + userEntityProducerService.publish(user); }; } diff --git a/src/test/java/no/fintlabs/user/UserServiceTest.java b/src/test/java/no/fintlabs/user/UserServiceTest.java index 5c93d17..9d47995 100644 --- a/src/test/java/no/fintlabs/user/UserServiceTest.java +++ b/src/test/java/no/fintlabs/user/UserServiceTest.java @@ -5,8 +5,6 @@ import no.vigoiks.resourceserver.security.FintJwtEndUserPrincipal; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.security.oauth2.jwt.Jwt; import java.util.List; import java.util.Optional; @@ -26,7 +24,7 @@ public class UserServiceTest { public void init(){ authorizationClient = mock(AuthorizationClient.class); userRepository = mock(UserRepository.class); - userService = new UserService(userRepository, null, authorizationClient); + userService = new UserService(userRepository, null, null, authorizationClient); } @Test public void testGetAllAutorizedOrgUnitIDs() {