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 [BindAddInternetServiceToXmlPayload.java]

nameclass, %method, %block, %line, %
BindAddInternetServiceToXmlPayload.java100% (1/1)75%  (3/4)92%  (103/112)83%  (15/18)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindAddInternetServiceToXmlPayload100% (1/1)75%  (3/4)92%  (103/112)83%  (15/18)
bindToRequest (HttpRequest, Object): HttpRequest 0%   (0/1)0%   (0/5)0%   (0/1)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)96%  (85/89)83%  (10/12)
BindAddInternetServiceToXmlPayload (): void 100% (1/1)100% (3/3)100% (1/1)
getMonitorString (Map): String 100% (1/1)100% (15/15)100% (4/4)

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.jclouds.trmk.vcloud_0_8.reference.TerremarkConstants.PROPERTY_TERREMARK_EXTENSION_NS;
23 
24import java.util.Map;
25import java.util.concurrent.ExecutionException;
26 
27import javax.inject.Inject;
28import javax.inject.Named;
29import javax.inject.Singleton;
30 
31import org.jclouds.http.HttpRequest;
32import org.jclouds.rest.MapBinder;
33import org.jclouds.rest.binders.BindToStringPayload;
34import org.jclouds.util.Patterns;
35import org.jclouds.util.Strings2;
36 
37import com.google.common.base.Throwables;
38import com.google.common.collect.ImmutableMap;
39 
40/**
41 * 
42 * @author Adrian Cole
43 * 
44 */
45@Singleton
46public class BindAddInternetServiceToXmlPayload implements MapBinder {
47   @Inject
48   @Named("CreateInternetService")
49   private String xmlTemplate;
50   @Inject
51   private BindToStringPayload stringBinder;
52   @Inject
53   @Named(PROPERTY_TERREMARK_EXTENSION_NS)
54   private String ns;
55 
56   @Override
57   public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
58 
59      String name = checkNotNull(postParams.get("name"), "name parameter not present");
60      String protocol = checkNotNull(postParams.get("protocol"), "protocol parameter not present");
61      String port = checkNotNull(postParams.get("port"), "port parameter not present");
62      String enabled = checkNotNull(postParams.get("enabled"), "enabled parameter not present");
63      String description = postParams.get("description");
64      String payload = Strings2.replaceTokens(xmlTemplate,
65            ImmutableMap.of("name", name, "protocol", protocol, "port", port, "enabled", enabled, "ns", ns));
66      try {
67         payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("description"), description == null ? ""
68               : String.format("\n\t<Description>%s</Description>", description));
69         payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("monitor"), getMonitorString(postParams));
70      } catch (ExecutionException e) {
71         Throwables.propagate(e);
72      }
73      return stringBinder.bindToRequest(request, payload);
74   }
75   
76   private String getMonitorString(Map<String, String> postParams)
77   {
78      // Sending no <Monitor> element to Terremark will result in default behavior, which is to create a monitor.
79      String monitor = postParams.get("monitor");
80      if (monitor == null || "true".equalsIgnoreCase(monitor)) {
81          return "";
82      }
83      return "\n\t<Monitor><MonitorType>Disabled</MonitorType></Monitor>";
84   }
85 
86   @Override
87   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
88      throw new IllegalStateException("CreateInternetService needs parameters");
89   }
90 
91}

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