Permalink
Browse files

Merge pull request #24 from christav/job-input-output-assets-working

Exposing Links as public properties on entities
  • Loading branch information...
christav committed Dec 13, 2012
2 parents b014c4a + 8cdcb8b commit a50bed1c5b06631701aa43eee3b6ab8e6d60a2eb
@@ -17,12 +17,16 @@
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.LinkInfo;
import com.microsoft.windowsazure.services.media.models.ListResult;
/**
@@ -40,6 +44,7 @@ protected ODataEntity(EntryType entry, T content) {
this.content = content;
}
+ @SuppressWarnings({ "unchecked", "rawtypes" })
protected ODataEntity(T content) {
this.content = content;
@@ -65,6 +70,66 @@ protected T getContent() {
return content;
}
+ /**
+ * Test if the entity contains a link with the given rel attribute.
+ *
+ * @param rel
+ * Rel of link to check for
+ * @return True if link is found, false if not.
+ */
+ public boolean hasLink(String rel) {
+ return getLink(rel) != null;
+ }
+
+ /**
+ * Get the link with the given rel attribute
+ *
+ * @param rel
+ * rel of link to retrieve
+ * @return The link if found, null if not.
+ */
+ public LinkInfo getLink(String rel) {
+ for (Object child : entry.getEntryChildren()) {
+
+ LinkType link = LinkFromChild(child);
+ if (link != null && link.getRel().equals(rel)) {
+ return new LinkInfo(link);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Return the links from this entry
+ *
+ * @return List of the links.
+ */
+ public List<LinkInfo> getLinks() {
+ ArrayList<LinkInfo> links = new ArrayList<LinkInfo>();
+ for (Object child : entry.getEntryChildren()) {
+ LinkType link = LinkFromChild(child);
+ if (link != null) {
+ links.add(new LinkInfo(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
*
@@ -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();
+ }
+}
@@ -0,0 +1,114 @@
+/**
+ * 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.LinkInfo;
+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<LinkType>(linkName, LinkType.class, link1));
+ entry.getEntryChildren().add(new JAXBElement<LinkType>(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<MediaProcessorType>(Constants.ODATA_PROPERTIES_ELEMENT_NAME, MediaProcessorType.class,
+ payload));
+
+ entry.getEntryChildren().add(
+ new JAXBElement<ContentType>(Constants.ATOM_CONTENT_ELEMENT_NAME, ContentType.class, contentElement));
+
+ info = new MediaProcessorInfo(entry, payload);
+ }
+
+ @Test
+ public void canRetrieveSingleLinkFromEntity() {
+ assertTrue(info.hasLink(link1.getRel()));
+ }
+
+ @Test
+ public void getFalseWhenLinkIsntThere() {
+ assertFalse(info.hasLink("noSuchLink"));
+ }
+
+ @Test
+ public void canRetrieveEntireLinkByRel() {
+ LinkInfo link = info.getLink(link2.getRel());
+
+ assertLinksEqual(link2, link);
+ }
+
+ @Test
+ public void getNullWhenLinkIsntThere() {
+ assertNull(info.getLink("noSuchLink"));
+ }
+
+ @Test
+ public void getLinksReturnsTwoExpectedLinksInOrder() {
+ List<LinkInfo> links = info.getLinks();
+
+ assertEquals(2, links.size());
+ assertLinksEqual(link1, links.get(0));
+ assertLinksEqual(link2, links.get(1));
+ }
+
+ 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());
+ }
+}

0 comments on commit a50bed1

Please sign in to comment.