Commit b7842a03 authored by harshit aggarwal's avatar harshit aggarwal
Browse files

commenting out UT

parent 4373c55d
Pipeline #30616 failed with stages
in 45 seconds
// 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.register.provider.azure.subscriber;
import org.junit.jupiter.api.BeforeEach;
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.cosmosdb.CosmosStore;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.di.CosmosContainerConfig;
import org.opengroup.osdu.register.provider.azure.util.CryptographyUtil;
import org.opengroup.osdu.register.provider.azure.util.TopicsHelper;
import org.opengroup.osdu.register.subscriber.model.GsaSecret;
import org.opengroup.osdu.register.subscriber.model.GsaSecretValue;
import org.opengroup.osdu.register.subscriber.model.HmacSecret;
import org.opengroup.osdu.register.subscriber.model.Secret;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
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.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class SubscriptionRepositoryTest {
private static final String dataPartitionId = "data-partition-id";
private static final String subscriptionId = "id-1";
private static final String subscriptionContainer = "ddms-container";
private static final String cosmosDatabase = "cosmos-database";
private static final String secretValue = "secret-value";
private static final String secretType = "secret-type";
private static final Timestamp currentTime = new Timestamp(System.currentTimeMillis());
private static final String topicName = "topic-name";
@Mock
private DpsHeaders dpsHeaders;
@Mock
private CosmosStore cosmosStore;
@Mock
private AzureBootstrapConfig azureBootstrapConfig;
@Mock
private CosmosContainerConfig cosmosContainerConfig;
@Mock
private JaxRsDpsLog logger;
@Mock
private CryptographyUtil cryptographyUtil;
@Mock
private PushSubscription pushSubscription;
@Mock
private Subscription subscription;
@Mock
private SubscriptionDoc subscriptionDoc;
@Mock
private TopicsHelper topicsHelper;
@InjectMocks
private SubscriptionRepository repo;
@BeforeEach
void init() {
lenient().doReturn(dataPartitionId).when(dpsHeaders).getPartitionId();
lenient().when(azureBootstrapConfig.getCosmosDBName()).thenReturn(cosmosDatabase);
lenient().when(cosmosContainerConfig.getSubscriptionContainerName()).thenReturn(subscriptionContainer);
lenient().when(subscription.getId()).thenReturn(subscriptionId);
lenient().when(subscription.getSecret()).thenReturn(new HmacSecret(secretValue));
lenient().when(subscription.getNotificationId()).thenReturn(subscriptionId);
lenient().when(subscription.getTopic()).thenReturn(topicName);
lenient().when(topicsHelper.nameResolver(topicName)).thenReturn(topicName);
lenient().when(topicsHelper.extractDisplayName(topicName)).thenReturn(topicName);
lenient().when(subscriptionDoc.getSecretType()).thenReturn(secretType);
lenient().when(subscriptionDoc.getSecretValue()).thenReturn(secretValue);
lenient().when(subscriptionDoc.getCreatedOnEpoch()).thenReturn(currentTime);
lenient().when(subscriptionDoc.getNotificationId()).thenReturn(subscriptionId);
lenient().when(subscriptionDoc.getTopic()).thenReturn(topicName);
}
@Test
public void createSubscriptionThrows409WhenIdExists() {
doThrow(new AppException(409, "Reason", "Message")).when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doNothing().when(pushSubscription).checkIfPushSubscriptionExists(subscriptionId, topicName);
AppException exception = assertThrows(AppException.class, () -> {
repo.create(subscription);
});
assertEquals(409, exception.getError().getCode());
assertEquals("Conflict", exception.getError().getReason());
assertEquals("A subscriber already exists with the same topic and endpoint combination", exception.getError().getMessage());
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(cosmosContainerConfig, times(2)).getSubscriptionContainerName();
verify(azureBootstrapConfig, times(2)).getCosmosDBName();
verify(subscription, times(2)).getId();
verify(dpsHeaders, times(5)).getPartitionId();
verify(logger, times(1)).error("A subscriber already exists with the same topic and endpoint combination");
}
@Test
public void createSubscriptionDocumentInsertedSuccessfullyAndPushSubscriptionCreated() {
doNothing().when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
when(cryptographyUtil.encryptData(secretValue)).thenReturn("some-string");
Subscription output = repo.create(subscription);
assertEquals(subscription, output);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(dpsHeaders, times(3)).getPartitionId();
verify(cryptographyUtil, times(1)).encryptData(secretValue);
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(subscription, times(3)).getSecret();
verify(subscription, times(2)).getId();
verifyCallsToSubscriptionMock();
}
@Test
public void createSubscriptionDocumentInsertedSuccessfullyButPushSubscriptionCreationFailed() throws Exception {
doNothing().when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doNothing().when(cosmosStore).deleteItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId));
doThrow(new AppException(500, "Server Error", "Unexpected error creating Push subscription")).when(pushSubscription).createPushSubscription(subscriptionId, topicName);
when(cryptographyUtil.encryptData(secretValue)).thenReturn("some-string");
AppException exception = assertThrows(AppException.class, () -> {
repo.create(subscription);
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error creating subscription", exception.getError().getMessage());
verify(cosmosStore, times(1)).deleteItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId);
verify(azureBootstrapConfig, times(2)).getCosmosDBName();
verify(cosmosContainerConfig, times(2)).getSubscriptionContainerName();
verify(subscription, times(4)).getId();
verify(subscription, times(3)).getSecret();
verify(dpsHeaders, times(5)).getPartitionId();
verify(cryptographyUtil, times(1)).encryptData(secretValue);
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(pushSubscription, times(1)).createPushSubscription(subscriptionId, topicName);
verify(logger, times(1)).error("Unexpected error creating subscription");
verifyCallsToSubscriptionMock();
}
@Test
public void subscriptionCreatedSuccessfullyIfRecordExistsInCosmosDbButPushSubscriptionIsNotPresent() throws Exception {
doThrow(new AppException(409, "Reason", "Message")).when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doThrow(new AppException(404, "Reason", "Message")).when(pushSubscription).checkIfPushSubscriptionExists(subscriptionId, topicName);
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doNothing().when(cosmosStore).upsertItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
when(cryptographyUtil.encryptData(secretValue)).thenReturn("some-string");
Subscription output = repo.create(subscription);
assertEquals(output, subscription);
verify(azureBootstrapConfig, times(3)).getCosmosDBName();
verify(cosmosContainerConfig, times(3)).getSubscriptionContainerName();
verify(subscription, times(2)).getId();
verify(subscription, times(3)).getSecret();
verify(dpsHeaders, times(7)).getPartitionId();
verify(cryptographyUtil, times(1)).encryptData(secretValue);
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(pushSubscription, times(1)).createPushSubscription(subscriptionId, topicName);
verify(pushSubscription, times(1)).checkIfPushSubscriptionExists(subscriptionId, topicName);
verifyCallsToSubscriptionMock();
}
@Test
public void createSubscriptionShouldThrow500ExceptionIfRecordExistsInCosmosDbButFailedToCreatePushSubscription() {
doThrow(new AppException(409, "Reason", "Message")).when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doThrow(new AppException(404, "Reason", "Message")).when(pushSubscription).checkIfPushSubscriptionExists(subscriptionId, topicName);
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doThrow(new AppException(500, "Server Error", "Unexpected error creating Push subscription")).when(pushSubscription).createPushSubscription(subscriptionId, topicName);
doNothing().when(cosmosStore).upsertItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
when(cryptographyUtil.encryptData(secretValue)).thenReturn("some-string");
AppException exception = assertThrows(AppException.class, () -> {
repo.create(subscription);
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error creating subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(3)).getCosmosDBName();
verify(cosmosContainerConfig, times(3)).getSubscriptionContainerName();
verify(subscription, times(2)).getId();
verify(subscription, times(3)).getSecret();
verify(dpsHeaders, times(7)).getPartitionId();
verify(cryptographyUtil, times(1)).encryptData(secretValue);
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(pushSubscription, times(1)).createPushSubscription(subscriptionId, topicName);
verify(pushSubscription, times(1)).checkIfPushSubscriptionExists(subscriptionId, topicName);
verify(logger, times(1)).error("Unexpected error creating subscription");
verifyCallsToSubscriptionMock();
}
@Test
public void createSubscriptionShouldThrow500ExceptionIfRecordExistsInCosmosDbButUpsertOperationFailed() {
doThrow(new AppException(409, "Reason", "Message")).when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doThrow(new AppException(404, "Reason", "Message")).when(pushSubscription).checkIfPushSubscriptionExists(subscriptionId, topicName);
doThrow(new AppException(500, "Reason", "Message")).when(cosmosStore).upsertItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
when(cryptographyUtil.encryptData(secretValue)).thenReturn("some-string");
AppException exception = assertThrows(AppException.class, () -> {
repo.create(subscription);
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error creating subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(3)).getCosmosDBName();
verify(cosmosContainerConfig, times(3)).getSubscriptionContainerName();
verify(subscription, times(2)).getId();
verify(subscription, times(3)).getSecret();
verify(dpsHeaders, times(7)).getPartitionId();
verify(cryptographyUtil, times(1)).encryptData(secretValue);
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(pushSubscription, times(1)).checkIfPushSubscriptionExists(subscriptionId, topicName);
verify(logger, times(1)).error("Unexpected error creating subscription");
verifyCallsToSubscriptionMock();
}
@Test
public void createSubscriptionShouldThrow500ExceptionIfRecordExistsInCosmosDbButFailedToGetPushSubscription() {
doThrow(new AppException(409, "Reason", "Message")).when(cosmosStore).createItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doThrow(new AppException(500, "Reason", "Message")).when(pushSubscription).checkIfPushSubscriptionExists(subscriptionId, topicName);
when(cryptographyUtil.encryptData(secretValue)).thenReturn("some-string");
AppException exception = assertThrows(AppException.class, () -> {
repo.create(subscription);
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error creating subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(2)).getCosmosDBName();
verify(cosmosContainerConfig, times(2)).getSubscriptionContainerName();
verify(subscription, times(2)).getId();
verify(subscription, times(3)).getSecret();
verify(dpsHeaders, times(5)).getPartitionId();
verify(cryptographyUtil, times(1)).encryptData(secretValue);
verify(cryptographyUtil, times(1)).getKeyName();
verify(cryptographyUtil, times(1)).getKeyVersion();
verify(pushSubscription, times(1)).checkIfPushSubscriptionExists(subscriptionId, topicName);
verify(logger, times(1)).error("Unexpected error creating subscription");
verifyCallsToSubscriptionMock();
}
@Test
public void getSubscriptionThrows404WhenIdNotFound() {
doReturn(Optional.empty()).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
AppException exception = assertThrows(AppException.class, () -> {
repo.get(subscriptionId);
});
assertEquals(404, exception.getError().getCode());
assertEquals("Not found", exception.getError().getReason());
assertEquals("Subscriber with id id-1 does not exist.", exception.getError().getMessage());
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(dpsHeaders, times(2)).getPartitionId();
verify(logger, times(1)).error("Subscriber with id id-1 does not exist.");
}
@Test
public void getSubscriptionItemRetrievedSuccessfully() {
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
when(cryptographyUtil.decryptData(secretValue)).thenReturn(secretValue);
Subscription expected = getSubscription();
Subscription output = repo.get(subscriptionId);
expected.setCreatedOnEpoch(output.getCreatedOnEpoch());
assertEquals(expected, output);
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(cryptographyUtil, times(1)).decryptData(secretValue);
verify(dpsHeaders, times(2)).getPartitionId();
verifyCallsToSubscriptionDocMock();
}
@Test
public void getSubscriptionItemRetrievedSuccessfullyWithGSASecretType() {
String audience = "audience";
String key = "key";
String gsaSecretStringValue = audience + "`" + key;
GsaSecretValue gsaSecretValue = new GsaSecretValue(audience, key);
Subscription expected = getSubscription();
expected.setSecret(new GsaSecret(gsaSecretValue));
when(subscriptionDoc.getSecretType()).thenReturn("GSA");
when(subscriptionDoc.getSecretValue()).thenReturn(gsaSecretStringValue);
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
when(cryptographyUtil.decryptData(gsaSecretStringValue)).thenReturn(gsaSecretStringValue);
Subscription output = repo.get(subscriptionId);
expected.setCreatedOnEpoch(output.getCreatedOnEpoch());
assertEquals(expected, output);
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(cryptographyUtil, times(1)).decryptData(gsaSecretStringValue);
verify(dpsHeaders, times(2)).getPartitionId();
verifyCallsToSubscriptionDocMock();
}
@Test
public void querySubscriptionsReturnItemsSuccessfully() {
List<SubscriptionDoc> docsList = Collections.singletonList(subscriptionDoc);
Subscription expected = getSubscription();
doReturn(docsList).when(cosmosStore).queryItems(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), any(), any(), any());
when(cryptographyUtil.decryptData(secretValue)).thenReturn(secretValue);
List<Subscription> output = repo.query("notification-id");
expected.setCreatedOnEpoch(output.get(0).getCreatedOnEpoch());
assertEquals(Collections.singletonList(expected), output);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(cryptographyUtil, times(1)).decryptData(secretValue);
verify(dpsHeaders, times(2)).getPartitionId();
verifyCallsToSubscriptionDocMock();
}
@Test
public void deleteSubscriptionReturnsFalseWhenIdNotFound() {
doReturn(Optional.empty()).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
boolean output = repo.delete(subscriptionId);
assertFalse(output);
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(dpsHeaders, times(2)).getPartitionId();
}
@Test
public void deleteSubscriptionReturnsTrueWhenDeletedSuccessfully() {
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doNothing().when(cosmosStore).deleteItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId));
doNothing().when(pushSubscription).deletePushSubscription(subscriptionId, topicName);
boolean output = repo.delete(subscriptionId);
assertTrue(output);
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(cosmosStore, times(1)).deleteItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId);
verify(azureBootstrapConfig, times(2)).getCosmosDBName();
verify(cosmosContainerConfig, times(2)).getSubscriptionContainerName();
verify(subscriptionDoc, times(1)).getTopic();
verify(subscriptionDoc, times(2)).getNotificationId();
verify(dpsHeaders, times(4)).getPartitionId();
verify(pushSubscription, times(1)).deletePushSubscription(subscriptionId, topicName);
}
@Test
public void deleteSubscriptionThrows500ExceptionWhenDeletionFailedInCosmosDb() {
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doNothing().when(pushSubscription).deletePushSubscription(subscriptionId, topicName);
doThrow(new AppException(500, "Server Error", "Unexpected error deleting subscription")).when(cosmosStore).deleteItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId));
AppException exception = assertThrows(AppException.class, () -> {
repo.delete(subscriptionId);
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error deleting subscription", exception.getError().getMessage());
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(cosmosStore, times(1)).deleteItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId);
verify(azureBootstrapConfig, times(2)).getCosmosDBName();
verify(cosmosContainerConfig, times(2)).getSubscriptionContainerName();
verify(dpsHeaders, times(4)).getPartitionId();
verify(pushSubscription, times(1)).deletePushSubscription(subscriptionId, topicName);
verify(logger, times(1)).error("Unexpected error deleting subscription");
}
@Test
public void deleteSubscriptionThrows500ExceptionWhenDeletingPushSubscriptionFailed() {
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doThrow(new AppException(500, "Server Error", "Unexpected error deleting subscription")).when(pushSubscription).deletePushSubscription(subscriptionId, topicName);
AppException exception = assertThrows(AppException.class, () -> {
repo.delete(subscriptionId);
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error deleting subscription", exception.getError().getMessage());
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(dpsHeaders, times(2)).getPartitionId();
verify(pushSubscription, times(1)).deletePushSubscription(subscriptionId, topicName);
verify(logger, times(1)).error("Unexpected error deleting subscription");
}
@Test
public void deleteSubscriptionThrows409ExceptionWhenMultipleDeletePushSubscriptionCalls() {
doReturn(Optional.of(subscriptionDoc)).when(cosmosStore).findItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(subscriptionId), eq(dataPartitionId), any());
doThrow(new AppException(409, "Conflict", "Another request is trying to delete the same subscription")).when(pushSubscription).deletePushSubscription(subscriptionId, topicName);
AppException exception = assertThrows(AppException.class, () -> {
repo.delete(subscriptionId);
});
assertNotNull(exception);
assertEquals(409, exception.getError().getCode());
assertEquals("Conflict", exception.getError().getReason());
assertEquals("Another request is trying to delete the same subscription", exception.getError().getMessage());
verify(cosmosStore, times(1)).findItem(dataPartitionId, cosmosDatabase, subscriptionContainer, subscriptionId, dataPartitionId, SubscriptionDoc.class);
verify(azureBootstrapConfig, times(1)).getCosmosDBName();
verify(cosmosContainerConfig, times(1)).getSubscriptionContainerName();
verify(dpsHeaders, times(2)).getPartitionId();
verify(pushSubscription, times(1)).deletePushSubscription(subscriptionId, topicName);
verify(logger, times(1)).error("Another request is trying to delete the same subscription");
}
@Test
public void patchShouldSuccessfullyUpdateSecret() {
Secret secret = Mockito.mock(Secret.class);
when(secret.getSecretType()).thenReturn(secretType);
when(secret.toString()).thenReturn(secretValue);
when(cryptographyUtil.encryptData(secretValue)).thenReturn(secretValue);
doNothing().when(cosmosStore).upsertItem(eq(dataPartitionId), eq(cosmosDatabase), eq(subscriptionContainer), eq(dataPartitionId), any());
boolean output = repo.patch(subscription, secret);
assertTrue(output);
verify(secret, times(1)).getSecretType();
verify(subscription, times(1)).getId();