PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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