Commit 52446518 authored by Komal Makkar's avatar Komal Makkar
Browse files

intermediate changes

parent 66e97025
# The file makes Jacoco understand that the Lambok's @Data's creation, should not be accounted for Jacoco's analysis.
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
......@@ -17,9 +17,10 @@ package org.opengroup.osdu.azure.eventgrid;
import com.microsoft.azure.eventgrid.EventGridClient;
import com.microsoft.azure.eventgrid.TopicCredentials;
import com.microsoft.azure.eventgrid.implementation.EventGridClientImpl;
import lombok.SneakyThrows;
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.common.Validators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -31,7 +32,7 @@ import org.springframework.stereotype.Component;
public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFactory {
@Autowired
private PartitionServiceClient partitionService;
private PartitionServiceEventGridClient partitionService;
@Autowired
private EventGridTopicClientCache clientCache;
......@@ -41,8 +42,9 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
* @param topicName Topic Name
* @return EventGridClient
*/
@SneakyThrows
@Override
public EventGridClient getClient(final String dataPartitionId, final TopicName topicName) {
public EventGridClient getClient(final String dataPartitionId, final String topicName) {
Validators.checkNotNullAndNotEmpty(dataPartitionId, "dataPartitionId");
Validators.checkNotNull(topicName, "topicName");
......@@ -51,15 +53,14 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
return this.clientCache.get(cacheKey);
}
PartitionInfoAzure pi = this.partitionService.getPartition(dataPartitionId);
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure =
this.partitionService.getEventGridTopicsInPartition(dataPartitionId, topicName);
TopicCredentials topicCredentials = null;
if (topicName == TopicName.RECORDS_CHANGED) {
topicCredentials = new TopicCredentials(pi.getEventGridRecordsTopicAccessKey());
}
TopicCredentials topicCredentials =
new TopicCredentials(eventGridTopicPartitionInfoAzure.getTopicAccessKey().getValue().toString());
EventGridClient eventGridClient = new EventGridClientImpl(topicCredentials);
this.clientCache.put(cacheKey, eventGridClient);
return eventGridClient;
}
}
}
\ No newline at end of file
......@@ -16,8 +16,11 @@ package org.opengroup.osdu.azure.eventgrid;
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;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -25,10 +28,7 @@ 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.
......@@ -65,27 +65,25 @@ public class EventGridTopicStore {
private ILogger logger;
@Autowired
private PartitionServiceClient partitionService;
private PartitionServiceEventGridClient eventGridPartitionClient;
/**
* @param dataPartitionId Data partition id
* @param topicName Topic name
* @param eventsList List of Event Grid Events
*/
public void publishToEventGridTopic(final String dataPartitionId, final TopicName topicName, final List<EventGridEvent> eventsList) {
PartitionInfoAzure pi = this.partitionService.getPartition(dataPartitionId);
@SneakyThrows
public void publishToEventGridTopic(final String dataPartitionId, final String topicName, final List<EventGridEvent> eventsList) {
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure = this.eventGridPartitionClient.getEventGridTopicsInPartition(dataPartitionId, topicName);
String endpoint = "";
if (topicName == TopicName.RECORDS_CHANGED) {
try {
endpoint = String.format("https://%s/", new URI(pi.getEventGridRecordsTopicEndpoint()).getHost());
} catch (URISyntaxException e) {
throw new AppException(500, "Invalid Event Grid endpoint URI", "PartitionInfo for eventgrid-recordstopic " + pi.getEventGridRecordsTopicEndpoint(), e);
}
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);
}
EventGridClient eventGridClient = eventGridTopicClientFactory.getClient(dataPartitionId, topicName);
eventGridClient.publishEvents(endpoint, eventsList);
}
}
......@@ -27,5 +27,5 @@ public interface IEventGridTopicClientFactory {
* @param topicName Topic name
* @return EventGridClient
*/
EventGridClient getClient(String dataPartitionId, TopicName topicName);
EventGridClient getClient(String dataPartitionId, String topicName);
}
// 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.eventgrid;
/**
* Topic Name Enum.
* The Topic Stores and client are controlled by the same.
*/
public enum TopicName {
RECORDS_CHANGED
}
......@@ -20,10 +20,10 @@ import lombok.NoArgsConstructor;
import lombok.Setter;
import org.opengroup.osdu.core.common.partition.Property;
@AllArgsConstructor
@Setter
@Getter
@NoArgsConstructor
@Setter
@AllArgsConstructor
public class EventGridTopicPartitionInfoAzure {
Property TopicName;
Property TopicAccessKey;
......
......@@ -87,71 +87,5 @@ 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
// 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 com.azure.security.keyvault.secrets.SecretClient;
import org.apache.http.HttpStatus;
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.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
@Service
@Lazy
public class PartitionServiceEventGridClient {
@Autowired
private IPartitionFactory partitionFactory;
@Autowired
private SecretClient secretClient;
@Autowired
private AzureServicePrincipleTokenService tokenService;
@Autowired
private DpsHeaders headers;
/**
*
* @param partitionId
* @param topicName
* @return
* @throws AppException
* @throws PartitionException
*/
public EventGridTopicPartitionInfoAzure getEventGridTopicsInPartition(final String partitionId, String topicName) throws AppException, PartitionException {
Validators.checkNotNullAndNotEmpty(partitionId, "partitionId");
Map<String,EventGridTopicPartitionInfoAzure> eventGridTopicPartitionInfoAzure = getEventGridTopicsInPartition(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 <>");
}
return eventGridTopicPartitionInfoAzure.get(topicName);
}
/**
* Get partition info.
*
* @param partitionId Partition Id
* @return Partition info
* @throws AppException Exception thrown by {@link IPartitionFactory}
*/
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;
}
/**
*
* @param partitionId
* @return
* @throws PartitionException
*/
PartitionInfo getPartitionInfo(String partitionId) throws PartitionException {
IPartitionProvider serviceClient = getServiceClient();
return serviceClient.get(partitionId);
}
/**
*
* @param property
* @return
*/
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));
}
/**
*
* @param topics
* @param property
* @param stringTokenizer
*/
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);
}
}
/**
*
* @param topics
* @param property
* @param stringTokenizer
*/
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);
}
}
/**
* 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);
}
}
......@@ -116,46 +116,4 @@ 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);
}
}
// 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 com.azure.security.keyvault.secrets.SecretClient;
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.util.AzureServicePrincipleTokenService;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceEventGridClientTest {
@Mock
private SecretClient secretClient;
@Mock
private IPartitionFactory partitionFactory;
@Mock
private AzureServicePrincipleTokenService tokenService;
@Mock
private DpsHeaders headers;
@InjectMocks
private PartitionServiceEventGridClient sut;
private static final String PARTITION_ID = "dataPartitionId";
@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());
// Valid property names
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());
// Invalid Names. These should not get picked.
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");
// Assert
assertEquals(eventGridTopicPartitionInfoAzureMap.size(), 2);
assertTrue(eventGridTopicPartitionInfoAzureMap.containsKey(topicId1));
assertTrue(eventGridTopicPartitionInfoAzureMap.containsKey(topicId2));
// Validate that the EventGridTopicPartitionInfo is mapped correctly.
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