Commit 0836b7ee authored by Hema Vishnu Pola [Microsoft]'s avatar Hema Vishnu Pola [Microsoft]
Browse files

Merge branch 'partition-api-list' into 'master'

Implementation of get All partitions. After partition implemented that list API

See merge request !37
parents 80673222 c3015cfe
Pipeline #13730 passed with stages
in 10 minutes and 9 seconds
......@@ -21,7 +21,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.37</version>
<version>0.0.38</version>
<name>core-lib-azure</name>
<properties>
......@@ -79,7 +79,7 @@
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-common</artifactId>
<version>0.3.12</version>
<version>0.3.18</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
......
......@@ -30,6 +30,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Implementation for ITenantFactory.
......@@ -78,10 +80,23 @@ public class TenantFactoryImpl implements ITenantFactory {
* @return list of tenantInfo objects for all the tenants
*/
public Collection<TenantInfo> listTenantInfo() {
// todo: requires get all partitions on Partition service.
return null;
return this.partitionService.listPartitions().stream().map(this::buildTenantInfo).collect(Collectors.toList());
}
/**
* Build TenanInfo Object.
*
* @param partitionId Partition Id
* @return TenantInfo object
*/
private TenantInfo buildTenantInfo(final String partitionId) {
TenantInfo tenantInfo = new TenantInfo();
tenantInfo.setName(partitionId);
tenantInfo.setDataPartitionId(partitionId);
return tenantInfo;
}
/**
* @param tenantName Tenant name
* @param host Host name
......
......@@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Partition service client implementation.
*/
......@@ -43,10 +45,8 @@ public class PartitionServiceClient {
*/
public PartitionInfoAzure getPartition(final String partitionId) throws AppException {
Validators.checkNotNullAndNotEmpty(partitionId, "partitionId");
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
try {
IPartitionProvider serviceClient = this.partitionFactory.create(headers);
IPartitionProvider serviceClient = getServiceClient();
PartitionInfo partitionInfo = serviceClient.get(partitionId);
return convert(partitionInfo);
} catch (PartitionException e) {
......@@ -64,4 +64,30 @@ public class PartitionServiceClient {
infoAzure.configureSecretClient(secretClient);
return infoAzure;
}
/**
* List of all partitions.
*
* @return List of Partitions
* @throws AppException Exception thrown by {@link IPartitionFactory}
*/
public List<String> listPartitions() throws AppException {
try {
IPartitionProvider serviceClient = getServiceClient();
return serviceClient.list();
} catch (PartitionException e) {
throw new AppException(HttpStatus.SC_FORBIDDEN, "Service unavailable", "Error getting list of partitions", e);
}
}
/**
* Get Service client for Partition Service.
*
* @return PartitionServiceClient
*/
private IPartitionProvider getServiceClient() {
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
return this.partitionFactory.create(headers);
}
}
\ No newline at end of file
......@@ -22,6 +22,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class TenantFactoryImplTest {
......@@ -85,21 +86,21 @@ public class TenantFactoryImplTest {
assertNull(result);
}
/**
* todo: requires list all tenants on Partition service.
*/
@Disabled
@Test
public void returnsListOfAllTenants() {
public void returnsEmptyListTenants() {
List<TenantInfo> result = new ArrayList<>(tenantFactory.listTenantInfo());
assertNotNull(result);
}
TenantInfo tenantInfo = new TenantInfo();
tenantInfo.setName(tenantName);
tenantInfo.setDataPartitionId(tenantName);
tenantInfo.setComplianceRuleSet(complianceRuleSet);
List<TenantInfo> expected = Collections.singletonList(tenantInfo);
@Test
public void returnsListOfTenants() {
List<String> partitions = new ArrayList<>();
partitions.add("tenant1");
partitions.add("tenant2");
when(partitionServiceClient.listPartitions()).thenReturn(partitions);
assertEquals(expected, result);
List<TenantInfo> result = new ArrayList<>(tenantFactory.listTenantInfo());
assertNotNull(result);
assertEquals(partitions.size(), result.size());
}
}
......@@ -12,7 +12,9 @@ import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.partition.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
......@@ -93,4 +95,20 @@ public class PartitionServiceClientTest {
fail("Should not get any other exception. Received " + ex.getClass());
}
}
@Test
public void should_return_ListPartitions() throws PartitionException {
PartitionService partitionService = mock(PartitionService.class);
when(this.tokenService.getAuthorizationToken()).thenReturn("token");
when(this.partitionFactory.create(this.headers)).thenReturn(partitionService);
List<String> partitions = new ArrayList<>();
partitions.add("tenant1");
partitions.add("tenant2");
when(partitionService.list()).thenReturn(partitions);
List<String> partitionList = this.sut.listPartitions();
assertNotNull(partitionList);
assertEquals(partitions.size(), partitionList.size());
}
}
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