update bom and upgrade media service authentication (#435)
* update bom and upgrade media service authentication with AAD
This commit is contained in:
Родитель
ae3eddecb4
Коммит
b188b31ebe
|
@ -56,7 +56,7 @@
|
|||
<azure.applicationinsights.version>2.1.2</azure.applicationinsights.version>
|
||||
<spring.data.gremlin.version>2.0.0</spring.data.gremlin.version>
|
||||
<microsoft.client-runtime.version>1.3.1</microsoft.client-runtime.version>
|
||||
<azure.dependencies.bom.version>1.0.0.M3</azure.dependencies.bom.version>
|
||||
<azure.dependencies.bom.version>1.0.0.M4</azure.dependencies.bom.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
|
@ -9,7 +9,7 @@ If you are using Maven, add the following dependency.
|
|||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-mediaservices-spring-boot-starter</artifactId>
|
||||
<version>0.2.3</version>
|
||||
<version>2.0.5</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
@ -18,8 +18,10 @@ If you are using Maven, add the following dependency.
|
|||
Open `application.properties` file and add below properties with your Azure Media Services credentials.
|
||||
|
||||
```
|
||||
azure.mediaservices.account-name=put-your-media-services-account-name-here
|
||||
azure.mediaservices.account-key=put-your-media-services-account-key-here
|
||||
azure.mediaservices.tenant=put-your-media-service-azure-ad-tenant-domain-here
|
||||
azure.mediaservices.client-id=put-your-azure-ad-client-id-here
|
||||
azure.mediaservices.client-secret=put-your-azure-ad-client-secret-here
|
||||
azure.mediaservices.rest-api-endpoint=put-your-media-service-rest-api-endpoint-here
|
||||
```
|
||||
|
||||
# Optional
|
||||
|
|
|
@ -12,6 +12,10 @@ import com.microsoft.windowsazure.exception.ServiceException;
|
|||
import com.microsoft.windowsazure.services.media.MediaConfiguration;
|
||||
import com.microsoft.windowsazure.services.media.MediaContract;
|
||||
import com.microsoft.windowsazure.services.media.MediaService;
|
||||
import com.microsoft.windowsazure.services.media.authentication.AzureAdClientSymmetricKey;
|
||||
import com.microsoft.windowsazure.services.media.authentication.AzureAdTokenCredentials;
|
||||
import com.microsoft.windowsazure.services.media.authentication.AzureAdTokenProvider;
|
||||
import com.microsoft.windowsazure.services.media.authentication.AzureEnvironments;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
|
@ -20,62 +24,67 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static com.microsoft.windowsazure.Configuration.*;
|
||||
import static com.microsoft.windowsazure.Configuration.PROPERTY_HTTP_PROXY_HOST;
|
||||
import static com.microsoft.windowsazure.Configuration.PROPERTY_HTTP_PROXY_PORT;
|
||||
import static com.microsoft.windowsazure.Configuration.PROPERTY_HTTP_PROXY_SCHEME;
|
||||
import static java.util.Objects.isNull;
|
||||
import static java.util.Objects.nonNull;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(MediaContract.class)
|
||||
@EnableConfigurationProperties(MediaServicesProperties.class)
|
||||
@ConditionalOnProperty(prefix = "azure.mediaservices", value = {"account-name", "account-key"})
|
||||
@ConditionalOnProperty(prefix = "azure.mediaservices",
|
||||
value = {"tenant", "client-id", "client-secret", "rest-api-endpoint"})
|
||||
public class MediaServicesAutoConfiguration {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MediaServicesAutoConfiguration.class);
|
||||
|
||||
private final MediaServicesProperties mediaServicesProperties;
|
||||
private final MediaServicesProperties properties;
|
||||
private final TelemetryProxy telemetryProxy;
|
||||
|
||||
public MediaServicesAutoConfiguration(MediaServicesProperties mediaServicesProperties) {
|
||||
this.mediaServicesProperties = mediaServicesProperties;
|
||||
this.properties = mediaServicesProperties;
|
||||
this.telemetryProxy = new TelemetryProxy(mediaServicesProperties.isAllowTelemetry());
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare MediaContract bean.
|
||||
*
|
||||
* @return MediaContract bean
|
||||
* @throws ServiceException
|
||||
*/
|
||||
@Bean
|
||||
public MediaContract mediaContract() throws ServiceException {
|
||||
public MediaContract mediaContract() throws ServiceException, MalformedURLException, URISyntaxException {
|
||||
LOG.debug("mediaContract called");
|
||||
trackCustomEvent();
|
||||
return createMediaContract();
|
||||
}
|
||||
|
||||
private MediaContract createMediaContract() throws ServiceException {
|
||||
private MediaContract createMediaContract() throws ServiceException, MalformedURLException, URISyntaxException {
|
||||
LOG.debug("createMediaContract called");
|
||||
final com.microsoft.windowsazure.Configuration configuration = MediaConfiguration
|
||||
.configureWithOAuthAuthentication(
|
||||
MediaServicesProperties.MEDIA_SERVICE_URI,
|
||||
MediaServicesProperties.OAUTH_URI,
|
||||
mediaServicesProperties.getAccountName(),
|
||||
mediaServicesProperties.getAccountKey(),
|
||||
MediaServicesProperties.SCOPE);
|
||||
|
||||
if (nonNull(mediaServicesProperties.getProxyHost())
|
||||
&& nonNull(mediaServicesProperties.getProxyPort())) {
|
||||
configuration.getProperties().put(PROPERTY_HTTP_PROXY_HOST, mediaServicesProperties.getProxyHost());
|
||||
configuration.getProperties().put(PROPERTY_HTTP_PROXY_PORT, mediaServicesProperties.getProxyPort());
|
||||
configuration.getProperties().put(PROPERTY_HTTP_PROXY_SCHEME, mediaServicesProperties.getProxyScheme());
|
||||
} else if (nonNull(mediaServicesProperties.getProxyHost()) && isNull(mediaServicesProperties.getProxyPort())) {
|
||||
throw new ServiceException("Please Set Network Proxy port in application.properties");
|
||||
} else if (nonNull(mediaServicesProperties.getProxyPort()) && isNull(mediaServicesProperties.getProxyHost())) {
|
||||
throw new ServiceException("Please Set Network Proxy host in application.properties");
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
final AzureAdTokenCredentials credentials = new AzureAdTokenCredentials(properties.getTenant(),
|
||||
new AzureAdClientSymmetricKey(properties.getClientId(), properties.getClientSecret()),
|
||||
AzureEnvironments.AZURE_CLOUD_ENVIRONMENT);
|
||||
|
||||
final AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(credentials, executorService);
|
||||
|
||||
final com.microsoft.windowsazure.Configuration configuration = MediaConfiguration
|
||||
.configureWithAzureAdTokenProvider(new URI(properties.getRestApiEndpoint()), tokenProvider);
|
||||
|
||||
if (!StringUtils.isEmpty(properties.getProxyHost()) && nonNull(properties.getProxyPort())) {
|
||||
configuration.getProperties().put(PROPERTY_HTTP_PROXY_HOST, properties.getProxyHost());
|
||||
configuration.getProperties().put(PROPERTY_HTTP_PROXY_PORT, properties.getProxyPort());
|
||||
configuration.getProperties().put(PROPERTY_HTTP_PROXY_SCHEME, properties.getProxyScheme());
|
||||
} else if (!StringUtils.isEmpty(properties.getProxyHost()) && isNull(properties.getProxyPort())) {
|
||||
throw new ServiceException("Please configure azure.mediaservices.proxy-port");
|
||||
} else if (nonNull(properties.getProxyPort()) && StringUtils.isEmpty(properties.getProxyHost())) {
|
||||
throw new ServiceException("Please configure azure.mediaservices.proxy-host");
|
||||
}
|
||||
|
||||
return MediaService.create(configuration);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,138 +8,68 @@ package com.microsoft.azure.spring.autoconfigure.mediaservices;
|
|||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Validated
|
||||
@ConfigurationProperties("azure.mediaservices")
|
||||
public class MediaServicesProperties {
|
||||
@NotEmpty(message = "azure.mediaservices.tenant property must be configured.")
|
||||
@Getter
|
||||
@Setter
|
||||
private String tenant;
|
||||
|
||||
/**
|
||||
* Media service configuration URI.
|
||||
* Media service Azure Active Directory client-id(application id).
|
||||
*/
|
||||
public static final String MEDIA_SERVICE_URI = "https://media.windows.net/API/";
|
||||
@NotEmpty(message = "azure.mediaservices.client-id property must be configured.")
|
||||
@Getter
|
||||
@Setter
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* Media service OAuth configuration URI.
|
||||
* Media service Azure Active Directory client secret.
|
||||
*/
|
||||
public static final String OAUTH_URI = "https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13";
|
||||
@NotEmpty(message = "azure.mediaservices.client-secret property must be configured.")
|
||||
@Getter
|
||||
@Setter
|
||||
private String clientSecret;
|
||||
|
||||
/**
|
||||
* Media service scope sent to OAuth.
|
||||
* Media service REST API endpoint.
|
||||
*/
|
||||
public static final String SCOPE = "urn:WindowsAzureMediaServices";
|
||||
/**
|
||||
* Media service account name.
|
||||
*/
|
||||
@NotEmpty(message = "Please Set azure.mediaservices.account-name in application.properties")
|
||||
private String accountName;
|
||||
/**
|
||||
* Media service account key.
|
||||
*/
|
||||
@NotEmpty(message = "Please Set azure.mediaservices.account-key in application.properties")
|
||||
private String accountKey;
|
||||
@NotEmpty(message = "azure.mediaservices.rest-api-endpoint property must be configured.")
|
||||
@Getter
|
||||
@Setter
|
||||
private String restApiEndpoint;
|
||||
|
||||
/**
|
||||
* Proxy host if to use proxy.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private String proxyHost;
|
||||
|
||||
/**
|
||||
* Proxy port if to use proxy.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private Integer proxyPort;
|
||||
|
||||
/**
|
||||
* Proxy scheme if to use proxy. Default is http.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private String proxyScheme = "http";
|
||||
|
||||
/**
|
||||
* Weather allow telemetry collecting.
|
||||
* Whether allow telemetry collecting.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean allowTelemetry = true;
|
||||
|
||||
/**
|
||||
* return allow telemery or not
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isAllowTelemetry() {
|
||||
return allowTelemetry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set allowTelemetry
|
||||
*
|
||||
* @param allowTelemetry
|
||||
*/
|
||||
public void setAllowTelemetry(boolean allowTelemetry) {
|
||||
this.allowTelemetry = allowTelemetry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the accountName
|
||||
*/
|
||||
public String getAccountName() {
|
||||
return accountName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param accountName the accountName to set
|
||||
*/
|
||||
public void setAccountName(String accountName) {
|
||||
this.accountName = accountName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the accountKey
|
||||
*/
|
||||
public String getAccountKey() {
|
||||
return accountKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param accountKey the accountKey to set
|
||||
*/
|
||||
public void setAccountKey(String accountKey) {
|
||||
this.accountKey = accountKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the proxyHost
|
||||
*/
|
||||
public String getProxyHost() {
|
||||
return proxyHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param proxyHost the proxyHost to set
|
||||
*/
|
||||
public void setProxyHost(String proxyHost) {
|
||||
this.proxyHost = proxyHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the proxyPort
|
||||
*/
|
||||
public Integer getProxyPort() {
|
||||
return proxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param proxyPort the proxyPort to set
|
||||
*/
|
||||
public void setProxyPort(Integer proxyPort) {
|
||||
this.proxyPort = proxyPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the proxyScheme
|
||||
*/
|
||||
public String getProxyScheme() {
|
||||
return proxyScheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param proxyScheme the proxyScheme to set
|
||||
*/
|
||||
public void setProxyScheme(String proxyScheme) {
|
||||
this.proxyScheme = proxyScheme;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,14 +7,16 @@
|
|||
package com.microsoft.azure.spring.autoconfigure.mediaservices;
|
||||
|
||||
public class Constants {
|
||||
public static final String ACCOUNT_NAME = "some accountname";
|
||||
public static final String ACCOUNT_NAME_PROPERTY = "azure.mediaservices.account-name";
|
||||
public static final String ACCOUNT_KEY = "some accountKey";
|
||||
public static final String ACCOUNT_KEY_PROPERTY = "azure.mediaservices.account-key";
|
||||
public static final String TENANT_PROP = "azure.mediaservices.tenant";
|
||||
public static final String TENANT = "fake.microsoft.media.service.tenant";
|
||||
public static final String CLIENT_ID_PROP = "azure.mediaservices.client-id";
|
||||
public static final String CLIENT_ID = "fake-application-id-123456";
|
||||
public static final String CLIENT_SECRET_PROP = "azure.mediaservices.client-secret";
|
||||
public static final String CLIENT_SECRET = "fake-client-secret";
|
||||
public static final String REST_API_ENDPOINT_PROP = "azure.mediaservices.rest-api-endpoint";
|
||||
public static final String REST_API_ENDPOINT = "https://fake.media.service.media.azure.net/api/";
|
||||
public static final String PROXY_HOST = "localhost";
|
||||
public static final String PROXY_HOST_PROPERTY = "azure.mediaservices.proxy-host";
|
||||
public static final String PROXY_HOST_PROP = "azure.mediaservices.proxy-host";
|
||||
public static final String PROXY_PORT = "8080";
|
||||
public static final String PROXY_PORT_PROPERTY = "azure.mediaservices.proxy-port";
|
||||
public static final String PROXY_SCHEME = "https";
|
||||
public static final String PROXY_SCHEME_PROPERTY = "azure.mediaservices.proxy-scheme";
|
||||
public static final String PROXY_PORT_PROP = "azure.mediaservices.proxy-port";
|
||||
}
|
||||
|
|
|
@ -6,94 +6,57 @@
|
|||
|
||||
package com.microsoft.azure.spring.autoconfigure.mediaservices;
|
||||
|
||||
import com.microsoft.windowsazure.exception.ServiceException;
|
||||
import com.microsoft.windowsazure.services.media.MediaContract;
|
||||
import com.microsoft.windowsazure.services.media.implementation.MediaExceptionProcessor;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static com.microsoft.azure.spring.autoconfigure.mediaservices.Constants.*;
|
||||
import static com.microsoft.azure.utils.TestUtils.propPair;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MediaServicesAutoConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(MediaServicesAutoConfiguration.class))
|
||||
.withPropertyValues(propPair(TENANT_PROP, TENANT),
|
||||
propPair(CLIENT_ID_PROP, CLIENT_ID),
|
||||
propPair(CLIENT_SECRET_PROP, CLIENT_SECRET),
|
||||
propPair(REST_API_ENDPOINT_PROP, REST_API_ENDPOINT));
|
||||
|
||||
@Test
|
||||
public void createMediaServiceAccount() {
|
||||
System.setProperty(Constants.ACCOUNT_KEY_PROPERTY, Constants.ACCOUNT_KEY);
|
||||
System.setProperty(Constants.ACCOUNT_NAME_PROPERTY, Constants.ACCOUNT_NAME);
|
||||
|
||||
createAndVerifyMediaContract();
|
||||
|
||||
System.clearProperty(Constants.ACCOUNT_KEY_PROPERTY);
|
||||
System.clearProperty(Constants.ACCOUNT_NAME_PROPERTY);
|
||||
public void mediaContractBeanCanBeCreated() {
|
||||
contextRunner.run(context -> assertThat(context).hasSingleBean(MediaContract.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMediaServiceAccountWithProxy() {
|
||||
System.setProperty(Constants.ACCOUNT_KEY_PROPERTY, Constants.ACCOUNT_KEY);
|
||||
System.setProperty(Constants.ACCOUNT_NAME_PROPERTY, Constants.ACCOUNT_NAME);
|
||||
System.setProperty(Constants.PROXY_HOST_PROPERTY, Constants.PROXY_HOST);
|
||||
System.setProperty(Constants.PROXY_PORT_PROPERTY, Constants.PROXY_PORT);
|
||||
System.setProperty(Constants.PROXY_SCHEME_PROPERTY, Constants.PROXY_SCHEME);
|
||||
|
||||
createAndVerifyMediaContract();
|
||||
|
||||
System.clearProperty(Constants.ACCOUNT_KEY_PROPERTY);
|
||||
System.clearProperty(Constants.ACCOUNT_NAME_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_HOST_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_PORT_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_SCHEME_PROPERTY);
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void byDefaultMediaContractBeanNotCreated() {
|
||||
new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(MediaServicesAutoConfiguration.class))
|
||||
.withPropertyValues().run(context -> context.getBean(MediaContract.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMediaServiceAccountWithProxyHostMissing() {
|
||||
System.setProperty(Constants.ACCOUNT_KEY_PROPERTY, Constants.ACCOUNT_KEY);
|
||||
System.setProperty(Constants.ACCOUNT_NAME_PROPERTY, Constants.ACCOUNT_NAME);
|
||||
System.setProperty(Constants.PROXY_PORT_PROPERTY, Constants.PROXY_PORT);
|
||||
|
||||
createAndFailWithCause("Please Set Network Proxy host in application.properties");
|
||||
|
||||
System.clearProperty(Constants.ACCOUNT_KEY_PROPERTY);
|
||||
System.clearProperty(Constants.ACCOUNT_NAME_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_PORT_PROPERTY);
|
||||
contextRunner.withPropertyValues(propPair(PROXY_PORT_PROP, PROXY_PORT)).run(context -> {
|
||||
try {
|
||||
context.getBean(MediaContract.class);
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(ExceptionUtils.indexOfThrowable(e, ServiceException.class)).isGreaterThan(-1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMediaServiceAccountWithProxyPortMissing() {
|
||||
System.setProperty(Constants.ACCOUNT_KEY_PROPERTY, Constants.ACCOUNT_KEY);
|
||||
System.setProperty(Constants.ACCOUNT_NAME_PROPERTY, Constants.ACCOUNT_NAME);
|
||||
System.setProperty(Constants.PROXY_HOST_PROPERTY, Constants.PROXY_HOST);
|
||||
|
||||
createAndFailWithCause("Please Set Network Proxy port in application.properties");
|
||||
|
||||
System.clearProperty(Constants.ACCOUNT_KEY_PROPERTY);
|
||||
System.clearProperty(Constants.ACCOUNT_NAME_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_HOST_PROPERTY);
|
||||
}
|
||||
|
||||
private void createAndVerifyMediaContract() {
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
||||
context.register(MediaServicesAutoConfiguration.class);
|
||||
context.refresh();
|
||||
|
||||
final MediaContract mediaContract = context.getBean(MediaContract.class);
|
||||
assertThat(mediaContract).isNotNull();
|
||||
assertThat(mediaContract).isExactlyInstanceOf(MediaExceptionProcessor.class);
|
||||
}
|
||||
}
|
||||
|
||||
private void createAndFailWithCause(String cause) {
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
||||
context.register(MediaServicesAutoConfiguration.class);
|
||||
|
||||
Exception exception = null;
|
||||
contextRunner.withPropertyValues(propPair(PROXY_HOST_PROP, PROXY_HOST)).run(context -> {
|
||||
try {
|
||||
context.refresh();
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
context.getBean(MediaContract.class);
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(ExceptionUtils.indexOfThrowable(e, ServiceException.class)).isGreaterThan(-1);
|
||||
}
|
||||
|
||||
assertThat(exception).isNotNull();
|
||||
assertThat(exception).isExactlyInstanceOf(BeanCreationException.class);
|
||||
assertThat(exception.getCause().getCause().toString()).contains(cause);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,87 +7,86 @@
|
|||
package com.microsoft.azure.spring.autoconfigure.mediaservices;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBindException;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.ObjectError;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static com.microsoft.azure.spring.autoconfigure.mediaservices.Constants.*;
|
||||
import static com.microsoft.azure.spring.autoconfigure.mediaservices.Constants.REST_API_ENDPOINT;
|
||||
import static com.microsoft.azure.utils.TestUtils.propPair;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MediaServicesPropertiesTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(MediaServicesAutoConfiguration.class))
|
||||
.withPropertyValues(propPair(TENANT_PROP, TENANT),
|
||||
propPair(CLIENT_ID_PROP, CLIENT_ID),
|
||||
propPair(CLIENT_SECRET_PROP, CLIENT_SECRET),
|
||||
propPair(REST_API_ENDPOINT_PROP, REST_API_ENDPOINT));
|
||||
|
||||
@Test
|
||||
public void canSetProperties() {
|
||||
System.setProperty(Constants.ACCOUNT_NAME_PROPERTY, Constants.ACCOUNT_NAME);
|
||||
System.setProperty(Constants.ACCOUNT_KEY_PROPERTY, Constants.ACCOUNT_KEY);
|
||||
System.setProperty(Constants.PROXY_HOST_PROPERTY, Constants.PROXY_HOST);
|
||||
System.setProperty(Constants.PROXY_PORT_PROPERTY, Constants.PROXY_PORT);
|
||||
System.setProperty(Constants.PROXY_SCHEME_PROPERTY, Constants.PROXY_SCHEME);
|
||||
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
|
||||
contextRunner.run(context -> {
|
||||
final MediaServicesProperties properties = context.getBean(MediaServicesProperties.class);
|
||||
assertThat(properties.getAccountName()).isEqualTo(Constants.ACCOUNT_NAME);
|
||||
assertThat(properties.getAccountKey()).isEqualTo(Constants.ACCOUNT_KEY);
|
||||
assertThat(properties.getProxyHost()).isEqualTo(Constants.PROXY_HOST);
|
||||
assertThat(properties.getProxyPort()).isEqualTo(Integer.valueOf(Constants.PROXY_PORT));
|
||||
assertThat(properties.getProxyScheme()).isEqualTo(Constants.PROXY_SCHEME);
|
||||
}
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(properties.getTenant()).isEqualTo(TENANT);
|
||||
assertThat(properties.getClientId()).isEqualTo(CLIENT_ID);
|
||||
assertThat(properties.getClientSecret()).isEqualTo(CLIENT_SECRET);
|
||||
assertThat(properties.getRestApiEndpoint()).isEqualTo(REST_API_ENDPOINT);
|
||||
});
|
||||
}
|
||||
|
||||
System.clearProperty(Constants.ACCOUNT_NAME_PROPERTY);
|
||||
System.clearProperty(Constants.ACCOUNT_KEY_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_HOST_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_PORT_PROPERTY);
|
||||
System.clearProperty(Constants.PROXY_SCHEME_PROPERTY);
|
||||
|
||||
@Test
|
||||
public void emptyTenantNotAllowed() {
|
||||
contextRunner.withPropertyValues(propPair(TENANT_PROP, ""), propPair(CLIENT_ID_PROP, CLIENT_ID),
|
||||
propPair(CLIENT_SECRET_PROP, CLIENT_SECRET), propPair(REST_API_ENDPOINT_PROP, REST_API_ENDPOINT))
|
||||
.run(context -> {
|
||||
try {
|
||||
context.getBean(MediaServicesProperties.class);
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getCause().getCause()).isInstanceOf(ConfigurationPropertiesBindException.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptySettingNotAllowed() {
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
||||
Exception exception = null;
|
||||
|
||||
context.register(Config.class);
|
||||
|
||||
try {
|
||||
context.refresh();
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
|
||||
assertThat(exception).isNotNull();
|
||||
assertThat(exception).isExactlyInstanceOf(ConfigurationPropertiesBindException.class);
|
||||
|
||||
final BindValidationException bindException = (BindValidationException) exception.getCause().getCause();
|
||||
final List<ObjectError> errors = bindException.getValidationErrors().getAllErrors();
|
||||
final List<String> errorStrings = errors.stream().map(e -> e.toString()).collect(Collectors.toList());
|
||||
|
||||
Collections.sort(errorStrings);
|
||||
|
||||
final List<String> errorStringsExpected = Arrays.asList(
|
||||
"Field error in object 'azure.mediaservices' on field 'accountKey': rejected value [null];",
|
||||
"Field error in object 'azure.mediaservices' on field 'accountName': rejected value [null];"
|
||||
);
|
||||
|
||||
assertThat(errorStrings.size()).isEqualTo(errorStringsExpected.size());
|
||||
|
||||
for (int i = 0; i < errorStrings.size(); i++) {
|
||||
assertThat(errorStrings.get(i)).contains(errorStringsExpected.get(i));
|
||||
}
|
||||
}
|
||||
public void emptyClientIdtNotAllowed() {
|
||||
contextRunner.withPropertyValues(propPair(TENANT_PROP, TENANT), propPair(CLIENT_ID_PROP, ""),
|
||||
propPair(CLIENT_SECRET_PROP, CLIENT_SECRET), propPair(REST_API_ENDPOINT_PROP, REST_API_ENDPOINT))
|
||||
.run(context -> {
|
||||
try {
|
||||
context.getBean(MediaServicesProperties.class);
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getCause().getCause()).isInstanceOf(ConfigurationPropertiesBindException.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MediaServicesProperties.class)
|
||||
static class Config {
|
||||
@Test
|
||||
public void emptyClientSecretNotAllowed() {
|
||||
contextRunner.withPropertyValues(propPair(TENANT_PROP, ""), propPair(CLIENT_ID_PROP, CLIENT_ID),
|
||||
propPair(CLIENT_SECRET_PROP, ""), propPair(REST_API_ENDPOINT_PROP, REST_API_ENDPOINT))
|
||||
.run(context -> {
|
||||
try {
|
||||
context.getBean(MediaServicesProperties.class);
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getCause().getCause()).isInstanceOf(ConfigurationPropertiesBindException.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyRestApiEndpointNotAllowed() {
|
||||
contextRunner.withPropertyValues(propPair(TENANT_PROP, ""), propPair(CLIENT_ID_PROP, CLIENT_ID),
|
||||
propPair(CLIENT_SECRET_PROP, CLIENT_SECRET), propPair(REST_API_ENDPOINT_PROP, ""))
|
||||
.run(context -> {
|
||||
try {
|
||||
context.getBean(MediaServicesProperties.class);
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getCause().getCause()).isInstanceOf(ConfigurationPropertiesBindException.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* 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.utils;
|
||||
|
||||
public class TestUtils {
|
||||
private TestUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param propName property name
|
||||
* @param propValue value of property
|
||||
* @return property name and value pair. e.g., prop.name=prop.value
|
||||
*/
|
||||
public static String propPair(String propName, String propValue) {
|
||||
return propName + "=" + propValue;
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче