Add unit test of Spring cloud auto config
This commit is contained in:
Родитель
3e800af53f
Коммит
eb651fb88b
|
@ -6,12 +6,10 @@
|
|||
|
||||
package com.microsoft.azure.spring.cloud.autoconfigure.cache;
|
||||
|
||||
import com.microsoft.azure.management.Azure;
|
||||
import com.microsoft.azure.management.redis.RedisAccessKeys;
|
||||
import com.microsoft.azure.management.redis.RedisCache;
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.context.AzureContextAutoConfiguration;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import com.microsoft.azure.spring.cloud.context.core.CredentialsProvider;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
|
@ -55,16 +53,6 @@ public class AzureRedisAutoConfigurationTest {
|
|||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
public CredentialsProvider credentialsProvider() {
|
||||
return mock(CredentialsProvider.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
Azure azure() {
|
||||
return mock(Azure.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AzureAdmin azureAdmin() {
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ import com.microsoft.azure.management.Azure;
|
|||
import com.microsoft.azure.management.resources.Subscription;
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.telemetry.TelemetryTracker;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import com.microsoft.azure.spring.cloud.context.core.CredentialsProvider;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
@ -22,64 +21,53 @@ import static org.mockito.Mockito.mock;
|
|||
import static org.powermock.api.mockito.PowerMockito.when;
|
||||
|
||||
public class AzureContextAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(AzureContextAutoConfiguration.class))
|
||||
.withUserConfiguration(TestConfiguration.class);
|
||||
private ApplicationContextRunner contextRunner =
|
||||
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(AzureContextAutoConfiguration.class))
|
||||
.withUserConfiguration(TestConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void testAzurePropertiesConfigured() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.withPropertyValues("spring.cloud.azure.region=westUS")
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureProperties.class);
|
||||
assertThat(context.getBean(AzureProperties.class).getCredentialFilePath()).isEqualTo("credential");
|
||||
assertThat(context.getBean(AzureProperties.class).getResourceGroup()).isEqualTo("group1");
|
||||
assertThat(context.getBean(AzureProperties.class).getRegion()).isEqualTo("westUS");
|
||||
});
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.withPropertyValues("spring.cloud.azure.region=westUS").run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureProperties.class);
|
||||
assertThat(context.getBean(AzureProperties.class).getCredentialFilePath()).isEqualTo("credential");
|
||||
assertThat(context.getBean(AzureProperties.class).getResourceGroup()).isEqualTo("group1");
|
||||
assertThat(context.getBean(AzureProperties.class).getRegion()).isEqualTo("westUS");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzurePropertiesTelemetryMissing() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.run(context -> assertThat(context.getBean(TelemetryTracker.class)).isNotNull());
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.run(context -> assertThat(context.getBean(TelemetryTracker.class)).isNotNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzurePropertiesTelemetryConfigured() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.withPropertyValues("spring.cloud.azure.telemetryAllowed=true")
|
||||
.run(context -> assertThat(context.getBean(TelemetryTracker.class)).isNotNull());
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.withPropertyValues("spring.cloud.azure.telemetryAllowed=true")
|
||||
.run(context -> assertThat(context.getBean(TelemetryTracker.class)).isNotNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzurePropertiesTelemetryConfiguredException() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.withPropertyValues("spring.cloud.azure.telemetryAllowed=false")
|
||||
.run(context -> assertThat(context).doesNotHaveBean(TelemetryTracker.class));
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.credentialFilePath=credential")
|
||||
.withPropertyValues("spring.cloud.azure.resourceGroup=group1")
|
||||
.withPropertyValues("spring.cloud.azure.telemetryAllowed=false")
|
||||
.run(context -> assertThat(context).doesNotHaveBean(TelemetryTracker.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithoutAzureProperties() {
|
||||
this.contextRunner
|
||||
.run(context -> assertThat(context).doesNotHaveBean(AzureProperties.class));
|
||||
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AzureProperties.class));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
public CredentialsProvider credentialsProvider() {
|
||||
return mock(CredentialsProvider.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
Azure azure() {
|
||||
final Azure azure = mock(Azure.class);
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.cloud.autoconfigure.eventhub;
|
||||
|
||||
import com.microsoft.azure.Page;
|
||||
import com.microsoft.azure.PagedList;
|
||||
import com.microsoft.azure.management.eventhub.EventHubAuthorizationKey;
|
||||
import com.microsoft.azure.management.eventhub.EventHubNamespace;
|
||||
import com.microsoft.azure.management.eventhub.EventHubNamespaceAuthorizationRule;
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.context.AzureContextAutoConfiguration;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import com.microsoft.rest.RestException;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class AzureEventHubAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
|
||||
AutoConfigurations.of(AzureContextAutoConfiguration.class, AzureEventHubAutoConfiguration.class))
|
||||
.withUserConfiguration(
|
||||
TestConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void testWithoutAzureEventHubProperties() {
|
||||
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AzureEventHubProperties.class));
|
||||
}
|
||||
|
||||
@Ignore("org.apache.kafka.common.serialization.StringSerializer required on classpath")
|
||||
@Test
|
||||
public void testAzureEventHubPropertiesConfigured() {
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.eventhub.namespace=ns1").run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureEventHubProperties.class);
|
||||
assertThat(context.getBean(AzureEventHubProperties.class).getNamespace()).isEqualTo("ns1");
|
||||
assertThat(context).hasSingleBean(KafkaProperties.class);
|
||||
assertThat(context.getBean(KafkaProperties.class).getBootstrapServers().get(0)).isEqualTo("localhost:9093");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
AzureAdmin azureAdmin() {
|
||||
|
||||
AzureAdmin azureAdmin = mock(AzureAdmin.class);
|
||||
EventHubNamespace namespace = mock(EventHubNamespace.class);
|
||||
EventHubAuthorizationKey key = mock(EventHubAuthorizationKey.class);
|
||||
when(key.primaryConnectionString()).thenReturn("connectionString1");
|
||||
EventHubNamespaceAuthorizationRule rule = mock(EventHubNamespaceAuthorizationRule.class);
|
||||
when(rule.getKeys()).thenReturn(key);
|
||||
PagedList<EventHubNamespaceAuthorizationRule> rules = new PagedList<EventHubNamespaceAuthorizationRule>() {
|
||||
@Override
|
||||
public Page<EventHubNamespaceAuthorizationRule> nextPage(String nextPageLink)
|
||||
throws RestException, IOException {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
rules.add(rule);
|
||||
when(namespace.listAuthorizationRules()).thenReturn(rules);
|
||||
when(namespace.serviceBusEndpoint()).thenReturn("localhost");
|
||||
return azureAdmin;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.cloud.autoconfigure.servicebus;
|
||||
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.context.AzureContextAutoConfiguration;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import com.microsoft.azure.spring.integration.servicebus.factory.ServiceBusQueueClientFactory;
|
||||
import com.microsoft.azure.spring.integration.servicebus.queue.ServiceBusQueueOperation;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class AzureServiceBusQueueAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
|
||||
AutoConfigurations.of(AzureContextAutoConfiguration.class, AzureServiceBusQueueAutoConfiguration.class))
|
||||
.withUserConfiguration(
|
||||
TestConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void testWithoutAzureServiceBusProperties() {
|
||||
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AzureServiceBusProperties.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzureServiceBusPropertiesConfigured() {
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.servicebus.namespace=ns1").run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureServiceBusProperties.class);
|
||||
assertThat(context.getBean(AzureServiceBusProperties.class).getNamespace()).isEqualTo("ns1");
|
||||
assertThat(context).hasSingleBean(ServiceBusQueueClientFactory.class);
|
||||
assertThat(context).hasSingleBean(ServiceBusQueueOperation.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
AzureAdmin azureAdmin() {
|
||||
return mock(AzureAdmin.class);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.cloud.autoconfigure.servicebus;
|
||||
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.context.AzureContextAutoConfiguration;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import com.microsoft.azure.spring.integration.servicebus.factory.ServiceBusTopicClientFactory;
|
||||
import com.microsoft.azure.spring.integration.servicebus.topic.ServiceBusTopicOperation;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class AzureServiceBusTopicAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
|
||||
AutoConfigurations.of(AzureContextAutoConfiguration.class, AzureServiceBusTopicAutoConfiguration.class))
|
||||
.withUserConfiguration(
|
||||
TestConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void testWithoutAzureServiceBusProperties() {
|
||||
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AzureServiceBusProperties.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzureServiceBusPropertiesConfigured() {
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.servicebus.namespace=ns1").run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureServiceBusProperties.class);
|
||||
assertThat(context.getBean(AzureServiceBusProperties.class).getNamespace()).isEqualTo("ns1");
|
||||
assertThat(context).hasSingleBean(ServiceBusTopicClientFactory.class);
|
||||
assertThat(context).hasSingleBean(ServiceBusTopicOperation.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
AzureAdmin azureAdmin() {
|
||||
return mock(AzureAdmin.class);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.cloud.autoconfigure.sql;
|
||||
|
||||
import com.microsoft.azure.management.sql.SqlDatabase;
|
||||
import com.microsoft.azure.management.sql.SqlServer;
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.context.AzureContextAutoConfiguration;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class AzureSqlAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
|
||||
AutoConfigurations.of(AzureContextAutoConfiguration.class, AzureSqlAutoConfiguration.class))
|
||||
.withUserConfiguration(
|
||||
TestConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void testWithoutAzureSqlProperties() {
|
||||
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AzureSqlProperties.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzureSqlPropertiesConfigured() {
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.sql.databaseName=db1").
|
||||
withPropertyValues("spring.cloud.azure.sql.enabled=true").
|
||||
withPropertyValues("spring.cloud.azure.sql.serverName=server1").
|
||||
withPropertyValues("spring.datasource.password=ps1").run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureSqlProperties.class);
|
||||
assertThat(context.getBean(AzureSqlProperties.class).getDatabaseName()).isEqualTo("db1");
|
||||
assertThat(context.getBean(AzureSqlProperties.class).getServerName()).isEqualTo("server1");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoJdbc() {
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.sql.enabled=true")
|
||||
.withClassLoader(new FilteredClassLoader(EmbeddedDatabaseType.class, DataSource.class))
|
||||
.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(DataSource.class);
|
||||
assertThat(context).doesNotHaveBean(DataSourceProperties.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
AzureAdmin azureAdmin() {
|
||||
AzureAdmin azureAdmin = mock(AzureAdmin.class);
|
||||
SqlDatabase sqlDatabase = mock(SqlDatabase.class);
|
||||
SqlServer sqlServer = mock(SqlServer.class);
|
||||
when(sqlServer.administratorLogin()).thenReturn("user");
|
||||
when(azureAdmin.getSqlServer("server1")).thenReturn(sqlServer);
|
||||
when(azureAdmin.getOrCreateSqlServer(isA(String.class), isA(String.class), isA(String.class)))
|
||||
.thenReturn(sqlServer);
|
||||
when(azureAdmin.getOrCreateSqlDatabase("server1", "db1")).thenReturn(sqlDatabase);
|
||||
when(sqlServer.fullyQualifiedDomainName()).thenReturn("");
|
||||
|
||||
return azureAdmin;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.spring.cloud.autoconfigure.storage;
|
||||
|
||||
import com.microsoft.azure.spring.cloud.autoconfigure.context.AzureContextAutoConfiguration;
|
||||
import com.microsoft.azure.spring.cloud.context.core.AzureAdmin;
|
||||
import com.microsoft.azure.storage.CloudStorageAccount;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class AzureStorageAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
|
||||
AutoConfigurations.of(AzureContextAutoConfiguration.class, AzureStorageAutoConfiguration.class))
|
||||
.withUserConfiguration(
|
||||
TestConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void testWithoutAzureStorageProperties() {
|
||||
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AzureStorageProperties.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAzureStoragePropertiesConfigured() {
|
||||
this.contextRunner.withPropertyValues("spring.cloud.azure.storage.account=acc1").run(context -> {
|
||||
assertThat(context).hasSingleBean(AzureStorageProperties.class);
|
||||
assertThat(context.getBean(AzureStorageProperties.class).getAccount()).isEqualTo("acc1");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
AzureAdmin azureAdmin() {
|
||||
return mock(AzureAdmin.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CloudStorageAccount cloudStorageAccount() {
|
||||
return mock(CloudStorageAccount.class);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче