PageRenderTime 71ms CodeModel.GetById 38ms app.highlight 3ms RepoModel.GetById 10ms app.codeStats 0ms

/apis/cloudservers/src/test/java/org/jclouds/cloudservers/functions/ParseImageFromJsonResponseTest.java

https://github.com/edwardt/jclouds
Java | 92 lines | 52 code | 17 blank | 23 comment | 0 complexity | 6650c431ca405f0ac714666e592ef922 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.cloudservers.functions;
20
21import static org.testng.Assert.assertEquals;
22
23import java.io.InputStream;
24import java.net.UnknownHostException;
25
26import org.jclouds.cloudservers.domain.Image;
27import org.jclouds.cloudservers.domain.ImageStatus;
28import org.jclouds.date.DateService;
29import org.jclouds.http.HttpResponse;
30import org.jclouds.http.functions.UnwrapOnlyJsonValue;
31import org.jclouds.io.Payloads;
32import org.jclouds.json.config.GsonModule;
33import org.jclouds.json.config.GsonModule.DateAdapter;
34import org.jclouds.json.config.GsonModule.Iso8601DateAdapter;
35import org.testng.annotations.Test;
36
37import com.google.inject.AbstractModule;
38import com.google.inject.Guice;
39import com.google.inject.Injector;
40import com.google.inject.Key;
41import com.google.inject.TypeLiteral;
42
43/**
44 * Tests behavior of {@code ParseImageFromJsonResponse}
45 * 
46 * @author Adrian Cole
47 */
48@Test(groups = "unit")
49public class ParseImageFromJsonResponseTest {
50   Injector i = Guice.createInjector(new AbstractModule() {
51
52      @Override
53      protected void configure() {
54         bind(DateAdapter.class).to(Iso8601DateAdapter.class);
55      }
56
57   }, new GsonModule());
58
59   DateService dateService = i.getInstance(DateService.class);
60
61   public void testApplyInputStreamDetails() throws UnknownHostException {
62      Image response = parseImage();
63
64      assertEquals(response.getId(), 2);
65      assertEquals(response.getName(), "CentOS 5.2");
66      assertEquals(response.getCreated(), dateService.iso8601SecondsDateParse("2010-08-10T12:00:00Z"));
67      assertEquals(response.getProgress(), new Integer(80));
68      assertEquals(response.getServerId(), new Integer(12));
69      assertEquals(response.getStatus(), ImageStatus.SAVING);
70      assertEquals(response.getUpdated(), dateService.iso8601SecondsDateParse(("2010-10-10T12:00:00Z")));
71
72   }
73
74   public static Image parseImage() {
75      Injector i = Guice.createInjector(new AbstractModule() {
76
77         @Override
78         protected void configure() {
79            bind(DateAdapter.class).to(Iso8601DateAdapter.class);
80         }
81
82      }, new GsonModule());
83
84      InputStream is = ParseImageFromJsonResponseTest.class.getResourceAsStream("/test_get_image_details.json");
85
86      UnwrapOnlyJsonValue<Image> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Image>>() {
87      }));
88      Image response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
89      return response;
90   }
91
92}