Skip to content
Snippets Groups Projects
Commit 4f02dd46 authored by Neelesh Thakur's avatar Neelesh Thakur
Browse files

tests

parent 3dc92f09
No related branches found
No related tags found
1 merge request!348add subscriber notification request request/dependency logging
......@@ -3,6 +3,7 @@ package org.opengroup.osdu.notification.provider.azure.util;
import org.opengroup.osdu.azure.logging.DependencyLogger;
import org.opengroup.osdu.azure.logging.DependencyLoggingOptions;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.model.notification.Subscription;
import org.opengroup.osdu.notification.provider.interfaces.ISubscriberNotificationRequestLogger;
import org.springframework.stereotype.Component;
......@@ -17,16 +18,19 @@ public class SubscriberNotificationRequestLogger implements ISubscriberNotificat
this.dependencyLogger = dependencyLogger;
}
public void log(String notificationId, String endpoint, HttpResponse response) {
public void log(String notificationId, Subscription subscription, HttpResponse response) {
final DependencyLoggingOptions loggingOptions = DependencyLoggingOptions.builder()
.type(DEPENDENCY_TYPE)
.name(DEPENDENCY_NAME)
.data(notificationId)
.target(endpoint)
.target(subscription.getPushEndpoint())
.timeTakenInMs(response.getLatency())
.resultCode(response.getResponseCode())
.success(response.isSuccessCode())
.build();
final String data = response.isSuccessCode()
? String.format("notificationId=%s subscriptionId=%s", notificationId, subscription.getId())
: String.format("notificationId=%s subscriptionId=%s response=%s", notificationId, subscription.getId(), response.getBody());
loggingOptions.setData(data);
this.dependencyLogger.logDependency(loggingOptions);
}
}
package org.opengroup.osdu.notification.util;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.azure.logging.DependencyLogger;
import org.opengroup.osdu.azure.logging.DependencyLoggingOptions;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.model.notification.HmacSecret;
import org.opengroup.osdu.core.common.model.notification.Subscription;
import org.opengroup.osdu.notification.provider.azure.util.SubscriberNotificationRequestLogger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class SubscriberNotificationRequestLoggerTest {
private final HttpResponse response = new HttpResponse();
private static final String NOTIFICATION_ID = "test-notification-id";
private static Subscription subscription;
@Mock
private DependencyLogger dependencyLogger;
@InjectMocks
private SubscriberNotificationRequestLogger sut;
@BeforeClass
public static void setup() {
subscription = new Subscription();
subscription.setName("hamc_test_subscription");
subscription.setPushEndpoint("http://challenge");
subscription.setDescription("Description");
subscription.setTopic("records-changed");
subscription.setNotificationId(NOTIFICATION_ID);
subscription.setId("id_1");
subscription.setCreatedBy("test@test.com");
HmacSecret secret = new HmacSecret();
secret.setValue("testsecret");
subscription.setSecret(secret);
}
@Test
public void log_notifySubscriber_success() {
response.setResponseCode(200);
this.sut.log(NOTIFICATION_ID, subscription, response);
ArgumentCaptor<DependencyLoggingOptions> loggingOptionsArgumentCaptor = ArgumentCaptor.forClass(DependencyLoggingOptions.class);
verify(dependencyLogger, times(1)).logDependency(loggingOptionsArgumentCaptor.capture());
DependencyLoggingOptions actualLoggingOptions = loggingOptionsArgumentCaptor.getValue();
verifyDependencyLogging(actualLoggingOptions, "notificationId=test-notification-id subscriptionId=id_1", "http://challenge", 200, true);
}
@Test
public void log_notifySubscriber_failure() {
response.setResponseCode(403);
response.setBody("unauthorized");
this.sut.log(NOTIFICATION_ID, subscription, response);
ArgumentCaptor<DependencyLoggingOptions> loggingOptionsArgumentCaptor = ArgumentCaptor.forClass(DependencyLoggingOptions.class);
verify(dependencyLogger, times(1)).logDependency(loggingOptionsArgumentCaptor.capture());
DependencyLoggingOptions actualLoggingOptions = loggingOptionsArgumentCaptor.getValue();
verifyDependencyLogging(actualLoggingOptions, "notificationId=test-notification-id subscriptionId=id_1 response=unauthorized", "http://challenge", 403, false);
}
private void verifyDependencyLogging(DependencyLoggingOptions capturedLoggingOptions, String data, String target, int resultCode, boolean success) {
assertEquals("Notification", capturedLoggingOptions.getType());
assertEquals("NotifySubscriber", capturedLoggingOptions.getName());
assertEquals(data, capturedLoggingOptions.getData());
assertEquals(target, capturedLoggingOptions.getTarget());
assertEquals(resultCode, capturedLoggingOptions.getResultCode());
assertEquals(success, capturedLoggingOptions.isSuccess());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment