/providers/gogrid/src/test/java/org/jclouds/gogrid/handlers/GoGridErrorHandlerTest.java

https://github.com/regularfry/jclouds · Java · 94 lines · 52 code · 16 blank · 26 comment · 0 complexity · 0766a123805b575466384b966e081518 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.gogrid.handlers;
  20. import static org.testng.Assert.assertNotNull;
  21. import static org.testng.Assert.assertTrue;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import org.jclouds.gogrid.mock.HttpCommandMock;
  26. import org.jclouds.http.HttpCommand;
  27. import org.jclouds.http.HttpResponse;
  28. import org.jclouds.io.Payloads;
  29. import org.jclouds.json.config.GsonModule;
  30. import org.testng.TestException;
  31. import org.testng.annotations.Test;
  32. import com.google.inject.Guice;
  33. /**
  34. * Tests that the GoGridErrorHandler is correctly handling the exceptions.
  35. *
  36. * @author Oleksiy Yarmula
  37. */
  38. //NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
  39. @Test(groups = "unit", testName = "GoGridErrorHandlerTest")
  40. public class GoGridErrorHandlerTest {
  41. @Test
  42. public void testHandler() {
  43. InputStream is = getClass().getResourceAsStream("/test_error_handler.json");
  44. GoGridErrorHandler handler = Guice.createInjector(new GsonModule()).getInstance(GoGridErrorHandler.class);
  45. HttpCommand command = createHttpCommand();
  46. handler.handleError(command, new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
  47. Exception createdException = command.getException();
  48. assertNotNull(createdException, "There should've been an exception generated");
  49. String message = createdException.getMessage();
  50. assertTrue(message.contains("No object found that matches your input criteria."),
  51. "Didn't find the expected error cause in the exception message");
  52. assertTrue(message.contains("IllegalArgumentException"),
  53. "Didn't find the expected error code in the exception message");
  54. // make sure the InputStream is closed
  55. try {
  56. is.available();
  57. throw new TestException("Stream wasn't closed by the GoGridErrorHandler when it should've");
  58. } catch (IOException e) {
  59. // this is the excepted output
  60. }
  61. }
  62. HttpCommand createHttpCommand() {
  63. return new HttpCommandMock() {
  64. private Exception exception;
  65. @Override
  66. public void setException(Exception exception) {
  67. this.exception = exception;
  68. }
  69. @Override
  70. public Exception getException() {
  71. return exception;
  72. }
  73. };
  74. }
  75. InputStream createInputStreamFromString(String s) {
  76. return new ByteArrayInputStream(s.getBytes());
  77. }
  78. }