Skip to content
Snippets Groups Projects
Commit 06481ad9 authored by harshit aggarwal's avatar harshit aggarwal
Browse files

Using event grid custom topic

parent b69a139d
No related branches found
No related tags found
1 merge request!13Using event grid custom topic
Pipeline #6285 passed
......@@ -46,9 +46,9 @@
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure.eventgrid.v2020_04_01_preview</groupId>
<groupId>com.microsoft.azure.eventgrid.v2019_01_01</groupId>
<artifactId>azure-mgmt-eventgrid</artifactId>
<version>1.0.0-beta-3</version>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
......
......@@ -22,7 +22,7 @@ import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
import com.microsoft.azure.management.eventgrid.v2019_01_01.implementation.EventGridManager;
import com.microsoft.rest.LogLevel;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
......
package org.opengroup.osdu.register.provider.azure.subscriber;
import com.microsoft.azure.arm.model.Indexable;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.WebHookEventSubscriptionDestination;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
import com.microsoft.azure.management.eventgrid.v2019_01_01.WebHookEventSubscriptionDestination;
import com.microsoft.azure.management.eventgrid.v2019_01_01.implementation.EventGridManager;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.subscriber.model.Subscription;
......@@ -32,9 +32,11 @@ public class PushSubscription {
subscriptionDestination.withEndpointUrl(serviceConfig.getRecordsChangePubsubEndpoint());
String subscriptionId = subscription.getNotificationId();
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), azureBootstrapConfig.getResourceGroupName(), subscription.getTopic());
AtomicBoolean res = new AtomicBoolean(true);
eventGridManager.systemTopicEventSubscriptions().define(subscriptionId)
.withExistingSystemTopic(azureBootstrapConfig.getResourceGroupName(), subscription.getTopic() )
eventGridManager.eventSubscriptions().define(subscriptionId)
.withScope(scope)
.withDestination(subscriptionDestination)
.createAsync()
.toBlocking()
......@@ -44,7 +46,7 @@ public class PushSubscription {
logger.error(String.format("Creating subscription with id %s failed with error %s", subscriptionId, throwable.getMessage()));
res.set(false);
},
() -> logger.info(String.format("Subscription with id %s deleted successfully", subscriptionId))
() -> logger.info(String.format("Subscription with id %s created successfully", subscriptionId))
);
return res.get();
......@@ -53,7 +55,9 @@ public class PushSubscription {
public boolean deletePushSubscription(String subscriptionId, String topicName) {
AtomicBoolean res = new AtomicBoolean(true);
eventGridManager.systemTopicEventSubscriptions().deleteAsync(azureBootstrapConfig.getResourceGroupName(), topicName, subscriptionId)
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), azureBootstrapConfig.getResourceGroupName(), topicName);
eventGridManager.eventSubscriptions().deleteAsync(scope, subscriptionId)
.subscribe(() -> {
logger.info(String.format("Subscription with id %s deleted successfully", subscriptionId));
}, (Throwable error) -> {
......
......@@ -15,9 +15,9 @@
package org.opengroup.osdu.register.provider.azure.subscriber;
import com.microsoft.azure.arm.model.Indexable;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.SystemTopicEventSubscription;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.SystemTopicEventSubscriptions;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
import com.microsoft.azure.management.eventgrid.v2019_01_01.EventSubscription;
import com.microsoft.azure.management.eventgrid.v2019_01_01.EventSubscriptions;
import com.microsoft.azure.management.eventgrid.v2019_01_01.implementation.EventGridManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -47,6 +47,7 @@ public class PushSubscriptionTest {
private static final String resourceGroupName = "resource-group";
private static final String topicName = "topic-name";
private static final String errorMessage = "some error message";
private static final String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", subscriptionId, resourceGroupName, topicName);
private final Observable<Indexable> observable = Observable.from(new Indexable[]{});
private final Observable<Indexable> observableError = Observable.error(new Throwable(errorMessage));
private final Completable completable = Completable.complete();
......@@ -64,13 +65,13 @@ public class PushSubscriptionTest {
private Subscription subscription;
@Mock
private SystemTopicEventSubscriptions systemTopicEventSubscriptions;
private EventSubscriptions eventSubscriptions;
@Mock
private SystemTopicEventSubscription.DefinitionStages.Blank systemTopicEventSubscription;
private EventSubscription.DefinitionStages.Blank eventSubscription;
@Mock
private SystemTopicEventSubscription.DefinitionStages.WithCreate systemTopicEventSubscriptionWithCreate;
private EventSubscription.DefinitionStages.WithCreate eventSubscriptionWithCreate;
@Mock
private AppServiceConfig serviceConfig;
......@@ -81,72 +82,77 @@ public class PushSubscriptionTest {
@BeforeEach
public void init() {
lenient().when(azureBootstrapConfig.getResourceGroupName()).thenReturn(resourceGroupName);
lenient().when(azureBootstrapConfig.getAzureSubscriptionId()).thenReturn(subscriptionId);
lenient().when(subscription.getNotificationId()).thenReturn(subscriptionId);
lenient().when(subscription.getTopic()).thenReturn(topicName);
lenient().when(serviceConfig.getRecordsChangePubsubEndpoint()).thenReturn(pushEndpoint);
lenient().when(eventGridManager.systemTopicEventSubscriptions()).thenReturn(systemTopicEventSubscriptions);
lenient().when(systemTopicEventSubscriptions.define(subscriptionId)).thenReturn(systemTopicEventSubscription);
lenient().when(systemTopicEventSubscription.withExistingSystemTopic(resourceGroupName, topicName)).thenReturn(systemTopicEventSubscriptionWithCreate);
lenient().when(systemTopicEventSubscriptionWithCreate.withDestination(any())).thenReturn(systemTopicEventSubscriptionWithCreate);
lenient().when(eventGridManager.eventSubscriptions()).thenReturn(eventSubscriptions);
lenient().when(eventSubscriptions.define(subscriptionId)).thenReturn(eventSubscription);
lenient().when(eventSubscription.withScope(scope)).thenReturn(eventSubscriptionWithCreate);
lenient().when(eventSubscriptionWithCreate.withDestination(any())).thenReturn(eventSubscriptionWithCreate);
}
@Test
public void shouldReturnTrueWhenPushSubscriptionCreated() {
when(systemTopicEventSubscriptionWithCreate.createAsync()).thenReturn(observable);
when(eventSubscriptionWithCreate.createAsync()).thenReturn(observable);
boolean result = pushSubscription.createPushSubscription(subscription);
assertTrue(result);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(subscription, times(1)).getNotificationId();
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(subscription, times(1)).getTopic();
verify(eventGridManager, times(1)).systemTopicEventSubscriptions();
verify(systemTopicEventSubscriptions, times(1)).define(subscriptionId);
verify(systemTopicEventSubscription, times(1)).withExistingSystemTopic(resourceGroupName, topicName);
verify(logger, times(1)).info(String.format("Subscription with id %s deleted successfully", subscriptionId));
verify(eventGridManager, times(1)).eventSubscriptions();
verify(eventSubscriptions, times(1)).define(subscriptionId);
verify(eventSubscription, times(1)).withScope(scope);
verify(logger, times(1)).info(String.format("Subscription with id %s created successfully", subscriptionId));
}
@Test
public void shouldReturnFalseWhenPushSubscriptionCreationFailed() {
when(systemTopicEventSubscriptionWithCreate.createAsync()).thenReturn(observableError);
when(eventSubscriptionWithCreate.createAsync()).thenReturn(observableError);
boolean result = pushSubscription.createPushSubscription(subscription);
assertFalse(result);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(subscription, times(1)).getNotificationId();
verify(subscription, times(1)).getTopic();
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(eventGridManager, times(1)).systemTopicEventSubscriptions();
verify(systemTopicEventSubscriptions, times(1)).define(subscriptionId);
verify(systemTopicEventSubscription, times(1)).withExistingSystemTopic(resourceGroupName, topicName);
verify(eventGridManager, times(1)).eventSubscriptions();
verify(eventSubscriptions, times(1)).define(subscriptionId);
verify(eventSubscription, times(1)).withScope(scope);
verify(logger, times(1)).error(String.format("Creating subscription with id %s failed with error %s", subscriptionId, errorMessage));
}
@Test
public void shouldReturnTrueWhenPushSubscriptionDeleted() {
when(systemTopicEventSubscriptions.deleteAsync(resourceGroupName, topicName, subscriptionId)).thenReturn(completable);
when(eventSubscriptions.deleteAsync(scope, subscriptionId)).thenReturn(completable);
boolean result = pushSubscription.deletePushSubscription(subscriptionId, topicName);
assertTrue(result);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(eventGridManager, times(1)).systemTopicEventSubscriptions();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).info(String.format("Subscription with id %s deleted successfully", subscriptionId));
}
@Test
public void shouldReturnFalseWhenPushSubscriptionDeletionFailed() {
when(systemTopicEventSubscriptions.deleteAsync(resourceGroupName, topicName, subscriptionId)).thenReturn(completableError);
when(eventSubscriptions.deleteAsync(scope, subscriptionId)).thenReturn(completableError);
boolean result = pushSubscription.deletePushSubscription(subscriptionId, topicName);
assertFalse(result);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(eventGridManager, times(1)).systemTopicEventSubscriptions();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).error(String.format("Delete operation on subscription with id %s failed with error %s", subscriptionId, errorMessage));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment