cloudservers imageextension expect tests are passing

This commit is contained in:
David Ribeiro Alves 2012-05-10 07:33:53 +01:00
parent c7469bbf4a
commit fc159d1686
3 changed files with 193 additions and 71 deletions

View File

@ -1,71 +1,91 @@
/**
* 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.cloudservers.compute.predicates;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import java.net.URI;
import java.util.Map;
import java.util.Properties;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.cloudservers.CloudServersApiMetadata;
import org.jclouds.cloudservers.internal.BaseCloudServersComputeServiceExpectTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Image;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.keystone.v1_1.internal.BaseKeystoneRestClientExpectTest;
import org.jclouds.predicates.PredicateWithResult;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.net.HttpHeaders;
import com.google.inject.Injector;
import com.google.inject.Module;
/**
*
* @author David Alves
*
*/
@Test(groups = "unit", testName = "GetImageWhenStatusActivePredicateWithResultExpectTest")
public class GetImageWhenStatusActivePredicateWithResultExpectTest extends BaseKeystoneRestClientExpectTest<Injector>
implements Function<ComputeServiceContext, Injector> {
public class GetImageWhenStatusActivePredicateWithResultExpectTest extends
BaseCloudServersComputeServiceExpectTest<Injector> {
private final HttpRequest listImagesDetail = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://lon.servers.api.rackspacecloud.com/v1.0/10001786/images/detail?format=json"))
.headers(ImmutableMultimap.<String, String> builder().put("X-Auth-Token", authToken)
.put(HttpHeaders.ACCEPT, "application/json").build()).build();
.endpoint(
URI.create("https://lon.servers.api.rackspacecloud.com/v1.0/10001786/images/detail?format=json&now=1257695648897"))
.headers(ImmutableMultimap.<String, String> builder().put(HttpHeaders.ACCEPT, "application/json")
.put("X-Auth-Token", authToken).build()).build();
private final HttpResponse listImagesResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/test_list_images_detail_imageextension.json")).build();
Map<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder()
.put(initialAuth, responseWithAuth).put(listImagesDetail, listImagesResponse).build();
private final Map<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder()
.put(listImagesDetail, listImagesResponse).put(initialAuth, responseWithAuth).build();
public GetImageWhenStatusActivePredicateWithResultExpectTest() {
provider = "cloudservers";
public void testReturnsFalseOnQueuedAndSavingAndTrueOnActive() {
Injector injector = requestsSendResponses(requestResponseMap);
PredicateWithResult<Integer, Image> predicate = injector
.getInstance(GetImageWhenStatusActivePredicateWithResult.class);
assertTrue(predicate.apply(2));
assertFalse(predicate.apply(743));
assertFalse(predicate.apply(744));
}
@Override
protected ApiMetadata createApiMetadata() {
return new CloudServersApiMetadata();
public void testFailsOnOtherStatuses() {
Injector injector = requestsSendResponses(requestResponseMap);
PredicateWithResult<Integer, Image> predicate = injector
.getInstance(GetImageWhenStatusActivePredicateWithResult.class);
assertTrue(illegalStateExceptionThrown(predicate, 745));
assertTrue(illegalStateExceptionThrown(predicate, 746));
assertTrue(illegalStateExceptionThrown(predicate, 747));
assertTrue(illegalStateExceptionThrown(predicate, 748));
}
@Override
protected Properties setupProperties() {
Properties overrides = new Properties();
overrides.setProperty(PROPERTY_REGIONS, "US");
overrides.setProperty(provider + ".endpoint", endpoint);
return overrides;
}
@Override
public Injector createClient(Function<HttpRequest, HttpResponse> fn, Module module, Properties props) {
return apply(createComputeServiceContext(fn, module, props));
}
private ComputeServiceContext createComputeServiceContext(Function<HttpRequest, HttpResponse> fn, Module module,
Properties props) {
return createInjector(fn, module, props).getInstance(ComputeServiceContext.class);
private boolean illegalStateExceptionThrown(PredicateWithResult<Integer, Image> predicate, Integer id) {
try {
predicate.apply(id);
} catch (IllegalStateException e) {
return true;
}
return false;
}
@Override
@ -73,10 +93,4 @@ public class GetImageWhenStatusActivePredicateWithResultExpectTest extends BaseK
return input.utils().injector();
}
public void testReturnsFalseOnQueuedAndSavingAndTrueOnActive() {
Injector injector = requestsSendResponses(requestResponseMap);
PredicateWithResult<Integer, Image> predicate = injector
.getInstance(GetImageWhenStatusActivePredicateWithResult.class);
assertTrue(predicate.apply(2));
}
}

View File

@ -0,0 +1,103 @@
/**
* 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.cloudservers.internal;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import java.util.Date;
import java.util.Properties;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.cloudservers.CloudServersApiMetadata;
import org.jclouds.cloudservers.config.CloudServersRestClientModule;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.keystone.v1_1.config.AuthenticationServiceModule;
import org.jclouds.openstack.keystone.v1_1.internal.BaseKeystoneRestClientExpectTest;
import org.jclouds.rest.ConfiguresRestClient;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.inject.Module;
/**
*
* @author David Alves
*
*/
public abstract class BaseCloudServersComputeServiceExpectTest<T> extends BaseKeystoneRestClientExpectTest<T> implements
Function<ComputeServiceContext, T> {
public BaseCloudServersComputeServiceExpectTest() {
provider = "cloudservers";
}
protected static final String CONSTANT_DATE = "2009-11-08T15:54:08.897Z";
public static class TestAuthenticationServiceModule extends AuthenticationServiceModule {
@Override
protected void configure() {
super.configure();
}
}
@Override
protected Module createModule() {
return new TestCloudServersRestClientModule();
}
@ConfiguresRestClient
protected static class TestCloudServersRestClientModule extends CloudServersRestClientModule {
@Override
public Supplier<Date> provideCacheBusterDate() {
return new Supplier<Date>() {
public Date get() {
return new SimpleDateFormatDateService().iso8601DateParse(CONSTANT_DATE);
}
};
}
}
@Override
protected ApiMetadata createApiMetadata() {
return new CloudServersApiMetadata();
}
@Override
protected Properties setupProperties() {
Properties overrides = new Properties();
overrides.setProperty(PROPERTY_REGIONS, "US");
overrides.setProperty(provider + ".endpoint", endpoint);
return overrides;
}
@Override
public T createClient(Function<HttpRequest, HttpResponse> fn, Module module, Properties props) {
return apply(createComputeServiceContext(fn, module, props));
}
private ComputeServiceContext createComputeServiceContext(Function<HttpRequest, HttpResponse> fn, Module module,
Properties props) {
return createInjector(fn, module, props).getInstance(ComputeServiceContext.class);
}
}

View File

@ -3,58 +3,63 @@
{
"id" : 2,
"name" : "CentOS 5.2",
"updated" : "2010-10-10T12:00:00Z",
"updated" : "2010-10-10T12:00:00Z",
"created" : "2010-08-10T12:00:00Z",
"status" : "ACTIVE"
"status" : "ACTIVE"
},
{
"id" : 743,
"name" : "My Server Backup1",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"name" : "My Server Backup",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2009-07-07T09:56:16-05:00",
"status" : "SAVING",
"progress" : 80
"status" : "SAVING",
"progress" : 80
}
,
{
"id" : 744,
"name" : "My Server Backup2",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"name" : "My Server Backup",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2009-07-07T09:56:16-05:00",
"status" : "UNRECOGNIZED",
"status" : "QUEUED"
}
,
{
"id" : 745,
"name" : "My Server Backup3",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"name" : "My Server Backup",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2009-07-07T09:56:16-05:00",
"status" : "UNKNOWN",
"status" : "UNRECOGNIZED"
}
,
{
"id" : 746,
"name" : "My Server Backup4",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"name" : "My Server Backup",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2009-07-07T09:56:16-05:00",
"status" : "PREPARING",
}
"status" : "UNKNOWN"
}
,
{
"id" : 747,
"name" : "My Server Backup5",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"name" : "My Server Backup",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2009-07-07T09:56:16-05:00",
"status" : "QUEUED",
}
"status" : "PREPARING"
}
,
{
"id" : 748,
"name" : "My Server Backup6",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"name" : "My Server Backup",
"serverId" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2009-07-07T09:56:16-05:00",
"status" : "FAILED",
"status" : "FAILED"
}
]
}
}