Add ListLoadBalancerRulesResponseTest; fix issues in parsing the response (with some code from Andrei Savu)

This commit is contained in:
andreisavu 2011-12-20 14:08:28 +02:00 committed by Richard Downer
parent edf0ce24a2
commit c3e09cc09f
5 changed files with 125 additions and 4 deletions

View File

@ -19,13 +19,17 @@
package org.jclouds.cloudstack.config;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.inject.Singleton;
import com.google.common.collect.Sets;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.Account.State;
import org.jclouds.cloudstack.domain.LoadBalancerRule;
import org.jclouds.cloudstack.domain.User;
import com.google.common.collect.ImmutableMap;
@ -46,6 +50,51 @@ import com.google.inject.TypeLiteral;
*/
public class CloudStackParserModule extends AbstractModule {
@Singleton
public static class LoadBalancerRuleAdapter implements JsonSerializer<LoadBalancerRule>, JsonDeserializer<LoadBalancerRule> {
public JsonElement serialize(LoadBalancerRule src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src);
}
public LoadBalancerRule deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return apply(context.<LoadBalancerRuleInternal> deserialize(json, LoadBalancerRuleInternal.class));
}
public LoadBalancerRule apply(LoadBalancerRuleInternal in) {
Set<String> cidrSet = Sets.newHashSet(in.CIDRs.split(","));
return LoadBalancerRule.builder().id(in.id).account(in.account).algorithm(in.algorithm)
.description(in.description).domain(in.domain).domainId(in.domainId).name(in.name)
.privatePort(in.privatePort).publicIP(in.publicIP).publicIPId(in.publicIPId)
.publicPort(in.publicPort).state(in.state).CIDRs(cidrSet).zoneId(in.zoneId).build();
}
static final class LoadBalancerRuleInternal {
private long id;
private String account;
private LoadBalancerRule.Algorithm algorithm;
private String description;
private String domain;
@SerializedName("domainid")
private long domainId;
private String name;
@SerializedName("privateport")
private int privatePort;
@SerializedName("publicip")
private String publicIP;
@SerializedName("publicipid")
private long publicIPId;
@SerializedName("publicport")
private int publicPort;
private LoadBalancerRule.State state;
@SerializedName("cidrlist")
private String CIDRs;
@SerializedName("zoneId")
private long zoneId;
}
}
@Singleton
public static class BreakGenericSetAdapter implements JsonSerializer<Account>, JsonDeserializer<Account> {
@ -134,7 +183,7 @@ public class CloudStackParserModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<Map<Type, Object>>() {
}).toInstance(ImmutableMap.<Type, Object> of(Account.class, new BreakGenericSetAdapter()));
}).toInstance(ImmutableMap.<Type, Object> of(Account.class, new BreakGenericSetAdapter(), LoadBalancerRule.class, new LoadBalancerRuleAdapter()));
}
}

View File

@ -207,7 +207,7 @@ public class LoadBalancerRule implements Comparable<LoadBalancerRule> {
this.publicPort = publicPort;
this.state = state;
this.zoneId = zoneId;
this.CIDRs = Sets.newHashSet(CIDRs);
this.CIDRs = ImmutableSet.copyOf(CIDRs);
}
@ -299,7 +299,7 @@ public class LoadBalancerRule implements Comparable<LoadBalancerRule> {
* @return the cidr list to forward traffic from
*/
public Set<String> getCIDRs() {
return Collections.unmodifiableSet(CIDRs);
return CIDRs;
}
/**

View File

@ -26,7 +26,7 @@ import com.google.common.collect.ImmutableSet;
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api/user/listLoadBalancerRules.html"
* />
* @author Adrian Cole
* @author Adrian Cole, Andrei Savu
*/
public class ListLoadBalancerRulesOptions extends AccountInDomainOptions {

View File

@ -0,0 +1,70 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.parse;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.cloudstack.config.CloudStackParserModule;
import org.jclouds.cloudstack.domain.LoadBalancerRule;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import org.testng.collections.Sets;
import java.util.Set;
/**
*
* @author Andrei Savu
*/
@Test(groups = "unit")
public class ListLoadBalancerRulesResponseTest extends BaseSetParserTest<LoadBalancerRule> {
@Override
protected Injector injector() {
return Guice.createInjector(new CloudStackParserModule(), new GsonModule() {
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
super.configure();
}
});
}
@Override
public String resource() {
return "/listloadbalancerrulesresponse.json";
}
@Override
@SelectJson("loadbalancerrule")
public Set<LoadBalancerRule> expected() {
return ImmutableSet.<LoadBalancerRule> of(LoadBalancerRule.builder()
.id(93).account("admin").algorithm(LoadBalancerRule.Algorithm.ROUNDROBIN)
.description("null").domain("ROOT").domainId(1).name("Ranny").privatePort(80)
.publicIP("10.27.27.59").publicIPId(10).publicPort(80).state(LoadBalancerRule.State.ADD)
.CIDRs(Sets.<String>newHashSet()).zoneId(0)
.build());
}
}

View File

@ -0,0 +1,2 @@
{ "listloadbalancerrulesresponse" : { "count":1 ,"loadbalancerrule" : [
{"id":93,"name":"Ranny","publicipid":10,"publicip":"10.27.27.59","publicport":"80","privateport":"80","algorithm":"roundrobin","cidrlist":"","account":"admin","domainid":1,"domain":"ROOT","state":"Add","zoneid":1} ] } }