Issue 695: Added tests for missing virtualmachine(s) cases and introduced ReturnEmptyVirtualMachinesOnNotFoundOr404

This commit is contained in:
Jason King 2011-11-23 10:41:23 +00:00
parent 18f2060502
commit 42926bd5ad
5 changed files with 146 additions and 8 deletions

View File

@ -26,6 +26,7 @@ import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines;
import org.jclouds.tmrk.enterprisecloud.functions.ReturnEmptyVirtualMachinesOnNotFoundOr404;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -51,7 +52,7 @@ public interface VirtualMachineAsyncClient {
@GET @GET
@Consumes("application/vnd.tmrk.cloud.virtualMachine; type=collection") @Consumes("application/vnd.tmrk.cloud.virtualMachine; type=collection")
@JAXBResponseParser @JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class) @ExceptionParser(ReturnEmptyVirtualMachinesOnNotFoundOr404.class)
ListenableFuture<VirtualMachines> getVirtualMachines(@EndpointParam URI uri); ListenableFuture<VirtualMachines> getVirtualMachines(@EndpointParam URI uri);
/** /**

View File

@ -0,0 +1,57 @@
/**
* 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.tmrk.enterprisecloud.functions;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import org.jclouds.http.functions.ReturnTrueOn404;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines;
import javax.inject.Inject;
import javax.inject.Singleton;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.util.Throwables2.propagateOrNull;
/**
*
* @author Jason King
*/
@Singleton
public class ReturnEmptyVirtualMachinesOnNotFoundOr404 implements Function<Exception, Object> {
private final ReturnTrueOn404 rto404;
@Inject
ReturnEmptyVirtualMachinesOnNotFoundOr404(ReturnTrueOn404 rto404) {
this.rto404 = checkNotNull(rto404, "rto404");
}
public Object apply(Exception from) {
Iterable<ResourceNotFoundException> throwables = Iterables.filter(Throwables.getCausalChain(from),
ResourceNotFoundException.class);
if (Iterables.size(throwables) >= 1) {
return VirtualMachines.builder().build();
} else if (rto404.apply(from)) {
return VirtualMachines.builder().build();
}
return VirtualMachines.class.cast(propagateOrNull(from));
}
}

View File

@ -23,6 +23,7 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseXMLWithJAXB; import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.tmrk.enterprisecloud.functions.ReturnEmptyVirtualMachinesOnNotFoundOr404;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.IOException; import java.io.IOException;
@ -61,7 +62,7 @@ public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudA
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); assertExceptionParserClassEquals(method, ReturnEmptyVirtualMachinesOnNotFoundOr404.class);
checkFilters(httpRequest); checkFilters(httpRequest);
} }

View File

