PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/providers/softlayer/src/test/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandlerTest.java

https://github.com/andreisavu/jclouds
Java | 124 lines | 80 code | 22 blank | 22 comment | 1 complexity | d3deaef9116e7bdc8bee58153d1657a2 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.softlayer.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 SoftLayerErrorHandlerTest {
  40. @Test
  41. public void test500MakesResourceNotFoundExceptionOnUnableToDeterminePackage() {
  42. assertCodeMakes(
  43. "GET",
  44. URI.create("https://api.softlayer.com/foo"),
  45. 500,
  46. "",
  47. "{\"error\":\"Unable to determine package for 'node2102835255.me.org'.\"}",
  48. ResourceNotFoundException.class);
  49. }
  50. @Test
  51. public void test500MakesIllegalStateExceptionOnActiveTransaction() {
  52. assertCodeMakes(
  53. "GET",
  54. URI.create("https://api.softlayer.com/rest/v3/SoftLayer_Billing_Item/8676376/cancelService.json"),
  55. 500,
  56. "",
  57. "{\"error\":\"There is currently an active transaction.\"}",
  58. IllegalStateException.class);
  59. }
  60. @Test
  61. public void test401MakesAuthorizationException() {
  62. assertCodeMakes("GET", URI.create("https://api.softlayer.com/foo"), 401, "", "Unauthorized",
  63. AuthorizationException.class);
  64. }
  65. @Test
  66. public void test404MakesResourceNotFoundException() {
  67. assertCodeMakes("GET", URI.create("https://api.softlayer.com/foo"), 404, "", "Not Found",
  68. ResourceNotFoundException.class);
  69. }
  70. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
  71. Class<? extends Exception> expected) {
  72. assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
  73. }
  74. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
  75. String content, Class<? extends Exception> expected) {
  76. SoftLayerErrorHandler function = Guice.createInjector().getInstance(SoftLayerErrorHandler.class);
  77. HttpCommand command = createMock(HttpCommand.class);
  78. HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build();
  79. HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build();
  80. response.getPayload().getContentMetadata().setContentType(contentType);
  81. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  82. command.setException(classEq(expected));
  83. replay(command);
  84. function.handleError(command, response);
  85. verify(command);
  86. }
  87. public static Exception classEq(final Class<? extends Exception> in) {
  88. reportMatcher(new IArgumentMatcher() {
  89. @Override
  90. public void appendTo(StringBuffer buffer) {
  91. buffer.append("classEq(");
  92. buffer.append(in);
  93. buffer.append(")");
  94. }
  95. @Override
  96. public boolean matches(Object arg) {
  97. return arg.getClass() == in;
  98. }
  99. });
  100. return null;
  101. }
  102. }