PageRenderTime 5039ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/providers/glesys/src/test/java/org/jclouds/glesys/GleSYSErrorHandlerTest.java

http://github.com/jclouds/jclouds
Java | 118 lines | 81 code | 21 blank | 16 comment | 1 complexity | a1bb34c916e35e5f9a5cfd1881e2148c 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.glesys;
  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.glesys.handlers.GleSYSErrorHandler;
  26. import org.jclouds.http.HttpCommand;
  27. import org.jclouds.http.HttpRequest;
  28. import org.jclouds.http.HttpResponse;
  29. import org.jclouds.rest.AuthorizationException;
  30. import org.jclouds.rest.ResourceNotFoundException;
  31. import org.testng.annotations.Test;
  32. import com.google.inject.Guice;
  33. @Test(groups = { "unit" })
  34. public class GleSYSErrorHandlerTest {
  35. @Test
  36. public void test401MakesAuthorizationException() {
  37. assertCodeMakes("GET", URI.create("https://api.glesys.com/foo"), 401, "", "Unauthorized",
  38. AuthorizationException.class);
  39. }
  40. @Test
  41. public void test500LockedMakesIllegalStateException() {
  42. assertCodeMakes(
  43. "POST",
  44. URI.create("https://api.glesys.com/server/destroy/format/json"),
  45. 500,
  46. "",
  47. "{\"response\":{\"status\":{\"code\":606,\"timestamp\":\"2012-02-14T15:48:39+01:00\",\"text\":\"Server Locked\"},\"debug\":{\"input\":{\"serverid\":\"xm3270596\",\"keepip\":\"0\"}}}}",
  48. IllegalStateException.class);
  49. }
  50. @Test
  51. public void test400MakesResourceNotFoundExceptionOnCouldNotFind() {
  52. assertCodeMakes(
  53. "POST",
  54. URI.create("https://api.glesys.com/domain/delete/format/json"),
  55. 400,
  56. "",
  57. "{\"response\":{\"status\":{\"code\":400,\"timestamp\":\"2012-02-10T12:07:56+01:00\",\"text\":\"Could not find server with this id on this account.\n\"},\"debug\":{\"input\":{\"domainname\":\"email-test.jclouds.org\"}}}}",
  58. ResourceNotFoundException.class);
  59. }
  60. @Test
  61. public void test404MakesResourceNotFoundException() {
  62. assertCodeMakes("GET", URI.create("https://api.glesys.com/foo"), 404, "", "Not Found",
  63. ResourceNotFoundException.class);
  64. }
  65. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
  66. Class<? extends Exception> expected) {
  67. assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
  68. }
  69. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
  70. String content, Class<? extends Exception> expected) {
  71. GleSYSErrorHandler function = Guice.createInjector().getInstance(GleSYSErrorHandler.class);
  72. HttpCommand command = createMock(HttpCommand.class);
  73. HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build();
  74. HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build();
  75. response.getPayload().getContentMetadata().setContentType(contentType);
  76. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  77. command.setException(classEq(expected));
  78. replay(command);
  79. function.handleError(command, response);
  80. verify(command);
  81. }
  82. public static Exception classEq(final Class<? extends Exception> in) {
  83. reportMatcher(new IArgumentMatcher() {
  84. @Override
  85. public void appendTo(StringBuffer buffer) {
  86. buffer.append("classEq(");
  87. buffer.append(in);
  88. buffer.append(")");
  89. }
  90. @Override
  91. public boolean matches(Object arg) {
  92. return arg.getClass() == in;
  93. }
  94. });
  95. return null;
  96. }
  97. }