PageRenderTime 36ms CodeModel.GetById 19ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayloadTest.java

https://github.com/regularfry/jclouds
Java | 220 lines | 161 code | 36 blank | 23 comment | 0 complexity | d17cbc2a876a6945d76f7b0bc1009919 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.trmk.vcloud_0_8.binders;
 20
 21import static com.google.common.base.Preconditions.checkNotNull;
 22import static org.easymock.EasyMock.expect;
 23import static org.easymock.classextension.EasyMock.createMock;
 24import static org.easymock.classextension.EasyMock.replay;
 25import static org.easymock.classextension.EasyMock.verify;
 26import static org.jclouds.trmk.vcloud_0_8.domain.VAppConfiguration.Builder.changeNameTo;
 27
 28import java.io.IOException;
 29import java.net.URI;
 30import java.util.Map;
 31import java.util.Properties;
 32
 33import org.jclouds.cim.ResourceAllocationSettingData;
 34import org.jclouds.cim.ResourceAllocationSettingData.ResourceType;
 35import org.jclouds.rest.internal.GeneratedHttpRequest;
 36import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudPropertiesBuilder;
 37import org.jclouds.trmk.vcloud_0_8.binders.BindVAppConfigurationToXmlPayload;
 38import org.jclouds.trmk.vcloud_0_8.domain.Status;
 39import org.jclouds.trmk.vcloud_0_8.domain.VAppConfiguration;
 40import org.jclouds.trmk.vcloud_0_8.domain.internal.VAppImpl;
 41import org.jclouds.util.Strings2;
 42import org.testng.annotations.Test;
 43
 44import com.google.common.collect.ImmutableList;
 45import com.google.common.collect.ImmutableListMultimap;
 46import com.google.common.collect.ImmutableSet;
 47import com.google.common.collect.Maps;
 48import com.google.inject.AbstractModule;
 49import com.google.inject.Guice;
 50import com.google.inject.Injector;
 51import com.google.inject.name.Names;
 52
 53/**
 54 * Tests behavior of {@code BindVAppConfigurationToXmlPayload}
 55 * 
 56 * @author Adrian Cole
 57 */
 58@Test(groups = "unit")
 59public class BindVAppConfigurationToXmlPayloadTest {
 60   Injector injector = Guice.createInjector(new AbstractModule() {
 61
 62      @Override
 63      protected void configure() {
 64         Properties props = new Properties();
 65         Names
 66                  .bindProperties(binder(), checkNotNull(new TerremarkVCloudPropertiesBuilder(props).build(),
 67                           "properties"));
 68      }
 69   });
 70
 71   public void testChangeName() throws IOException {
 72      VAppImpl vApp = new VAppImpl("MyAppServer6", URI
 73               .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), Status.OFF, 4194304l, null,
 74               ImmutableListMultimap.<String, String> of(), null, null, null, ImmutableSet.of(
 75                        ResourceAllocationSettingData.builder().instanceID("1").elementName("foo").resourceType(
 76                                 ResourceType.PROCESSOR).virtualQuantity(2l).build(), ResourceAllocationSettingData
 77                                 .builder().instanceID("2").elementName("foo").resourceType(ResourceType.MEMORY)
 78                                 .virtualQuantity(1024l).build(), ResourceAllocationSettingData.builder().instanceID(
 79                                 "9").elementName("foo").addressOnParent("0").hostResource("1048576").resourceType(
 80                                 ResourceType.DISK_DRIVE).virtualQuantity(209152l).build()));
 81
 82      String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/configureVApp.xml"))
 83               .replace("eduardo", "roberto");
 84
 85      VAppConfiguration config = new VAppConfiguration().changeNameTo("roberto");
 86
 87      GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
 88      expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
 89      expect(request.getArgs()).andReturn(ImmutableList.<Object> of(vApp, config)).atLeastOnce();
 90      request.setPayload(expected);
 91      replay(request);
 92
 93      BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class);
 94
 95      Map<String, String> map = Maps.newHashMap();
 96      binder.bindToRequest(request, map);
 97      verify(request);
 98   }
 99
