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