diff --git a/pom.xml b/pom.xml index 955c93f1ae13df87c9e456b3d53357b13969e9fd..7a97ede97037d815b4c40508839673f18d6573a5 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ org.opengroup.osdu core-lib-azure jar - 0.0.22 + 0.0.23 core-lib-azure diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImpl.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImpl.java index 1a62a81a1ddd4adc2ecf4e2ed713f7cb4f9ebafa..72403bc6ee736018dbdb3daadead50f89255ca91 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImpl.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImpl.java @@ -20,28 +20,26 @@ import com.azure.storage.blob.BlobServiceClientBuilder; import org.opengroup.osdu.azure.di.BlobStoreConfiguration; import org.opengroup.osdu.common.Validators; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; /** * Implementation for IBlobServiceClientFactory. */ +@Component public class BlobServiceClientFactoryImpl implements IBlobServiceClientFactory { - @Autowired - private DefaultAzureCredential defaultAzureCredential; - - @Autowired - @Lazy - private BlobStoreConfiguration blobStoreConfiguration; - private BlobServiceClient blobServiceClient; /** - * Parameter-less constructor. - * This initializes the blobServiceClient. + * Constructor to initialize blobServiceClient. + * @param defaultAzureCredential Default azure credentials. + * @param blobStoreConfiguration Configuration details for blob storage. */ - public BlobServiceClientFactoryImpl() { - Validators.checkNotNull(defaultAzureCredential, "Credential cannot be null"); + @Autowired + public BlobServiceClientFactoryImpl( + final DefaultAzureCredential defaultAzureCredential, + final BlobStoreConfiguration blobStoreConfiguration) { + Validators.checkNotNull(defaultAzureCredential, "Default credentials"); Validators.checkNotNullAndNotEmpty(blobStoreConfiguration.getStorageAccountName(), "Storage account name cannot be null"); String endpoint = String.format("https://%s.blob.core.windows.net", blobStoreConfiguration.getStorageAccountName()); diff --git a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImplTest.java b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c06fc6ca458a75013c34a14f8b6ffaf36e291a16 --- /dev/null +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobServiceClientFactoryImplTest.java @@ -0,0 +1,87 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.azure.blobstorage; + +import com.azure.identity.DefaultAzureCredential; +import com.azure.storage.blob.BlobServiceClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.azure.di.BlobStoreConfiguration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.lenient; +import static org.mockito.MockitoAnnotations.initMocks; + +@ExtendWith(MockitoExtension.class) +public class BlobServiceClientFactoryImplTest { + + @Mock + DefaultAzureCredential credential; + + @Mock + BlobStoreConfiguration configuration; + + BlobServiceClientFactoryImpl clientFactory; + + private static final String ACCOUNT_NAME = "testAccount"; + private static final String PARTITION_ID = "dataPartitionId"; + + @BeforeEach + void init() { + initMocks(this); + lenient().doReturn(ACCOUNT_NAME).when(configuration).getStorageAccountName(); + } + + @Test + public void ConstructorThrowsException_IfDefaultAzureCredentialIsNull() { + try { + clientFactory = new BlobServiceClientFactoryImpl(null, configuration); + } catch (NullPointerException ex) { + assertEquals("Default credentials cannot be null!", ex.getMessage()); + } catch (Exception ex) { + fail("Should not get any other exception. Received " + ex.getClass()); + } + } + + @Test + public void ConstructorThrowsException_IfBlobStoreConfigurationIsNull() { + doReturn("").when(configuration).getStorageAccountName(); + + try { + clientFactory = new BlobServiceClientFactoryImpl(credential, configuration); + } catch (IllegalArgumentException ex) { + assertEquals("Storage account name cannot be null cannot be empty!", ex.getMessage()); + } catch (Exception ex) { + fail("Should not get any other exception. Received " + ex.getClass()); + } + } + + @Test + public void testGetBlobServiceClient_Success() + { + try { + clientFactory = new BlobServiceClientFactoryImpl(credential, configuration); + BlobServiceClient client = clientFactory.getBlobServiceClient(PARTITION_ID); + assertEquals(ACCOUNT_NAME, client.getAccountName()); + } catch (Exception ex) { + fail("Should not fail."); + } + } +}