PageRenderTime 1064ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vcloud/terremark/src/test/java/org/jclouds/vcloud/terremark/binders/BindInternetServiceConfigurationToXmlPayloadTest.java

https://github.com/jraghu/jclouds
Java | 123 lines | 85 code | 15 blank | 23 comment | 0 complexity | 782584e74d0cd391d814a58715b30b87 MD5 | raw file
  1. /**
  2. *
  3. * Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
  4. *
  5. * ====================================================================
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.jclouds.vcloud.terremark.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 java.io.IOException;
  26. import java.net.URI;
  27. import java.util.Properties;
  28. import org.jclouds.rest.internal.GeneratedHttpRequest;
  29. import com.google.inject.name.Names;
  30. import org.jclouds.vcloud.VCloudPropertiesBuilder;
  31. import org.jclouds.vcloud.terremark.domain.InternetServiceConfiguration;
  32. import org.testng.annotations.Test;
  33. import com.google.common.collect.HashMultimap;
  34. import com.google.common.collect.Multimap;
  35. import com.google.common.collect.Multimaps;
  36. import com.google.inject.AbstractModule;
  37. import com.google.inject.Guice;
  38. import com.google.inject.Injector;
  39. /**
  40. * Tests behavior of {@code BindInternetServiceConfigurationToXmlPayload}
  41. *
  42. * @author Adrian Cole
  43. */
  44. @Test(groups = "unit", testName = "vcloud.BindInternetServiceConfigurationToXmlPayloadTest")
  45. public class BindInternetServiceConfigurationToXmlPayloadTest {
  46. Injector injector = Guice.createInjector(new AbstractModule() {
  47. @Override
  48. protected void configure() {
  49. Properties props = new Properties();
  50. Names.bindProperties(binder(), checkNotNull(new VCloudPropertiesBuilder(props).build(),
  51. "properties"));
  52. }
  53. });
  54. public void testChangeName() throws IOException {
  55. InternetServiceConfiguration config = new InternetServiceConfiguration()
  56. .changeNameTo("willie");
  57. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name></InternetService>";
  58. assertConfigMakesPayload(config, expectedPayload);
  59. }
  60. public void testChangeDescription() throws IOException {
  61. InternetServiceConfiguration config = new InternetServiceConfiguration()
  62. .changeDescriptionTo("description");
  63. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Description>description</Description></InternetService>";
  64. assertConfigMakesPayload(config, expectedPayload);
  65. }
  66. public void testChangeTimeout() throws IOException {
  67. InternetServiceConfiguration config = new InternetServiceConfiguration().changeTimeoutTo(3);
  68. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Timeout>3</Timeout></InternetService>";
  69. assertConfigMakesPayload(config, expectedPayload);
  70. }
  71. public void testEnableTraffic() throws IOException {
  72. InternetServiceConfiguration config = new InternetServiceConfiguration().enableTraffic();
  73. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Enabled>true</Enabled></InternetService>";
  74. assertConfigMakesPayload(config, expectedPayload);
  75. }
  76. public void testDisableTraffic() throws IOException {
  77. InternetServiceConfiguration config = new InternetServiceConfiguration().disableTraffic();
  78. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Enabled>false</Enabled></InternetService>";
  79. assertConfigMakesPayload(config, expectedPayload);
  80. }
  81. public void testTwoOptions() throws IOException {
  82. InternetServiceConfiguration config = new InternetServiceConfiguration().disableTraffic()
  83. .changeNameTo("willie");
  84. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name><Enabled>false</Enabled></InternetService>";
  85. assertConfigMakesPayload(config, expectedPayload);
  86. }
  87. @Test(expectedExceptions = IllegalArgumentException.class)
  88. public void testNoOptions() throws IOException {
  89. InternetServiceConfiguration config = new InternetServiceConfiguration();
  90. String expectedPayload = "<InternetService xmlns=\"urn:tmrk:vCloudExpress-1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name><Enabled>false</Enabled></InternetService>";
  91. assertConfigMakesPayload(config, expectedPayload);
  92. }
  93. private void assertConfigMakesPayload(InternetServiceConfiguration config, String expectedPayload) {
  94. BindInternetServiceConfigurationToXmlPayload binder = injector
  95. .getInstance(BindInternetServiceConfigurationToXmlPayload.class);
  96. Multimap<String, String> headers = Multimaps.synchronizedMultimap(HashMultimap
  97. .<String, String> create());
  98. GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
  99. expect(request.getArgs()).andReturn(new Object[] { config }).atLeastOnce();
  100. expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
  101. expect(request.getFirstHeaderOrNull("Content-Type")).andReturn(null).atLeastOnce();
  102. expect(request.getHeaders()).andReturn(headers).atLeastOnce();
  103. request.setPayload(expectedPayload);
  104. replay(request);
  105. binder.bindToRequest(request, config);
  106. verify(request);
  107. }
  108. }