Skip to content
Snippets Groups Projects
Commit 094f270d authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Added a property 'ENABLE_FULL_BUCKET_NAME'.

Added unit tests.
Changed int tests.
parent d0fa8c73
No related branches found
No related tags found
1 merge request!46Unique bucket name across GCP (GONRG-701)
Pipeline #9780 failed
...@@ -34,4 +34,5 @@ handlers: ...@@ -34,4 +34,5 @@ handlers:
env_variables: env_variables:
SPRING_PROFILES_ACTIVE: "ENVIRONMENT" SPRING_PROFILES_ACTIVE: "ENVIRONMENT"
ENABLE_FULL_BUCKET_NAME: 'ENABLE_FULL_BUCKET_NAME_VAR'
\ No newline at end of file
...@@ -4,13 +4,17 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo; ...@@ -4,13 +4,17 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader; import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReaderFactory; import org.opengroup.osdu.legal.provider.interfaces.IStorageReaderFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class StorageReaderFactoryImpl implements IStorageReaderFactory { public class StorageReaderFactoryImpl implements IStorageReaderFactory {
@Value("#{new Boolean('${enable.full.bucket.name}')}")
private Boolean isFullBucketName;
@Override @Override
public IStorageReader getReader(TenantInfo tenant, String projectRegion) { public IStorageReader getReader(TenantInfo tenant, String projectRegion) {
return new StorageReaderImpl(tenant, projectRegion); return new StorageReaderImpl(tenant, projectRegion, isFullBucketName);
} }
} }
...@@ -2,6 +2,7 @@ package org.opengroup.osdu.legal.countries; ...@@ -2,6 +2,7 @@ package org.opengroup.osdu.legal.countries;
import com.google.cloud.storage.*; import com.google.cloud.storage.*;
import java.util.Objects;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo; import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader; import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -14,16 +15,22 @@ public class StorageReaderImpl implements IStorageReader { ...@@ -14,16 +15,22 @@ public class StorageReaderImpl implements IStorageReader {
private String projectRegion; private String projectRegion;
private Storage storage; private Storage storage;
private static final String BUCKET_NAME = "legal-service-configuration"; protected static final String BUCKET_NAME = "legal-service-configuration";
private static final String FILE_NAME = "Legal_COO.json"; private static final String FILE_NAME = "Legal_COO.json";
private Boolean isFullBucketName = false;
public StorageReaderImpl(TenantInfo tenantInfo, String projectRegion) { public StorageReaderImpl(TenantInfo tenantInfo, String projectRegion) {
new StorageReaderImpl(tenantInfo, projectRegion, false);
}
public StorageReaderImpl(TenantInfo tenantInfo, String projectRegion, Boolean isFullBucketName) {
this.tenantInfo = tenantInfo; this.tenantInfo = tenantInfo;
this.projectRegion = projectRegion; this.projectRegion = projectRegion;
this.storage = getStorage(); this.storage = getStorage();
} this.isFullBucketName = isFullBucketName;
}
@Override @Override
public byte[] readAllBytes() { public byte[] readAllBytes() {
BlobId blobId = getBlobId(); BlobId blobId = getBlobId();
byte[] content = null; byte[] content = null;
...@@ -67,8 +74,11 @@ public class StorageReaderImpl implements IStorageReader { ...@@ -67,8 +74,11 @@ public class StorageReaderImpl implements IStorageReader {
this.storage.create(blobInfo, "".getBytes(UTF_8)); this.storage.create(blobInfo, "".getBytes(UTF_8));
} }
private String getTenantBucketName() { protected String getTenantBucketName() {
if (Objects.nonNull(isFullBucketName) && isFullBucketName) {
return this.tenantInfo.getProjectId() + "-" + this.tenantInfo.getName() + "-" + BUCKET_NAME; return this.tenantInfo.getProjectId() + "-" + this.tenantInfo.getName() + "-" + BUCKET_NAME;
}
return this.tenantInfo.getName() + "-" + BUCKET_NAME;
} }
} }
...@@ -8,4 +8,6 @@ server.port=8080 ...@@ -8,4 +8,6 @@ server.port=8080
JAVA_HEAP_OPTS=-Xms4096M -Xmx4096M JAVA_HEAP_OPTS=-Xms4096M -Xmx4096M
JAVA_GC_OPTS=-XX:+UseG1GC -XX:+UseStringDeduplication -XX:InitiatingHeapOccupancyPercent=45 JAVA_GC_OPTS=-XX:+UseG1GC -XX:+UseStringDeduplication -XX:InitiatingHeapOccupancyPercent=45
enable.full.bucket.name=${ENABLE_FULL_BUCKET_NAME}
...@@ -13,6 +13,7 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo; ...@@ -13,6 +13,7 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.opengroup.osdu.legal.countries.StorageReaderImpl.BUCKET_NAME;
import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
...@@ -74,4 +75,37 @@ public class StorageReaderImplTests { ...@@ -74,4 +75,37 @@ public class StorageReaderImplTests {
byte[] bytes = sut.readAllBytes(); byte[] bytes = sut.readAllBytes();
assertEquals(expectedBytes, bytes); assertEquals(expectedBytes, bytes);
} }
@Test
public void should_returnFullBucketName_when_IsFullBucketName_is_true() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(tenantInfo.getProjectId()).thenReturn("projectId1");
String bucketName = tenantInfo.getProjectId() + "-" + tenantInfo.getName() + "-" + BUCKET_NAME;
StorageReaderImpl storageReader = new StorageReaderImpl(tenantInfo, null,
true);
String resultBucketName = storageReader.getTenantBucketName();
assertEquals(bucketName, resultBucketName);
}
@Test
public void should_returnBucketName_when_IsFullBucketName_is_false() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(tenantInfo.getProjectId()).thenReturn("projectId1");
String bucketName = tenantInfo.getName() + "-" + BUCKET_NAME;
StorageReaderImpl storageReader = new StorageReaderImpl(tenantInfo, null,
false);
String resultBucketName = storageReader.getTenantBucketName();
assertEquals(bucketName, resultBucketName);
}
@Test
public void should_returnBucketName_when_IsFullBucketName_is_null() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(tenantInfo.getProjectId()).thenReturn("projectId1");
String bucketName = tenantInfo.getName() + "-" + BUCKET_NAME;
StorageReaderImpl storageReader = new StorageReaderImpl(tenantInfo, null,
null);
String resultBucketName = storageReader.getTenantBucketName();
assertEquals(bucketName, resultBucketName);
}
} }
\ No newline at end of file
enable.full.bucket.name=false
...@@ -12,6 +12,7 @@ import com.google.cloud.storage.BlobInfo; ...@@ -12,6 +12,7 @@ import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage; import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions; import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.Objects;
public class GCPLegalTagUtils extends LegalTagUtils { public class GCPLegalTagUtils extends LegalTagUtils {
private static final String BUCKET_NAME = "legal-service-configuration"; private static final String BUCKET_NAME = "legal-service-configuration";
...@@ -38,10 +39,19 @@ public class GCPLegalTagUtils extends LegalTagUtils { ...@@ -38,10 +39,19 @@ public class GCPLegalTagUtils extends LegalTagUtils {
} }
} }
private static String getTenantBucketName() { private static String getTenantBucketName() {
String tenantName = System.getProperty("MY_TENANT_PROJECT", System.getenv("MY_TENANT_PROJECT")).toLowerCase(); String tenantName = System
return tenantName + "-" +BUCKET_NAME; .getProperty("MY_TENANT_PROJECT", System.getenv("MY_TENANT_PROJECT")).toLowerCase();
String projectName = System.getProperty("GCLOUD_PROJECT", System.getenv("GCLOUD_PROJECT"))
.toLowerCase();
String enableFullBucketName = System.getProperty("ENABLE_FULL_BUCKET_NAME",
System.getenv("ENABLE_FULL_BUCKET_NAME")).toLowerCase();
if (Objects.nonNull(enableFullBucketName) && Boolean.valueOf(enableFullBucketName)) {
return projectName + "-" + tenantName + "-" + BUCKET_NAME;
} }
return tenantName + "-" + BUCKET_NAME;
}
@Override @Override
public synchronized String accessToken() throws Exception { public synchronized String accessToken() throws Exception {
......
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