/core/src/test/java/org/jclouds/providers/config/BindProviderMetadataContextAndCredentialsTest.java

https://github.com/andreisavu/jclouds · Java · 163 lines · 119 code · 22 blank · 22 comment · 0 complexity · f057f1dae168dedf8ca2cc083d359012 MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.providers.config;
  20. import static org.testng.Assert.assertEquals;
  21. import java.util.Properties;
  22. import java.util.Set;
  23. import javax.inject.Inject;
  24. import org.jclouds.Constants;
  25. import org.jclouds.Context;
  26. import org.jclouds.apis.ApiMetadata;
  27. import org.jclouds.domain.Credentials;
  28. import org.jclouds.domain.LoginCredentials;
  29. import org.jclouds.http.IntegrationTestAsyncClient;
  30. import org.jclouds.http.IntegrationTestClient;
  31. import org.jclouds.internal.FilterStringsBoundToInjectorByName;
  32. import org.jclouds.javax.annotation.Nullable;
  33. import org.jclouds.location.Iso3166;
  34. import org.jclouds.location.Provider;
  35. import org.jclouds.providers.AnonymousProviderMetadata;
  36. import org.jclouds.providers.ProviderMetadata;
  37. import org.jclouds.rest.annotations.Api;
  38. import org.jclouds.rest.annotations.ApiVersion;
  39. import org.jclouds.rest.annotations.BuildVersion;
  40. import org.jclouds.rest.annotations.Credential;
  41. import org.jclouds.rest.annotations.Identity;
  42. import org.testng.annotations.Test;
  43. import com.google.common.base.Predicates;
  44. import com.google.common.reflect.TypeToken;
  45. import com.google.inject.Guice;
  46. import com.google.inject.Key;
  47. import com.google.inject.name.Names;
  48. /**
  49. *
  50. * @author Adrian Cole
  51. */
  52. @Test(groups = "unit", testName = "BindProviderMetadataContextAndCredentialsTest")
  53. public class BindProviderMetadataContextAndCredentialsTest {
  54. @SuppressWarnings("unused")
  55. private static class ExpectedBindings {
  56. private final javax.inject.Provider<Context> backend;
  57. private final ProviderMetadata providerMetadata;
  58. private final Credentials creds;
  59. private final String identity;
  60. private final String credential;
  61. private final String providerId;
  62. private final Set<String> iso3166Codes;
  63. private final String apiId;
  64. private final String apiVersion;
  65. private final String buildVersion;
  66. @Inject
  67. private ExpectedBindings(@Provider javax.inject.Provider<Context> backend, ProviderMetadata providerMetadata,
  68. @Provider Credentials creds, @Identity String identity, @Nullable @Credential String credential,
  69. @Provider String providerId, @Iso3166 Set<String> iso3166Codes, @Api String apiId,
  70. @ApiVersion String apiVersion, @Nullable @BuildVersion String buildVersion,
  71. @Provider TypeToken<? extends Context> backendToken, FilterStringsBoundToInjectorByName filter) {
  72. this.backend = backend;
  73. assertEquals(backendToken, providerMetadata.getApiMetadata().getContext());
  74. this.providerMetadata = providerMetadata;
  75. Properties props = new Properties();
  76. props.putAll(filter.apply(Predicates.<String> alwaysTrue()));
  77. Properties expected = new Properties();
  78. expected.putAll(providerMetadata.getApiMetadata().getDefaultProperties());
  79. expected.putAll(providerMetadata.getDefaultProperties());
  80. assertEquals(props, expected);
  81. this.creds = creds;
  82. this.identity = identity;
  83. assertEquals(identity, creds.identity);
  84. this.credential = credential;
  85. assertEquals(credential, creds.credential);
  86. this.providerId = providerId;
  87. assertEquals(providerId, providerMetadata.getId());
  88. this.iso3166Codes = iso3166Codes;
  89. assertEquals(iso3166Codes, providerMetadata.getIso3166Codes());
  90. this.apiId = apiId;
  91. assertEquals(apiId, providerMetadata.getApiMetadata().getId());
  92. this.apiVersion = apiVersion;
  93. assertEquals(apiVersion, providerMetadata.getApiMetadata().getVersion());
  94. this.buildVersion = buildVersion;
  95. assertEquals(buildVersion, providerMetadata.getApiMetadata().getBuildVersion().orNull());
  96. }
  97. }
  98. @Test
  99. public void testExpectedBindingsWhenCredentialIsNotNull() {
  100. ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
  101. IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
  102. Credentials creds = LoginCredentials.builder().user("user").password("password").build();
  103. ExpectedBindings bindings = Guice.createInjector(new BindProviderMetadataContextAndCredentials(md, creds))
  104. .getInstance(ExpectedBindings.class);
  105. assertEquals(bindings.identity, "user");
  106. assertEquals(bindings.credential, "password");
  107. }
  108. @Test
  109. public void testExpectedBindingsWhenCredentialIsNull() {
  110. ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
  111. IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
  112. Credentials creds = LoginCredentials.builder().user("user").build();
  113. ExpectedBindings bindings = Guice.createInjector(new BindProviderMetadataContextAndCredentials(md, creds))
  114. .getInstance(ExpectedBindings.class);
  115. assertEquals(bindings.identity, "user");
  116. assertEquals(bindings.credential, null);
  117. }
  118. @Test
  119. public void testExpectedBindingsWhenBuildVersionAbsent() {
  120. ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
  121. IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
  122. ApiMetadata apiMd = md.getApiMetadata().toBuilder().buildVersion(null).build();
  123. md = md.toBuilder().apiMetadata(apiMd).build();
  124. Credentials creds = LoginCredentials.builder().user("user").build();
  125. ExpectedBindings bindings = Guice.createInjector(new BindProviderMetadataContextAndCredentials(md, creds))
  126. .getInstance(ExpectedBindings.class);
  127. assertEquals(bindings.buildVersion, null);
  128. }
  129. @Test
  130. public void testProviderOverridesApiMetadataProperty() {
  131. ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
  132. IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
  133. Properties defaultProps = md.getDefaultProperties();
  134. defaultProps.setProperty(Constants.PROPERTY_SESSION_INTERVAL, Integer.MAX_VALUE + "");
  135. md = md.toBuilder().defaultProperties(defaultProps).build();
  136. Credentials creds = LoginCredentials.builder().user("user").build();
  137. int session = Guice.createInjector(new BindProviderMetadataContextAndCredentials(md, creds)).getInstance(
  138. Key.get(int.class, Names.named(Constants.PROPERTY_SESSION_INTERVAL)));
  139. assertEquals(session, Integer.MAX_VALUE);
  140. }
  141. }