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