Commit 5e48b4bc authored by Komal Makkar's avatar Komal Makkar
Browse files

EG changes

parent 473fac2d
Pipeline #27999 failed with stages
in 52 seconds
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.57</version>
<version>0.0.58</version>
<name>core-lib-azure</name>
<properties>
......@@ -274,7 +274,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
</plugin>
<plugin>
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.version}</version>
......@@ -295,7 +295,7 @@
</goals>
</execution>
</executions>
</plugin>
</plugin>-->
</plugins>
</build>
......
......@@ -25,7 +25,10 @@ import org.springframework.stereotype.Component;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A simpler interface to interact with Azure Event Grid Topic.
......@@ -84,4 +87,5 @@ public class EventGridTopicStore {
EventGridClient eventGridClient = eventGridTopicClientFactory.getClient(dataPartitionId, topicName);
eventGridClient.publishEvents(endpoint, eventsList);
}
}
// 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.partition;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.opengroup.osdu.core.common.partition.Property;
@AllArgsConstructor
@Getter
@NoArgsConstructor
@Setter
public class EventGridTopicPartitionInfoAzure {
Property TopicName;
Property TopicAccessKey;
}
......@@ -8,15 +8,12 @@ import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.common.Validators;
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.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.*;
/**
* Partition service client implementation.
......@@ -90,4 +87,71 @@ public class PartitionServiceClient {
return this.partitionFactory.create(headers);
}
/**
* Get partition info.
*
* @param partitionId Partition Id
* @return Partition info
* @throws AppException Exception thrown by {@link IPartitionFactory}
*/
public Map<String, EventGridTopicPartitionInfoAzure> getEventGridTopicsInPartition(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)) {
StringTokenizer stringTokenizer = new StringTokenizer(property.getKey(), "-");
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 <>");
}
}
}
return topics;
}
private boolean isEventGridProperty(Map.Entry<String, Property> property) {
String accessKeyRegex = "^eventgrid-([a-zA-Z0-9]*)topic-accesskey$";
String topicNameRegex = "^eventgrid-([a-zA-Z0-9]*)topic$";
return property.getKey().matches((accessKeyRegex)) || property.getKey().matches((topicNameRegex));
}
public PartitionInfo getPartitionInfo(String partitionId) throws PartitionException {
IPartitionProvider serviceClient = getServiceClient();
return serviceClient.get(partitionId);
}
private void addEventGridAccessKey(Map<String, EventGridTopicPartitionInfoAzure> topics, Map.Entry<String, Property> property, StringTokenizer stringTokenizer) {
stringTokenizer.nextToken();
String key = stringTokenizer.nextToken();
if(topics.containsKey(key)){
EventGridTopicPartitionInfoAzure topic = topics.get(key);
topic.setTopicAccessKey(property.getValue());
} else {
EventGridTopicPartitionInfoAzure topic = new EventGridTopicPartitionInfoAzure();
topic.setTopicAccessKey(property.getValue());
topics.put(key, topic);
}
}
private void addEventGridTopicName(Map<String, EventGridTopicPartitionInfoAzure> topics, Map.Entry<String, Property> property, StringTokenizer stringTokenizer) {
stringTokenizer.nextToken();
String key = stringTokenizer.nextToken();
if(topics.containsKey(key)) {
EventGridTopicPartitionInfoAzure topic = topics.get(key);
topic.setTopicName(property.getValue());
} else {
EventGridTopicPartitionInfoAzure topic = new EventGridTopicPartitionInfoAzure();
topic.setTopicName(property.getValue());
topics.put(key, topic);
}
}
}
\ No newline at end of file
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 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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doReturn;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceClientTest {
......@@ -111,4 +116,46 @@ public class PartitionServiceClientTest {
assertNotNull(partitionList);
assertEquals(partitions.size(), partitionList.size());
}
@Test
public void should_returnAllEventGridTopics_ListPartitions() throws PartitionException {
// Setup
final String eventGridTopicName1 = "testEventGridTopicName1";
final String eventGridTopicAccessKey1 = "testEventGridTopicAccessKey1";
final String eventGridTopicName2 = "testEventGridTopicName2";
final String eventGridTopicAccessKey2 = "testEventGridTopicAccessKey2";
final String topicId1 = "recordstopic";
final String topicId2 = "testtopic";
Map<String, Property> properties = new HashMap<>();
properties.put("id", Property.builder().value(PARTITION_ID).build());
properties.put("eventgrid-recordstopic", Property.builder().value(eventGridTopicName1).build());
properties.put("eventgrid-recordstopic-accesskey", Property.builder().value(eventGridTopicAccessKey1).build());
properties.put("eventgrid-testtopic", Property.builder().value(eventGridTopicName2).build());
properties.put("eventgrid-testtopic-accesskey", Property.builder().value(eventGridTopicAccessKey2).build());
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();
PartitionServiceClient partitionServiceClientSpy = Mockito.spy(sut);
doReturn(partitionInfo).when(partitionServiceClientSpy).getPartitionInfo(anyString());
// Act
Map<String , EventGridTopicPartitionInfoAzure> eventGridTopicPartitionInfoAzureMap =
partitionServiceClientSpy.getEventGridTopicsInPartition("tenant1");
// Assert
assertEquals(eventGridTopicPartitionInfoAzureMap.size(), 2);
assertTrue(eventGridTopicPartitionInfoAzureMap.containsKey(topicId1));
assertTrue(eventGridTopicPartitionInfoAzureMap.containsKey(topicId2));
assertEquals(eventGridTopicPartitionInfoAzureMap.get(topicId1).getTopicName().getValue(), eventGridTopicName1);
assertEquals(eventGridTopicPartitionInfoAzureMap.get(topicId1).getTopicAccessKey().getValue(), eventGridTopicAccessKey1);
assertEquals(eventGridTopicPartitionInfoAzureMap.get(topicId2).getTopicName().getValue(), eventGridTopicName2);
assertEquals(eventGridTopicPartitionInfoAzureMap.get(topicId2).getTopicAccessKey().getValue(), eventGridTopicAccessKey2);
}
}
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