EMMA Coverage Report (generated Wed Aug 10 12:30:04 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)95%  (102/107)93%  (14/15)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindAddInternetServiceToXmlPayload100% (1/1)75%  (3/4)95%  (102/107)93%  (14/15)
bindToRequest (HttpRequest, Object): HttpRequest 0%   (0/1)0%   (0/5)0%   (0/1)
BindAddInternetServiceToXmlPayload (): void 100% (1/1)100% (3/3)100% (1/1)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)100% (84/84)100% (9/9)
getMonitorString (Map): String 100% (1/1)100% (15/15)100% (4/4)

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

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