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

https://github.com/richardcloudsoft/legacy-jclouds · Java · 81 lines · 41 code · 14 blank · 26 comment · 0 complexity · ce3d72caaf01fd768379d3a8e80da1ca 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.http.HttpCommand;
  26. import org.jclouds.http.HttpRequest;
  27. import org.jclouds.http.HttpResponse;
  28. import org.jclouds.json.config.GsonModule;
  29. import org.testng.TestException;
  30. import org.testng.annotations.Test;
  31. import com.google.inject.Guice;
  32. /**
  33. * Tests that the GoGridErrorHandler is correctly handling the exceptions.
  34. *
  35. * @author Oleksiy Yarmula
  36. */
  37. //NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
  38. @Test(groups = "unit", testName = "GoGridErrorHandlerTest")
  39. public class GoGridErrorHandlerTest {
  40. @Test
  41. public void testHandler() {
  42. InputStream is = getClass().getResourceAsStream("/test_error_handler.json");
  43. GoGridErrorHandler handler = Guice.createInjector(new GsonModule()).getInstance(GoGridErrorHandler.class);
  44. HttpCommand command = createHttpCommand();
  45. handler.handleError(command, HttpResponse.builder().statusCode(200).message("ok").payload(is).build());
  46. Exception createdException = command.getException();
  47. assertNotNull(createdException, "There should've been an exception generated");
  48. String message = createdException.getMessage();
  49. assertTrue(message.contains("No object found that matches your input criteria."),
  50. "Didn't find the expected error cause in the exception message");
  51. assertTrue(message.contains("IllegalArgumentException"),
  52. "Didn't find the expected error code in the exception message");
  53. // make sure the InputStream is closed
  54. try {
  55. is.available();
  56. throw new TestException("Stream wasn't closed by the GoGridErrorHandler when it should've");
  57. } catch (IOException e) {
  58. // this is the excepted output
  59. }
  60. }
  61. HttpCommand createHttpCommand() {
  62. return new HttpCommand(HttpRequest.builder().method("GET").endpoint("http://localhost").build());
  63. }
  64. InputStream createInputStreamFromString(String s) {
  65. return new ByteArrayInputStream(s.getBytes());
  66. }
  67. }