PageRenderTime 939ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v2_0/suppliers/RegionIdToURIFromAccessForTypeAndVersionTest.java

http://github.com/jclouds/jclouds
Java | 118 lines | 85 code | 17 blank | 16 comment | 0 complexity | 693eca01062fd5bf7f295946beb38c16 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.v2_0.suppliers;
  18. import static org.testng.Assert.assertEquals;
  19. import java.net.URI;
  20. import java.util.NoSuchElementException;
  21. import javax.inject.Singleton;
  22. import org.jclouds.location.Provider;
  23. import org.jclouds.location.suppliers.RegionIdToURISupplier;
  24. import org.jclouds.openstack.keystone.v2_0.domain.Access;
  25. import org.jclouds.openstack.keystone.v2_0.parse.ParseAccessTest;
  26. import org.jclouds.openstack.keystone.v2_0.parse.ParseRackspaceAccessTest;
  27. import org.testng.annotations.Test;
  28. import com.google.common.base.Supplier;
  29. import com.google.common.base.Suppliers;
  30. import com.google.common.collect.ImmutableMap;
  31. import com.google.common.collect.Maps;
  32. import com.google.inject.AbstractModule;
  33. import com.google.inject.Guice;
  34. import com.google.inject.Provides;
  35. import com.google.inject.TypeLiteral;
  36. import com.google.inject.assistedinject.FactoryModuleBuilder;
  37. @Test(groups = "unit", testName = "RegionIdToURIFromAccessForTypeAndVersionTest")
  38. public class RegionIdToURIFromAccessForTypeAndVersionTest {
  39. private final RegionIdToURISupplier.Factory factory = Guice.createInjector(new AbstractModule() {
  40. @Override
  41. protected void configure() {
  42. bindConstant().annotatedWith(Provider.class).to("openstack-keystone");
  43. bind(new TypeLiteral<Supplier<URI>>() {
  44. }).annotatedWith(Provider.class).toInstance(Suppliers.ofInstance(URI.create("https://identity")));
  45. install(new FactoryModuleBuilder().implement(RegionIdToURISupplier.class,
  46. RegionIdToURIFromAccessForTypeAndVersion.class).build(
  47. RegionIdToURISupplier.Factory.class));
  48. }
  49. @Provides
  50. @Singleton
  51. public Supplier<Access> provide() {
  52. return Suppliers.ofInstance(new ParseAccessTest().expected());
  53. }
  54. }).getInstance(RegionIdToURISupplier.Factory.class);
  55. @SuppressWarnings("CheckReturnValue")
  56. @Test(expectedExceptions = NoSuchElementException.class)
  57. public void testRegionUnmatches() {
  58. Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.0").get(),
  59. Suppliers.<URI> supplierFunction());
  60. }
  61. public void testRegionMatches() {
  62. assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("compute", "2").get(), Suppliers
  63. .<URI> supplierFunction()), ImmutableMap.of("az-1.region-a.geo-1", URI
  64. .create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/3456"), "az-2.region-a.geo-1", URI
  65. .create("https://az-2.region-a.geo-1.compute.hpcloudsvc.com/v2/3456"), "az-3.region-a.geo-1", URI
  66. .create("https://az-3.region-a.geo-1.compute.hpcloudsvc.com/v2/3456")));
  67. }
  68. private final RegionIdToURISupplier.Factory raxFactory = Guice.createInjector(new AbstractModule() {
  69. @Override
  70. protected void configure() {
  71. bindConstant().annotatedWith(Provider.class).to("rackspace");
  72. bind(new TypeLiteral<Supplier<URI>>() {
  73. }).annotatedWith(Provider.class).toInstance(Suppliers.ofInstance(URI.create("https://identity")));
  74. install(new FactoryModuleBuilder().implement(RegionIdToURISupplier.class,
  75. RegionIdToURIFromAccessForTypeAndVersion.class).build(
  76. RegionIdToURISupplier.Factory.class));
  77. }
  78. @Provides
  79. @Singleton
  80. public Supplier<Access> provide() {
  81. return Suppliers.ofInstance(new ParseRackspaceAccessTest().expected());
  82. }
  83. }).getInstance(RegionIdToURISupplier.Factory.class);
  84. @Test(expectedExceptions = NoSuchElementException.class)
  85. public void testWhenNotInList() {
  86. assertEquals(Maps.transformValues(raxFactory.createForApiTypeAndVersion("goo", "1.0").get(), Suppliers
  87. .<URI> supplierFunction()), ImmutableMap.of("rackspace", URI
  88. .create("https://servers.api.rackspacecloud.com/v1.0/40806637803162")));
  89. }
  90. public void testProviderWhenNoRegions() {
  91. assertEquals(Maps.transformValues(raxFactory.createForApiTypeAndVersion("compute", "1.0").get(), Suppliers
  92. .<URI> supplierFunction()), ImmutableMap.of("rackspace", URI
  93. .create("https://servers.api.rackspacecloud.com/v1.0/40806637803162")));
  94. }
  95. public void testOkWithNoVersions() {
  96. assertEquals(Maps.transformValues(raxFactory.createForApiTypeAndVersion("rax:database", null).get(), Suppliers
  97. .<URI> supplierFunction()), ImmutableMap.of("DFW", URI
  98. .create("https://dfw.databases.api.rackspacecloud.com/v1.0/40806637803162"), "ORD", URI
  99. .create("https://ord.databases.api.rackspacecloud.com/v1.0/40806637803162")));
  100. }
  101. }