PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/vkris/jclouds
Java | 100 lines | 65 code | 12 blank | 23 comment | 0 complexity | 3c88db98b9aa9838065f31f976674222 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.Constants.PROPERTY_API_VERSION;
  26. import static org.jclouds.Constants.PROPERTY_ENDPOINT;
  27. import static org.jclouds.trmk.vcloud_0_8.reference.TerremarkConstants.PROPERTY_TERREMARK_EXTENSION_NAME;
  28. import static org.jclouds.trmk.vcloud_0_8.reference.TerremarkConstants.PROPERTY_TERREMARK_EXTENSION_VERSION;
  29. import java.io.IOException;
  30. import java.net.URI;
  31. import java.util.Map;
  32. import java.util.Properties;
  33. import org.jclouds.http.HttpRequest;
  34. import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudPropertiesBuilder;
  35. import org.jclouds.trmk.vcloud_0_8.binders.BindNodeConfigurationToXmlPayload;
  36. import org.testng.annotations.Test;
  37. import com.google.common.collect.ImmutableMap;
  38. import com.google.inject.AbstractModule;
  39. import com.google.inject.Guice;
  40. import com.google.inject.Injector;
  41. import com.google.inject.name.Names;
  42. /**
  43. * Tests behavior of {@code BindNodeConfigurationToXmlPayload}
  44. *
  45. * @author Adrian Cole
  46. */
  47. @Test(groups = "unit")
  48. public class BindNodeConfigurationToXmlPayloadTest {
  49. Injector injector = Guice.createInjector(new AbstractModule() {
  50. @Override
  51. protected void configure() {
  52. Properties properties = new Properties();
  53. properties.setProperty(PROPERTY_API_VERSION, "0.8a-ext1.6");
  54. properties.setProperty(PROPERTY_TERREMARK_EXTENSION_NAME, "vCloudExpressExtensions");
  55. properties.setProperty(PROPERTY_TERREMARK_EXTENSION_VERSION, "1.6");
  56. properties.setProperty(PROPERTY_ENDPOINT, "https://services.vcloudexpress.terremark.com/api");
  57. Names.bindProperties(binder(), checkNotNull(new TerremarkVCloudPropertiesBuilder(properties).build(),
  58. "properties"));
  59. }
  60. });
  61. public void testChangeDescription() throws IOException {
  62. String expectedPayload = "<NodeService xmlns=\"urn:tmrk:vCloudExpressExtensions-1.6\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name><Enabled>true</Enabled><Description>description</Description></NodeService>";
  63. assertConfigMakesPayload(ImmutableMap.<String, String> of("name", "willie", "enabled", "true", "description",
  64. "description"), expectedPayload);
  65. }
  66. public void testDisableTraffic() throws IOException {
  67. String expectedPayload = "<NodeService xmlns=\"urn:tmrk:vCloudExpressExtensions-1.6\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name><Enabled>false</Enabled></NodeService>";
  68. assertConfigMakesPayload(ImmutableMap.<String, String> of("name", "willie", "enabled", "false"), expectedPayload);
  69. }
  70. public void testTwoOptions() throws IOException {
  71. String expectedPayload = "<NodeService xmlns=\"urn:tmrk:vCloudExpressExtensions-1.6\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name><Enabled>true</Enabled></NodeService>";
  72. assertConfigMakesPayload(ImmutableMap.<String, String> of("name", "willie", "enabled", "true"), expectedPayload);
  73. }
  74. @Test(expectedExceptions = NullPointerException.class)
  75. public void testNoOptions() throws IOException {
  76. String expectedPayload = "<NodeService xmlns=\"urn:tmrk:vCloudExpressExtensions-1.6\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Name>willie</Name><Enabled>false</Enabled></NodeService>";
  77. assertConfigMakesPayload(ImmutableMap.<String, String> of(), expectedPayload);
  78. }
  79. private void assertConfigMakesPayload(Map<String, String> config, String expectedPayload) {
  80. BindNodeConfigurationToXmlPayload binder = injector.getInstance(BindNodeConfigurationToXmlPayload.class);
  81. HttpRequest request = createMock(HttpRequest.class);
  82. expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
  83. request.setPayload(expectedPayload);
  84. replay(request);
  85. binder.bindToRequest(request, config);
  86. verify(request);
  87. }
  88. }