100   public void testRemoveDisk() throws IOException {
101      VAppImpl vApp = new VAppImpl("MyAppServer6", URI
102               .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), Status.OFF, 4194304l, null,
103               ImmutableListMultimap.<String, String> of(), null, null, null, ImmutableSet.of(
104                        ResourceAllocationSettingData.builder().instanceID("1").elementName("foo").resourceType(
105                                 ResourceType.PROCESSOR).virtualQuantity(2l).build(),//
106                        ResourceAllocationSettingData.builder().instanceID("2").elementName("foo").resourceType(
107                                 ResourceType.MEMORY).virtualQuantity(1024l).build(), //
108                        ResourceAllocationSettingData.builder().instanceID("9").elementName("foo").addressOnParent("0")
109                                 .hostResource("1048576").resourceType(ResourceType.DISK_DRIVE)
110                                 .virtualQuantity(209152l).build(),//
111                        ResourceAllocationSettingData.builder().instanceID("9").elementName("foo").addressOnParent("1")
112                                 .hostResource("1048576").resourceType(ResourceType.DISK_DRIVE)
113                                 .virtualQuantity(209152l).build()//
114                        )
115
116      );
117
118      String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/configureVApp.xml"))
119               .replace("eduardo", "MyAppServer6");
120
121      VAppConfiguration config = new VAppConfiguration().deleteDiskWithAddressOnParent(1);
122
123      GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
124      expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
125      expect(request.getArgs()).andReturn(ImmutableList.<Object> of(vApp, config)).atLeastOnce();
126      request.setPayload(expected);
127      replay(request);
128
129      BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class);
130
131      Map<String, String> map = Maps.newHashMap();
132      binder.bindToRequest(request, map);
133      verify(request);
134   }
135
136   public void testChangeAll() throws IOException {
137      VAppImpl vApp = new VAppImpl("MyAppServer6", URI
138               .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), Status.OFF, 4194304l, null,
139               ImmutableListMultimap.<String, String> of(), null, null, null, ImmutableSet.of(
140                        ResourceAllocationSettingData.builder().instanceID("1").elementName("foo").resourceType(
141                                 ResourceType.PROCESSOR).virtualQuantity(2l).build(), ResourceAllocationSettingData
142                                 .builder().instanceID("2").elementName("foo").resourceType(ResourceType.MEMORY)
143                                 .virtualQuantity(1024l).build(), ResourceAllocationSettingData.builder().instanceID(
144                                 "9").elementName("foo").addressOnParent("0").hostResource("1048576").resourceType(
145                                 ResourceType.DISK_DRIVE).virtualQuantity(209152l).build()));
146
147      String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/configureVAppAll.xml"));
148
149      VAppConfiguration config = changeNameTo("eduardo").changeMemoryTo(1536).changeProcessorCountTo(1).addDisk(
150               25 * 1048576).addDisk(25 * 1048576);
151
152      GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
153      expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
154      expect(request.getArgs()).andReturn(ImmutableList.<Object> of(vApp, config)).atLeastOnce();
155      request.setPayload(expected);
156      replay(request);
157
158      BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class);
159
160      Map<String, String> map = Maps.newHashMap();
161      binder.bindToRequest(request, map);
162      verify(request);
163   }
164
165   public void testChangeCPUCountTo4() throws IOException {
166      VAppImpl vApp = new VAppImpl("eduardo", URI
167               .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), Status.OFF, 4194304l, null,
168               ImmutableListMultimap.<String, String> of(), null, null, null, ImmutableSet.of(
169                        ResourceAllocationSettingData.builder().instanceID("1").elementName("foo").resourceType(
170                                 ResourceType.PROCESSOR).virtualQuantity(4l).build(), ResourceAllocationSettingData
171                                 .builder().instanceID("2").elementName("foo").resourceType(ResourceType.MEMORY)
172                                 .virtualQuantity(1024l).build(), ResourceAllocationSettingData.builder().instanceID(
173                                 "9").elementName("foo").addressOnParent("0").hostResource("1048576").resourceType(
174                                 ResourceType.DISK_DRIVE).virtualQuantity(209152l).build()));
175      String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/configureVApp4.xml"));
176
177      VAppConfiguration config = new VAppConfiguration().changeProcessorCountTo(4);
178
179      GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
180      expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
181      expect(request.getArgs()).andReturn(ImmutableList.<Object> of(vApp, config)).atLeastOnce();
182      request.setPayload(expected);
183      replay(request);
184
185      BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class);
186
187      Map<String, String> map = Maps.newHashMap();
188      binder.bindToRequest(request, map);
189      verify(request);
190   }
191
192   public void testChangeMemoryTo1536() throws IOException {
193      VAppImpl vApp = new VAppImpl("MyAppServer6", URI
194               .create("https://services.vcloudexpress/terremark.com/api/v0.8/vapp/4213"), Status.OFF, 4194304l, null,
195               ImmutableListMultimap.<String, String> of(), null, null, null, ImmutableSet.of(
196                        ResourceAllocationSettingData.builder().instanceID("1").elementName("foo").resourceType(
197                                 ResourceType.PROCESSOR).virtualQuantity(2l).build(), ResourceAllocationSettingData
198                                 .builder().instanceID("2").elementName("foo").resourceType(ResourceType.MEMORY)
199                                 .virtualQuantity(1536l).build(), ResourceAllocationSettingData.builder().instanceID(
200                                 "9").elementName("foo").addressOnParent("0").hostResource("1048576").resourceType(
201                                 ResourceType.DISK_DRIVE).virtualQuantity(209152l).build()));
202
203      String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/configureVApp.xml"))
204               .replace("eduardo", "MyAppServer6").replace("1024", "1536");
205
206      VAppConfiguration config = new VAppConfiguration().changeMemoryTo(1536);
207
208      GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
209      expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
210      expect(request.getArgs()).andReturn(ImmutableList.<Object> of(vApp, config)).atLeastOnce();
211      request.setPayload(expected);
212      replay(request);
213
214      BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class);
215
216      Map<String, String> map = Maps.newHashMap();
217      binder.bindToRequest(request, map);
218      verify(request);
219   }
220}