From c1a36994fdd641e613f585ee7d23c4ca4c9cfbd4 Mon Sep 17 00:00:00 2001 From: Jason King Date: Tue, 6 Dec 2011 17:20:37 +0000 Subject: [PATCH] Issue 695: Added ComputePoolMemoryUsage objects+service call+tests --- .../memory/ComputePoolMemoryUsage.java | 223 ++ ...putePoolMemoryUsageDetailSummaryEntry.java | 127 + .../resource/memory/MemoryUsageDetails.java | 106 + .../features/ResourceAsyncClient.java | 10 + .../features/ResourceClient.java | 13 + .../features/ResourceAsyncClientTest.java | 15 + .../features/ResourceClientLiveTest.java | 23 +- ...ComputePoolMemoryUsageJAXBParsingTest.java | 145 ++ .../test/resources/computePoolMemoryUsage.xml | 2039 +++++++++++++++++ 9 files changed, 2698 insertions(+), 3 deletions(-) create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsage.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsageDetailSummaryEntry.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/MemoryUsageDetails.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/ComputePoolMemoryUsageJAXBParsingTest.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/resources/computePoolMemoryUsage.xml diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsage.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsage.java new file mode 100644 index 0000000000..87d5651899 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsage.java @@ -0,0 +1,223 @@ +/** + * 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.memory; + +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; + +/** + * ComputePoolMemoryUsage is more than a simple wrapper as it extends Resource. + * + * @author Jason King + * + */ +@XmlRootElement(name = "ComputePoolMemoryUsage") +public class ComputePoolMemoryUsage extends Resource { + + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder toBuilder() { + return new Builder().fromComputePoolCpuUsage(this); + } + + public static class Builder extends Resource.Builder { + private Date startTime; + private Date endTime; + private MemoryUsageDetails details; + + /** + * @see ComputePoolMemoryUsage#getStartTime + */ + public Builder startTime(Date startTime) { + this.startTime =(checkNotNull(startTime,"startTime")); + return this; + } + + /** + * @see ComputePoolMemoryUsage#getEndTime + */ + public Builder endTime(Date endTime) { + this.endTime =(checkNotNull(endTime,"endTime")); + return this; + } + + /** + * @see ComputePoolMemoryUsage#getDetails + */ + public Builder details(MemoryUsageDetails details) { + this.details =(checkNotNull(details,"details")); + return this; + } + + @Override + public ComputePoolMemoryUsage build() { + return new ComputePoolMemoryUsage(href, type, name, links, actions, startTime, endTime, details); + } + + public Builder fromComputePoolCpuUsage(ComputePoolMemoryUsage in) { + return fromResource(in).startTime(in.getStartTime()).endTime(in.getEndTime()).details(in.getDetails()); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder fromBaseResource(BaseResource in) { + return Builder.class.cast(super.fromBaseResource(in)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder fromResource(Resource 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 links) { + return Builder.class.cast(super.links(links)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder actions(Set actions) { + return Builder.class.cast(super.actions(actions)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder fromAttributes(Map 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 = "MemoryUsageDetailSummary", required = false) + private MemoryUsageDetails details; + + private ComputePoolMemoryUsage(URI href, String type, String name, Set links, Set actions, Date startTime, Date endTime, @Nullable MemoryUsageDetails details) { + super(href, type, name, links, actions); + this.startTime = checkNotNull(startTime, "startTime"); + this.endTime = checkNotNull(endTime, "endTime"); + this.details = details; + } + + private ComputePoolMemoryUsage() { + //For JAXB + } + + public Date getStartTime() { + return startTime; + } + + public Date getEndTime() { + return endTime; + } + + public MemoryUsageDetails getDetails() { + return details; + } + + @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; + + ComputePoolMemoryUsage that = (ComputePoolMemoryUsage) o; + + if (details != null ? !details.equals(that.details) : that.details != null) + return false; + if (!endTime.equals(that.endTime)) return false; + if (!startTime.equals(that.startTime)) 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 + (details != null ? details.hashCode() : 0); + return result; + } + + @Override + public String string() { + return super.string()+", startTime="+startTime+", endTime="+endTime+", details="+details; + } +} \ No newline at end of file diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsageDetailSummaryEntry.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsageDetailSummaryEntry.java new file mode 100644 index 0000000000..5709166af0 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/ComputePoolMemoryUsageDetailSummaryEntry.java @@ -0,0 +1,127 @@ +/** + * 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.memory; + +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity; + +import javax.xml.bind.annotation.XmlElement; +import java.util.Date; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * + * @author Jason King + * + */ +public class ComputePoolMemoryUsageDetailSummaryEntry { + + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } + + /** + * {@inheritDoc} + */ + public Builder toBuilder() { + return new Builder().ComputePoolCpuUsageDetailSummaryEntry(this); + } + + public static class Builder { + private Date time; + private ResourceCapacity value; + + /** + * @see ComputePoolMemoryUsageDetailSummaryEntry#getTime + */ + public Builder time(Date time) { + this.time = time; + return this; + } + + /** + * @see ComputePoolMemoryUsageDetailSummaryEntry#getValue + */ + public Builder value(ResourceCapacity value) { + this.value = value; + return this; + } + + public ComputePoolMemoryUsageDetailSummaryEntry build() { + return new ComputePoolMemoryUsageDetailSummaryEntry(time,value); + } + + public Builder ComputePoolCpuUsageDetailSummaryEntry(ComputePoolMemoryUsageDetailSummaryEntry in) { + return time(in.getTime()).value(in.getValue()); + } + } + + @XmlElement(name = "Time", required = true) + private Date time; + + @XmlElement(name = "Value", required = false) + private ResourceCapacity value; + + + private ComputePoolMemoryUsageDetailSummaryEntry(Date time, @Nullable ResourceCapacity value) { + this.time = checkNotNull(time, "time"); + this.value = value; + } + + private ComputePoolMemoryUsageDetailSummaryEntry() { + //For JAXB + } + + public Date getTime() { + return time; + } + + public ResourceCapacity getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ComputePoolMemoryUsageDetailSummaryEntry that = (ComputePoolMemoryUsageDetailSummaryEntry) o; + + if (!time.equals(that.time)) return false; + if (value != null ? !value.equals(that.value) : that.value != null) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = time.hashCode(); + result = 31 * result + (value != null ? value.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "[time="+ time +", value="+value+"]"; + } + +} \ No newline at end of file diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/MemoryUsageDetails.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/MemoryUsageDetails.java new file mode 100644 index 0000000000..db4dcf38e6 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/resource/memory/MemoryUsageDetails.java @@ -0,0 +1,106 @@ +/** + * 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.memory; + +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 ComputePoolMemoryUsageDetailSummaryEntry elements. + * + * @author Jason King + */ +public class MemoryUsageDetails { + + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder().fromMemoryUsageDetails(this); + } + + public static class Builder { + + private Set entries = Sets.newLinkedHashSet(); + + /** + * @see MemoryUsageDetails#getEntries + */ + public Builder entries(Set entries) { + this.entries = Sets.newLinkedHashSet(checkNotNull(entries, "entries")); + return this; + } + + public Builder addEntry(ComputePoolMemoryUsageDetailSummaryEntry entry) { + entries.add(checkNotNull(entry,"entry")); + return this; + } + + public MemoryUsageDetails build() { + return new MemoryUsageDetails(entries); + } + + public Builder fromMemoryUsageDetails(MemoryUsageDetails in) { + return entries(in.getEntries()); + } + } + + private MemoryUsageDetails() { + //For JAXB and builder use + } + + private MemoryUsageDetails(Set entries) { + this.entries = Sets.newLinkedHashSet(entries); + } + + @XmlElement(name = "MemoryUsageDetail", required=false) + private Set entries = Sets.newLinkedHashSet(); + + public Set getEntries() { + return Collections.unmodifiableSet(entries); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MemoryUsageDetails tasks1 = (MemoryUsageDetails) o; + + if (!entries.equals(tasks1.entries)) return false; + + return true; + } + + @Override + public int hashCode() { + return entries.hashCode(); + } + + public String toString() { + return "["+ entries.toString()+"]"; + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClient.java index aa49071228..6349028949 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClient.java @@ -26,6 +26,7 @@ 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; import org.jclouds.tmrk.enterprisecloud.domain.resource.cpu.ComputePoolCpuUsageDetail; +import org.jclouds.tmrk.enterprisecloud.domain.resource.memory.ComputePoolMemoryUsage; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -80,4 +81,13 @@ public interface ResourceAsyncClient { @JAXBResponseParser @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture getComputePoolCpuUsageDetail(@EndpointParam URI uri); + + /** + * @see ResourceClient#getComputePoolMemoryUsage + */ + @GET + @Consumes("application/vnd.tmrk.cloud.computePoolMemoryUsage") + @JAXBResponseParser + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getComputePoolMemoryUsage(@EndpointParam URI uri); } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClient.java index 3d7ee76268..c609efa8a7 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClient.java @@ -23,6 +23,7 @@ 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; import org.jclouds.tmrk.enterprisecloud.domain.resource.cpu.ComputePoolCpuUsageDetail; +import org.jclouds.tmrk.enterprisecloud.domain.resource.memory.ComputePoolMemoryUsage; import java.net.URI; import java.util.concurrent.TimeUnit; @@ -96,4 +97,16 @@ public interface ResourceClient { */ ComputePoolCpuUsageDetail getComputePoolCpuUsageDetail(URI uri); + /** + * The Get Resources Usage Memory call returns information regarding memory usage + * for a specified compute pool defined in an environment at five minute intervals + * for the 24 hours ending one hour prior to the current time, rounded later. + * For example, if current time is 11:22, the end time 10:25. + * + * @param uri the uri of the call based upon the compute pool + * e.g. /cloudapi/ecloud/computepools/{id}/usage/memory + * @return The memory usage for the compute pool + */ + ComputePoolMemoryUsage getComputePoolMemoryUsage(URI uri); + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClientTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClientTest.java index cdd41609ef..3895022d5f 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClientTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceAsyncClientTest.java @@ -98,6 +98,21 @@ public class ResourceAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncCl checkFilters(httpRequest); } + public void testGetComputePoolMemoryUsage() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = ResourceAsyncClient.class.getMethod("getComputePoolMemoryUsage", URI.class); + HttpRequest httpRequest = processor.createRequest(method, URI.create("/cloudapi/ecloud/computepools/89/usage/memory")); + + assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/computepools/89/usage/memory HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, + "Accept: application/vnd.tmrk.cloud.computePoolMemoryUsage\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> createTypeLiteral() { return new TypeLiteral>() { diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClientLiveTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClientLiveTest.java index e807e969b1..84d41d7906 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClientLiveTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/ResourceClientLiveTest.java @@ -24,6 +24,7 @@ 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; import org.jclouds.tmrk.enterprisecloud.domain.resource.cpu.ComputePoolCpuUsageDetail; +import org.jclouds.tmrk.enterprisecloud.domain.resource.memory.ComputePoolMemoryUsage; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; @@ -67,14 +68,14 @@ public class ResourceClientLiveTest extends BaseTerremarkEnterpriseCloudClientLi } public void testGetComputePoolCpuUsage() throws Exception { - ComputePoolCpuUsage cpuUsage = client.getComputePoolCpuUsage(URI.create("/cloudapi/ecloud/computepools/89/usage/cpu")); - for(Link link: cpuUsage.getLinks()) { + ComputePoolCpuUsage usage = client.getComputePoolCpuUsage(URI.create("/cloudapi/ecloud/computepools/89/usage/cpu")); + assertNotNull(usage); + for(Link link: usage.getLinks()) { //The compute pool cpu usage has a link to a detail entry if( "application/vnd.tmrk.cloud.computePoolCpuUsageDetail".equals(link.getType())) { testGetComputePoolCpuUsageDetail(link.getHref()); } } - assertNotNull(cpuUsage); } public void testMissingComputePoolCpuUsage() { @@ -96,4 +97,20 @@ public class ResourceClientLiveTest extends BaseTerremarkEnterpriseCloudClientLi assertEquals(detail.getValue(), ResourceCapacity.builder().value(0).unit("MHz").build()); assertNull(detail.getVirtualMachinesCpuUsage()); } + + public void testGetComputePoolMemoryUsage() throws Exception { + ComputePoolMemoryUsage usage = client.getComputePoolMemoryUsage(URI.create("/cloudapi/ecloud/computepools/89/usage/memory")); + assertNotNull(usage); + for(Link link: usage.getLinks()) { + //The compute pool memory usage has a link to a detail entry + if( "application/vnd.tmrk.cloud.computePoolMemoryUsageDetail".equals(link.getType())) { + testGetComputePoolMemoryUsageDetail(link.getHref()); + } + } + } + + private void testGetComputePoolMemoryUsageDetail(URI uri) { + // ComputePoolMemoryUsageDetail detail = client.getComputePoolMemoryUsageDetail(uri); + // assertNotNull(detail.getTime()); + } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/ComputePoolMemoryUsageJAXBParsingTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/ComputePoolMemoryUsageJAXBParsingTest.java new file mode 100644 index 0000000000..36b9710258 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/ComputePoolMemoryUsageJAXBParsingTest.java @@ -0,0 +1,145 @@ +/** + * 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.Link; +import org.jclouds.tmrk.enterprisecloud.domain.resource.memory.ComputePoolMemoryUsage; +import org.jclouds.tmrk.enterprisecloud.domain.resource.memory.ComputePoolMemoryUsageDetailSummaryEntry; +import org.jclouds.tmrk.enterprisecloud.domain.resource.memory.MemoryUsageDetails; +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 ComputePoolMemoryUsage + * + * @author Jason King + */ +@Test(groups = "unit", testName = "ComputePoolMemoryUsageJAXBParsingTest") +public class ComputePoolMemoryUsageJAXBParsingTest extends BaseRestClientTest { + + private SimpleDateFormatDateService dateService; + @BeforeMethod + public void setUp() { + dateService = new SimpleDateFormatDateService(); + } + + @BeforeClass + void setupFactory() { + RestContextSpec contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo", + "credentialFoo", String.class, Integer.class, + ImmutableSet.of(new MockModule(), new NullLoggingModule(), new AbstractModule() { + + @Override + protected void configure() { + } + + @SuppressWarnings("unused") + @Provides + @Named("exception") + Set 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("getComputePoolMemoryUsage", URI.class); + HttpRequest request = factory(ResourceAsyncClient.class).createRequest(method,new URI("/1")); + assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class); + + Function parser = (Function) RestAnnotationProcessor + .createResponseParser(parserFactory, injector, method, request); + + InputStream is = getClass().getResourceAsStream("/computePoolMemoryUsage.xml"); + ComputePoolMemoryUsage memoryUsage = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is))); + + assertLinks(memoryUsage.getLinks()); + assertEquals(memoryUsage.getStartTime(), dateService.iso8601DateParse("2011-12-05T15:55:00.0Z")); + assertEquals(memoryUsage.getEndTime(), dateService.iso8601DateParse("2011-12-06T15:55:00.0Z")); + assertDetails(memoryUsage.getDetails()); + + Set entries = memoryUsage.getDetails().getEntries(); + ComputePoolMemoryUsageDetailSummaryEntry first = Iterables.getFirst(entries, null); + assertEquals(memoryUsage.getStartTime(),first.getTime()); + + ComputePoolMemoryUsageDetailSummaryEntry last = Iterables.getLast(entries, null); + assertEquals(memoryUsage.getEndTime(),last.getTime()); + } + + private void assertLinks(Set links) { + assertEquals(links.size(),2); + Link link = Iterables.get(links, 0); + assertEquals(link.getName(),"Default Compute Pool"); + assertEquals(link.getRelationship(), Link.Relationship.UP); + + Link link2 = Iterables.get(links, 1); + assertEquals(link2.getHref(), URI.create("/cloudapi/ecloud/computepools/89/usage/memory/details?time=2011-12-05t15%3a55%3a00z")); + assertEquals(link2.getType(), "application/vnd.tmrk.cloud.computePoolMemoryUsageDetail"); + assertEquals(link2.getRelationship(), Link.Relationship.DOWN); + } + + private void assertDetails(MemoryUsageDetails details) { + assertEquals(details.getEntries().size(), 289); + for(ComputePoolMemoryUsageDetailSummaryEntry entry: details.getEntries()) { + assertDetail(entry); + } + } + + private void assertDetail(ComputePoolMemoryUsageDetailSummaryEntry entry) { + assertNotNull(entry.getTime()); + assertNotNull(entry.getValue()); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/resources/computePoolMemoryUsage.xml b/sandbox-providers/tmrk-enterprisecloud/src/test/resources/computePoolMemoryUsage.xml new file mode 100644 index 0000000000..8e1ad1035a --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/resources/computePoolMemoryUsage.xml @@ -0,0 +1,2039 @@ + + + + + + 2011-12-05T15:55:00Z + 2011-12-06T15:55:00Z + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + + + MB + 403 + + + + \ No newline at end of file