Commit 467e65bc authored by Komal Makkar's avatar Komal Makkar
Browse files

intermediate changes

parent 52446518
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.58</version>
<version>0.6.1-SNAPSHOT</version>
<name>core-lib-azure</name>
<properties>
......@@ -49,9 +49,12 @@
<reactor.version>Dysprosium-SR12</reactor.version>
<netty.version>4.1.51.Final</netty.version>
<lombok.version>1.18.16</lombok.version>
<osdu.oscorecommon.version>0.3.18</osdu.oscorecommon.version>
<osdu.oscorecommon.version>0.3.23</osdu.oscorecommon.version>
<mockito-junit-jupiter.version>2.23.0</mockito-junit-jupiter.version>
<spring-boot-starter-log4j2.version>2.3.4.RELEASE</spring-boot-starter-log4j2.version>
<azure-mgmt-eventgrid.version>1.0.0-beta-3</azure-mgmt-eventgrid.version>
<azure-security-keyvault-keys.version>4.2.3</azure-security-keyvault-keys.version>
<documentdb-bulkexecutor.version>2.12.0</documentdb-bulkexecutor.version>
</properties>
<licenses>
......@@ -247,6 +250,39 @@
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-eventgrid</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<artifactId>azure-client-runtime</artifactId>
<groupId>com.microsoft.azure</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.microsoft.azure.eventgrid.v2020_04_01_preview</groupId>
<artifactId>azure-mgmt-eventgrid</artifactId>
<version>${azure-mgmt-eventgrid.version}</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-keys</artifactId>
<version>${azure-security-keyvault-keys.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>documentdb-bulkexecutor</artifactId>
<version>${documentdb-bulkexecutor.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.8.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
......
......@@ -54,7 +54,7 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
}
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure =
this.partitionService.getEventGridTopicsInPartition(dataPartitionId, topicName);
this.partitionService.getEventGridTopicInPartition(dataPartitionId, topicName);
TopicCredentials topicCredentials =
new TopicCredentials(eventGridTopicPartitionInfoAzure.getTopicAccessKey().getValue().toString());
......
......@@ -18,8 +18,6 @@ import com.microsoft.azure.eventgrid.EventGridClient;
import com.microsoft.azure.eventgrid.models.EventGridEvent;
import lombok.SneakyThrows;
import org.opengroup.osdu.azure.partition.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.AppException;
......@@ -74,13 +72,13 @@ public class EventGridTopicStore {
*/
@SneakyThrows
public void publishToEventGridTopic(final String dataPartitionId, final String topicName, final List<EventGridEvent> eventsList) {
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure = this.eventGridPartitionClient.getEventGridTopicsInPartition(dataPartitionId, topicName);
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure = this.eventGridPartitionClient.getEventGridTopicInPartition(dataPartitionId, topicName);
String endpoint = "";
try {
endpoint = String.format("https://%s/", new URI(eventGridTopicPartitionInfoAzure.getTopicName().getValue().toString()).getHost());
} catch (URISyntaxException e) {
throw new AppException(500, "Invalid Event Grid endpoint URI", "PartitionInfo for eventgrid-recordstopic " + endpoint, e);
throw new AppException(500, "Invalid Event Grid endpoint URI", "PartitionInfo for Event Grid Topic " + topicName, e);
}
EventGridClient eventGridClient = eventGridTopicClientFactory.getClient(dataPartitionId, topicName);
......
......@@ -15,15 +15,15 @@
package org.opengroup.osdu.azure.partition;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.opengroup.osdu.core.common.partition.Property;
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class EventGridTopicPartitionInfoAzure {
Property TopicName;
Property TopicAccessKey;
......
......@@ -43,19 +43,20 @@ public class PartitionServiceEventGridClient {
private DpsHeaders headers;
/**
*
* @param partitionId
* @param topicName
* @return
* @throws AppException
* @throws PartitionException
*/
public EventGridTopicPartitionInfoAzure getEventGridTopicsInPartition(final String partitionId, String topicName) throws AppException, PartitionException {
public EventGridTopicPartitionInfoAzure getEventGridTopicInPartition(final String partitionId, String topicName) throws AppException, PartitionException {
Validators.checkNotNullAndNotEmpty(partitionId, "partitionId");
Map<String,EventGridTopicPartitionInfoAzure> eventGridTopicPartitionInfoAzure = getEventGridTopicsInPartition(partitionId);
Validators.checkNotNullAndNotEmpty(topicName, "topicName");
Map<String, EventGridTopicPartitionInfoAzure> eventGridTopicPartitionInfoAzure = getAllEventGridTopicsInPartition(partitionId);
if(!eventGridTopicPartitionInfoAzure.containsKey(topicName)) {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid EventGrid Partition configuration for the partition "+ partitionId, "Please refer to wiki here <>");
if (!eventGridTopicPartitionInfoAzure.containsKey(topicName)) {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid EventGrid Partition configuration for the partition " + partitionId, "Please refer to wiki here <>");
}
return eventGridTopicPartitionInfoAzure.get(topicName);
}
......@@ -67,23 +68,22 @@ public class PartitionServiceEventGridClient {
* @return Partition info
* @throws AppException Exception thrown by {@link IPartitionFactory}
*/
Map<String, EventGridTopicPartitionInfoAzure> getEventGridTopicsInPartition(final String partitionId) throws AppException, PartitionException {
Map<String, EventGridTopicPartitionInfoAzure> getAllEventGridTopicsInPartition(final String partitionId) throws AppException, PartitionException {
Validators.checkNotNullAndNotEmpty(partitionId, "partitionId");
PartitionInfo partitionInfo = getPartitionInfo(partitionId);
Map<String, Property> propertyMap = partitionInfo.getProperties();
Map<String, EventGridTopicPartitionInfoAzure> topics = new HashMap<>();
for (Map.Entry<String,Property> property : propertyMap.entrySet())
{
if(isEventGridProperty(property)) {
for (Map.Entry<String, Property> property : propertyMap.entrySet()) {
if (isEventGridProperty(property)) {
StringTokenizer stringTokenizer = new StringTokenizer(property.getKey(), "-");
if(stringTokenizer.countTokens() == 2) {
if (stringTokenizer.countTokens() == 2) {
addEventGridTopicName(topics, property, stringTokenizer);
} else if (stringTokenizer.countTokens() == 3) {
addEventGridAccessKey(topics, property, stringTokenizer);
} else {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid EventGrid Partition configuration for the partition "+ partitionId, "Please refer to wiki here <>");
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid EventGrid Partition configuration for the partition " + partitionId, "Please refer to wiki here <>");
}
}
}
......@@ -91,7 +91,6 @@ public class PartitionServiceEventGridClient {
}
/**
*
* @param partitionId
* @return
* @throws PartitionException
......@@ -102,7 +101,6 @@ public class PartitionServiceEventGridClient {
}
/**
*
* @param property
* @return
*/
......@@ -113,7 +111,6 @@ public class PartitionServiceEventGridClient {
}
/**
*
* @param topics
* @param property
* @param stringTokenizer
......@@ -121,7 +118,7 @@ public class PartitionServiceEventGridClient {
private void addEventGridAccessKey(Map<String, EventGridTopicPartitionInfoAzure> topics, Map.Entry<String, Property> property, StringTokenizer stringTokenizer) {
stringTokenizer.nextToken();
String key = stringTokenizer.nextToken();
if(topics.containsKey(key)){
if (topics.containsKey(key)) {
EventGridTopicPartitionInfoAzure topic = topics.get(key);
topic.setTopicAccessKey(property.getValue());
} else {
......@@ -132,7 +129,6 @@ public class PartitionServiceEventGridClient {
}
/**
*
* @param topics
* @param property
* @param stringTokenizer
......@@ -140,7 +136,7 @@ public class PartitionServiceEventGridClient {
private void addEventGridTopicName(Map<String, EventGridTopicPartitionInfoAzure> topics, Map.Entry<String, Property> property, StringTokenizer stringTokenizer) {
stringTokenizer.nextToken();
String key = stringTokenizer.nextToken();
if(topics.containsKey(key)) {
if (topics.containsKey(key)) {
EventGridTopicPartitionInfoAzure topic = topics.get(key);
topic.setTopicName(property.getValue());
} else {
......
......@@ -22,22 +22,25 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.EventGridTopicClientCache;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.azure.partition.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.Property;
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.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class EventGridTopicClientFactoryImplTest {
private static final String VALID_TOPIC_NAME = "RecordsChanged";
private static final String VALID_TOPIC_NAME = "eventgrid-validtopic";
private static final String VALID_TOPICKEY_NAME = "eventgrid-validtopic-accesskey";
private static final String VALID_DATA_PARTIION_ID = "validDataPartitionId";
@Mock
private PartitionServiceClient partitionService;
private PartitionServiceEventGridClient partitionService;
@InjectMocks
private EventGridTopicClientFactoryImpl sut;
......@@ -49,7 +52,7 @@ class EventGridTopicClientFactoryImplTest {
public void should_throwException_given_nullDataPartitionId() {
NullPointerException nullPointerException = Assertions.assertThrows(NullPointerException.class,
() -> this.sut.getClient(null, TopicName.RECORDS_CHANGED));
() -> this.sut.getClient(null, "recordsChanged"));
assertEquals("dataPartitionId cannot be null!", nullPointerException.getMessage());
}
......@@ -57,7 +60,7 @@ class EventGridTopicClientFactoryImplTest {
public void should_throwException_given_emptyDataPartitionId() {
IllegalArgumentException illegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class,
() -> this.sut.getClient("", TopicName.RECORDS_CHANGED));
() -> this.sut.getClient("", "recordsChanged"));
assertEquals("dataPartitionId cannot be empty!", illegalArgumentException.getMessage());
}
......@@ -70,20 +73,20 @@ class EventGridTopicClientFactoryImplTest {
}
@Test
public void should_return_validClient_given_validPartitionId() {
public void should_return_validClient_given_validPartitionId() throws PartitionException {
// Setup
when(this.partitionService.getPartition(VALID_DATA_PARTIION_ID)).thenReturn(
PartitionInfoAzure.builder()
.idConfig(Property.builder().value(VALID_DATA_PARTIION_ID).build())
.eventGridRecordsTopicAccessKeyConfig(Property.builder().value(VALID_TOPIC_NAME).build()).build());
when(this.partitionService.getEventGridTopicInPartition(VALID_DATA_PARTIION_ID, "validtopic")).thenReturn(
EventGridTopicPartitionInfoAzure.builder()
.TopicName(Property.builder().value(VALID_TOPIC_NAME).build())
.TopicAccessKey(Property.builder().value(VALID_TOPICKEY_NAME).build()).build());
when(this.clientCache.containsKey(any())).thenReturn(false);
// Act
EventGridClient eventGridClient = this.sut.getClient(VALID_DATA_PARTIION_ID, TopicName.RECORDS_CHANGED);
EventGridClient eventGridClient = this.sut.getClient(VALID_DATA_PARTIION_ID, "validtopic");
// Assert
assertNotNull(eventGridClient);
verify(this.clientCache, times(1)).put(any(), any());
}
}
\ No newline at end of file
}
......@@ -22,10 +22,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.azure.partition.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.Property;
import java.util.ArrayList;
......@@ -37,23 +38,19 @@ import static org.mockito.MockitoAnnotations.initMocks;
@ExtendWith(MockitoExtension.class)
class EventGridTopicStoreTest {
private static final String VALID_DATA_PARTIION_ID = "validDataPartitionId";
private static final String VALID_TOPIC = "validTopic";
private static final String INVALID_URI = "://invalidUri";
@Mock
EventGridTopicClientFactoryImpl eventGridTopicClientFactory;
@Mock
EventGridClient eventGridClient;
@InjectMocks
EventGridTopicStore sut;
@Mock
private ILogger logger;
@Mock
private PartitionServiceClient partitionService;
@InjectMocks
EventGridTopicStore sut;
private static final String VALID_DATA_PARTIION_ID = "validDataPartitionId";
private static final String INVALID_URI = "://invalidUri";
private PartitionServiceEventGridClient partitionService;
@BeforeEach
void setUp() {
......@@ -61,28 +58,31 @@ class EventGridTopicStoreTest {
}
@Test
public void should_throwException_given_invalidURI() {
when(this.partitionService.getPartition(VALID_DATA_PARTIION_ID)).thenReturn(
PartitionInfoAzure.builder()
.idConfig(Property.builder().value(VALID_DATA_PARTIION_ID).build())
.eventGridRecordsTopicEndpointConfig(Property.builder().value(INVALID_URI).build()).build());
public void should_throwException_given_invalidURI() throws PartitionException {
doReturn(EventGridTopicPartitionInfoAzure.builder()
.TopicName(Property.builder().value(INVALID_URI).build())
.TopicAccessKey(Property.builder().value("validkey").build()).build())
.when(this.partitionService).getEventGridTopicInPartition(anyString(), anyString());
AppException appException = Assertions.assertThrows(AppException.class,
() -> this.sut.publishToEventGridTopic(VALID_DATA_PARTIION_ID, TopicName.RECORDS_CHANGED, new ArrayList<>()));
assertEquals("PartitionInfo for eventgrid-recordstopic ://invalidUri", appException.getError().getMessage());
() -> this.sut.publishToEventGridTopic(VALID_DATA_PARTIION_ID, VALID_TOPIC, new ArrayList<>()));
assertEquals("PartitionInfo for Event Grid Topic " + VALID_TOPIC, appException.getError().getMessage());
verify(this.eventGridClient, times(0)).publishEvents(any(), any());
}
@Test
public void should_should_invoke_publishEvents() {
when(this.partitionService.getPartition(VALID_DATA_PARTIION_ID)).thenReturn(
PartitionInfoAzure.builder()
.idConfig(Property.builder().value(VALID_DATA_PARTIION_ID).build())
.eventGridRecordsTopicEndpointConfig(Property.builder().value("validURL").build()).build());
when(this.eventGridTopicClientFactory.getClient(VALID_DATA_PARTIION_ID, TopicName.RECORDS_CHANGED)).thenReturn(this.eventGridClient);
public void should_should_invoke_publishEvents() throws PartitionException {
doReturn(EventGridTopicPartitionInfoAzure.builder()
.TopicName(Property.builder().value(VALID_TOPIC).build())
.TopicAccessKey(Property.builder().value("validkey").build()).build())
.when(this.partitionService).getEventGridTopicInPartition(anyString(), anyString());
when(this.eventGridTopicClientFactory.getClient(VALID_DATA_PARTIION_ID, "validTopic")).thenReturn(this.eventGridClient);
this.sut.publishToEventGridTopic(VALID_DATA_PARTIION_ID, TopicName.RECORDS_CHANGED, new ArrayList<>());
this.sut.publishToEventGridTopic(VALID_DATA_PARTIION_ID, "validTopic", new ArrayList<>());
verify(this.eventGridClient, times(1)).publishEvents(any(), any());
}
}
\ No newline at end of file
}
......@@ -22,21 +22,23 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
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.*;
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.Property;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doReturn;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceEventGridClientTest {
private static final String PARTITION_ID = "dataPartitionId";
@Mock
private SecretClient secretClient;
@Mock
......@@ -48,8 +50,6 @@ public class PartitionServiceEventGridClientTest {
@InjectMocks
private PartitionServiceEventGridClient sut;
private static final String PARTITION_ID = "dataPartitionId";
@Test
public void should_returnAllEventGridTopics_ListPartitions() throws PartitionException {
// Setup
......@@ -71,18 +71,13 @@ public class PartitionServiceEventGridClientTest {
properties.put("event_grid-testtopic-accesskey", Property.builder().value(eventGridTopicName2).build());
properties.put("eventgrid-testtopic-accesskey-", Property.builder().value(eventGridTopicName2).build());
/*PartitionService partitionService = mock(PartitionService.class);
List<String> partitions = new ArrayList<>();
partitions.add("tenant1");
lenient().when(partitionService.list()).thenReturn(partitions);*/
PartitionInfo partitionInfo = PartitionInfo.builder().properties(properties).build();
PartitionServiceEventGridClient partitionServiceClientSpy = Mockito.spy(sut);
doReturn(partitionInfo).when(partitionServiceClientSpy).getPartitionInfo(anyString());
// Act
Map<String , EventGridTopicPartitionInfoAzure> eventGridTopicPartitionInfoAzureMap =
partitionServiceClientSpy.getEventGridTopicsInPartition("tenant1");
Map<String, EventGridTopicPartitionInfoAzure> eventGridTopicPartitionInfoAzureMap =
partitionServiceClientSpy.getAllEventGridTopicsInPartition("tenant1");
// Assert
assertEquals(eventGridTopicPartitionInfoAzureMap.size(), 2);
......@@ -95,4 +90,34 @@ public class PartitionServiceEventGridClientTest {
assertEquals(eventGridTopicPartitionInfoAzureMap.get(topicId2).getTopicName().getValue(), eventGridTopicName2);
assertEquals(eventGridTopicPartitionInfoAzureMap.get(topicId2).getTopicAccessKey().getValue(), eventGridTopicAccessKey2);
}
@Test
public void getTopicName() throws PartitionException {
final String eventGridTopicName1 = "testEventGridTopicName1";
final String eventGridTopicAccessKey1 = "testEventGridTopicAccessKey1";
Map<String, Property> properties = new HashMap<>();
properties.put("id", Property.builder().value(PARTITION_ID).build());
// Valid property names
properties.put("eventgrid-recordstopic", Property.builder().value(eventGridTopicName1).build());
properties.put("eventgrid-recordstopic-accesskey", Property.builder().value(eventGridTopicAccessKey1).build());
PartitionInfo partitionInfo = PartitionInfo.builder().properties(properties).build();
PartitionServiceEventGridClient partitionServiceClientSpy = Mockito.spy(sut);
doReturn(partitionInfo).when(partitionServiceClientSpy).getPartitionInfo(anyString());
// Act
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure =
partitionServiceClientSpy.getEventGridTopicInPartition("tenant1", "recordstopic");
// Assert
assertEquals(eventGridTopicPartitionInfoAzure.getTopicName().getValue(), eventGridTopicName1);
assertEquals(eventGridTopicPartitionInfoAzure.getTopicAccessKey().getValue(), eventGridTopicAccessKey1);
// Assert negative
AppException exception = assertThrows(AppException.class, () -> {
partitionServiceClientSpy.getEventGridTopicInPartition("tenant1", "recordschangedtopic");
});
assertEquals(500, exception.getError().getCode());
}
}
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