Issue 695: Added PerformanceStatistics+daily cpu service call+tests

This commit is contained in:
Jason King 2011-12-07 19:32:35 +00:00
parent eebe515511
commit dd5ba49218
10 changed files with 873 additions and 0 deletions

View File

@ -0,0 +1,123 @@
/**
* 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.domain.resource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* <xs:complexType name="PerformanceStatistic">
* @author Jason King
*
*/
@XmlRootElement(name = "PerformanceStatistic")
public class PerformanceStatistic {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromPerformanceStatistic(this);
}
public static class Builder {
private Date time;
private ResourceCapacity used;
/**
* @see PerformanceStatistic#getTime
*/
public Builder time(Date time) {
this.time =(checkNotNull(time,"time"));
return this;
}
/**
* @see PerformanceStatistic#getUsed
*/
public Builder used(ResourceCapacity used) {
this.used =(checkNotNull(used,"used"));
return this;
}
public PerformanceStatistic build() {
return new PerformanceStatistic(time,used);
}
public Builder fromPerformanceStatistic(PerformanceStatistic in) {
return time(in.getTime()).used(in.getUsed());
}
}
@XmlElement(name = "Time", required = true)
private Date time;
@XmlElement(name = "Used", required = false)
private ResourceCapacity used;
private PerformanceStatistic(Date time, ResourceCapacity used) {
this.time = checkNotNull(time, "time");
this.used = used;
}
private PerformanceStatistic() {
//For JAXB
}
public Date getTime() {
return time;
}
public ResourceCapacity getUsed() {
return used;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PerformanceStatistic that = (PerformanceStatistic) o;
if (!time.equals(that.time)) return false;
if (used != null ? !used.equals(that.used) : that.used != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = time.hashCode();
result = 31 * result + (used != null ? used.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[time="+time+", used="+used+"]";
}
}

View File

@ -0,0 +1,224 @@
/**
* 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.domain.resource;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Resource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* PerformanceStatistics is more than a simple wrapper as it extends BaseResource.
* <xs:complexType name="PerformanceStatistics">
* @author Jason King
*
*/
@XmlRootElement(name = "PerformanceStatistics")
public class PerformanceStatistics extends Resource<PerformanceStatistics> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromPerformanceStatistics(this);
}
public static class Builder extends Resource.Builder<PerformanceStatistics> {
private Date startTime;
private Date endTime;
private VirtualMachinePerformanceStatistics statistics;
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics#getStartTime
*/
public Builder startTime(Date startTime) {
this.startTime =(checkNotNull(startTime,"startTime"));
return this;
}
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics#getEndTime
*/
public Builder endTime(Date endTime) {
this.endTime =(checkNotNull(endTime,"endTime"));
return this;
}
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics#getStatistics
*/
public Builder statistics(VirtualMachinePerformanceStatistics statistics) {
this.statistics =(checkNotNull(statistics,"statistics"));
return this;
}
@Override
public PerformanceStatistics build() {
return new PerformanceStatistics(href, type, name, links, actions, startTime, endTime, statistics);
}
public Builder fromPerformanceStatistics(PerformanceStatistics in) {
return fromResource(in).startTime(in.getStartTime()).endTime(in.getEndTime()).statistics(in.getStatistics());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromBaseResource(BaseResource<PerformanceStatistics> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource<PerformanceStatistics> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "StartTime", required = true)
private Date startTime;
@XmlElement(name = "EndTime", required = true)
private Date endTime;
@XmlElement(name = "VirtualMachines", required = false)
private VirtualMachinePerformanceStatistics statistics;
private PerformanceStatistics(URI href, String type, String name, Set<Link> links, Set<Action> actions,
Date startTime, Date endTime, @Nullable VirtualMachinePerformanceStatistics statistics) {
super(href, type, name, links, actions);
this.startTime = startTime;
this.endTime = endTime;
this.statistics = statistics;
}
private PerformanceStatistics() {
//For JAXB
}
public Date getStartTime() {
return startTime;
}
public Date getEndTime() {
return endTime;
}
public VirtualMachinePerformanceStatistics getStatistics() {
return statistics;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
PerformanceStatistics that = (PerformanceStatistics) o;
if (!endTime.equals(that.endTime)) return false;
if (!startTime.equals(that.startTime)) return false;
if (statistics != null ? !statistics.equals(that.statistics) : that.statistics != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + startTime.hashCode();
result = 31 * result + endTime.hashCode();
result = 31 * result + (statistics != null ? statistics.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", startTime="+ startTime+", endTime="+ endTime+", statistics="+ statistics;
}
}

View File

@ -0,0 +1,184 @@
/**
* 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.domain.resource;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Resource;
import javax.xml.bind.annotation.XmlElement;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* VirtualMachinePerformanceStatistic is more than a simple wrapper as it extends BaseResource.
* <xs:complexType name="VirtualMachinePerformanceStatistic">
* @author Jason King
*
*/
public class VirtualMachinePerformanceStatistic extends Resource<VirtualMachinePerformanceStatistic> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromVirtualMachinePerformanceStatistic(this);
}
public static class Builder extends Resource.Builder<VirtualMachinePerformanceStatistic> {
private Set<PerformanceStatistic> statistics = Sets.newLinkedHashSet();
/**
* @see VirtualMachinePerformanceStatistic#getStatistics
*/
public Builder statistics(Set<PerformanceStatistic> statistics) {
this.statistics =(checkNotNull(statistics,"statistics"));
return this;
}
@Override
public VirtualMachinePerformanceStatistic build() {
return new VirtualMachinePerformanceStatistic(href, type, name, links, actions, statistics);
}
public Builder fromVirtualMachinePerformanceStatistic(VirtualMachinePerformanceStatistic in) {
return fromResource(in).statistics(in.getStatistics());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromBaseResource(BaseResource<VirtualMachinePerformanceStatistic> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource<VirtualMachinePerformanceStatistic> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "PerformanceStatistic", required = false)
private Set<PerformanceStatistic> statistics = Sets.newLinkedHashSet();
private VirtualMachinePerformanceStatistic(URI href, String type, String name, Set<Link> links, Set<Action> actions, Set<PerformanceStatistic> statistics) {
super(href, type, name, links, actions);
this.statistics = ImmutableSet.copyOf(statistics);
}
private VirtualMachinePerformanceStatistic() {
//For JAXB
}
public Set<PerformanceStatistic> getStatistics() {
return Collections.unmodifiableSet(statistics);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
VirtualMachinePerformanceStatistic templates = (VirtualMachinePerformanceStatistic) o;
if (statistics != null ? !statistics.equals(templates.statistics) : templates.statistics != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (statistics != null ? statistics.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", statistics="+ statistics;
}
}

View File

@ -0,0 +1,102 @@
/**
* 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.domain.resource;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import javax.xml.bind.annotation.XmlElement;
import java.util.Collections;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Wraps individual VirtualMachinePerformanceStatistic elements.
* Needed because parsing is done with JAXB and it does not handle Generic collections
* @author Jason King
*/
public class VirtualMachinePerformanceStatistics {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromVirtualMachinePerformanceStatistics(this);
}
public static class Builder {
private Set<VirtualMachinePerformanceStatistic> virtualMachines = Sets.newLinkedHashSet();
/**
* @see VirtualMachinePerformanceStatistics#getVirtualMachinesPerformanceStatistics
*/
public Builder virtualMachines(Set<VirtualMachinePerformanceStatistic> virtualMachines) {
this.virtualMachines = Sets.newLinkedHashSet(checkNotNull(virtualMachines, "virtualMachines"));
return this;
}
public VirtualMachinePerformanceStatistics build() {
return new VirtualMachinePerformanceStatistics(virtualMachines);
}
public Builder fromVirtualMachinePerformanceStatistics(VirtualMachinePerformanceStatistics in) {
return virtualMachines(in.getVirtualMachinesPerformanceStatistics());
}
}
@XmlElement(name = "VirtualMachine", required = false)
private Set<VirtualMachinePerformanceStatistic> virtualMachines = Sets.newLinkedHashSet();
private VirtualMachinePerformanceStatistics() {
//For JAXB
}
private VirtualMachinePerformanceStatistics(Set<VirtualMachinePerformanceStatistic> virtualMachines) {
this.virtualMachines = ImmutableSet.copyOf(virtualMachines);
}
public Set<VirtualMachinePerformanceStatistic> getVirtualMachinesPerformanceStatistics() {
return Collections.unmodifiableSet(virtualMachines);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VirtualMachinePerformanceStatistics links1 = (VirtualMachinePerformanceStatistics) o;
if (!virtualMachines.equals(links1.virtualMachines)) return false;
return true;
}
@Override
public int hashCode() {
return virtualMachines.hashCode();
}
public String toString() {
return "["+ virtualMachines.toString()+"]";
}
}

View File

@ -23,6 +23,7 @@ import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.rest.annotations.*;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolPerformanceStatistics;
import org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics;
import org.jclouds.tmrk.enterprisecloud.domain.resource.cpu.ComputePoolCpuUsage;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummary;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummaryList;
@ -120,4 +121,13 @@ public interface ResourceAsyncClient {
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<ComputePoolPerformanceStatistics> getComputePoolPerformanceStatistics(@EndpointParam URI uri);
/**
* @see ResourceClient#getDailyCpuPerformanceStatistics
*/
@GET
@Consumes("application/vnd.tmrk.cloud.performanceStatistics")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<PerformanceStatistics> getDailyCpuPerformanceStatistics(@EndpointParam URI uri);
}

View File

@ -20,6 +20,7 @@ package org.jclouds.tmrk.enterprisecloud.features;
import org.jclouds.concurrent.Timeout;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolPerformanceStatistics;
import org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics;
import org.jclouds.tmrk.enterprisecloud.domain.resource.cpu.ComputePoolCpuUsage;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummary;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummaryList;
@ -152,4 +153,20 @@ public interface ResourceClient {
* @return
*/
ComputePoolPerformanceStatistics getComputePoolPerformanceStatistics(URI uri);
/**
* The Get Resources Performance Statistics Processor Daily call returns daily
* information regarding processor performance for a specified compute pool
*
* returns statistics for the previous seven days.
*
* The default endTime is midnight the beginning of the current day and the default
* startTime is midnight seven days prior to the endTime.
* For example, if the call is made at 2011-07-12T14:48:00Z, then startTime is 2011-07-05T00:00:00Z
* and endTime is 2011-07-12T00:00:00Z.
* @param uri uri the uri of the call based upon the compute pool
* e.g. /cloudapi/ecloud/computepools/{id}/usage/cpu/performanceStatistics/daily
* @return
*/
PerformanceStatistics getDailyCpuPerformanceStatistics(URI uri);
}

View File

@ -158,6 +158,20 @@ public class ResourceAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncCl
checkFilters(httpRequest);
}
public void testGetDailyCpuPerformanceStatistics() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException {
Method method = ResourceAsyncClient.class.getMethod("getDailyCpuPerformanceStatistics", URI.class);
HttpRequest httpRequest = processor.createRequest(method, URI.create("/cloudapi/ecloud/computepools/89/usage/cpu/performancestatistics/daily"));
assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/computepools/89/usage/cpu/performancestatistics/daily HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.performanceStatistics\nx-tmrk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<ResourceAsyncClient>> createTypeLiteral() {

View File

@ -20,6 +20,7 @@ package org.jclouds.tmrk.enterprisecloud.features;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity;
import org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics;
import org.jclouds.tmrk.enterprisecloud.domain.resource.cpu.ComputePoolCpuUsage;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummary;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummaryList;
@ -120,4 +121,9 @@ public class ResourceClientLiveTest extends BaseTerremarkEnterpriseCloudClientLi
ComputePoolStorageUsageDetail usage = client.getComputePoolStorageUsage(URI.create("/cloudapi/ecloud/computepools/89/usage/storage"));
assertNotNull(usage);
}
public void testGetDailyCpuPerformanceStatistics() throws Exception {
PerformanceStatistics stats = client.getDailyCpuPerformanceStatistics(URI.create("/cloudapi/ecloud/computepools/89/usage/cpu/performancestatistics/daily"));
assertNotNull(stats);
}
}

View File

@ -0,0 +1,123 @@
/**
* 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.xml;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Provides;
import org.jclouds.crypto.Crypto;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientTest;
import org.jclouds.rest.RestContextSpec;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistic;
import org.jclouds.tmrk.enterprisecloud.domain.resource.PerformanceStatistics;
import org.jclouds.tmrk.enterprisecloud.domain.resource.VirtualMachinePerformanceStatistic;
import org.jclouds.tmrk.enterprisecloud.features.ResourceAsyncClient;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.inject.Named;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Set;
import static org.jclouds.io.Payloads.newInputStreamPayload;
import static org.jclouds.rest.RestContextFactory.contextSpec;
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/**
* Tests behavior of JAXB parsing for PerformanceStatistics
*
* @author Jason King
*/
@Test(groups = "unit", testName = "PerformanceStatisticsJAXBParsingTest")
public class PerformanceStatisticsJAXBParsingTest extends BaseRestClientTest {
private SimpleDateFormatDateService dateService;
@BeforeMethod
public void setUp() {
dateService = new SimpleDateFormatDateService();
}
@BeforeClass
void setupFactory() {
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo",
"credentialFoo", String.class, Integer.class,
ImmutableSet.<Module>of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
@Override
protected void configure() {
}
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> exception() {
throw new AuthorizationException();
}
}));
injector = createContextBuilder(contextSpec).buildInjector();
parserFactory = injector.getInstance(ParseSax.Factory.class);
crypto = injector.getInstance(Crypto.class);
}
public void testParseWithJAXB() throws Exception {
Method method = ResourceAsyncClient.class.getMethod("getDailyCpuPerformanceStatistics", URI.class);
HttpRequest request = factory(ResourceAsyncClient.class).createRequest(method,new URI("/1"));
assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class);
Function<HttpResponse, PerformanceStatistics> parser = (Function<HttpResponse, PerformanceStatistics>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
InputStream is = getClass().getResourceAsStream("/computePoolPerformanceStatisticsDaily.xml");
PerformanceStatistics statistics = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is)));
assertEquals(statistics.getLinks().size(),2);
assertEquals(statistics.getStartTime(),dateService.iso8601DateParse("2011-11-30T00:00:00.0Z"));
assertEquals(statistics.getEndTime(),dateService.iso8601DateParse("2011-12-06T00:00:00.0Z"));
assertVirtalMachineStatistics(statistics.getStatistics().getVirtualMachinesPerformanceStatistics());
}
private void assertVirtalMachineStatistics(Set<VirtualMachinePerformanceStatistic> vmStatistics) {
assertEquals(vmStatistics.size(),1);
VirtualMachinePerformanceStatistic statistic = Iterables.getOnlyElement(vmStatistics);
assertEquals(statistic.getStatistics().size(), 24);
for(PerformanceStatistic stat: statistic.getStatistics()) {
assertNotNull(stat.getTime());
assertNotNull(stat.getUsed());
}
}
}

