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

COVERAGE SUMMARY FOR SOURCE FILE [ProductOrderToJson.java]

nameclass, %method, %block, %line, %
ProductOrderToJson.java100% (6/6)100% (10/10)100% (133/133)100% (31/31)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ProductOrderToJson100% (1/1)100% (3/3)100% (64/64)100% (11/11)
ProductOrderToJson (Json): void 100% (1/1)100% (6/6)100% (3/3)
bindToRequest (HttpRequest, Object): HttpRequest 100% (1/1)100% (16/16)100% (4/4)
buildJson (ProductOrder): String 100% (1/1)100% (42/42)100% (4/4)
     
class ProductOrderToJson$1100% (1/1)100% (2/2)100% (13/13)100% (2/2)
ProductOrderToJson$1 (ProductOrderToJson): void 100% (1/1)100% (6/6)100% (1/1)
apply (ProductItemPrice): ProductOrderToJson$Price 100% (1/1)100% (7/7)100% (1/1)
     
class ProductOrderToJson$2100% (1/1)100% (2/2)100% (14/14)100% (2/2)
ProductOrderToJson$2 (ProductOrderToJson): void 100% (1/1)100% (6/6)100% (1/1)
apply (VirtualGuest): ProductOrderToJson$HostnameAndDomain 100% (1/1)100% (8/8)100% (1/1)
     
class ProductOrderToJson$HostnameAndDomain100% (1/1)100% (1/1)100% (9/9)100% (4/4)
ProductOrderToJson$HostnameAndDomain (String, String): void 100% (1/1)100% (9/9)100% (4/4)
     
class ProductOrderToJson$OrderData100% (1/1)100% (1/1)100% (27/27)100% (10/10)
ProductOrderToJson$OrderData (long, String, Set, Set, long, boolean): void 100% (1/1)100% (27/27)100% (10/10)
     
class ProductOrderToJson$Price100% (1/1)100% (1/1)100% (6/6)100% (3/3)
ProductOrderToJson$Price (long): void 100% (1/1)100% (6/6)100% (3/3)

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.softlayer.binders;
20 
21import com.google.common.base.Function;
22import com.google.common.collect.ImmutableList;
23import com.google.common.collect.ImmutableMap;
24import com.google.common.collect.Iterables;
25import com.google.common.collect.Sets;
26import org.jclouds.http.HttpRequest;
27import org.jclouds.json.Json;
28import org.jclouds.rest.Binder;
29import org.jclouds.softlayer.domain.ProductItemPrice;
30import org.jclouds.softlayer.domain.ProductOrder;
31import org.jclouds.softlayer.domain.VirtualGuest;
32 
33import javax.inject.Inject;
34import java.util.Set;
35 
36import static com.google.common.base.Preconditions.checkNotNull;
37 
38/**
39 * Converts a ProductOrder into a json string valid for placing an order via the softlayer api The
40 * String is set into the payload of the HttpRequest
41 * 
42 * @author Jason King
43 */
44public class ProductOrderToJson implements Binder {
45 
46   private Json json;
47 
48   @Inject
49   public ProductOrderToJson(Json json) {
50      this.json = json;
51   }
52 
53   @Override
54   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
55      checkNotNull(input, "order");
56      ProductOrder order = ProductOrder.class.cast(input);
57      request.setPayload(buildJson(order));
58      return request;
59   }
60 
61   /**
62    * Builds a Json string suitable for sending to the softlayer api
63    * 
64    * @param order
65    * @return
66    */
67   String buildJson(ProductOrder order) {
68 
69      Iterable<Price> prices = Iterables.transform(order.getPrices(), new Function<ProductItemPrice, Price>() {
70         @Override
71         public Price apply(ProductItemPrice productItemPrice) {
72            return new Price(productItemPrice.getId());
73         }
74      });
75 
76      Iterable<HostnameAndDomain> hosts = Iterables.transform(order.getVirtualGuests(),
77               new Function<VirtualGuest, HostnameAndDomain>() {
78                  @Override
79                  public HostnameAndDomain apply(VirtualGuest virtualGuest) {
80                     return new HostnameAndDomain(virtualGuest.getHostname(), virtualGuest.getDomain());
81                  }
82               });
83 
84      OrderData data = new OrderData(order.getPackageId(), order.getLocation(), Sets.newLinkedHashSet(prices), Sets
85               .newLinkedHashSet(hosts), order.getQuantity(), order.getUseHourlyPricing());
86 
87      return json.toJson(ImmutableMap.of("parameters", ImmutableList.<OrderData> of(data)));
88   }
89 
90   @SuppressWarnings("unused")
91   private static class OrderData {
92      private String complexType = "SoftLayer_Container_Product_Order_Virtual_Guest";
93      private long packageId = -1;
94      private String location;
95      private Set<Price> prices;
96      private Set<HostnameAndDomain> virtualGuests;
97      private long quantity;
98      private boolean useHourlyPricing;
99 
100      public OrderData(long packageId, String location, Set<Price> prices, Set<HostnameAndDomain> virtualGuests,
101               long quantity, boolean useHourlyPricing) {
102         this.packageId = packageId;
103         this.location = location;
104         this.prices = prices;
105         this.virtualGuests = virtualGuests;
106         this.quantity = quantity;
107         this.useHourlyPricing = useHourlyPricing;
108      }
109 
110   }
111 
112   @SuppressWarnings("unused")
113   private static class HostnameAndDomain {
114      private String hostname;
115      private String domain;
116 
117      public HostnameAndDomain(String hostname, String domain) {
118         this.hostname = hostname;
119         this.domain = domain;
120      }
121 
122   }
123 
124   @SuppressWarnings("unused")
125   private static class Price {
126      private long id;
127 
128      public Price(long id) {
129         this.id = id;
130      }
131   }
132 
133}

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