PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ParseAsyncJobFromHttpResponseTest.java

https://github.com/alasdairhodge/jclouds
Java | 222 lines | 159 code | 37 blank | 26 comment | 0 complexity | dade4332bd36184102678e24b8ffe7ea 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.cloudstack.functions;
  20. import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode;
  21. import static org.jclouds.cloudstack.domain.AsyncJob.Status;
  22. import static org.jclouds.cloudstack.domain.AsyncJobError.ErrorCode;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertTrue;
  25. import java.io.InputStream;
  26. import org.jclouds.cloudstack.domain.AsyncJob;
  27. import org.jclouds.cloudstack.domain.AsyncJobError;
  28. import org.jclouds.cloudstack.domain.IPForwardingRule;
  29. import org.jclouds.cloudstack.domain.PublicIPAddress;
  30. import org.jclouds.cloudstack.domain.Template;
  31. import org.jclouds.cloudstack.domain.TemplateExtraction;
  32. import org.jclouds.date.internal.SimpleDateFormatDateService;
  33. import org.jclouds.domain.JsonBall;
  34. import org.jclouds.http.HttpResponse;
  35. import org.jclouds.io.Payloads;
  36. import org.jclouds.json.config.GsonModule;
  37. import org.testng.annotations.Test;
  38. import com.google.common.collect.ImmutableMap;
  39. import com.google.inject.Guice;
  40. import com.google.inject.Injector;
  41. /**
  42. * @author Adrian Cole
  43. */
  44. @Test(groups = "unit")
  45. public class ParseAsyncJobFromHttpResponseTest {
  46. Injector i = Guice.createInjector(new GsonModule() {
  47. @Override
  48. protected void configure() {
  49. bind(DateAdapter.class).to(Iso8601DateAdapter.class);
  50. super.configure();
  51. }
  52. });
  53. public void testWithNoResult() {
  54. String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":860,\"jobstatus\":0,\"jobprocstatus\":0,\"jobresultcode\":0} }";
  55. AsyncJob<PublicIPAddress> expects = AsyncJob.<PublicIPAddress>builder()
  56. .id(860)
  57. .status(Status.IN_PROGRESS)
  58. .progress(0)
  59. .resultCode(ResultCode.SUCCESS).build();
  60. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  61. @SuppressWarnings("unchecked")
  62. AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
  63. Payloads.newStringPayload(input)));
  64. assertEquals(response, expects);
  65. }
  66. public void testWithSuccessTrueResultSetsNullResult() {
  67. String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":1138,\"jobstatus\":1,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresulttype\":\"object\",\"jobresult\":{\"success\":true}} }";
  68. AsyncJob<PublicIPAddress> expects = AsyncJob.<PublicIPAddress>builder()
  69. .id(1138)
  70. .status(Status.SUCCEEDED)
  71. .progress(0)
  72. .resultType("object")
  73. .resultCode(ResultCode.SUCCESS).build();
  74. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  75. @SuppressWarnings("unchecked")
  76. AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
  77. Payloads.newStringPayload(input)));
  78. assertEquals(response, expects);
  79. }
  80. public void testWithErrorSetsResultNullSoToAvoidClassCastExceptions() {
  81. String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":1103,\"jobstatus\":2,\"jobprocstatus\":0,\"jobresultcode\":530,\"jobresulttype\":\"object\",\"jobresult\":{\"errorcode\":530,\"errortext\":\"Internal error executing command, please contact your system administrator\"}} }";
  82. AsyncJob<PublicIPAddress> expects = AsyncJob
  83. .<PublicIPAddress>builder()
  84. .id(1103)
  85. .status(Status.FAILED)
  86. .progress(0)
  87. .resultType("object")
  88. .error(new AsyncJobError(ErrorCode.INTERNAL_ERROR, "Internal error executing " +
  89. "command, please contact your system administrator"))
  90. .resultCode(ResultCode.FAIL).build();
  91. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  92. @SuppressWarnings("unchecked")
  93. AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
  94. Payloads.newStringPayload(input)));
  95. assertEquals(response, expects);
  96. }
  97. public void testWithUnknownResultReturnsStringifiedJson() {
  98. String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":860,\"jobstatus\":0,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresult\":{\"foo\":{\"bar\":1}}}}";
  99. AsyncJob<?> expects = AsyncJob.builder()
  100. .id(860)
  101. .status(Status.IN_PROGRESS)
  102. .progress(0)
  103. .resultCode(ResultCode.SUCCESS)
  104. .result("{\"bar\":1}")
  105. .build();
  106. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  107. @SuppressWarnings("unchecked")
  108. AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
  109. Payloads.newStringPayload(input)));
  110. assertEquals(response, expects);
  111. }
  112. public void testWithBadResultReturnsMap() {
  113. // Not the best result object, but this is an unexpected error case.
  114. // Cloud.com have verified
  115. // that this case will not happen. This code is only here to prevent
  116. // exceptions from being
  117. // thrown in case they change their minds.
  118. String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":860,\"jobstatus\":0,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresult\":{\"foo\":{\"bar\":1},\"foo2\":{\"bar2\":2}}}}";
  119. AsyncJob<?> expects = AsyncJob.builder()
  120. .id(860)
  121. .status(Status.IN_PROGRESS)
  122. .progress(0)
  123. .resultCode(ResultCode.SUCCESS)
  124. .result(ImmutableMap.of("foo", new JsonBall("{\"bar\":1}"), "foo2", new JsonBall("{\"bar2\":2}"))).build();
  125. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  126. @SuppressWarnings("unchecked")
  127. AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
  128. Payloads.newStringPayload(input)));
  129. assertEquals(response, expects);
  130. }
  131. public void testPublicIPAddress() {
  132. InputStream is = getClass().getResourceAsStream("/queryasyncjobresultresponse-ipaddress.json");
  133. AsyncJob<PublicIPAddress> expects = AsyncJob
  134. .<PublicIPAddress>builder()
  135. .id(860)
  136. .status(Status.SUCCEEDED)
  137. .progress(0)
  138. .resultType("object")
  139. .resultCode(ResultCode.SUCCESS)
  140. .result(
  141. PublicIPAddress
  142. .builder()
  143. .id(6)
  144. .IPAddress("72.52.126.35")
  145. .allocated(
  146. new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-23T20:15:01-0800"))
  147. .zoneId(1).zoneName("San Jose 1").isSourceNAT(false).account("adrian").domainId(1)
  148. .domain("ROOT").usesVirtualNetwork(true).isStaticNAT(false).associatedNetworkId(204)
  149. .networkId(200).state(PublicIPAddress.State.ALLOCATING).build()
  150. ).build();
  151. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  152. @SuppressWarnings("unchecked")
  153. AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
  154. Payloads.newInputStreamPayload(is)));
  155. assertEquals(response, expects);
  156. }
  157. public void testIPForwardingRule() {
  158. InputStream is = getClass().getResourceAsStream("/queryasyncjobresultresponse-ipforwardingrule.json");
  159. AsyncJob<IPForwardingRule> expects = AsyncJob
  160. .<IPForwardingRule>builder()
  161. .id(1133)
  162. .status(Status.SUCCEEDED)
  163. .progress(0)
  164. .resultType("object")
  165. .resultCode(ResultCode.SUCCESS)
  166. .result(
  167. IPForwardingRule.builder().id(109).protocol("tcp").virtualMachineId(226)
  168. .virtualMachineName("i-3-226-VM").IPAddressId(36).IPAddress("72.52.126.65").startPort(22)
  169. .endPort(22).state("Active").build()).build();
  170. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  171. @SuppressWarnings("unchecked")
  172. AsyncJob<IPForwardingRule> response = (AsyncJob<IPForwardingRule>) parser.apply(new HttpResponse(200, "ok",
  173. Payloads.newInputStreamPayload(is)));
  174. assertEquals(response, expects);
  175. }
  176. public void testOverloadedKeyName() {
  177. InputStream is = getClass().getResourceAsStream("/queryasyncjobresultresponse-createtemplate.json");
  178. ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
  179. AsyncJob<?> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
  180. assertTrue(response.getResult() instanceof Template, "response expected to be Template, actually is " + response.getResult().getClass());
  181. is = getClass().getResourceAsStream("/queryasyncjobresultresponse-extracttemplate.json");
  182. response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
  183. assertTrue(response.getResult() instanceof TemplateExtraction, "response expected to be TemplateExtraction, actually is " + response.getResult().getClass());
  184. }
  185. }