EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.trmk.vcloud_0_8.binders]

COVERAGE SUMMARY FOR SOURCE FILE [BindCloneVAppParamsToXmlPayload.java]

nameclass, %method, %block, %line, %
BindCloneVAppParamsToXmlPayload.java100% (1/1)71%  (5/7)78%  (174/224)71%  (27/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindCloneVAppParamsToXmlPayload100% (1/1)71%  (5/7)78%  (174/224)71%  (27/38)
bindToRequest (HttpRequest, Object): HttpRequest 0%   (0/1)0%   (0/5)0%   (0/1)
ifNullDefaultTo (String, String): String 0%   (0/1)0%   (0/9)0%   (0/1)
findOptionsInArgsOrNull (GeneratedHttpRequest): CloneVAppOptions 100% (1/1)51%  (18/35)57%  (4/7)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)72%  (50/69)60%  (9/15)
BindCloneVAppParamsToXmlPayload (BindToStringPayload, String, String): void 100% (1/1)100% (12/12)100% (5/5)
buildRoot (String, boolean, boolean): XMLBuilder 100% (1/1)100% (49/49)100% (2/2)
generateXml (String, String, CloneVAppOptions): String 100% (1/1)100% (45/45)100% (7/7)

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.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static com.google.common.base.Preconditions.checkState;
24import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE;
25import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA;
26 
27import java.util.Map;
28import java.util.Properties;
29 
30import javax.inject.Named;
31import javax.inject.Singleton;
32import javax.xml.parsers.FactoryConfigurationError;
33import javax.xml.parsers.ParserConfigurationException;
34import javax.xml.transform.TransformerException;
35 
36import org.jclouds.http.HttpRequest;
37import org.jclouds.rest.MapBinder;
38import org.jclouds.rest.binders.BindToStringPayload;
39import org.jclouds.rest.internal.GeneratedHttpRequest;
40import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudMediaType;
41import org.jclouds.trmk.vcloud_0_8.options.CloneVAppOptions;
42 
43import com.google.inject.Inject;
44import com.jamesmurty.utils.XMLBuilder;
45 
46/**
47 * 
48 * @author Adrian Cole
49 * 
50 */
51@Singleton
52public class BindCloneVAppParamsToXmlPayload implements MapBinder {
53 
54   protected final String ns;
55   protected final String schema;
56   private final BindToStringPayload stringBinder;
57 
58   @Inject
59   public BindCloneVAppParamsToXmlPayload(BindToStringPayload stringBinder,
60         @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) {
61      this.ns = ns;
62      this.schema = schema;
63      this.stringBinder = stringBinder;
64   }
65 
66   @Override
67   public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
68      checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>,
69            "this binder is only valid for GeneratedHttpRequests!");
70      GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request;
71      checkState(gRequest.getArgs() != null, "args should be initialized at this point");
72      String newName = checkNotNull(postParams.remove("newName"), "newName");
73      String vApp = checkNotNull(postParams.remove("vApp"), "vApp");
74 
75      CloneVAppOptions options = findOptionsInArgsOrNull(gRequest);
76      if (options == null) {
77         options = new CloneVAppOptions();
78      }
79      try {
80         return stringBinder.bindToRequest(request, generateXml(newName, vApp, options));
81      } catch (ParserConfigurationException e) {
82         throw new RuntimeException(e);
83      } catch (FactoryConfigurationError e) {
84         throw new RuntimeException(e);
85      } catch (TransformerException e) {
86         throw new RuntimeException(e);
87      }
88 
89   }
90 
91   protected XMLBuilder buildRoot(String name, boolean deploy, boolean powerOn) throws ParserConfigurationException,
92         FactoryConfigurationError {
93      XMLBuilder rootBuilder = XMLBuilder.create("CloneVAppParams").a("name", name).a("deploy", deploy + "")
94            .a("powerOn", powerOn + "").a("xmlns", ns).a("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
95            .a("xsi:schemaLocation", ns + " " + schema);
96      return rootBuilder;
97   }
98 
99   protected String generateXml(String newName, String vApp, CloneVAppOptions options)
100         throws ParserConfigurationException, FactoryConfigurationError, TransformerException {
101      XMLBuilder rootBuilder = buildRoot(newName, options.isDeploy(), options.isPowerOn());
102      if (options.getDescription() != null)
103         rootBuilder.e("Description").text(options.getDescription());
104      rootBuilder.e("VApp").a("xmlns", ns).a("href", vApp).a("type", TerremarkVCloudMediaType.VAPP_XML);
105      Properties outputProperties = new Properties();
106      outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
107      return rootBuilder.asString(outputProperties);
108   }
109 
110   protected CloneVAppOptions findOptionsInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
111      for (Object arg : gRequest.getArgs()) {
112         if (arg instanceof CloneVAppOptions) {
113            return (CloneVAppOptions) arg;
114         } else if (arg instanceof CloneVAppOptions[]) {
115            CloneVAppOptions[] options = (CloneVAppOptions[]) arg;
116            return (options.length > 0) ? options[0] : null;
117         }
118      }
119      return null;
120   }
121 
122   @Override
123   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
124      throw new IllegalStateException("CloneVAppParams is needs parameters");
125   }
126 
127   protected String ifNullDefaultTo(String value, String defaultValue) {
128      return value != null ? value : checkNotNull(defaultValue, "defaultValue");
129   }
130}

[all classes][org.jclouds.trmk.vcloud_0_8.binders]
EMMA 2.0.5312 (C) Vladimir Roubtsov