View File

@ -0,0 +1,70 @@
<PerformanceStatistics
href="/cloudapi/ecloud/computepools/89/usage/cpu/performancestatistics/daily?starttime=2011-11-30t00%3a00%3a00z&amp;endtime=2011-12-06t00%3a00%3a00z"
type="application/vnd.tmrk.cloud.performanceStatistics"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Links>
<Link href="/cloudapi/ecloud/computepools/89"
name="Default Compute Pool"
type="application/vnd.tmrk.cloud.computePool" rel="up"/>
<Link href="/cloudapi/ecloud/computepools/89/performancestatistics"
type="application/vnd.tmrk.cloud.computePoolPerformanceStatistics"
rel="up"/>
</Links>
<StartTime>2011-11-30T00:00:00Z</StartTime>
<EndTime>2011-12-06T00:00:00Z</EndTime>
<VirtualMachines>
<VirtualMachine href="/cloudapi/ecloud/virtualmachines/5504"
name="helloworld"
type="application/vnd.tmrk.cloud.virtualMachine">
<PerformanceStatistic>
<Time>2011-11-30T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>43</Value>
</Used>
</PerformanceStatistic>
<PerformanceStatistic>
<Time>2011-12-01T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>43</Value>
</Used>
</PerformanceStatistic>
<PerformanceStatistic>
<Time>2011-12-02T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>42</Value>
</Used>
</PerformanceStatistic>
<PerformanceStatistic>
<Time>2011-12-03T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>43</Value>
</Used>
</PerformanceStatistic>
<PerformanceStatistic>
<Time>2011-12-04T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>44</Value>
</Used>
</PerformanceStatistic>
<PerformanceStatistic>
<Time>2011-12-05T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>42</Value>
</Used>
</PerformanceStatistic>
<PerformanceStatistic>
<Time>2011-12-06T00:00:00Z</Time>
<Used>
<Unit>MHz</Unit>
<Value>43</Value>
</Used>
</PerformanceStatistic>
</VirtualMachine>
</VirtualMachines>
</PerformanceStatistics>