From 3494e055414d8a7c63fedf87355c20a46e3becb6 Mon Sep 17 00:00:00 2001 From: Chris Tavares Date: Wed, 12 Dec 2012 16:25:50 -0800 Subject: [PATCH 1/3] Exposing links from ODataEntity class --- .../media/implementation/ODataEntity.java | 64 ++++++++++ .../implementation/LinkRetrievalTest.java | 112 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java index 4d3ce09d1dc9..3cea0cd4c1ad 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java @@ -17,11 +17,14 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; import javax.xml.bind.JAXBElement; import com.microsoft.windowsazure.services.media.implementation.atom.ContentType; import com.microsoft.windowsazure.services.media.implementation.atom.EntryType; +import com.microsoft.windowsazure.services.media.implementation.atom.LinkType; import com.microsoft.windowsazure.services.media.implementation.content.Constants; import com.microsoft.windowsazure.services.media.models.ListResult; @@ -40,6 +43,7 @@ protected ODataEntity(EntryType entry, T content) { this.content = content; } + @SuppressWarnings({ "unchecked", "rawtypes" }) protected ODataEntity(T content) { this.content = content; @@ -65,6 +69,66 @@ protected T getContent() { return content; } + /** + * Test if the entity contains a link with the given title. + * + * @param title + * Title of link to check for + * @return True if link is included, false if not. + */ + public boolean hasLink(String title) { + return getLink(title) != null; + } + + /** + * Get the link with the given title + * + * @param title + * Title of link to retrieve + * @return The link if found, null if not. + */ + public LinkType getLink(String title) { + for (Object child : entry.getEntryChildren()) { + + LinkType link = LinkFromChild(child); + if (link != null && link.getTitle().equals(title)) { + return link; + } + } + return null; + } + + /** + * Return the links from this entry + * + * @return List of the links. + */ + public List getLinks() { + ArrayList links = new ArrayList(); + for (Object child : entry.getEntryChildren()) { + LinkType link = LinkFromChild(child); + if (link != null) { + links.add(link); + } + } + return links; + } + + @SuppressWarnings("rawtypes") + private static LinkType LinkFromChild(Object child) { + if (child instanceof JAXBElement) { + return LinkFromElement((JAXBElement) child); + } + return null; + } + + private static LinkType LinkFromElement(@SuppressWarnings("rawtypes") JAXBElement element) { + if (element.getDeclaredType() == LinkType.class) { + return (LinkType) element.getValue(); + } + return null; + } + /** * Is the given type inherited from ODataEntity * diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java new file mode 100644 index 000000000000..7e4180eb6da2 --- /dev/null +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java @@ -0,0 +1,112 @@ +/** + * Copyright 2012 Microsoft Corporation + * + * Licensed 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 com.microsoft.windowsazure.services.media.implementation; + +import static org.junit.Assert.*; + +import java.util.List; + +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; + +import org.junit.Before; +import org.junit.Test; + +import com.microsoft.windowsazure.services.media.implementation.atom.ContentType; +import com.microsoft.windowsazure.services.media.implementation.atom.EntryType; +import com.microsoft.windowsazure.services.media.implementation.atom.LinkType; +import com.microsoft.windowsazure.services.media.implementation.content.Constants; +import com.microsoft.windowsazure.services.media.implementation.content.MediaProcessorType; +import com.microsoft.windowsazure.services.media.models.MediaProcessorInfo; + +/** + * Testing retrieval of links from ATOM entities + * + */ +public class LinkRetrievalTest { + private static QName linkName = new QName("link", Constants.ATOM_NS); + private MediaProcessorInfo info; + private LinkType link1; + private LinkType link2; + + @Before + public void setup() { + EntryType entry = new EntryType(); + + link1 = new LinkType(); + link1.setTitle("someLink"); + link1.setRel("Related/something"); + link1.setHref("some/uri/somewhere"); + + link2 = new LinkType(); + link2.setTitle("someOtherLink"); + link2.setRel("Related/else"); + link2.setHref("some/other/href/somewhere"); + + entry.getEntryChildren().add(new JAXBElement(linkName, LinkType.class, link1)); + entry.getEntryChildren().add(new JAXBElement(linkName, LinkType.class, link2)); + + MediaProcessorType payload = new MediaProcessorType().setId("DummyId").setName("Dummy Name") + .setVersion("0.0.0").setVendor("Contoso").setSku("sku skiddo").setDescription("For testing links only"); + + ContentType contentElement = new ContentType(); + contentElement.getContent().add( + new JAXBElement(Constants.ODATA_PROPERTIES_ELEMENT_NAME, MediaProcessorType.class, + payload)); + + entry.getEntryChildren().add( + new JAXBElement(Constants.ATOM_CONTENT_ELEMENT_NAME, ContentType.class, contentElement)); + + info = new MediaProcessorInfo(entry, payload); + } + + @Test + public void canRetrieveSingleLinkFromEntity() { + assertTrue(info.hasLink("someLink")); + } + + @Test + public void getFalseWhenLinkIsntThere() { + assertFalse(info.hasLink("noSuchLink")); + } + + @Test + public void canRetrieveEntireLinkByTitle() { + LinkType link = info.getLink("someOtherLink"); + + assertLinksEqual(link2, link); + } + + @Test + public void getNullWhenLinkIsntThere() { + assertNull(info.getLink("noSuchLink")); + } + + @Test + public void getLinksReturnsTwoExpectedLinksInOrder() { + List links = info.getLinks(); + + assertEquals(2, links.size()); + assertLinksEqual(link1, links.get(0)); + assertLinksEqual(link2, links.get(1)); + } + + private static void assertLinksEqual(LinkType expected, LinkType actual) { + assertEquals(expected.getTitle(), actual.getTitle()); + assertEquals(expected.getRel(), actual.getRel()); + assertEquals(expected.getHref(), actual.getHref()); + } +} From c1e21262eac469d936c55e2c7f02fa79765455c2 Mon Sep 17 00:00:00 2001 From: Chris Tavares Date: Wed, 12 Dec 2012 16:34:01 -0800 Subject: [PATCH 2/3] Looking for links based on rel, not title --- .../media/implementation/ODataEntity.java | 22 +++++++++---------- .../implementation/LinkRetrievalTest.java | 6 ++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java index 3cea0cd4c1ad..a77f800b6c0f 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java @@ -70,28 +70,28 @@ protected T getContent() { } /** - * Test if the entity contains a link with the given title. + * Test if the entity contains a link with the given rel attribute. * - * @param title - * Title of link to check for - * @return True if link is included, false if not. + * @param rel + * Rel of link to check for + * @return True if link is found, false if not. */ - public boolean hasLink(String title) { - return getLink(title) != null; + public boolean hasLink(String rel) { + return getLink(rel) != null; } /** - * Get the link with the given title + * Get the link with the given rel attribute * - * @param title - * Title of link to retrieve + * @param rel + * rel of link to retrieve * @return The link if found, null if not. */ - public LinkType getLink(String title) { + public LinkType getLink(String rel) { for (Object child : entry.getEntryChildren()) { LinkType link = LinkFromChild(child); - if (link != null && link.getTitle().equals(title)) { + if (link != null && link.getRel().equals(rel)) { return link; } } diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java index 7e4180eb6da2..a0dac584bcdc 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java @@ -75,7 +75,7 @@ public void setup() { @Test public void canRetrieveSingleLinkFromEntity() { - assertTrue(info.hasLink("someLink")); + assertTrue(info.hasLink(link1.getRel())); } @Test @@ -84,8 +84,8 @@ public void getFalseWhenLinkIsntThere() { } @Test - public void canRetrieveEntireLinkByTitle() { - LinkType link = info.getLink("someOtherLink"); + public void canRetrieveEntireLinkByRel() { + LinkType link = info.getLink(link2.getRel()); assertLinksEqual(link2, link); } From 8cdcb8bdb71152b09c3b9d5ab55cd40f7feb3afb Mon Sep 17 00:00:00 2001 From: Chris Tavares Date: Wed, 12 Dec 2012 16:41:28 -0800 Subject: [PATCH 3/3] Exposing immutable LinkInfo from public API, with only properties OData sets. --- .../media/implementation/ODataEntity.java | 11 +-- .../services/media/models/LinkInfo.java | 69 +++++++++++++++++++ .../implementation/LinkRetrievalTest.java | 8 ++- 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/LinkInfo.java diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java index a77f800b6c0f..fa1a382f7211 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java @@ -26,6 +26,7 @@ import com.microsoft.windowsazure.services.media.implementation.atom.EntryType; import com.microsoft.windowsazure.services.media.implementation.atom.LinkType; import com.microsoft.windowsazure.services.media.implementation.content.Constants; +import com.microsoft.windowsazure.services.media.models.LinkInfo; import com.microsoft.windowsazure.services.media.models.ListResult; /** @@ -87,12 +88,12 @@ public boolean hasLink(String rel) { * rel of link to retrieve * @return The link if found, null if not. */ - public LinkType getLink(String rel) { + public LinkInfo getLink(String rel) { for (Object child : entry.getEntryChildren()) { LinkType link = LinkFromChild(child); if (link != null && link.getRel().equals(rel)) { - return link; + return new LinkInfo(link); } } return null; @@ -103,12 +104,12 @@ public LinkType getLink(String rel) { * * @return List of the links. */ - public List getLinks() { - ArrayList links = new ArrayList(); + public List getLinks() { + ArrayList links = new ArrayList(); for (Object child : entry.getEntryChildren()) { LinkType link = LinkFromChild(child); if (link != null) { - links.add(link); + links.add(new LinkInfo(link)); } } return links; diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/LinkInfo.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/LinkInfo.java new file mode 100644 index 000000000000..0acf7e6b2e43 --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/LinkInfo.java @@ -0,0 +1,69 @@ +/** + * Copyright 2012 Microsoft Corporation + * + * Licensed 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 com.microsoft.windowsazure.services.media.models; + +import com.microsoft.windowsazure.services.media.implementation.atom.LinkType; + +/** + * Provides access to OData links + * + */ +public class LinkInfo { + private final LinkType rawLink; + + /** + * Construct a new {@link LinkInfo} instance + */ + public LinkInfo(LinkType rawLink) { + this.rawLink = rawLink; + } + + /** + * Get link rel + * + * @return the rel + */ + public String getRel() { + return rawLink.getRel(); + } + + /** + * Get link type + * + * @return the type + */ + public String getType() { + return rawLink.getType(); + } + + /** + * Get link href + * + * @return the href + */ + public String getHref() { + return rawLink.getHref(); + } + + /** + * Get link title + * + * @return the title + */ + public String getTitle() { + return rawLink.getTitle(); + } +} diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java index a0dac584bcdc..56ff698b68c3 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java @@ -30,6 +30,7 @@ import com.microsoft.windowsazure.services.media.implementation.atom.LinkType; import com.microsoft.windowsazure.services.media.implementation.content.Constants; import com.microsoft.windowsazure.services.media.implementation.content.MediaProcessorType; +import com.microsoft.windowsazure.services.media.models.LinkInfo; import com.microsoft.windowsazure.services.media.models.MediaProcessorInfo; /** @@ -85,7 +86,7 @@ public void getFalseWhenLinkIsntThere() { @Test public void canRetrieveEntireLinkByRel() { - LinkType link = info.getLink(link2.getRel()); + LinkInfo link = info.getLink(link2.getRel()); assertLinksEqual(link2, link); } @@ -97,16 +98,17 @@ public void getNullWhenLinkIsntThere() { @Test public void getLinksReturnsTwoExpectedLinksInOrder() { - List links = info.getLinks(); + List links = info.getLinks(); assertEquals(2, links.size()); assertLinksEqual(link1, links.get(0)); assertLinksEqual(link2, links.get(1)); } - private static void assertLinksEqual(LinkType expected, LinkType actual) { + private static void assertLinksEqual(LinkType expected, LinkInfo actual) { assertEquals(expected.getTitle(), actual.getTitle()); assertEquals(expected.getRel(), actual.getRel()); assertEquals(expected.getHref(), actual.getHref()); + assertEquals(expected.getType(), actual.getType()); } }