Commit 905c406f authored by Ronak Sakhuja's avatar Ronak Sakhuja
Browse files

Replaced logging

parent f164e679
Pipeline #43469 failed with stage
in 38 seconds
......@@ -18,12 +18,10 @@ import com.azure.storage.common.policy.RequestRetryOptions;
import com.azure.storage.common.policy.RetryPolicyType;
import lombok.Getter;
import lombok.Setter;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.time.Duration;
import java.util.Collections;
/**
* Config for BlogStorage Retry.
......@@ -34,8 +32,9 @@ import java.util.Collections;
@Setter
public class BlobStoreRetryConfiguration {
@Autowired
private ILogger logger;
public static final String LOGGER_NAME = BlobStoreRetryConfiguration.class.getName();
private static final int DEFAULT_INT_VALUE = -1;
private static final String DEFAULT_STRING_VALUE = "";
......@@ -52,7 +51,7 @@ public class BlobStoreRetryConfiguration {
* @return true if value is configured in app.properties
*/
private boolean valueConfigured(final int val) {
if (val != -1) {
if (val != DEFAULT_INT_VALUE) {
return true;
}
return false;
......@@ -64,7 +63,7 @@ public class BlobStoreRetryConfiguration {
* @return true if value is configured in app.properties
*/
private boolean valueConfigured(final String val) {
if (val == null || val.isEmpty()) {
if (val == null || val.equals(DEFAULT_STRING_VALUE)) {
return false;
}
return true;
......@@ -79,8 +78,13 @@ public class BlobStoreRetryConfiguration {
// Check whether the variables have been set, else keep them as null.
// Value has to be sent as null incase where they are not configured to use the default configurations (As specified in RequestRetryOptions.class)
// https://azure.github.io/azure-storage-java-async/com/microsoft/azure/storage/blob/RequestRetryOptions.html
RetryPolicyType retryPolicyType;
try {
retryPolicyType = valueConfigured(retryPolicyTypeValue) ? RetryPolicyType.valueOf(retryPolicyTypeValue) : RetryPolicyType.EXPONENTIAL;
} catch (Exception ex) {
retryPolicyType = RetryPolicyType.EXPONENTIAL;
}
RetryPolicyType retryPolicyType = valueConfigured(retryPolicyTypeValue) ? RetryPolicyType.valueOf(retryPolicyTypeValue) : RetryPolicyType.EXPONENTIAL;
Integer maxTriesValue = valueConfigured(this.maxTries) ? this.maxTries : null;
Duration tryTimeout = valueConfigured(tryTimeoutInSeconds) ? Duration.ofSeconds((long) tryTimeoutInSeconds) : null;
Duration retryDelay = valueConfigured(retryDelayInMs) ? Duration.ofMillis(retryDelayInMs) : null;
......@@ -90,8 +94,8 @@ public class BlobStoreRetryConfiguration {
RequestRetryOptions requestRetryOptions = new RequestRetryOptions(retryPolicyType, maxTriesValue, tryTimeout, retryDelay, maxRetryDelay, secondaryHostValue);
this.logger.info("BlobStoreRetryConfiguration", String.format("Retry Options on BlobStorage with RetryPolicyType = %s , maxTries = %d , tryTimeout = %d , retryDelay = %d , maxRetryDelay = %d , secondaryHost = %s.",
retryPolicyType.toString(), requestRetryOptions.getMaxTries(), requestRetryOptions.getTryTimeoutDuration().getSeconds(), requestRetryOptions.getRetryDelay().toMillis(), requestRetryOptions.getMaxRetryDelay().toMillis(), requestRetryOptions.getSecondaryHost()), Collections.emptyMap());
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info(String.format("Retry Options on BlobStorage with RetryPolicyType = %s , maxTries = %d , tryTimeout = %d , retryDelay = %d , maxRetryDelay = %d , secondaryHost = %s.",
retryPolicyType.toString(), requestRetryOptions.getMaxTries(), requestRetryOptions.getTryTimeoutDuration().getSeconds(), requestRetryOptions.getRetryDelay().toMillis(), requestRetryOptions.getMaxRetryDelay().toMillis(), requestRetryOptions.getSecondaryHost()));
return requestRetryOptions;
}
......
......@@ -15,16 +15,23 @@
package org.opengroup.osdu.azure.di;
import com.azure.storage.common.policy.RequestRetryOptions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.logging.CoreLogger;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.opengroup.osdu.core.common.logging.ILogger;
import java.lang.reflect.Field;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
@ExtendWith(MockitoExtension.class)
......@@ -34,10 +41,50 @@ public class BlobStoreRetryConfigurationTest {
@Mock
private ILogger logger;
RequestRetryOptions defaultRequestRetryOptions = new RequestRetryOptions();
@Mock
private CoreLoggerFactory coreLoggerFactory;
@Mock
private CoreLogger coreLogger;
/**
* Workaround for inability to mock static methods like getInstance().
*
* @param mock CoreLoggerFactory mock instance
*/
private void mockSingleton(CoreLoggerFactory mock) {
try {
Field instance = CoreLoggerFactory.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, mock);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Reset workaround for inability to mock static methods like getInstance().
*/
private void resetSingleton() {
try {
Field instance = CoreLoggerFactory.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, null);
instance.setAccessible(false);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@BeforeEach
void setUp() {
openMocks(this);
mockSingleton(coreLoggerFactory);
when(coreLoggerFactory.getLogger(anyString())).thenReturn(coreLogger);
}
@AfterEach
public void takeDown() {
resetSingleton();
}
@Test
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment