PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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