Skip to content

Commit

Permalink
feat: add authorization headers for api client
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Oct 27, 2020
1 parent ef5b276 commit b88eb89
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.messageservice.api.service.helper.ServiceHelper;
import de.caritas.cob.messageservice.userservice.generated.ApiClient;
import de.caritas.cob.messageservice.userservice.generated.web.LiveproxyControllerApi;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

/**
* Service class to provide live event triggers to the live proxy endpoint in user service.
Expand All @@ -15,6 +19,7 @@
public class LiveEventNotificationService {

private final @NonNull LiveproxyControllerApi liveproxyControllerApi;
private final @NonNull ServiceHelper serviceHelper;

/**
* Triggers a live event to proxy endpoint of user service.
Expand All @@ -23,8 +28,19 @@ public class LiveEventNotificationService {
*/
public void sendLiveEvent(String rcGroupId) {
if (isNotBlank(rcGroupId)) {
this.liveproxyControllerApi.sendLiveEvent(rcGroupId);
addDefaultHeaders(this.liveproxyControllerApi.getApiClient());
try {
this.liveproxyControllerApi.sendLiveEvent(rcGroupId);
} catch (RestClientException e) {
LogService.logInternalServerError(
String.format("Unable to trigger live event for rc group id %s", rcGroupId), e);
}
}
}

private void addDefaultHeaders(ApiClient apiClient) {
HttpHeaders headers = this.serviceHelper.getKeycloakAndCsrfHttpHeaders();
headers.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next()));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.caritas.cob.messageservice.config.apiclient;

import de.caritas.cob.messageservice.userservice.generated.web.LiveproxyControllerApi;
import de.caritas.cob.messageservice.userservice.generated.ApiClient;
import de.caritas.cob.messageservice.userservice.generated.web.LiveproxyControllerApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ springfox.docuPath=/messages/docs
user.service.api.url=${app.base.url}:${server.port}/users
user.service.api.new.message.notification=${user.service.api.url}/mails/messages/new
user.service.api.new.feedback.message.notification=${user.service.api.url}/mails/messages/feedback/new
user.service.api.liveproxy.url=${app.base.url}:${server.port}/liveproxy/send
user.service.api.liveproxy.url=${app.base.url}:${server.port}

# CSRF token
csrf.header.property=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package de.caritas.cob.messageservice.api.service;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.powermock.reflect.Whitebox.setInternalState;

import de.caritas.cob.messageservice.api.service.helper.ServiceHelper;
import de.caritas.cob.messageservice.userservice.generated.ApiClient;
import de.caritas.cob.messageservice.userservice.generated.web.LiveproxyControllerApi;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestClientException;

@RunWith(MockitoJUnitRunner.class)
public class LiveEventNotificationServiceTest {
Expand All @@ -21,25 +32,59 @@ public class LiveEventNotificationServiceTest {
@Mock
private LiveproxyControllerApi liveproxyControllerApi;

@Mock
private ServiceHelper serviceHelper;

@Mock
private Logger logger;

@Before
public void setup() {
setInternalState(LogService.class, "LOGGER", logger);
}

@Test
public void sendLiveEvent_Should_notTriggerLiveEvent_When_rcGroupIdIsNull() {
this.liveEventNotificationService.sendLiveEvent(null);

verifyZeroInteractions(this.liveproxyControllerApi);
verifyZeroInteractions(this.serviceHelper);
}

@Test
public void sendLiveEvent_Should_notTriggerLiveEvent_When_rcGroupIdIsEmpty() {
this.liveEventNotificationService.sendLiveEvent("");

verifyZeroInteractions(this.liveproxyControllerApi);
verifyZeroInteractions(this.serviceHelper);
}

@Test
public void sendLiveEvent_Should_triggerLiveEvent_When_rcGroupIdIsValid() {
public void sendLiveEvent_Should_triggerLiveEventWithHeaders_When_rcGroupIdIsValid() {
ApiClient apiClient = mock(ApiClient.class);
when(this.liveproxyControllerApi.getApiClient()).thenReturn(apiClient);
HttpHeaders headers = new HttpHeaders();
headers.add("header 1", "value 1");
headers.add("header 2", "value 2");
when(this.serviceHelper.getKeycloakAndCsrfHttpHeaders()).thenReturn(headers);

this.liveEventNotificationService.sendLiveEvent("valid");

verify(this.liveproxyControllerApi, times(1)).sendLiveEvent(eq("valid"));
verify(this.serviceHelper, times(1)).getKeycloakAndCsrfHttpHeaders();
verify(apiClient, times(2)).addDefaultHeader(anyString(), anyString());
}

@Test
public void sendLiveEvent_Should_logError_When_apiClientThrowsRestClientException() {
doThrow(new RestClientException("")).when(this.liveproxyControllerApi)
.sendLiveEvent(anyString());
when(this.liveproxyControllerApi.getApiClient()).thenReturn(mock(ApiClient.class));
when(this.serviceHelper.getKeycloakAndCsrfHttpHeaders()).thenReturn(new HttpHeaders());

this.liveEventNotificationService.sendLiveEvent("valid");

verify(this.logger, times(1)).error(anyString(), anyString(), anyString());
}

}

0 comments on commit b88eb89

Please sign in to comment.