/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/handlers/CloudStackErrorHandlerTest.java

https://github.com/regularfry/jclouds · Java · 128 lines · 82 code · 24 blank · 22 comment · 1 complexity · aaf0521370f41073062725af0be998fc 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.cloudstack.handlers;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.EasyMock.reportMatcher;
  22. import static org.easymock.classextension.EasyMock.createMock;
  23. import static org.easymock.classextension.EasyMock.replay;
  24. import static org.easymock.classextension.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.io.Payloads;
  31. import org.jclouds.rest.AuthorizationException;
  32. import org.jclouds.rest.ResourceNotFoundException;
  33. import org.jclouds.util.Strings2;
  34. import org.testng.annotations.Test;
  35. import com.google.inject.Guice;
  36. /**
  37. *
  38. * @author Adrian Cole
  39. */
  40. @Test(groups = { "unit" })
  41. public class CloudStackErrorHandlerTest {
  42. @Test
  43. public void test400MakesIllegalArgumentException() {
  44. assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 400, "", "Bad Request",
  45. IllegalArgumentException.class);
  46. }
  47. @Test
  48. public void test401MakesAuthorizationException() {
  49. assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 401, "", "Unauthorized",
  50. AuthorizationException.class);
  51. }
  52. @Test
  53. public void test404MakesResourceNotFoundException() {
  54. assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 404, "", "Not Found",
  55. ResourceNotFoundException.class);
  56. }
  57. @Test
  58. public void test405MakesIllegalArgumentException() {
  59. assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 405, "", "Method Not Allowed",
  60. IllegalArgumentException.class);
  61. }
  62. @Test
  63. public void test431MakesIllegalStateException() {
  64. assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 431, "", "Method Not Allowed",
  65. IllegalStateException.class);
  66. }
  67. @Test
  68. public void test409MakesIllegalStateException() {
  69. assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 409, "", "Conflict", IllegalStateException.class);
  70. }
  71. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
  72. Class<? extends Exception> expected) {
  73. assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
  74. }
  75. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
  76. String content, Class<? extends Exception> expected) {
  77. CloudStackErrorHandler function = Guice.createInjector().getInstance(CloudStackErrorHandler.class);
  78. HttpCommand command = createMock(HttpCommand.class);
  79. HttpRequest request = new HttpRequest(method, uri);
  80. HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
  81. .toInputStream(content)));
  82. response.getPayload().getContentMetadata().setContentType(contentType);
  83. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  84. command.setException(classEq(expected));
  85. replay(command);
  86. function.handleError(command, response);
  87. verify(command);
  88. }
  89. public static Exception classEq(final Class<? extends Exception> in) {
  90. reportMatcher(new IArgumentMatcher() {
  91. @Override
  92. public void appendTo(StringBuffer buffer) {
  93. buffer.append("classEq(");
  94. buffer.append(in);
  95. buffer.append(")");
  96. }
  97. @Override
  98. public boolean matches(Object arg) {
  99. return arg.getClass() == in;
  100. }
  101. });
  102. return null;
  103. }
  104. }