Commit 76aa678d authored by Kelly Domico's avatar Kelly Domico
Browse files

Fix corelogger mocks in blobstore tests

parent fcf12f9e
Pipeline #24273 failed with stage
in 1 minute and 49 seconds
......@@ -28,7 +28,6 @@ import com.azure.storage.blob.specialized.BlockBlobClient;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.opengroup.osdu.azure.logging.DependencyPayload;
import org.opengroup.osdu.azure.logging.ICoreLogger;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.AppException;
......@@ -82,9 +81,10 @@ import java.time.OffsetDateTime;
* </pre>
*/
public class BlobStore {
private static final String LOGGER_NAME = BlobStore.class.getName();
private IBlobServiceClientFactory blobServiceClientFactory;
private ILogger logger;
private ICoreLogger coreLogger;
/**
* Constructor to create BlobStore.
......@@ -95,7 +95,6 @@ public class BlobStore {
public BlobStore(final IBlobServiceClientFactory factory, final ILogger loggerInstance) {
this.blobServiceClientFactory = factory;
this.logger = loggerInstance;
this.coreLogger = CoreLoggerFactory.getInstance().getLogger(BlobStore.class);
}
private static final String LOG_PREFIX = "azure-core-lib";
......@@ -116,7 +115,7 @@ public class BlobStore {
int statusCode = HttpStatus.SC_OK;
try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) {
blockBlobClient.download(downloadStream);
coreLogger.info("{} {}", LOG_PREFIX, String.format("Done reading from %s", filePath));
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("{} {}", LOG_PREFIX, String.format("Done reading from %s", filePath));
return downloadStream.toString(StandardCharsets.UTF_8.name());
} catch (BlobStorageException ex) {
statusCode = ex.getStatusCode();
......@@ -153,7 +152,7 @@ public class BlobStore {
int statusCode = HttpStatus.SC_OK;
try {
blockBlobClient.delete();
coreLogger.info("{} {}", LOG_PREFIX, String.format("Done deleting blob at %s", filePath));
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("{} {}", LOG_PREFIX, String.format("Done deleting blob at %s", filePath));
return true;
} catch (BlobStorageException ex) {
statusCode = ex.getStatusCode();
......@@ -188,7 +187,7 @@ public class BlobStore {
int statusCode = HttpStatus.SC_OK;
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) {
blockBlobClient.upload(dataStream, bytesSize, true);
coreLogger.info("{} {}", LOG_PREFIX, String.format("Done uploading file content to %s", filePath));
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("{} {}", LOG_PREFIX, String.format("Done uploading file content to %s", filePath));
} catch (BlobStorageException ex) {
statusCode = ex.getStatusCode();
throw handleBlobStoreException(500, "Failed to upload file content.", ex);
......@@ -324,7 +323,7 @@ public class BlobStore {
* @return Instance of AppException
*/
private AppException handleBlobStoreException(final int status, final String errorMessage, final Exception ex) {
coreLogger.warn("{} {}", LOG_PREFIX, errorMessage);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).warn("{} {}", LOG_PREFIX, errorMessage);
return new AppException(status, errorMessage, ex.getMessage(), ex);
}
......@@ -343,6 +342,6 @@ public class BlobStore {
payload.setType("BlobStore");
payload.setTarget(target);
coreLogger.logDependency(payload);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).logDependency(payload);
}
}
......@@ -24,6 +24,7 @@ import com.azure.storage.blob.sas.BlobContainerSasPermission;
import com.azure.storage.blob.sas.BlobSasPermission;
import com.azure.storage.blob.sas.BlobServiceSasSignatureValues;
import com.azure.storage.blob.specialized.BlockBlobClient;
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;
......@@ -31,10 +32,13 @@ import org.mockito.ArgumentCaptor;
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 org.opengroup.osdu.core.common.model.http.AppException;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.time.Duration;
import java.time.OffsetDateTime;
......@@ -53,8 +57,11 @@ public class BlobStoreTest {
private static final String STORAGE_CONTAINER_NAME = "containerName";
private static final String SOURCE_FILE_URL = "http://someURL";
@InjectMocks
private BlobStore blobStore;
@Mock
private CoreLoggerFactory coreLoggerFactory;
@Mock
private CoreLogger coreLogger;
@Mock
private IBlobServiceClientFactory blobServiceClientFactory;
......@@ -89,9 +96,45 @@ public class BlobStoreTest {
@Mock
private BlobSasPermission blobSasPermission;
@InjectMocks
private BlobStore blobStore;
/**
* 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 init() {
initMocks(this);
mockSingleton(coreLoggerFactory);
when(coreLoggerFactory.getLogger(anyString())).thenReturn(coreLogger);
lenient().when(blobClient.getBlockBlobClient()).thenReturn(blockBlobClient);
lenient().when(blobContainerClient.getBlobClient(FILE_PATH)).thenReturn(blobClient);
lenient().when(blobServiceClient.getBlobContainerClient(STORAGE_CONTAINER_NAME)).thenReturn(blobContainerClient);
......@@ -99,6 +142,11 @@ public class BlobStoreTest {
lenient().doNothing().when(logger).warning(eq("azure-core-lib"), any(), anyMap());
}
@AfterEach
public void takeDown() {
resetSingleton();
}
@Test
public void readFromStorageContainer_ErrorCreatingBlobContainerClient() {
doThrow(BlobStorageException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID));
......
......@@ -106,7 +106,7 @@ class CosmosStoreTest {
try {
Field instance = CoreLoggerFactory.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(instance, mock);
instance.set(null, mock);
} catch (Exception e) {
throw new RuntimeException(e);
}
......
Markdown is supported
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