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