PageRenderTime 121ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/apis/elasticstack/src/test/java/org/jclouds/elasticstack/handlers/ElasticStackErrorHandlerTest.java

https://github.com/regularfry/jclouds
Java | 140 lines | 93 code | 25 blank | 22 comment | 1 complexity | 19f393e9a6a4eb01f8534f6295f845b5 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.elasticstack.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 ElasticStackErrorHandlerTest {
  42. @Test
  43. public void test400MakesIllegalArgumentException() {
  44. assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 400, "", "Bad Request",
  45. IllegalArgumentException.class);
  46. }
  47. @Test
  48. public void test400MakesResourceNotFoundExceptionOnInfo() {
  49. assertCodeMakes("GET", URI.create("https://elasticstack.com/foo/info"), 400, "", "",
  50. ResourceNotFoundException.class);
  51. }
  52. @Test
  53. public void test400MakesResourceNotFoundExceptionOnMessageNotFound() {
  54. assertCodeMakes(
  55. "GET",
  56. URI.create("https://elasticstack.com/foo"),
  57. 400,
  58. "",
  59. "errors:system Drive 8f9b42b1-26de-49ad-a3fd-d4fa06524339 could not be found. Please re-validate your entry.",
  60. ResourceNotFoundException.class);
  61. }
  62. @Test
  63. public void test401MakesAuthorizationException() {
  64. assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 401, "", "Unauthorized",
  65. AuthorizationException.class);
  66. }
  67. @Test
  68. public void test404MakesResourceNotFoundException() {
  69. assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 404, "", "Not Found",
  70. ResourceNotFoundException.class);
  71. }
  72. @Test
  73. public void test405MakesIllegalArgumentException() {
  74. assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 405, "", "Method Not Allowed",
  75. IllegalArgumentException.class);
  76. }
  77. @Test
  78. public void test409MakesIllegalStateException() {
  79. assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 409, "", "Conflict",
  80. IllegalStateException.class);
  81. }
  82. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
  83. Class<? extends Exception> expected) {
  84. assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
  85. }
  86. private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
  87. String content, Class<? extends Exception> expected) {
  88. ElasticStackErrorHandler function = Guice.createInjector().getInstance(ElasticStackErrorHandler.class);
  89. HttpCommand command = createMock(HttpCommand.class);
  90. HttpRequest request = new HttpRequest(method, uri);
  91. HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
  92. .toInputStream(content)));
  93. response.getPayload().getContentMetadata().setContentType(contentType);
  94. expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
  95. command.setException(classEq(expected));
  96. replay(command);
  97. function.handleError(command, response);
  98. verify(command);
  99. }
  100. public static Exception classEq(final Class<? extends Exception> in) {
  101. reportMatcher(new IArgumentMatcher() {
  102. @Override
  103. public void appendTo(StringBuffer buffer) {
  104. buffer.append("classEq(");
  105. buffer.append(in);
  106. buffer.append(")");
  107. }
  108. @Override
  109. public boolean matches(Object arg) {
  110. return arg.getClass() == in;
  111. }
  112. });
  113. return null;
  114. }
  115. }