From 99e8f018e28319b3832b7b790215945fc4a40bf7 Mon Sep 17 00:00:00 2001 From: hui Date: Wed, 31 Aug 2022 18:18:08 +0800 Subject: [PATCH 01/11] fix servicebus consumer auto create topic/subscription --- .../crud/ServiceBusTopicSubscriptionCrud.java | 6 +- .../ServiceBusTopicSubscriptionCrudTests.java | 85 +++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java index fa03fb65c8eb5..f85912eef3d38 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java @@ -6,6 +6,7 @@ import com.azure.core.management.exception.ManagementException; import com.azure.resourcemanager.AzureResourceManager; import com.azure.resourcemanager.servicebus.models.ServiceBusSubscription; +import com.azure.resourcemanager.servicebus.models.Topic; import com.azure.spring.cloud.core.properties.resource.AzureResourceMetadata; import reactor.util.function.Tuple3; import reactor.util.function.Tuples; @@ -34,8 +35,9 @@ String getResourceType() { @Override public ServiceBusSubscription internalGet(Tuple3 subscriptionCoordinate) { try { - return new ServiceBusTopicCrud(this.resourceManager, this.resourceMetadata) - .get(Tuples.of(subscriptionCoordinate.getT1(), subscriptionCoordinate.getT2())) + Topic topic = new ServiceBusTopicCrud(this.resourceManager, this.resourceMetadata) + .get(Tuples.of(subscriptionCoordinate.getT1(), subscriptionCoordinate.getT2())); + return topic == null ? null :topic .subscriptions() .getByName(subscriptionCoordinate.getT2()); } catch (ManagementException e) { diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java new file mode 100644 index 0000000000000..9362f634a103f --- /dev/null +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -0,0 +1,85 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.resourcemanager.implementation.crud; + +import com.azure.core.management.exception.ManagementException; +import com.azure.resourcemanager.servicebus.models.ServiceBusNamespace; +import com.azure.resourcemanager.servicebus.models.ServiceBusNamespaces; +import com.azure.resourcemanager.servicebus.models.ServiceBusSubscription; +import com.azure.resourcemanager.servicebus.models.ServiceBusSubscriptions; +import com.azure.resourcemanager.servicebus.models.Topic; +import com.azure.resourcemanager.servicebus.models.Topics; +import reactor.util.function.Tuple3; +import reactor.util.function.Tuples; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ServiceBusTopicSubscriptionCrudTests extends AbstractResourceCrudTests>{ + + private static final String NAMESPACE = "namespace"; + private static final String TOPIC_NAME = "topic"; + private static final String SUBSCRIPTION_NAME = "subscription"; + + @Override + AbstractResourceCrud> getResourceCrud() { + return new ServiceBusTopicSubscriptionCrud(resourceManager, resourceMetadata); + } + + @Override + void getStubManagementException(int statusCode, String message) { + ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); + ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); + + when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); + ManagementException exception = getManagementException(statusCode, message); + when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) + .thenReturn(namespace); + + Topics topics = mock(Topics.class); + Topic topic = mock(Topic.class); + ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); + when(namespace.topics()).thenReturn(topics); + when(topics.getByName(getKey().getT2())).thenReturn(topic); + when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); + when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); + + } + + + @Override + void createStubManagementException() { + ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); + ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); + when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); + ManagementException exception = getManagementException(500, "Create service bus namespace exception"); + when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) + .thenReturn(namespace); + + Topics topics = mock(Topics.class); + Topic topic = mock(Topic.class); + ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); + when(namespace.topics()).thenReturn(topics); + when(topics.getByName(getKey().getT2())).thenReturn(topic); + when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); + when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); + + ServiceBusTopicCrud serviceBusTopicCrud = mock(ServiceBusTopicCrud.class); + when(serviceBusTopicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenThrow(exception); + +// ServiceBusSubscription.DefinitionStages.Blank define = mock(ServiceBusSubscription.DefinitionStages.Blank.class); +// when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); +// when(define.create()).thenThrow(exception); +// +// ServiceBusSubscription.DefinitionStages.WithCreate create = mock(ServiceBusSubscription.DefinitionStages.WithCreate.class); +// when(create.create()).thenThrow(exception); + } + + @Override + Tuple3 getKey() { + return Tuples.of(NAMESPACE, TOPIC_NAME, SUBSCRIPTION_NAME); + } + +} From c970636060e67c281b0c0d7104f4cc51b09e8d93 Mon Sep 17 00:00:00 2001 From: hui Date: Wed, 31 Aug 2022 18:44:59 +0800 Subject: [PATCH 02/11] update tests --- .../ServiceBusTopicSubscriptionCrudTests.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index 9362f634a103f..ff164c1c0ec3d 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -44,11 +44,9 @@ void getStubManagementException(int statusCode, String message) { when(namespace.topics()).thenReturn(topics); when(topics.getByName(getKey().getT2())).thenReturn(topic); when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); - when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); - + when(serviceBusSubscriptions.getByName(getKey().getT2())).thenThrow(exception); } - @Override void createStubManagementException() { ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); @@ -67,14 +65,14 @@ void createStubManagementException() { when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); ServiceBusTopicCrud serviceBusTopicCrud = mock(ServiceBusTopicCrud.class); - when(serviceBusTopicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenThrow(exception); - -// ServiceBusSubscription.DefinitionStages.Blank define = mock(ServiceBusSubscription.DefinitionStages.Blank.class); -// when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); -// when(define.create()).thenThrow(exception); -// -// ServiceBusSubscription.DefinitionStages.WithCreate create = mock(ServiceBusSubscription.DefinitionStages.WithCreate.class); -// when(create.create()).thenThrow(exception); + when(serviceBusTopicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(null); + + ServiceBusSubscription.DefinitionStages.Blank define = mock(ServiceBusSubscription.DefinitionStages.Blank.class); + when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); + when(define.create()).thenThrow(exception); + + ServiceBusSubscription.DefinitionStages.WithCreate create = mock(ServiceBusSubscription.DefinitionStages.WithCreate.class); + when(create.create()).thenThrow(exception); } @Override From fd9f237826c854805ce4fbc999f44a3e19a90d74 Mon Sep 17 00:00:00 2001 From: hui Date: Wed, 31 Aug 2022 18:53:42 +0800 Subject: [PATCH 03/11] add changelog --- sdk/spring/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sdk/spring/CHANGELOG.md b/sdk/spring/CHANGELOG.md index cf8fe0e459e1a..1a27484918a56 100644 --- a/sdk/spring/CHANGELOG.md +++ b/sdk/spring/CHANGELOG.md @@ -14,6 +14,11 @@ This section includes changes in `spring-cloud-azure-autoconfigure` module. #### Features Added - Support auto start-up for the auto-configured Service Bus Processor Client by enabling a new property of `spring.cloud.azure.servicebus.processor.auto-startup`. [#29997](https://github.com/Azure/azure-sdk-for-java/issues/29997) +### Spring Cloud Azure Resource Manager + +#### Bugs Fixed +- Fix bug: Service Bus stream binder cannot automatically create Topic/Subscriptions from consumer. + ## 4.3.0 (2022-06-29) - This release is compatible with Spring Boot 2.5.0-2.5.14, 2.6.0-2.6.9, 2.7.0-2.7.1. (Note: 2.5.x (x>14), 2.6.y (y>9) and 2.7.z (z>1) should be supported, but they aren't tested with this release.) - This release is compatible with Spring Cloud 2020.0.3-2020.0.5, 2021.0.0-2021.0.3. (Note: 2020.0.x (x>5) and 2021.0.y (y>3) should be supported, but they aren't tested with this release.) From d074c7503de25018751bf37fc5bae0b2da681243 Mon Sep 17 00:00:00 2001 From: hui Date: Thu, 1 Sep 2022 09:43:54 +0800 Subject: [PATCH 04/11] fix checkstyle error --- .../implementation/crud/ServiceBusTopicSubscriptionCrud.java | 2 +- .../crud/ServiceBusTopicSubscriptionCrudTests.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java index f85912eef3d38..3d787befd6495 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java @@ -37,7 +37,7 @@ public ServiceBusSubscription internalGet(Tuple3 subscri try { Topic topic = new ServiceBusTopicCrud(this.resourceManager, this.resourceMetadata) .get(Tuples.of(subscriptionCoordinate.getT1(), subscriptionCoordinate.getT2())); - return topic == null ? null :topic + return topic == null ? null : topic .subscriptions() .getByName(subscriptionCoordinate.getT2()); } catch (ManagementException e) { diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index ff164c1c0ec3d..bfc2c36121a5d 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -17,7 +17,7 @@ import static org.mockito.Mockito.when; public class ServiceBusTopicSubscriptionCrudTests extends AbstractResourceCrudTests>{ + Tuple3> { private static final String NAMESPACE = "namespace"; private static final String TOPIC_NAME = "topic"; @@ -77,7 +77,7 @@ void createStubManagementException() { @Override Tuple3 getKey() { - return Tuples.of(NAMESPACE, TOPIC_NAME, SUBSCRIPTION_NAME); + return Tuples.of(NAMESPACE, TOPIC_NAME, SUBSCRIPTION_NAME); } } From a609f94e19f2d2cc58fe625ac7de8c924b0cce78 Mon Sep 17 00:00:00 2001 From: hui Date: Thu, 1 Sep 2022 13:41:48 +0800 Subject: [PATCH 05/11] update changelog and tests --- sdk/spring/CHANGELOG.md | 2 +- .../crud/ServiceBusTopicSubscriptionCrudTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/spring/CHANGELOG.md b/sdk/spring/CHANGELOG.md index 1a27484918a56..fe42302ea5b1e 100644 --- a/sdk/spring/CHANGELOG.md +++ b/sdk/spring/CHANGELOG.md @@ -17,7 +17,7 @@ This section includes changes in `spring-cloud-azure-autoconfigure` module. ### Spring Cloud Azure Resource Manager #### Bugs Fixed -- Fix bug: Service Bus stream binder cannot automatically create Topic/Subscriptions from consumer. +- Fix the Service Bus stream binder cannot automatically create Topic/Subscriptions from consumer. [#30722](https://github.com/Azure/azure-sdk-for-java/pull/30722). ## 4.3.0 (2022-06-29) - This release is compatible with Spring Boot 2.5.0-2.5.14, 2.6.0-2.6.9, 2.7.0-2.7.1. (Note: 2.5.x (x>14), 2.6.y (y>9) and 2.7.z (z>1) should be supported, but they aren't tested with this release.) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index bfc2c36121a5d..52b1e95d6e23f 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -16,7 +16,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class ServiceBusTopicSubscriptionCrudTests extends AbstractResourceCrudTests> { private static final String NAMESPACE = "namespace"; From 5414339c6a5b25045e815b7977ac1a243e35a5c0 Mon Sep 17 00:00:00 2001 From: hui Date: Thu, 1 Sep 2022 19:18:21 +0800 Subject: [PATCH 06/11] update tests --- .../ServiceBusTopicSubscriptionCrudTests.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index 52b1e95d6e23f..d27ba8c4c610e 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -10,6 +10,8 @@ import com.azure.resourcemanager.servicebus.models.ServiceBusSubscriptions; import com.azure.resourcemanager.servicebus.models.Topic; import com.azure.resourcemanager.servicebus.models.Topics; +import org.junit.jupiter.api.Test; +import org.springframework.util.Assert; import reactor.util.function.Tuple3; import reactor.util.function.Tuples; @@ -80,4 +82,42 @@ Tuple3 getKey() { return Tuples.of(NAMESPACE, TOPIC_NAME, SUBSCRIPTION_NAME); } + @Test + void topicDoesNotExistReturnNull(){ + ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); + ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); + + when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); + when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), NAMESPACE)) + .thenReturn(namespace); + + Topics topics = mock(Topics.class); + when(namespace.topics()).thenReturn(topics); + Assert.isNull(namespace.topics().getByName(TOPIC_NAME)); + + } + + @Test + void topicDoesNotExistReturnNullToCreate(){ + ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); + ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); + + when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); + when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), NAMESPACE)) + .thenReturn(namespace); + + Topics topics = mock(Topics.class); + Topic topic = mock(Topic.class); + when(namespace.topics()).thenReturn(topics); + Assert.isNull(namespace.topics().getByName(TOPIC_NAME)); + when(topics.getByName(TOPIC_NAME)).thenReturn(topic); + + Topic.DefinitionStages.Blank define = mock(Topic.DefinitionStages.Blank.class); + when(topics.define(TOPIC_NAME)).thenReturn(define); + + Topic.DefinitionStages.WithCreate create = mock(Topic.DefinitionStages.WithCreate.class); + when(define.withNewSubscription(SUBSCRIPTION_NAME)).thenReturn(create); + when(create.create()).thenReturn(topic); + } + } From 058edbc0c18b1557ea8e900deda9d2f41261652b Mon Sep 17 00:00:00 2001 From: hui Date: Thu, 1 Sep 2022 19:32:30 +0800 Subject: [PATCH 07/11] update tests --- .../crud/ServiceBusTopicSubscriptionCrudTests.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index d27ba8c4c610e..7e3e2b80b3bf9 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -94,7 +94,6 @@ void topicDoesNotExistReturnNull(){ Topics topics = mock(Topics.class); when(namespace.topics()).thenReturn(topics); Assert.isNull(namespace.topics().getByName(TOPIC_NAME)); - } @Test @@ -112,12 +111,19 @@ void topicDoesNotExistReturnNullToCreate(){ Assert.isNull(namespace.topics().getByName(TOPIC_NAME)); when(topics.getByName(TOPIC_NAME)).thenReturn(topic); + ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); + ServiceBusSubscription serviceBusSubscription = mock(ServiceBusSubscription.class); + when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); + when(serviceBusSubscriptions.getByName(getKey().getT2())).thenReturn(serviceBusSubscription); + Topic.DefinitionStages.Blank define = mock(Topic.DefinitionStages.Blank.class); when(topics.define(TOPIC_NAME)).thenReturn(define); Topic.DefinitionStages.WithCreate create = mock(Topic.DefinitionStages.WithCreate.class); when(define.withNewSubscription(SUBSCRIPTION_NAME)).thenReturn(create); when(create.create()).thenReturn(topic); + Assert.notNull(topic); + Assert.notNull(topic.subscriptions()); } } From b51644ea640fb37a6559e5786a2202ab8904e277 Mon Sep 17 00:00:00 2001 From: hui Date: Fri, 2 Sep 2022 10:44:01 +0800 Subject: [PATCH 08/11] update tests --- .../crud/ServiceBusTopicSubscriptionCrud.java | 15 ++++- .../ServiceBusTopicSubscriptionCrudTests.java | 57 ++++++++----------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java index 3d787befd6495..8f2a04303c392 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java @@ -17,11 +17,20 @@ public class ServiceBusTopicSubscriptionCrud extends AbstractResourceCrud> { - + private ServiceBusTopicCrud serviceBusTopicCrud; public ServiceBusTopicSubscriptionCrud(AzureResourceManager azureResourceManager, AzureResourceMetadata azureResourceMetadata) { + this(azureResourceManager, azureResourceMetadata, + new ServiceBusTopicCrud(azureResourceManager, azureResourceMetadata)); + } + + ServiceBusTopicSubscriptionCrud(AzureResourceManager azureResourceManager, + AzureResourceMetadata azureResourceMetadata, + ServiceBusTopicCrud serviceBusTopicCrud) { super(azureResourceManager, azureResourceMetadata); + this.serviceBusTopicCrud = serviceBusTopicCrud; } + @Override String getResourceName(Tuple3 key) { return key.getT3(); @@ -35,7 +44,7 @@ String getResourceType() { @Override public ServiceBusSubscription internalGet(Tuple3 subscriptionCoordinate) { try { - Topic topic = new ServiceBusTopicCrud(this.resourceManager, this.resourceMetadata) + Topic topic = this.serviceBusTopicCrud .get(Tuples.of(subscriptionCoordinate.getT1(), subscriptionCoordinate.getT2())); return topic == null ? null : topic .subscriptions() @@ -51,7 +60,7 @@ public ServiceBusSubscription internalGet(Tuple3 subscri @Override public ServiceBusSubscription internalCreate(Tuple3 subscriptionCoordinate) { - return new ServiceBusTopicCrud(this.resourceManager, this.resourceMetadata) + return this.serviceBusTopicCrud .getOrCreate(Tuples.of(subscriptionCoordinate.getT1(), subscriptionCoordinate.getT2())) .subscriptions() .define(subscriptionCoordinate.getT3()) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index 7e3e2b80b3bf9..e67076399b309 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -10,8 +10,8 @@ import com.azure.resourcemanager.servicebus.models.ServiceBusSubscriptions; import com.azure.resourcemanager.servicebus.models.Topic; import com.azure.resourcemanager.servicebus.models.Topics; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.springframework.util.Assert; import reactor.util.function.Tuple3; import reactor.util.function.Tuples; @@ -83,47 +83,40 @@ Tuple3 getKey() { } @Test - void topicDoesNotExistReturnNull(){ - ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); - ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); + void topicDoesNotExistReturnNull() { + ServiceBusTopicCrud topicCrud = mock(ServiceBusTopicCrud.class); + ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, + this.resourceMetadata, topicCrud); - when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); - when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), NAMESPACE)) - .thenReturn(namespace); + when(topicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(null); - Topics topics = mock(Topics.class); - when(namespace.topics()).thenReturn(topics); - Assert.isNull(namespace.topics().getByName(TOPIC_NAME)); + ServiceBusSubscription actualGet = topicSubCrud.get(getKey()); + Assertions.assertNull(actualGet); } @Test - void topicDoesNotExistReturnNullToCreate(){ - ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); - ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); - - when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); - when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), NAMESPACE)) - .thenReturn(namespace); + void topicDoesNotExistReturnNullToCreate() { + ServiceBusTopicCrud topicCrud = mock(ServiceBusTopicCrud.class); + ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, + this.resourceMetadata, topicCrud); - Topics topics = mock(Topics.class); Topic topic = mock(Topic.class); - when(namespace.topics()).thenReturn(topics); - Assert.isNull(namespace.topics().getByName(TOPIC_NAME)); - when(topics.getByName(TOPIC_NAME)).thenReturn(topic); - ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); - ServiceBusSubscription serviceBusSubscription = mock(ServiceBusSubscription.class); when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); - when(serviceBusSubscriptions.getByName(getKey().getT2())).thenReturn(serviceBusSubscription); - - Topic.DefinitionStages.Blank define = mock(Topic.DefinitionStages.Blank.class); - when(topics.define(TOPIC_NAME)).thenReturn(define); - Topic.DefinitionStages.WithCreate create = mock(Topic.DefinitionStages.WithCreate.class); - when(define.withNewSubscription(SUBSCRIPTION_NAME)).thenReturn(create); - when(create.create()).thenReturn(topic); - Assert.notNull(topic); - Assert.notNull(topic.subscriptions()); + ServiceBusSubscription.DefinitionStages.Blank define = + mock(ServiceBusSubscription.DefinitionStages.Blank.class); + when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); + ServiceBusSubscription serviceBusSubscription = mock(ServiceBusSubscription.class); + when(define.create()).thenReturn(serviceBusSubscription); + when(topicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(null); + when(topicCrud.getOrCreate(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(topic); + + ServiceBusSubscription actualGet = topicSubCrud.get(getKey()); + ServiceBusSubscription actualCreate = topicSubCrud.create(getKey()); + Assertions.assertNull(actualGet); + Assertions.assertNotNull(actualCreate); + Assertions.assertEquals(serviceBusSubscription, actualCreate); } } From ac542df2e5a434abff747e6b8fab757e3bf8df68 Mon Sep 17 00:00:00 2001 From: hui Date: Fri, 2 Sep 2022 15:00:24 +0800 Subject: [PATCH 09/11] add tests --- .../ServiceBusTopicSubscriptionCrudTests.java | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index e67076399b309..6b3e40a12b69e 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -66,15 +66,9 @@ void createStubManagementException() { when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); - ServiceBusTopicCrud serviceBusTopicCrud = mock(ServiceBusTopicCrud.class); - when(serviceBusTopicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(null); - ServiceBusSubscription.DefinitionStages.Blank define = mock(ServiceBusSubscription.DefinitionStages.Blank.class); when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); when(define.create()).thenThrow(exception); - - ServiceBusSubscription.DefinitionStages.WithCreate create = mock(ServiceBusSubscription.DefinitionStages.WithCreate.class); - when(create.create()).thenThrow(exception); } @Override @@ -119,4 +113,40 @@ void topicDoesNotExistReturnNullToCreate() { Assertions.assertEquals(serviceBusSubscription, actualCreate); } + @Test + void topicExistSubscriptionDoesNotExist() { + ServiceBusNamespaceCrud namespaceCrud = mock(ServiceBusNamespaceCrud.class); + ServiceBusTopicCrud topicCrud = new ServiceBusTopicCrud(this.resourceManager, + this.resourceMetadata); + ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, + this.resourceMetadata, topicCrud); + + ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); + ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); + when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); + when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) + .thenReturn(namespace); + + Topics topics = mock(Topics.class); + when(namespace.topics()).thenReturn(topics); + Topic topic = mock(Topic.class); + when(topics.getByName(getKey().getT2())).thenReturn(topic); + ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); + when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); + + Topic.DefinitionStages.Blank define = mock(Topic.DefinitionStages.Blank.class); + when(topics.define(TOPIC_NAME)).thenReturn(define); + when(define.create()).thenReturn(topic); + when(namespaceCrud.get(NAMESPACE)).thenReturn(namespace); + when(namespaceCrud.getOrCreate(NAMESPACE)).thenReturn(namespace); + + Topic actualGetTopic = topicCrud.get(getKey()); + Topic actualCreateTopic = topicCrud.create(getKey()); + Assertions.assertNotNull(actualGetTopic); + Assertions.assertNotNull(actualCreateTopic); + + ServiceBusSubscription actualGetSub = topicSubCrud.get(getKey()); + Assertions.assertNull(actualGetSub); + } + } From 3c0b39123b69c0a95c2e636d2ab1e8ca26508460 Mon Sep 17 00:00:00 2001 From: hui Date: Fri, 2 Sep 2022 16:24:10 +0800 Subject: [PATCH 10/11] add tests --- .../ServiceBusTopicSubscriptionCrudTests.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index 6b3e40a12b69e..29d79b3a1bc28 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -11,6 +11,7 @@ import com.azure.resourcemanager.servicebus.models.Topic; import com.azure.resourcemanager.servicebus.models.Topics; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import reactor.util.function.Tuple3; import reactor.util.function.Tuples; @@ -24,29 +25,28 @@ class ServiceBusTopicSubscriptionCrudTests extends AbstractResourceCrudTests> getResourceCrud() { - return new ServiceBusTopicSubscriptionCrud(resourceManager, resourceMetadata); + return new ServiceBusTopicSubscriptionCrud(resourceManager, resourceMetadata, this.topicCrud); } @Override void getStubManagementException(int statusCode, String message) { - ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); - ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); - - when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); ManagementException exception = getManagementException(statusCode, message); - when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) - .thenReturn(namespace); - - Topics topics = mock(Topics.class); Topic topic = mock(Topic.class); ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); - when(namespace.topics()).thenReturn(topics); - when(topics.getByName(getKey().getT2())).thenReturn(topic); + + when(this.topicCrud.get(Tuples.of(getKey().getT1(),getKey().getT2()))).thenReturn(topic); when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); - when(serviceBusSubscriptions.getByName(getKey().getT2())).thenThrow(exception); + when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); } @Override @@ -78,7 +78,6 @@ Tuple3 getKey() { @Test void topicDoesNotExistReturnNull() { - ServiceBusTopicCrud topicCrud = mock(ServiceBusTopicCrud.class); ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, this.resourceMetadata, topicCrud); @@ -90,7 +89,6 @@ void topicDoesNotExistReturnNull() { @Test void topicDoesNotExistReturnNullToCreate() { - ServiceBusTopicCrud topicCrud = mock(ServiceBusTopicCrud.class); ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, this.resourceMetadata, topicCrud); From 229180bef269d213f7cb2af98b35d185e12800cc Mon Sep 17 00:00:00 2001 From: Xiaolu Dai Date: Fri, 2 Sep 2022 16:54:15 +0800 Subject: [PATCH 11/11] refactor test cases --- .../crud/ServiceBusTopicSubscriptionCrud.java | 2 +- .../crud/AbstractResourceCrudTests.java | 2 +- .../crud/EventHubNamespaceCrudTests.java | 4 +- .../crud/EventHubsCrudTests.java | 4 +- .../crud/ServiceBusNamespaceCrudTests.java | 4 +- .../ServiceBusTopicSubscriptionCrudTests.java | 108 +++++++----------- 6 files changed, 51 insertions(+), 73 deletions(-) diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java index 8f2a04303c392..9fb00602b6897 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/main/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrud.java @@ -48,7 +48,7 @@ public ServiceBusSubscription internalGet(Tuple3 subscri .get(Tuples.of(subscriptionCoordinate.getT1(), subscriptionCoordinate.getT2())); return topic == null ? null : topic .subscriptions() - .getByName(subscriptionCoordinate.getT2()); + .getByName(subscriptionCoordinate.getT3()); } catch (ManagementException e) { if (e.getResponse().getStatusCode() == RESOURCE_NOT_FOUND) { return null; diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/AbstractResourceCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/AbstractResourceCrudTests.java index e793c87bad846..85323cf225041 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/AbstractResourceCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/AbstractResourceCrudTests.java @@ -26,7 +26,7 @@ abstract class AbstractResourceCrudTests { abstract void createStubManagementException(); abstract K getKey(); - ManagementException getManagementException(int statusCode, String message) { + ManagementException createManagementException(int statusCode, String message) { HttpResponse response = mock(HttpResponse.class); when(response.getStatusCode()).thenReturn(statusCode); return new ManagementException(message, response); diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubNamespaceCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubNamespaceCrudTests.java index c4c0d365a97ef..c3b78eb440da0 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubNamespaceCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubNamespaceCrudTests.java @@ -23,7 +23,7 @@ AbstractResourceCrud getResourceCrud() { void getStubManagementException(int statusCode, String message) { EventHubNamespaces namespaces = mock(EventHubNamespaces.class); when(resourceManager.eventHubNamespaces()).thenReturn(namespaces); - ManagementException exception = getManagementException(statusCode, message); + ManagementException exception = createManagementException(statusCode, message); when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey())) .thenThrow(exception); } @@ -32,7 +32,7 @@ void getStubManagementException(int statusCode, String message) { void createStubManagementException() { EventHubNamespaces namespaces = mock(EventHubNamespaces.class); when(resourceManager.eventHubNamespaces()).thenReturn(namespaces); - ManagementException exception = getManagementException(500, "Create event hubs namespace exception"); + ManagementException exception = createManagementException(500, "Create event hubs namespace exception"); EventHubNamespace.DefinitionStages.Blank define = mock(EventHubNamespace.DefinitionStages.Blank.class); when(namespaces.define(NAMESPACE)).thenReturn(define); diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubsCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubsCrudTests.java index 9ab116ccb9da1..ed7d2083af57c 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubsCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/EventHubsCrudTests.java @@ -30,7 +30,7 @@ void getStubManagementException(int statusCode, String message) { EventHubNamespace eventHubNamespace = mock(EventHubNamespace.class); when(resourceManager.eventHubNamespaces()).thenReturn(namespaces); - ManagementException exception = getManagementException(statusCode, message); + ManagementException exception = createManagementException(statusCode, message); when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) .thenReturn(eventHubNamespace); @@ -44,7 +44,7 @@ void getStubManagementException(int statusCode, String message) { void createStubManagementException() { EventHubNamespaces namespaces = mock(EventHubNamespaces.class); EventHubNamespace eventHubNamespace = mock(EventHubNamespace.class); - ManagementException exception = getManagementException(500, "Create event hubs namespace exception"); + ManagementException exception = createManagementException(500, "Create event hubs namespace exception"); when(resourceManager.eventHubNamespaces()).thenReturn(namespaces); when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) .thenReturn(eventHubNamespace); diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusNamespaceCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusNamespaceCrudTests.java index ef019c0ba7cb5..d030e9ec6a500 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusNamespaceCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusNamespaceCrudTests.java @@ -23,7 +23,7 @@ AbstractResourceCrud getResourceCrud() { void getStubManagementException(int statusCode, String message) { ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); - ManagementException exception = getManagementException(statusCode, message); + ManagementException exception = createManagementException(statusCode, message); when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey())) .thenThrow(exception); } @@ -32,7 +32,7 @@ void getStubManagementException(int statusCode, String message) { void createStubManagementException() { ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); - ManagementException exception = getManagementException(500, "Create service bus namespace exception"); + ManagementException exception = createManagementException(500, "Create service bus namespace exception"); ServiceBusNamespace.DefinitionStages.Blank define = mock(ServiceBusNamespace.DefinitionStages.Blank.class); when(namespaces.define(NAMESPACE)).thenReturn(define); diff --git a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java index 29d79b3a1bc28..e9aebede4fe4d 100644 --- a/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java +++ b/sdk/spring/spring-cloud-azure-resourcemanager/src/test/java/com/azure/spring/cloud/resourcemanager/implementation/crud/ServiceBusTopicSubscriptionCrudTests.java @@ -4,15 +4,13 @@ package com.azure.spring.cloud.resourcemanager.implementation.crud; import com.azure.core.management.exception.ManagementException; -import com.azure.resourcemanager.servicebus.models.ServiceBusNamespace; -import com.azure.resourcemanager.servicebus.models.ServiceBusNamespaces; import com.azure.resourcemanager.servicebus.models.ServiceBusSubscription; import com.azure.resourcemanager.servicebus.models.ServiceBusSubscriptions; import com.azure.resourcemanager.servicebus.models.Topic; -import com.azure.resourcemanager.servicebus.models.Topics; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import reactor.util.function.Tuple2; import reactor.util.function.Tuple3; import reactor.util.function.Tuples; @@ -38,59 +36,50 @@ AbstractResourceCrud> get return new ServiceBusTopicSubscriptionCrud(resourceManager, resourceMetadata, this.topicCrud); } + @Override + Tuple3 getKey() { + return Tuples.of(NAMESPACE, TOPIC_NAME, SUBSCRIPTION_NAME); + } + @Override void getStubManagementException(int statusCode, String message) { - ManagementException exception = getManagementException(statusCode, message); + ManagementException exception = createManagementException(statusCode, message); Topic topic = mock(Topic.class); ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); - when(this.topicCrud.get(Tuples.of(getKey().getT1(),getKey().getT2()))).thenReturn(topic); + Tuple3 subscriptionKey = getKey(); + + when(this.topicCrud.get(Tuples.of(subscriptionKey.getT1(), subscriptionKey.getT2()))).thenReturn(topic); when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); - when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); + when(serviceBusSubscriptions.getByName(subscriptionKey.getT3())).thenThrow(exception); } @Override void createStubManagementException() { - ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); - ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); - when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); - ManagementException exception = getManagementException(500, "Create service bus namespace exception"); - when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) - .thenReturn(namespace); - - Topics topics = mock(Topics.class); + ManagementException exception = createManagementException(500, "Create service bus namespace exception"); Topic topic = mock(Topic.class); ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); - when(namespace.topics()).thenReturn(topics); - when(topics.getByName(getKey().getT2())).thenReturn(topic); - when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); - when(serviceBusSubscriptions.getByName(getKey().getT3())).thenThrow(exception); - ServiceBusSubscription.DefinitionStages.Blank define = mock(ServiceBusSubscription.DefinitionStages.Blank.class); + + Tuple3 subscriptionKey = getKey(); + + when(this.topicCrud.getOrCreate(Tuples.of(subscriptionKey.getT1(), subscriptionKey.getT2()))).thenReturn(topic); + when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); when(define.create()).thenThrow(exception); } - @Override - Tuple3 getKey() { - return Tuples.of(NAMESPACE, TOPIC_NAME, SUBSCRIPTION_NAME); - } - @Test void topicDoesNotExistReturnNull() { - ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, - this.resourceMetadata, topicCrud); - when(topicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(null); - - ServiceBusSubscription actualGet = topicSubCrud.get(getKey()); + ServiceBusSubscription actualGet = getResourceCrud().get(getKey()); Assertions.assertNull(actualGet); } @Test - void topicDoesNotExistReturnNullToCreate() { - ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, - this.resourceMetadata, topicCrud); + void topicDoesNotExistsShouldReturnNullTopicAndCreateSub() { + Tuple3 subscriptionKey = getKey(); + Tuple2 topicKey = Tuples.of(subscriptionKey.getT1(), subscriptionKey.getT2()); Topic topic = mock(Topic.class); ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); @@ -98,53 +87,42 @@ void topicDoesNotExistReturnNullToCreate() { ServiceBusSubscription.DefinitionStages.Blank define = mock(ServiceBusSubscription.DefinitionStages.Blank.class); - when(serviceBusSubscriptions.define(SUBSCRIPTION_NAME)).thenReturn(define); + when(serviceBusSubscriptions.define(subscriptionKey.getT3())).thenReturn(define); ServiceBusSubscription serviceBusSubscription = mock(ServiceBusSubscription.class); when(define.create()).thenReturn(serviceBusSubscription); - when(topicCrud.get(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(null); - when(topicCrud.getOrCreate(Tuples.of(NAMESPACE, TOPIC_NAME))).thenReturn(topic); + when(topicCrud.get(topicKey)).thenReturn(null); + when(topicCrud.getOrCreate(topicKey)).thenReturn(topic); - ServiceBusSubscription actualGet = topicSubCrud.get(getKey()); - ServiceBusSubscription actualCreate = topicSubCrud.create(getKey()); + ServiceBusSubscription actualGet = getResourceCrud().get(subscriptionKey); + ServiceBusSubscription actualCreate = getResourceCrud().create(subscriptionKey); Assertions.assertNull(actualGet); Assertions.assertNotNull(actualCreate); Assertions.assertEquals(serviceBusSubscription, actualCreate); } @Test - void topicExistSubscriptionDoesNotExist() { - ServiceBusNamespaceCrud namespaceCrud = mock(ServiceBusNamespaceCrud.class); - ServiceBusTopicCrud topicCrud = new ServiceBusTopicCrud(this.resourceManager, - this.resourceMetadata); - ServiceBusTopicSubscriptionCrud topicSubCrud = new ServiceBusTopicSubscriptionCrud(this.resourceManager, - this.resourceMetadata, topicCrud); - - ServiceBusNamespaces namespaces = mock(ServiceBusNamespaces.class); - ServiceBusNamespace namespace = mock(ServiceBusNamespace.class); - when(resourceManager.serviceBusNamespaces()).thenReturn(namespaces); - when(namespaces.getByResourceGroup(resourceMetadata.getResourceGroup(), getKey().getT1())) - .thenReturn(namespace); - - Topics topics = mock(Topics.class); - when(namespace.topics()).thenReturn(topics); + void topicExistsSubscriptionDoesNotExistShouldReturnNonNullTopicAndCreateSub() { + Tuple3 subscriptionKey = getKey(); + Tuple2 topicKey = Tuples.of(subscriptionKey.getT1(), subscriptionKey.getT2()); + Topic topic = mock(Topic.class); - when(topics.getByName(getKey().getT2())).thenReturn(topic); ServiceBusSubscriptions serviceBusSubscriptions = mock(ServiceBusSubscriptions.class); - when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); + ServiceBusSubscription subscription = mock(ServiceBusSubscription.class); + ServiceBusSubscription.DefinitionStages.Blank define = + mock(ServiceBusSubscription.DefinitionStages.Blank.class); - Topic.DefinitionStages.Blank define = mock(Topic.DefinitionStages.Blank.class); - when(topics.define(TOPIC_NAME)).thenReturn(define); - when(define.create()).thenReturn(topic); - when(namespaceCrud.get(NAMESPACE)).thenReturn(namespace); - when(namespaceCrud.getOrCreate(NAMESPACE)).thenReturn(namespace); + when(this.topicCrud.getOrCreate(topicKey)).thenReturn(topic); + when(topic.subscriptions()).thenReturn(serviceBusSubscriptions); + when(serviceBusSubscriptions.getByName(subscriptionKey.getT3())).thenReturn(null); + when(serviceBusSubscriptions.define(subscriptionKey.getT3())).thenReturn(define); + when(define.create()).thenReturn(subscription); - Topic actualGetTopic = topicCrud.get(getKey()); - Topic actualCreateTopic = topicCrud.create(getKey()); - Assertions.assertNotNull(actualGetTopic); - Assertions.assertNotNull(actualCreateTopic); + ServiceBusSubscription actualGet = getResourceCrud().get(subscriptionKey); + ServiceBusSubscription actualCreate = getResourceCrud().create(subscriptionKey); - ServiceBusSubscription actualGetSub = topicSubCrud.get(getKey()); - Assertions.assertNull(actualGetSub); + Assertions.assertNull(actualGet); + Assertions.assertNotNull(actualCreate); + Assertions.assertEquals(subscription, actualCreate); } }