PageRenderTime 2807ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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