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