Commit 942968b9 authored by ethiraj krishnamanaidu's avatar ethiraj krishnamanaidu
Browse files

Merge branch 'fix_authorization_header_bug_in_partition_service_client' into 'master'

Fix authorization header bug in PartitionServiceClient

See merge request !81
parents 3ed2b5de 1779c713
Pipeline #30030 passed with stages
in 6 minutes and 45 seconds
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.6.1</version>
<version>0.6.2</version>
<name>core-lib-azure</name>
<properties>
......
......@@ -86,8 +86,8 @@ public class PartitionServiceClient {
* @return PartitionServiceClient
*/
private IPartitionProvider getServiceClient() {
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
return this.partitionFactory.create(headers);
DpsHeaders newHeaders = DpsHeaders.createFromMap(headers.getHeaders());
newHeaders.put(DpsHeaders.AUTHORIZATION, "Bearer " + tokenService.getAuthorizationToken());
return partitionFactory.create(newHeaders);
}
}
\ No newline at end of file
......@@ -193,8 +193,9 @@ public class PartitionServiceEventGridClient {
* @return PartitionServiceClient
*/
private IPartitionProvider getServiceClient() {
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
return this.partitionFactory.create(headers);
DpsHeaders newHeaders = DpsHeaders.createFromMap(headers.getHeaders());
newHeaders.put(DpsHeaders.AUTHORIZATION, "Bearer " + tokenService.getAuthorizationToken());
return partitionFactory.create(newHeaders);
}
}
package org.opengroup.osdu.azure.partition;
import com.azure.security.keyvault.secrets.SecretClient;
import com.microsoft.azure.servicebus.SubscriptionClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.servicebus.SubscriptionClientFactoryImpl;
import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.core.common.http.HttpResponse;
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 org.opengroup.osdu.core.common.partition.IPartitionFactory;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.partition.PartitionService;
import org.opengroup.osdu.core.common.partition.Property;
import javax.validation.constraints.AssertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceClientTest {
......@@ -45,7 +51,7 @@ public class PartitionServiceClientTest {
@Test
public void should_throwException_given_nullDataPartitionId() {
try {
this.sut.getPartition(null);
sut.getPartition(null);
} catch (NullPointerException ex) {
assertEquals("partitionId cannot be null!", ex.getMessage());
} catch (Exception ex) {
......@@ -56,7 +62,7 @@ public class PartitionServiceClientTest {
@Test
public void should_throwException_given_emptyDataPartitionId() {
try {
this.sut.getPartition("");
sut.getPartition("");
} catch (IllegalArgumentException ex) {
assertEquals("partitionId cannot be empty!", ex.getMessage());
} catch (Exception ex) {
......@@ -67,8 +73,8 @@ public class PartitionServiceClientTest {
@Test
public void should_return_validPartition_given_partitionExists() throws PartitionException {
PartitionService partitionService = mock(PartitionService.class);
when(this.tokenService.getAuthorizationToken()).thenReturn("token");
when(this.partitionFactory.create(this.headers)).thenReturn(partitionService);
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(partitionService);
final String storageAccountKey = "testStorageAccountKey";
final String cosmosEndpoint = "testCosmosEndpoint";
......@@ -79,21 +85,22 @@ public class PartitionServiceClientTest {
PartitionInfo partitionInfo = PartitionInfo.builder().properties(properties).build();
when(partitionService.get(PARTITION_ID)).thenReturn(partitionInfo);
PartitionInfoAzure partitionInfoAzure = this.sut.getPartition(PARTITION_ID);
PartitionInfoAzure partitionInfoAzure = sut.getPartition(PARTITION_ID);
assertNotNull(partitionInfoAzure);
assertEquals(cosmosEndpoint, partitionInfoAzure.getCosmosEndpoint());
assertEquals(storageAccountKey, partitionInfoAzure.getStorageAccountKey());
verify(headers, never()).put(eq(DpsHeaders.AUTHORIZATION), anyString());
}
@Test
public void should_throwException_when_partitionNotFound() {
PartitionService partitionService = mock(PartitionService.class);
when(this.tokenService.getAuthorizationToken()).thenReturn("token");
when(this.partitionFactory.create(this.headers)).thenReturn(partitionService);
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(partitionService);
try {
when(partitionService.get(PARTITION_ID)).thenThrow(new PartitionException("unknown error", new HttpResponse()));
this.sut.getPartition(PARTITION_ID);
sut.getPartition(PARTITION_ID);
} catch (AppException ex) {
assertEquals("unknown error", ex.getOriginalException().getMessage());
} catch (Exception ex) {
......@@ -104,16 +111,17 @@ public class PartitionServiceClientTest {
@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);
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(partitionService);
List<String> partitions = new ArrayList<>();
partitions.add("tenant1");
partitions.add("tenant2");
when(partitionService.list()).thenReturn(partitions);
List<String> partitionList = this.sut.listPartitions();
List<String> partitionList = sut.listPartitions();
assertNotNull(partitionList);
assertEquals(partitions.size(), partitionList.size());
verify(headers, never()).put(eq(DpsHeaders.AUTHORIZATION), anyString());
}
}
......@@ -25,6 +25,7 @@ import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
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.IPartitionFactory;
import org.opengroup.osdu.core.common.partition.IPartitionProvider;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.partition.Property;
......@@ -32,9 +33,16 @@ import org.opengroup.osdu.core.common.partition.Property;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceEventGridClientTest {
......@@ -118,4 +126,14 @@ public class PartitionServiceEventGridClientTest {
AppException exception = assertThrows(AppException.class, () -> partitionServiceClientSpy.getEventGridTopicInPartition("tenant1", "recordschangedtopic"));
assertEquals(500, exception.getError().getCode());
}
@Test
public void shouldNotModifyDpsHeaders() throws PartitionException {
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(mock(IPartitionProvider.class));
sut.getPartitionInfo("test");
verify(headers, never()).put(DpsHeaders.AUTHORIZATION, "Bearer token");
}
}
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