Skip to content

Commit

Permalink
added producer for user
Browse files Browse the repository at this point in the history
  • Loading branch information
Erling Jahr committed Nov 20, 2023
1 parent 2d27084 commit 8e7991d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/no/fintlabs/user/UserEntityProducerService.java
Original file line number Diff line number Diff line change
@@ -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<User> 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.<User>builder()
.topicNameParameters(entityTopicNameParameters)
.key(key)
.value(user)
.build()
);
}



}
8 changes: 5 additions & 3 deletions src/main/java/no/fintlabs/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
};
}

Expand All @@ -53,6 +54,7 @@ private Consumer<User> onSaveExistingUser(User user) {
log.info("Update user: " + user.getId());
memberService.process(memberService.create(user));
userRepository.save(user);
userEntityProducerService.publish(user);
};
}

Expand Down
4 changes: 1 addition & 3 deletions src/test/java/no/fintlabs/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit 8e7991d

Please sign in to comment.