PageRenderTime 936ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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