PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java

https://github.com/vkris/jclouds
Java | 124 lines | 79 code | 20 blank | 25 comment | 1 complexity | a59713d6564b33fcb7c59d15102adc8c 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.aws.ec2.xml;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.easymock.classextension.EasyMock.replay;
  23. import static org.testng.Assert.assertEquals;
  24. import java.io.InputStream;
  25. import org.jclouds.aws.ec2.domain.AWSRunningInstance;
  26. import org.jclouds.aws.ec2.domain.MonitoringState;
  27. import org.jclouds.date.DateService;
  28. import org.jclouds.ec2.domain.InstanceState;
  29. import org.jclouds.ec2.domain.InstanceType;
  30. import org.jclouds.ec2.domain.Reservation;
  31. import org.jclouds.ec2.domain.RunningInstance;
  32. import org.jclouds.ec2.xml.BaseEC2HandlerTest;
  33. import org.jclouds.http.functions.ParseSax;
  34. import org.jclouds.http.functions.config.SaxParserModule;
  35. import org.jclouds.location.Region;
  36. import org.jclouds.rest.internal.GeneratedHttpRequest;
  37. import org.testng.annotations.BeforeTest;
  38. import org.testng.annotations.Test;
  39. import com.google.common.collect.ImmutableList;
  40. import com.google.common.collect.ImmutableSet;
  41. import com.google.inject.AbstractModule;
  42. import com.google.inject.Guice;
  43. /**
  44. * Tests behavior of {@code RunInstancesResponseHandler}
  45. *
  46. * @author Adrian Cole
  47. */
  48. // NOTE:without testName, this will not call @Before* and fail w/NPE during
  49. // surefire
  50. @Test(groups = "unit", testName = "RunInstancesResponseHandlerTest")
  51. public class AWSRunInstancesResponseHandlerTest extends BaseEC2HandlerTest {
  52. private DateService dateService;
  53. @BeforeTest
  54. @Override
  55. protected void setUpInjector() {
  56. injector = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
  57. @Override
  58. protected void configure() {
  59. bind(String.class).annotatedWith(Region.class).toInstance("us-east-1");
  60. bind(RunningInstance.Builder.class).to(AWSRunningInstance.Builder.class);
  61. }
  62. });
  63. factory = injector.getInstance(ParseSax.Factory.class);
  64. dateService = injector.getInstance(DateService.class);
  65. assert dateService != null;
  66. }
  67. public void testApplyInputStream() {
  68. InputStream is = getClass().getResourceAsStream("/run_instances.xml");
  69. Reservation<? extends AWSRunningInstance> expected = new Reservation<AWSRunningInstance>(defaultRegion,
  70. ImmutableSet.of("default"), ImmutableSet.of(
  71. new AWSRunningInstance.Builder().region(defaultRegion).groupId("default").amiLaunchIndex("0")
  72. .imageId("ami-60a54009").instanceId("i-2ba64342").instanceState(InstanceState.PENDING)
  73. .instanceType(InstanceType.M1_SMALL).keyName("example-key-name")
  74. .launchTime(dateService.iso8601DateParse("2007-08-07T11:51:50.000Z"))
  75. .monitoringState(MonitoringState.ENABLED).availabilityZone("us-east-1b").build(),
  76. new AWSRunningInstance.Builder().region(defaultRegion).groupId("default").amiLaunchIndex("1")
  77. .imageId("ami-60a54009").instanceId("i-2bc64242").instanceState(InstanceState.PENDING)
  78. .instanceType(InstanceType.M1_SMALL).keyName("example-key-name")
  79. .launchTime(dateService.iso8601DateParse("2007-08-07T11:51:50.000Z"))
  80. .monitoringState(MonitoringState.ENABLED).availabilityZone("us-east-1b").build(),
  81. new AWSRunningInstance.Builder().region(defaultRegion).groupId("default").amiLaunchIndex("2")
  82. .imageId("ami-60a54009").instanceId("i-2be64332").instanceState(InstanceState.PENDING)
  83. .instanceType(InstanceType.M1_SMALL).keyName("example-key-name")
  84. .launchTime(dateService.iso8601DateParse("2007-08-07T11:51:50.000Z"))
  85. .monitoringState(MonitoringState.ENABLED).availabilityZone("us-east-1b").build())
  86. , "AIDADH4IGTRXXKCD", null, "r-47a5402e");
  87. AWSRunInstancesResponseHandler handler = injector.getInstance(AWSRunInstancesResponseHandler.class);
  88. addDefaultRegionToHandler(handler);
  89. Reservation<? extends RunningInstance> result = factory.create(handler).parse(is);
  90. assertEquals(result.toString(), expected.toString());
  91. }
  92. public void testApplyInputStreamDoesntNPE() {
  93. InputStream is = getClass().getResourceAsStream("/run_instances_1.xml");
  94. AWSRunInstancesResponseHandler handler = injector.getInstance(AWSRunInstancesResponseHandler.class);
  95. addDefaultRegionToHandler(handler);
  96. factory.create(handler).parse(is);
  97. }
  98. private void addDefaultRegionToHandler(ParseSax.HandlerWithResult<?> handler) {
  99. GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
  100. expect(request.getArgs()).andReturn(ImmutableList.<Object> of()).atLeastOnce();
  101. replay(request);
  102. handler.setContext(request);
  103. }
  104. }