/apis/deltacloud/src/test/java/org/jclouds/deltacloud/handlers/DeltacloudRedirectionRetryHandlerTest.java

https://github.com/regularfry/jclouds · Java · 106 lines · 60 code · 23 blank · 23 comment · 0 complexity · 7af5ba9089532ae47f3194931e53b114 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.deltacloud.handlers;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.easymock.classextension.EasyMock.replay;
  23. import static org.easymock.classextension.EasyMock.verify;
  24. import java.net.URI;
  25. import javax.inject.Named;
  26. import javax.inject.Singleton;
  27. import org.jclouds.http.HttpCommand;
  28. import org.jclouds.http.HttpRequest;
  29. import org.jclouds.http.HttpResponse;
  30. import org.jclouds.rest.BaseRestClientTest.MockModule;
  31. import org.jclouds.rest.config.RestModule;
  32. import org.testng.annotations.Test;
  33. import com.google.common.collect.LinkedHashMultimap;
  34. import com.google.common.collect.Multimap;
  35. import com.google.inject.AbstractModule;
  36. import com.google.inject.Guice;
  37. import com.google.inject.Injector;
  38. import com.google.inject.Provides;
  39. /**
  40. * Tests behavior of {@code DeltacloudRedirectionRetry}
  41. *
  42. * @author Adrian Cole
  43. */
  44. @Test(groups = "unit")
  45. public class DeltacloudRedirectionRetryHandlerTest {
  46. Injector injector = Guice.createInjector(new MockModule(), new RestModule(), new AbstractModule() {
  47. @SuppressWarnings("unused")
  48. @Provides
  49. @Singleton
  50. @Named("CONSTANTS")
  51. protected Multimap<String, String> constants() {
  52. return LinkedHashMultimap.create();
  53. }
  54. @Override
  55. protected void configure() {
  56. }
  57. });
  58. @Test
  59. public void test302DoesNotRetryOnDelete() {
  60. HttpCommand command = createMock(HttpCommand.class);
  61. HttpRequest request = HttpRequest.builder().method("DELETE").endpoint(URI.create("http://localhost")).build();
  62. HttpResponse response = new HttpResponse(302, "HTTP/1.1 302 Found", null);
  63. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  64. replay(command);
  65. DeltacloudRedirectionRetryHandler retry = injector.getInstance(
  66. DeltacloudRedirectionRetryHandler.class);
  67. assert !retry.shouldRetryRequest(command, response);
  68. verify(command);
  69. }
  70. @Test
  71. public void test302DoesRetryOnGET() {
  72. HttpCommand command = createMock(HttpCommand.class);
  73. HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://localhost")).build();
  74. HttpResponse response = new HttpResponse(302, "HTTP/1.1 302 Found", null);
  75. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  76. expect(command.incrementRedirectCount()).andReturn(1);
  77. replay(command);
  78. DeltacloudRedirectionRetryHandler retry = injector.getInstance(
  79. DeltacloudRedirectionRetryHandler.class);
  80. assert !retry.shouldRetryRequest(command, response);
  81. verify(command);
  82. }
  83. }