Issue 123: added test case for RimuHostingCreateServerResponse

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2395 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-10 17:44:21 +00:00
parent 9c2628cdba
commit 0f835d1fdb
3 changed files with 199 additions and 62 deletions

View File

@ -39,7 +39,7 @@ import com.google.common.collect.Sets;
* @author Ivan Meredith
*/
public class CreateServerResponseImpl implements CreateServerResponse {
private static final Comparator<InetAddress> ADDRESS_COMPARATOR = new Comparator<InetAddress>() {
public static final Comparator<InetAddress> ADDRESS_COMPARATOR = new Comparator<InetAddress>() {
@Override
public int compare(InetAddress o1, InetAddress o2) {
@ -95,4 +95,62 @@ public class CreateServerResponseImpl implements CreateServerResponse {
return credentials;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((credentials == null) ? 0 : credentials.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + loginPort;
result = prime * result + ((loginType == null) ? 0 : loginType.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((privateAddresses == null) ? 0 : privateAddresses.hashCode());
result = prime * result + ((publicAddresses == null) ? 0 : publicAddresses.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CreateServerResponseImpl other = (CreateServerResponseImpl) obj;
if (credentials == null) {
if (other.credentials != null)
return false;
} else if (!credentials.equals(other.credentials))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (loginPort != other.loginPort)
return false;
if (loginType == null) {
if (other.loginType != null)
return false;
} else if (!loginType.equals(other.loginType))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (privateAddresses == null) {
if (other.privateAddresses != null)
return false;
} else if (!privateAddresses.equals(other.privateAddresses))
return false;
if (publicAddresses == null) {
if (other.publicAddresses != null)
return false;
} else if (!publicAddresses.equals(other.publicAddresses))
return false;
return true;
}
}

View File

@ -23,77 +23,48 @@
*/
package org.jclouds.rimuhosting.miro.servers;
import org.jclouds.compute.domain.CreateServerResponse;
import org.jclouds.compute.domain.LoginType;
import org.jclouds.domain.Credentials;
import org.jclouds.rimuhosting.miro.domain.Server;
import org.jclouds.rimuhosting.miro.domain.NewServerResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.SortedSet;
import java.util.TreeSet;
import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.internal.CreateServerResponseImpl;
import org.jclouds.domain.Credentials;
import org.jclouds.rimuhosting.miro.domain.NewServerResponse;
import org.jclouds.rimuhosting.miro.domain.Server;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
/**
* @author Ivan Meredith
*/
public class RimuHostingCreateServerResponse implements CreateServerResponse {
private Server rhServer;
private NewServerResponse rhServerResponse;
public class RimuHostingCreateServerResponse extends CreateServerResponseImpl {
public RimuHostingCreateServerResponse(NewServerResponse rhServerResponse){
this.rhServer = rhServerResponse.getServer();
this.rhServerResponse = rhServerResponse;
}
public String getId() {
return rhServer.getId().toString();
public RimuHostingCreateServerResponse(NewServerResponse rhServerResponse) {
super(rhServerResponse.getServer().getId().toString(),
rhServerResponse.getServer().getName(), getPublicAddresses(rhServerResponse
.getServer()), ImmutableList.<InetAddress> of(), 22, LoginType.SSH,
new Credentials("root", rhServerResponse.getNewInstanceRequest().getCreateOptions()
.getPassword()));
}
public String getName() {
return rhServer.getName();
}
@VisibleForTesting
static Iterable<InetAddress> getPublicAddresses(Server rhServer) {
Iterable<String> addresses = Iterables.concat(ImmutableList.of(rhServer.getIpAddresses()
.getPrimaryIp()), rhServer.getIpAddresses().getSecondaryIps());
return Iterables.transform(addresses, new Function<String, InetAddress>() {
public SortedSet<InetAddress> getPublicAddresses() {
SortedSet<InetAddress> ipAddresses = new TreeSet<InetAddress>();
try {
InetAddress address = InetAddress.getByName(rhServer.getIpAddresses().getPrimaryIp());
ipAddresses.add(address);
} catch (UnknownHostException e) {
//TODO: log the failure.
}
for(String ip : rhServer.getIpAddresses().getSecondaryIps()){
try {
InetAddress address = InetAddress.getByName(rhServer.getIpAddresses().getPrimaryIp());
ipAddresses.add(address);
} catch (UnknownHostException e) {
//TODO: log the failure.
@Override
public InetAddress apply(String from) {
try {
return InetAddress.getByName(from);
} catch (UnknownHostException e) {
// TODO: log the failure.
return null;
}
}
}
return null;
}
/**
* Rimuhosting does not support private addressess at this time.
* @return null
*/
public SortedSet<InetAddress> getPrivateAddresses() {
return null;
}
/**
* Default port is always 22.
* @return 22
*/
public int getLoginPort() {
return 22;
}
public LoginType getLoginType() {
return LoginType.SSH;
}
public Credentials getCredentials() {
return new Credentials("root",rhServerResponse.getNewInstanceRequest().getCreateOptions().getPassword());
});
}
}

View File

@ -0,0 +1,108 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.rimuhosting.miro.servers;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.jclouds.compute.domain.LoginType;
import org.jclouds.domain.Credentials;
import org.jclouds.rimuhosting.miro.data.CreateOptions;
import org.jclouds.rimuhosting.miro.data.NewServerData;
import org.jclouds.rimuhosting.miro.domain.IpAddresses;
import org.jclouds.rimuhosting.miro.domain.NewServerResponse;
import org.jclouds.rimuhosting.miro.domain.Server;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Sets;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "rimuhosting.RimuHostingCreateServerResponse")
public class RimuHostingCreateServerResponseTest {
public void testParseInetAddress() throws UnknownHostException {
Server rhServer = createMock(Server.class);
IpAddresses addresses = createMock(IpAddresses.class);
expect(rhServer.getIpAddresses()).andReturn(addresses).atLeastOnce();
expect(addresses.getPrimaryIp()).andReturn("127.0.0.1");
expect(addresses.getSecondaryIps()).andReturn(ImmutableSortedSet.of("www.yahoo.com"));
replay(rhServer);
replay(addresses);
assertEquals(Sets.newLinkedHashSet(RimuHostingCreateServerResponse
.getPublicAddresses(rhServer)), ImmutableSet.of(InetAddress.getByName("127.0.0.1"),
InetAddress.getByName("www.yahoo.com")));
}
public void test() throws UnknownHostException {
NewServerResponse nsResponse = createMock(NewServerResponse.class);
Server rhServer = createMock(Server.class);
expect(nsResponse.getServer()).andReturn(rhServer).atLeastOnce();
expect(rhServer.getId()).andReturn(new Long(1));
expect(rhServer.getName()).andReturn("name");
IpAddresses addresses = createMock(IpAddresses.class);
expect(rhServer.getIpAddresses()).andReturn(addresses).atLeastOnce();
expect(addresses.getPrimaryIp()).andReturn("127.0.0.1");
expect(addresses.getSecondaryIps()).andReturn(ImmutableSortedSet.<String> of());
NewServerData data = createMock(NewServerData.class);
expect(nsResponse.getNewInstanceRequest()).andReturn(data).atLeastOnce();
CreateOptions options = createMock(CreateOptions.class);
expect(data.getCreateOptions()).andReturn(options);
expect(options.getPassword()).andReturn("password");
replay(nsResponse);
replay(rhServer);
replay(addresses);
replay(data);
replay(options);
RimuHostingCreateServerResponse response = new RimuHostingCreateServerResponse(nsResponse);
assertEquals(response.getId(), "1");
assertEquals(response.getName(), "name");
assertEquals(response.getPublicAddresses(), ImmutableSet.<InetAddress> of(InetAddress
.getByName("127.0.0.1")));
assertEquals(response.getPrivateAddresses(), ImmutableSet.<InetAddress> of());
assertEquals(response.getLoginPort(), 22);
assertEquals(response.getLoginType(), LoginType.SSH);
assertEquals(response.getCredentials(), new Credentials("root", "password"));
}
}