PageRenderTime 689ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/auth/suppliers/RegionIdToAdminURIFromAccessForTypeAndVersionTest.java

http://github.com/jclouds/jclouds
Java | 125 lines | 92 code | 15 blank | 18 comment | 0 complexity | 70c8f845c89e79e5dfa9c396e5417429 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.openstack.keystone.auth.suppliers;
  18. import static org.testng.Assert.assertEquals;
  19. import java.net.URI;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.NoSuchElementException;
  23. import javax.inject.Singleton;
  24. import org.jclouds.location.Provider;
  25. import org.jclouds.openstack.keystone.auth.domain.AuthInfo;
  26. import org.jclouds.openstack.keystone.catalog.ServiceEndpoint;
  27. import org.jclouds.openstack.keystone.catalog.suppliers.RegionIdToAdminURIFromServiceEndpointsForTypeAndVersion;
  28. import org.jclouds.openstack.keystone.catalog.suppliers.RegionIdToAdminURISupplier;
  29. import org.jclouds.openstack.keystone.v2_0.catalog.V2ServiceCatalog;
  30. import org.jclouds.openstack.keystone.v2_0.parse.ParseAccessTest;
  31. import org.jclouds.openstack.keystone.v2_0.parse.ParseRackspaceAccessTest;
  32. import org.testng.annotations.Test;
  33. import com.google.common.base.Supplier;
  34. import com.google.common.base.Suppliers;
  35. import com.google.common.collect.ImmutableMap;
  36. import com.google.common.collect.Maps;
  37. import com.google.inject.AbstractModule;
  38. import com.google.inject.Guice;
  39. import com.google.inject.Provides;
  40. import com.google.inject.Scopes;
  41. import com.google.inject.TypeLiteral;
  42. import com.google.inject.assistedinject.FactoryModuleBuilder;
  43. @Test(groups = "unit", testName = "RegionIdToAdminURIFromAccessForTypeAndVersionTest")
  44. public class RegionIdToAdminURIFromAccessForTypeAndVersionTest {
  45. private final RegionIdToAdminURISupplier.Factory factory = Guice.createInjector(new AbstractModule() {
  46. @Override
  47. protected void configure() {
  48. bindConstant().annotatedWith(Provider.class).to("openstack-keystone");
  49. bind(new TypeLiteral<Supplier<URI>>() {
  50. }).annotatedWith(Provider.class).toInstance(Suppliers.ofInstance(URI.create("https://identity")));
  51. install(new FactoryModuleBuilder().implement(RegionIdToAdminURISupplier.class,
  52. RegionIdToAdminURIFromServiceEndpointsForTypeAndVersion.class).build(RegionIdToAdminURISupplier.Factory.class));
  53. // We test against a 2.0 service catalog but it is OK for the purpose of this test
  54. bind(new TypeLiteral<Supplier<List<ServiceEndpoint>>>() {
  55. }).to(V2ServiceCatalog.class).in(Scopes.SINGLETON);
  56. }
  57. @Provides
  58. @Singleton
  59. public Supplier<AuthInfo> provide() {
  60. return Suppliers.<AuthInfo> ofInstance(new ParseAccessTest().expected());
  61. }
  62. }).getInstance(RegionIdToAdminURISupplier.Factory.class);
  63. public void testRegionMatches() {
  64. assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("identity", "2.0").get(), Suppliers
  65. .<URI> supplierFunction()), ImmutableMap.of("region-a.geo-1", URI.create("https://csnode.jclouds.org:35357/v2.0/")));
  66. Map<String, URI> map = Maps.newLinkedHashMap();
  67. map.put("region-a.geo-1", null);
  68. map.put("region-b.geo-1", null);
  69. map.put("region-c.geo-1", null);
  70. assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("compute", "2").get(), Suppliers
  71. .<URI> supplierFunction()), map);
  72. }
  73. private final RegionIdToAdminURISupplier.Factory raxFactory = Guice.createInjector(new AbstractModule() {
  74. @Override
  75. protected void configure() {
  76. bindConstant().annotatedWith(Provider.class).to("rackspace");
  77. bind(new TypeLiteral<Supplier<URI>>() {
  78. }).annotatedWith(Provider.class).toInstance(Suppliers.ofInstance(URI.create("https://identity")));
  79. install(new FactoryModuleBuilder().implement(RegionIdToAdminURISupplier.class,
  80. RegionIdToAdminURIFromServiceEndpointsForTypeAndVersion.class).build(RegionIdToAdminURISupplier.Factory.class));
  81. // We test against a 2.0 service catalog but it is OK for the purpose of this test
  82. bind(new TypeLiteral<Supplier<List<ServiceEndpoint>>>() {
  83. }).to(V2ServiceCatalog.class).in(Scopes.SINGLETON);
  84. }
  85. @Provides
  86. @Singleton
  87. public Supplier<AuthInfo> provide() {
  88. return Suppliers.<AuthInfo> ofInstance(new ParseRackspaceAccessTest().expected());
  89. }
  90. }).getInstance(RegionIdToAdminURISupplier.Factory.class);
  91. @Test(expectedExceptions = NoSuchElementException.class)
  92. public void testWhenNotInList() {
  93. assertEquals(Maps.transformValues(raxFactory.createForApiTypeAndVersion("goo", "1.0").get(), Suppliers
  94. .<URI> supplierFunction()), ImmutableMap.of("rackspace", URI
  95. .create("https://servers.api.rackspacecloud.com/v1.0/40806637803162")));
  96. }
  97. public void testProviderWhenNoRegions() {
  98. Map<String, URI> map = Maps.newLinkedHashMap();
  99. map.put("rackspace", null);
  100. assertEquals(Maps.transformValues(raxFactory.createForApiTypeAndVersion("compute", "1.0").get(), Suppliers
  101. .<URI> supplierFunction()), map);
  102. }
  103. public void testOkWithNoVersions() {
  104. Map<String, URI> map = Maps.newLinkedHashMap();
  105. map.put("DFW", null);
  106. map.put("ORD", null);
  107. assertEquals(Maps.transformValues(raxFactory.createForApiTypeAndVersion("rax:database", null).get(), Suppliers
  108. .<URI> supplierFunction()), map);
  109. }
  110. }