Add generateUsageRecords API call and tests

This commit is contained in:
Richard Downer 2011-12-16 15:18:15 +00:00
parent e64807ee59
commit 41329c3528
4 changed files with 104 additions and 3 deletions

View File

@ -19,18 +19,18 @@
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.binders.BindEndDateAsYyyyMmDdToQueryParams;
import org.jclouds.cloudstack.binders.BindStartDateAsYyyyMmDdToQueryParams;
import org.jclouds.cloudstack.domain.JobResult; import org.jclouds.cloudstack.domain.JobResult;
import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.functions.DateToYyyyMmDd;
import org.jclouds.cloudstack.options.GenerateUsageRecordsOptions; import org.jclouds.cloudstack.options.GenerateUsageRecordsOptions;
import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.ParamParser;
import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.annotations.SelectJson;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.Date; import java.util.Date;
@ -46,4 +46,10 @@ import java.util.Date;
@QueryParams(keys = "response", values = "json") @QueryParams(keys = "response", values = "json")
public interface GlobalUsageAsyncClient { public interface GlobalUsageAsyncClient {
@GET
@QueryParams(keys = "command", values = "generateUsageRecords")
@SelectJson("generateusagerecordsresponse")
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<JobResult> generateUsageRecords(@QueryParam("startdate") @ParamParser(DateToYyyyMmDd.class) Date start, @QueryParam("enddate") @ParamParser(DateToYyyyMmDd.class) Date end, GenerateUsageRecordsOptions... options);
} }

View File

@ -39,4 +39,6 @@ import java.util.concurrent.TimeUnit;
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface GlobalUsageClient { public interface GlobalUsageClient {
JobResult generateUsageRecords(Date start, Date end, GenerateUsageRecordsOptions... options);
} }

View File

@ -0,0 +1,43 @@
/**
* 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.functions;
import com.google.common.base.Function;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Convert a Date object into a "yyyy-MM-dd" String
*
* @author Richard Downer
*/
public class DateToYyyyMmDd implements Function<Object, String> {
public String apply(Object input) {
checkNotNull(input, "input cannot be null");
checkArgument(input instanceof Date, "input must be a Date");
return new SimpleDateFormat("yyyy-MM-dd").format((Date)input);
}
}

View File

@ -39,6 +39,56 @@ import java.util.TimeZone;
@Test(groups = "unit", testName = "GlobalUsageAsyncClientTest") @Test(groups = "unit", testName = "GlobalUsageAsyncClientTest")
public class GlobalUsageAsyncClientTest extends BaseCloudStackAsyncClientTest<GlobalUsageAsyncClient> { public class GlobalUsageAsyncClientTest extends BaseCloudStackAsyncClientTest<GlobalUsageAsyncClient> {
public void testGenerateUsageRecords() throws Exception {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.set(Calendar.YEAR, 2012);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
Date start = c.getTime();
c.set(Calendar.DAY_OF_MONTH, 31);
Date end = c.getTime();
Method method = GlobalUsageAsyncClient.class.getMethod("generateUsageRecords",
Date.class, Date.class, GenerateUsageRecordsOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, start, end);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=generateUsageRecords&startdate=2012-01-01&enddate=2012-01-31 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testGenerateUsageRecordsOptions() throws Exception {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.set(Calendar.YEAR, 2012);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
Date start = c.getTime();
c.set(Calendar.DAY_OF_MONTH, 31);
Date end = c.getTime();
Method method = GlobalUsageAsyncClient.class.getMethod("generateUsageRecords",
Date.class, Date.class, GenerateUsageRecordsOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, start, end, GenerateUsageRecordsOptions.Builder.domainId(42));
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=generateUsageRecords&startdate=2012-01-01&enddate=2012-01-31&domainid=42 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
@Override @Override
protected TypeLiteral<RestAnnotationProcessor<GlobalUsageAsyncClient>> createTypeLiteral() { protected TypeLiteral<RestAnnotationProcessor<GlobalUsageAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<GlobalUsageAsyncClient>>() { return new TypeLiteral<RestAnnotationProcessor<GlobalUsageAsyncClient>>() {