PageRenderTime 761ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudRestClientModuleTest.java

https://github.com/jraghu/jclouds
Java | 159 lines | 114 code | 24 blank | 21 comment | 1 complexity | 72542ada7a3ea1f00b8587dddb56bb7c MD5 | raw file
  1. /**
  2. *
  3. * Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
  4. *
  5. * ====================================================================
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.jclouds.vcloud.config;
  20. import static com.google.common.base.Preconditions.checkNotNull;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.testng.Assert.assertEquals;
  23. import java.net.URI;
  24. import java.util.Map;
  25. import java.util.concurrent.ExecutionException;
  26. import java.util.concurrent.Executor;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.TimeoutException;
  29. import java.util.concurrent.atomic.AtomicInteger;
  30. import org.jclouds.http.HttpRetryHandler;
  31. import org.jclouds.http.TransformingHttpCommandExecutorService;
  32. import org.jclouds.http.handlers.DelegatingErrorHandler;
  33. import org.jclouds.http.handlers.DelegatingRetryHandler;
  34. import org.jclouds.http.handlers.RedirectionRetryHandler;
  35. import org.jclouds.rest.config.RestModule;
  36. import org.jclouds.vcloud.VCloudPropertiesBuilder;
  37. import org.jclouds.vcloud.domain.NamedResource;
  38. import org.jclouds.vcloud.handlers.ParseVCloudErrorFromHttpResponse;
  39. import org.jclouds.vcloud.internal.VCloudLoginAsyncClient;
  40. import org.jclouds.vcloud.internal.VCloudLoginAsyncClient.VCloudSession;
  41. import org.testng.annotations.Test;
  42. import com.google.common.base.Supplier;
  43. import com.google.common.util.concurrent.ListenableFuture;
  44. import com.google.inject.AbstractModule;
  45. import com.google.inject.Guice;
  46. import com.google.inject.Injector;
  47. import com.google.inject.name.Names;
  48. /**
  49. * @author Adrian Cole
  50. */
  51. @Test(groups = "unit", testName = "vcloud.VCloudRestClientModuleTest")
  52. public class VCloudRestClientModuleTest {
  53. protected Injector createInjector() {
  54. return Guice.createInjector(new VCloudRestClientModule(), new RestModule(),
  55. new AbstractModule() {
  56. @Override
  57. protected void configure() {
  58. Names.bindProperties(binder(), checkNotNull(new VCloudPropertiesBuilder(URI
  59. .create("http://localhost"), "user", "pass").build(), "properties"));
  60. bind(TransformingHttpCommandExecutorService.class).toInstance(
  61. createMock(TransformingHttpCommandExecutorService.class));
  62. }
  63. });
  64. }
  65. @Test
  66. void testUpdatesOnlyOncePerSecond() throws NoSuchMethodException, InterruptedException {
  67. VCloudRestClientModule module = new VCloudRestClientModule();
  68. VCloudLoginAsyncClient login = new VCloudLoginAsyncClient() {
  69. private final AtomicInteger token = new AtomicInteger();
  70. public ListenableFuture<VCloudSession> login() {
  71. return new ListenableFuture<VCloudSession>() {
  72. @Override
  73. public VCloudSession get() throws InterruptedException, ExecutionException {
  74. return new VCloudSession() {
  75. public Map<String, NamedResource> getOrgs() {
  76. return null;
  77. }
  78. public String getVCloudToken() {
  79. return token.incrementAndGet() + "";
  80. }
  81. };
  82. }
  83. @Override
  84. public boolean cancel(boolean mayInterruptIfRunning) {
  85. return false;
  86. }
  87. @Override
  88. public VCloudSession get(long timeout, TimeUnit unit) throws InterruptedException,
  89. ExecutionException, TimeoutException {
  90. return get();
  91. }
  92. @Override
  93. public boolean isCancelled() {
  94. return false;
  95. }
  96. @Override
  97. public boolean isDone() {
  98. return false;
  99. }
  100. @Override
  101. public void addListener(Runnable listener, Executor exec) {
  102. }
  103. };
  104. }
  105. };
  106. Supplier<VCloudSession> map = module.provideVCloudTokenCache(1, login);
  107. for (int i = 0; i < 10; i++)
  108. map.get();
  109. assert "1".equals(map.get().getVCloudToken());
  110. Thread.sleep(1001);
  111. assert "2".equals(map.get().getVCloudToken());
  112. }
  113. @Test
  114. void testServerErrorHandler() {
  115. DelegatingErrorHandler handler = createInjector().getInstance(DelegatingErrorHandler.class);
  116. assertEquals(handler.getServerErrorHandler().getClass(),
  117. ParseVCloudErrorFromHttpResponse.class);
  118. }
  119. @Test
  120. void testClientErrorHandler() {
  121. DelegatingErrorHandler handler = createInjector().getInstance(DelegatingErrorHandler.class);
  122. assertEquals(handler.getClientErrorHandler().getClass(),
  123. ParseVCloudErrorFromHttpResponse.class);
  124. }
  125. @Test
  126. void testClientRetryHandler() {
  127. DelegatingRetryHandler handler = createInjector().getInstance(DelegatingRetryHandler.class);
  128. assertEquals(handler.getClientErrorRetryHandler(), HttpRetryHandler.NEVER_RETRY);
  129. }
  130. @Test
  131. void testRedirectionRetryHandler() {
  132. DelegatingRetryHandler handler = createInjector().getInstance(DelegatingRetryHandler.class);
  133. assertEquals(handler.getRedirectionRetryHandler().getClass(), RedirectionRetryHandler.class);
  134. }
  135. }