@ -30,9 +30,7 @@ import org.testng.annotations.Test;
import java.net.URI; import java.net.URI;
import java.util.Set; import java.util.Set;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.*;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
/** /**
* Tests behavior of {@code VirtualMachineClient} * Tests behavior of {@code VirtualMachineClient}
@ -53,27 +51,42 @@ public class VirtualMachineClientLiveTest extends BaseTerremarkEnterpriseCloudCl
VirtualMachines virtualMachines = client.getVirtualMachines(new URI("/cloudapi/ecloud/virtualMachines/computePools/89")); VirtualMachines virtualMachines = client.getVirtualMachines(new URI("/cloudapi/ecloud/virtualMachines/computePools/89"));
for( VirtualMachine vm : virtualMachines.getVirtualMachines()) { for( VirtualMachine vm : virtualMachines.getVirtualMachines()) {
VirtualMachine virtualMachine = client.getVirtualMachine(vm.getHref()); VirtualMachine virtualMachine = client.getVirtualMachine(vm.getHref());
assertNotNull(virtualMachine,"virtualMachine should not be null"); assertNotNull(virtualMachine);
assertEquals(virtualMachine.getStatus(),VirtualMachine.VirtualMachineStatus.DEPLOYED); assertEquals(virtualMachine.getStatus(),VirtualMachine.VirtualMachineStatus.DEPLOYED);
} }
} }
public void testGetVirtualMachinesWhenMissing() throws Exception {
VirtualMachines result = client.getVirtualMachines(new URI("/cloudapi/ecloud/virtualMachines/computePools/-1"));
assertEquals(result, VirtualMachines.builder().build());
}
public void testGetVirtualMachine() throws Exception { public void testGetVirtualMachine() throws Exception {
VirtualMachine virtualMachine = client.getVirtualMachine(new URI("/cloudapi/ecloud/virtualMachines/5504")); VirtualMachine virtualMachine = client.getVirtualMachine(new URI("/cloudapi/ecloud/virtualMachines/5504"));
assertNotNull(virtualMachine,"virtualMachine should not be null"); assertNotNull(virtualMachine,"virtualMachine should not be null");
assertEquals(virtualMachine.getStatus(), VirtualMachine.VirtualMachineStatus.DEPLOYED); assertEquals(virtualMachine.getStatus(), VirtualMachine.VirtualMachineStatus.DEPLOYED);
} }
public void testGetVirtualMachineWhenMissing() throws Exception {
VirtualMachine virtualMachine = client.getVirtualMachine(new URI("/cloudapi/ecloud/virtualMachines/-1"));
assertNull(virtualMachine);
}
public void testGetAssignedIpAddresses() throws Exception { public void testGetAssignedIpAddresses() throws Exception {
AssignedIpAddresses assignedIpAddresses = client.getAssignedIpAddresses(new URI("/cloudapi/ecloud/virtualMachines/5504/assignedips")); AssignedIpAddresses assignedIpAddresses = client.getAssignedIpAddresses(new URI("/cloudapi/ecloud/virtualMachines/5504/assignedips"));
assertNotNull(assignedIpAddresses,"assignedIpAddresses should not be null"); assertNotNull(assignedIpAddresses);
DeviceNetwork network = Iterables.getOnlyElement(assignedIpAddresses.getNetworks().getDeviceNetworks()); DeviceNetwork network = Iterables.getOnlyElement(assignedIpAddresses.getNetworks().getDeviceNetworks());
Set<String> ipAddresses = network.getIpAddresses().getIpAddresses(); Set<String> ipAddresses = network.getIpAddresses().getIpAddresses();
assertTrue(ipAddresses.size()>0, "vm has no assigned ip addresses"); assertTrue(ipAddresses.size()>0, "vm has no assigned ip addresses");
} }
public void testGetAssignedIpAddressesWhenMissing() throws Exception {
AssignedIpAddresses assignedIpAddresses = client.getAssignedIpAddresses(new URI("/cloudapi/ecloud/virtualMachines/-1/assignedips"));
assertNull(assignedIpAddresses);
}
public void testGetVirtualMachineConfigurationOptions() throws Exception { public void testGetVirtualMachineConfigurationOptions() throws Exception {
VirtualMachineConfigurationOptions virtualMachineConfigurationOptions = client.getVirtualMachineConfigurationOptions(new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions")); VirtualMachineConfigurationOptions virtualMachineConfigurationOptions = client.getVirtualMachineConfigurationOptions(new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions"));
assertNotNull(virtualMachineConfigurationOptions,"options should not be null"); assertNotNull(virtualMachineConfigurationOptions);
} }
} }

View File

@ -0,0 +1,66 @@
/**
* 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.tmrk.enterprisecloud.functions;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
import org.jclouds.http.functions.ReturnTrueOn404;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
/**
*
* @author Jason King
*/
@Test(groups = "unit", testName = "ReturnEmptyVirtualMachinesOnNotFoundOr404Test")
public class ReturnEmptyVirtualMachinesOnNotFoundOr404Test {
private ReturnEmptyVirtualMachinesOnNotFoundOr404 function;
private VirtualMachines expected;
@BeforeMethod
public void setUp() {
function = new ReturnEmptyVirtualMachinesOnNotFoundOr404(new ReturnTrueOn404());
expected = VirtualMachines.builder().build();
}
public void testOn404() {
VirtualMachines expected = VirtualMachines.builder().build();
assertEquals(function.apply(new HttpResponseException("response exception", null, new HttpResponse(404, "404 message", null))), expected);
}
public void testOnNotFound() {
VirtualMachines expected = VirtualMachines.builder().build();
assertEquals(function.apply(new ResourceNotFoundException()),expected);
}
public void testOnNotFoundChained() {
VirtualMachines expected = VirtualMachines.builder().build();
assertEquals(function.apply(new RuntimeException(new ResourceNotFoundException())),expected);
}
@Test(expectedExceptions = HttpResponseException.class)
public void testOn500() {
function.apply(new HttpResponseException("response exception", null, new HttpResponse(500, "500 message", null)));
}
}