PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/handlers/VCloudDirectorErrorHandlerTest.java

http://github.com/jclouds/jclouds
Java | 102 lines | 60 code | 20 blank | 22 comment | 1 complexity | 1ed34596eb9cfa516f8800449cfbb44f 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.vcloud.director.v1_5.handlers;
  20. import static org.easymock.EasyMock.createMock;
  21. import static org.easymock.EasyMock.expect;
  22. import static org.easymock.EasyMock.replay;
  23. import static org.easymock.EasyMock.reportMatcher;
  24. import static org.easymock.EasyMock.verify;
  25. import java.net.URI;
  26. import org.easymock.IArgumentMatcher;
  27. import org.jclouds.http.HttpCommand;
  28. import org.jclouds.http.HttpRequest;
  29. import org.jclouds.http.HttpResponse;
  30. import org.jclouds.rest.AuthorizationException;
  31. import org.jclouds.rest.ResourceNotFoundException;
  32. import org.testng.annotations.Test;
  33. import com.google.inject.Guice;
  34. /**
  35. *
  36. * @author Adrian Cole
  37. */
  38. @Test(groups = { "unit" })
  39. public class VCloudDirectorErrorHandlerTest {
  40. @Test
  41. public void test401MakesAuthorizationException() {
  42. assertCodeMakes("GET", URI.create("https://api.vcloud.director.com/foo"), 401, "", "Unauthorized",
  43. AuthorizationException.class);
  44. }
  45. @Test
  46. public void test404MakesResourceNotFoundException() {
  47. assertCodeMakes("GET", URI.create("https://api.vcloud.director.com/foo"), 404, "", "Not Found",
  48. ResourceNotFoundException.class);
  49. }
  50. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
  51. Class<? extends Exception> expected) {
  52. assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
  53. }
  54. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
  55. String content, Class<? extends Exception> expected) {
  56. VCloudDirectorErrorHandler function = Guice.createInjector().getInstance(VCloudDirectorErrorHandler.class);
  57. HttpCommand command = createMock(HttpCommand.class);
  58. HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build();
  59. HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build();
  60. response.getPayload().getContentMetadata().setContentType(contentType);
  61. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  62. command.setException(classEq(expected));
  63. replay(command);
  64. function.handleError(command, response);
  65. verify(command);
  66. }
  67. public static Exception classEq(final Class<? extends Exception> in) {
  68. reportMatcher(new IArgumentMatcher() {
  69. @Override
  70. public void appendTo(StringBuffer buffer) {
  71. buffer.append("classEq(");
  72. buffer.append(in);
  73. buffer.append(")");
  74. }
  75. @Override
  76. public boolean matches(Object arg) {
  77. return arg.getClass() == in;
  78. }
  79. });
  80. return null;
  81. }
  82. }