/labs/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/handlers/UltraDNSWSErrorHandlerTest.java

https://github.com/richardcloudsoft/legacy-jclouds · Java · 122 lines · 79 code · 21 blank · 22 comment · 0 complexity · e2ebcdcbe908f5b1b227f64f93f642cd 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.ultradns.ws.handlers;
  20. import static com.google.common.base.Throwables.propagate;
  21. import static org.jclouds.rest.internal.BaseRestApiExpectTest.payloadFromStringWithContentType;
  22. import static org.jclouds.util.Strings2.toStringAndClose;
  23. import static org.testng.Assert.assertEquals;
  24. import java.io.IOException;
  25. import org.jclouds.http.HttpCommand;
  26. import org.jclouds.http.HttpRequest;
  27. import org.jclouds.http.HttpResponse;
  28. import org.jclouds.http.functions.config.SaxParserModule;
  29. import org.jclouds.io.Payload;
  30. import org.jclouds.rest.ResourceNotFoundException;
  31. import org.jclouds.ultradns.ws.UltraDNSWSResponseException;
  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 UltraDNSWSErrorHandlerTest {
  40. UltraDNSWSErrorHandler function = Guice.createInjector(new SaxParserModule()).getInstance(
  41. UltraDNSWSErrorHandler.class);
  42. @Test
  43. public void testCode0SetsResourceNotFoundException() throws IOException {
  44. HttpRequest request = HttpRequest.builder().method("POST")
  45. .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
  46. .addHeader("Host", "ultra-api.ultradns.com:8443")
  47. .payload(payloadFromResource("/list_tasks.xml")).build();
  48. HttpCommand command = new HttpCommand(request);
  49. HttpResponse response = HttpResponse.builder().message("Server Error").statusCode(500)
  50. .payload(payloadFromResource("/task_doesnt_exist.xml")).build();
  51. function.handleError(command, response);
  52. assertEquals(command.getException().getClass(), ResourceNotFoundException.class);
  53. assertEquals(command.getException().getMessage(), "Cannot find task with guid AAAAAAAAAAAAAAAA");
  54. UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
  55. assertEquals(exception.getMessage(), "Error 0: Cannot find task with guid AAAAAAAAAAAAAAAA");
  56. assertEquals(exception.getError().getDescription(), "Cannot find task with guid AAAAAAAAAAAAAAAA");
  57. assertEquals(exception.getError().getCode(), 0);
  58. }
  59. @Test
  60. public void testCode2401SetsResourceNotFoundException() throws IOException {
  61. HttpRequest request = HttpRequest.builder().method("POST")
  62. .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
  63. .addHeader("Host", "ultra-api.ultradns.com:8443")
  64. .payload(payloadFromResource("/list_zones_by_account.xml")).build();
  65. HttpCommand command = new HttpCommand(request);
  66. HttpResponse response = HttpResponse.builder().message("Server Error").statusCode(500)
  67. .payload(payloadFromResource("/account_doesnt_exist.xml")).build();
  68. function.handleError(command, response);
  69. assertEquals(command.getException().getClass(), ResourceNotFoundException.class);
  70. assertEquals(command.getException().getMessage(), "Account not found in the system. ID: AAAAAAAAAAAAAAAA");
  71. UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
  72. assertEquals(exception.getMessage(), "Error 2401: Account not found in the system. ID: AAAAAAAAAAAAAAAA");
  73. assertEquals(exception.getError().getDescription(), "Account not found in the system. ID: AAAAAAAAAAAAAAAA");
  74. assertEquals(exception.getError().getCode(), 2401);
  75. }
  76. @Test
  77. public void testCode1801SetsResourceNotFoundException() throws IOException {
  78. HttpRequest request = HttpRequest.builder().method("POST")
  79. .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
  80. .addHeader("Host", "ultra-api.ultradns.com:8443")
  81. .payload(payloadFromResource("/get_zone.xml")).build();
  82. HttpCommand command = new HttpCommand(request);
  83. HttpResponse response = HttpResponse.builder().message("Server Error").statusCode(500)
  84. .payload(payloadFromResource("/zone_doesnt_exist.xml")).build();
  85. function.handleError(command, response);
  86. assertEquals(command.getException().getClass(), ResourceNotFoundException.class);
  87. assertEquals(command.getException().getMessage(), "Zone does not exist in the system.");
  88. UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
  89. assertEquals(exception.getMessage(), "Error 1801: Zone does not exist in the system.");
  90. assertEquals(exception.getError().getDescription(), "Zone does not exist in the system.");
  91. assertEquals(exception.getError().getCode(), 1801);
  92. }
  93. private Payload payloadFromResource(String resource) {
  94. try {
  95. return payloadFromStringWithContentType(toStringAndClose(getClass().getResourceAsStream(resource)),
  96. "application/xml");
  97. } catch (IOException e) {
  98. throw propagate(e);
  99. }
  100. }
  101. }