Skip to content

Commit

Permalink
fix: switch TopicControllerApi to new creation per statistics event t…
Browse files Browse the repository at this point in the history
…o fix 401 and introduce trycatch
  • Loading branch information
PhilippFr committed Sep 14, 2022
1 parent 82248c6 commit 1180295
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import de.caritas.cob.userservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/** Facade to encapsulate the steps to initialize a user account. */
@Service
@RequiredArgsConstructor
@Slf4j
public class CreateUserFacade {
private final @NonNull UserVerifier userVerifier;
private final @NonNull IdentityClient identityClient;
Expand Down Expand Up @@ -59,14 +61,18 @@ public void createUserAccountWithInitializedConsultingType(final UserDTO userDTO
createNewConsultingTypeFacade.initializeNewConsultingType(
userDTO, user, obtainConsultingTypeSettings(userDTO));

RegistrationStatisticsEvent registrationEvent =
new RegistrationStatisticsEvent(
userDTO,
user,
newRegistrationResponseDto.getSessionId(),
registrationStatisticsHelper.findTopicInternalIdentifier(userDTO.getMainTopicId()),
registrationStatisticsHelper.findTopicsInternalAttributes(userDTO.getTopicIds()));
statisticsService.fireEvent(registrationEvent);
try {
RegistrationStatisticsEvent registrationEvent =
new RegistrationStatisticsEvent(
userDTO,
user,
newRegistrationResponseDto.getSessionId(),
registrationStatisticsHelper.findTopicInternalIdentifier(userDTO.getMainTopicId()),
registrationStatisticsHelper.findTopicsInternalAttributes(userDTO.getTopicIds()));
statisticsService.fireEvent(registrationEvent);
} catch (Exception e) {
log.error("Could not create registration statistics event", e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -27,6 +27,9 @@ public class TopicService {
private final @NonNull SecurityHeaderSupplier securityHeaderSupplier;
private final @NonNull TenantHeaderSupplier tenantHeaderSupplier;

@Value("${consulting.type.service.api.url}")
private String topicServiceApiUrl;

@Cacheable(cacheNames = CacheManagerConfig.TOPICS_CACHE)
public List<TopicDTO> getAllTopics() {
log.info("Calling topic service to get all topics");
Expand All @@ -36,8 +39,15 @@ public List<TopicDTO> getAllTopics() {

public List<TopicDTO> getAllActiveTopics() {
log.info("Calling topic service to get all active topics");
addTenantHeaders(this.topicControllerApi.getApiClient());
return topicControllerApi.getAllActiveTopics();
return createTopicControllerApi().getAllActiveTopics();
}

public TopicControllerApi createTopicControllerApi() {
var apiClient = new ApiClient().setBasePath(this.topicServiceApiUrl);
addDefaultHeaders(apiClient);
TopicControllerApi topicControllerApi = new TopicControllerApi(apiClient);
topicControllerApi.setApiClient(apiClient);
return topicControllerApi;
}

private void addDefaultHeaders(ApiClient apiClient) {
Expand All @@ -46,12 +56,6 @@ private void addDefaultHeaders(ApiClient apiClient) {
headers.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next()));
}

private void addTenantHeaders(ApiClient apiClient) {
var httpHeaders = new HttpHeaders();
tenantHeaderSupplier.addTenantHeader(httpHeaders);
httpHeaders.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next()));
}

@Cacheable(cacheNames = CacheManagerConfig.TOPICS_CACHE)
public Map<Long, TopicDTO> getAllTopicsMap() {
var allTopics = this.getAllTopics();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.caritas.cob.userservice.api.service.statistics.event;

import static de.caritas.cob.userservice.api.testHelper.TestConstants.AGE;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.GENDER_VALUE;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.POSTCODE;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.RELATION_VALUE;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.SESSION_ID;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.TENANT_ID;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.USER_ID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import de.caritas.cob.userservice.api.adapters.web.dto.UserDTO;
import de.caritas.cob.userservice.api.model.User;
import de.caritas.cob.userservice.statisticsservice.generated.web.model.EventType;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class RegistrationStatisticsEventTest {

private RegistrationStatisticsEvent registrationStatisticsEvent;

@Before
public void setUp() {
UserDTO registeredUser =
UserDTO.builder()
.age(AGE)
.userGender(GENDER_VALUE)
.counsellingRelation(RELATION_VALUE)
.postcode(POSTCODE)
.build();
User createdUser = new User();
createdUser.setUserId(USER_ID);
createdUser.setTenantId(TENANT_ID);
LocalDateTime now = LocalDateTime.now();
createdUser.setCreateDate(now);
registrationStatisticsEvent =
new RegistrationStatisticsEvent(
registeredUser, createdUser, SESSION_ID, "alk", List.of("alk", "drogen"));
}

@Test
public void getEventType_Should_ReturnEventTypeRegistration() {
assertThat(registrationStatisticsEvent.getEventType(), is(EventType.REGISTRATION));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ public class TestConstants {
.offline(IS_NOT_OFFLINE)
.consultingType(15);
public static final List<AgencyDTO> AGENCY_DTO_LIST = Collections.singletonList(AGENCY_DTO_SUCHT);

/*
* Tenants
*/
public static final Long TENANT_ID = 1L;

/*
* Users / Consultants
*/
Expand Down

0 comments on commit 1180295

Please sign in to comment.