PageRenderTime 53ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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