From 84b5cfc8dc3b27dc2d375332ca5ac388b9a36bc9 Mon Sep 17 00:00:00 2001 From: Jason King Date: Tue, 20 Dec 2011 14:51:59 +0000 Subject: [PATCH] Issue 695: Added URISource and Function to get it. Added URISource to BaseResource --- .../domain/internal/BaseResource.java | 11 +++- .../enterprisecloud/functions/URISource.java | 35 +++++++++++ .../domain/internal/NamedResourceTest.java | 44 +++++++++++++ .../enterprisecloud/functions/GetURITest.java | 62 +++++++++++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/functions/URISource.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/internal/NamedResourceTest.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/functions/GetURITest.java diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/internal/BaseResource.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/internal/BaseResource.java index 20e69472af..6e98eacc3a 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/internal/BaseResource.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/internal/BaseResource.java @@ -18,6 +18,8 @@ */ package org.jclouds.tmrk.enterprisecloud.domain.internal; +import org.jclouds.tmrk.enterprisecloud.functions.URISource; + import javax.xml.bind.annotation.XmlAttribute; import java.net.URI; import java.util.Map; @@ -30,7 +32,7 @@ import static com.google.common.base.Preconditions.checkNotNull; * @author Adrian Cole * */ -public class BaseResource> { +public class BaseResource> implements URISource { public static > Builder builder() { return new Builder(); @@ -106,6 +108,13 @@ public class BaseResource> { return href; } + /** + * @see #getHref + */ + public URI getURI() { + return getHref(); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/functions/URISource.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/functions/URISource.java new file mode 100644 index 0000000000..c84660af5b --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/functions/URISource.java @@ -0,0 +1,35 @@ +package org.jclouds.tmrk.enterprisecloud.functions; + +import com.google.common.base.Function; + +import java.net.URI; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Defines the URI Source interface and a function that uses it to obtain the URI + * @author Jason King + */ +public interface URISource { + + URI getURI(); + + /** + * Converts a URI Source to a URI + */ + public class GetURI implements Function { + + /** + * Expects to be called with an object implementing the URISource interface + * Calls the getURI method to obtain the URI. + * @param source a URISource + * @return the URI + */ + @Override + public URI apply(Object source) { + checkNotNull(source,"source"); + URISource uriSource = URISource.class.cast(source); + return uriSource.getURI(); + } + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/internal/NamedResourceTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/internal/NamedResourceTest.java new file mode 100644 index 0000000000..8252b728c3 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/domain/internal/NamedResourceTest.java @@ -0,0 +1,44 @@ +/** + * 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.internal; + +import org.jclouds.tmrk.enterprisecloud.domain.NamedResource; +import org.jclouds.tmrk.enterprisecloud.functions.URISource; +import org.testng.annotations.Test; + +import java.net.URI; +import java.net.URISyntaxException; + +import static org.testng.Assert.assertEquals; + +/** + * @author Jason King + */ +@Test(groups = "unit", testName = "BaseResourceTest") +public class NamedResourceTest { + + /** + * Tests getURI on BaseResource via NamedResource subclass + */ + public void testGetURI() throws URISyntaxException { + URI uri = URI.create("/dev/null"); + URISource source = NamedResource.builder().href(uri).build(); + assertEquals(uri,source.getURI()); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/functions/GetURITest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/functions/GetURITest.java new file mode 100644 index 0000000000..97b4d5c395 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/functions/GetURITest.java @@ -0,0 +1,62 @@ +/** + * 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.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.net.URI; + +import static org.testng.Assert.assertEquals; + +/** + * + * @author Jason King + */ +@Test(groups = "unit", testName = "GetURITest") +public class GetURITest { + + private URISource.GetURI function; + + @BeforeMethod + public void setUp() { + function = new URISource.GetURI(); + } + + public void testApply() { + URI expected = URI.create("/dev/null"); + URISource source = new TestURISource(expected); + URI result = function.apply(source); + assertEquals(result,expected); + } + + private static class TestURISource implements URISource { + + private URI expected; + + public TestURISource(URI expected) { + this.expected = expected; + } + + @Override + public URI getURI() { + return expected; + } + } +}