PageRenderTime 5439ms CodeModel.GetById 95ms RepoModel.GetById 1ms app.codeStats 0ms

/apis/cloudsigma/src/test/java/org/jclouds/cloudsigma/handlers/CloudSigmaErrorHandlerTest.java

http://github.com/jclouds/jclouds
Java | 135 lines | 90 code | 25 blank | 20 comment | 1 complexity | 8c45229bb2aa210c8383ad9d8311e9a4 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.cloudsigma.handlers;
  18. import static org.easymock.EasyMock.createMock;
  19. import static org.easymock.EasyMock.expect;
  20. import static org.easymock.EasyMock.replay;
  21. import static org.easymock.EasyMock.reportMatcher;
  22. import static org.easymock.EasyMock.verify;
  23. import java.net.URI;
  24. import org.easymock.IArgumentMatcher;
  25. import org.jclouds.http.HttpCommand;
  26. import org.jclouds.http.HttpRequest;
  27. import org.jclouds.http.HttpResponse;
  28. import org.jclouds.rest.AuthorizationException;
  29. import org.jclouds.rest.ResourceNotFoundException;
  30. import org.testng.annotations.Test;
  31. import com.google.inject.Guice;
  32. /**
  33. *
  34. * @author Adrian Cole
  35. */
  36. @Test(groups = { "unit" })
  37. public class CloudSigmaErrorHandlerTest {
  38. @Test
  39. public void test400MakesIllegalArgumentException() {
  40. assertCodeMakes("GET", URI.create("https://cloudsigma.com/foo"), 400, "", "Bad Request",
  41. IllegalArgumentException.class);
  42. }
  43. @Test
  44. public void test400MakesResourceNotFoundExceptionOnInfo() {
  45. assertCodeMakes("GET", URI.create("https://cloudsigma.com/foo/info"), 400, "", "",
  46. ResourceNotFoundException.class);
  47. }
  48. @Test
  49. public void test400MakesResourceNotFoundExceptionOnMessageNotFound() {
  50. assertCodeMakes(
  51. "GET",
  52. URI.create("https://cloudsigma.com/foo"),
  53. 400,
  54. "",
  55. "errors:system Drive 8f9b42b1-26de-49ad-a3fd-d4fa06524339 could not be found. Please re-validate your entry.",
  56. ResourceNotFoundException.class);
  57. }
  58. @Test
  59. public void test401MakesAuthorizationException() {
  60. assertCodeMakes("GET", URI.create("https://cloudsigma.com/foo"), 401, "", "Unauthorized",
  61. AuthorizationException.class);
  62. }
  63. @Test
  64. public void test404MakesResourceNotFoundException() {
  65. assertCodeMakes("GET", URI.create("https://cloudsigma.com/foo"), 404, "", "Not Found",
  66. ResourceNotFoundException.class);
  67. }
  68. @Test
  69. public void test405MakesIllegalArgumentException() {
  70. assertCodeMakes("GET", URI.create("https://cloudsigma.com/foo"), 405, "", "Method Not Allowed",
  71. IllegalArgumentException.class);
  72. }
  73. @Test
  74. public void test409MakesIllegalStateException() {
  75. assertCodeMakes("GET", URI.create("https://cloudsigma.com/foo"), 409, "", "Conflict",
  76. IllegalStateException.class);
  77. }
  78. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
  79. Class<? extends Exception> expected) {
  80. assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
  81. }
  82. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
  83. String content, Class<? extends Exception> expected) {
  84. CloudSigmaErrorHandler function = Guice.createInjector().getInstance(CloudSigmaErrorHandler.class);
  85. HttpCommand command = createMock(HttpCommand.class);
  86. HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build();
  87. HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build();
  88. response.getPayload().getContentMetadata().setContentType(contentType);
  89. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  90. command.setException(classEq(expected));
  91. replay(command);
  92. function.handleError(command, response);
  93. verify(command);
  94. }
  95. public static Exception classEq(final Class<? extends Exception> in) {
  96. reportMatcher(new IArgumentMatcher() {
  97. @Override
  98. public void appendTo(StringBuffer buffer) {
  99. buffer.append("classEq(");
  100. buffer.append(in);
  101. buffer.append(")");
  102. }
  103. @Override
  104. public boolean matches(Object arg) {
  105. return arg.getClass() == in;
  106. }
  107. });
  108. return null;
  109. }
  110. }