diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/core/utils/pipeline/Exports.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/core/utils/pipeline/Exports.java index d5b9ddc05ce9f..e6a97e159afbc 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/core/utils/pipeline/Exports.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/core/utils/pipeline/Exports.java @@ -73,6 +73,7 @@ public ClientConfig create(String profile, Builder builder, Map public Client create(String profile, Builder builder, Map properties) { ClientConfig clientConfig = builder.build(profile, ClientConfig.class, properties); Client client = Client.create(clientConfig); + // client.addFilter(new LoggingFilter()); return client; } }); @@ -82,7 +83,7 @@ public Client create(String profile, Builder builder, Map proper public HttpURLConnectionClient create(String profile, Builder builder, Map properties) { ClientConfig clientConfig = builder.build(profile, ClientConfig.class, properties); HttpURLConnectionClient client = HttpURLConnectionClient.create(clientConfig); - //client.addFilter(new LoggingFilter()); + // client.addFilter(new LoggingFilter()); return client; } }); diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataAtomMarshaller.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataAtomMarshaller.java index ecf5726e6e230..2d9735db12f98 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataAtomMarshaller.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataAtomMarshaller.java @@ -34,6 +34,7 @@ import com.microsoft.windowsazure.services.media.implementation.atom.EntryType; import com.microsoft.windowsazure.services.media.implementation.atom.FeedType; import com.microsoft.windowsazure.services.media.implementation.content.AccessPolicyType; +import com.microsoft.windowsazure.services.media.implementation.content.AssetFileType; import com.microsoft.windowsazure.services.media.implementation.content.AssetType; import com.microsoft.windowsazure.services.media.implementation.content.Constants; import com.microsoft.windowsazure.services.media.implementation.content.ContentKeyRestType; @@ -131,6 +132,7 @@ private static Class[] getMarshalledClasses() { classes.add(LocatorRestType.class); classes.add(TaskType.class); classes.add(ContentKeyRestType.class); + classes.add(AssetFileType.class); return classes.toArray(new Class[0]); } } diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataDateAdapter.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataDateAdapter.java new file mode 100644 index 0000000000000..51c7c368152dd --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataDateAdapter.java @@ -0,0 +1,63 @@ +/** + * 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 java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; +import java.util.regex.Pattern; + +import javax.xml.bind.DatatypeConverter; +import javax.xml.bind.annotation.adapters.XmlAdapter; + +/** + * Adapter to convert OData time zone conventions into Java + * Dates and back. + * + */ +public class ODataDateAdapter extends XmlAdapter { + + private final static Pattern hasTimezoneRegex; + private final static TimeZone utc; + + static { + hasTimezoneRegex = Pattern.compile("^.*(\\+|-)\\d\\d:\\d\\d$"); + + utc = TimeZone.getDefault(); + utc.setRawOffset(0); + } + + @Override + public Date unmarshal(String dateString) throws Exception { + if (!hasTimezone(dateString)) { + dateString += "Z"; + } + Calendar parsedDate = DatatypeConverter.parseDateTime(dateString); + return parsedDate.getTime(); + } + + @Override + public String marshal(Date date) throws Exception { + Calendar dateToMarshal = Calendar.getInstance(); + dateToMarshal.setTime(date); + dateToMarshal.setTimeZone(utc); + return DatatypeConverter.printDateTime(dateToMarshal); + } + + private boolean hasTimezone(String dateString) { + return dateString.endsWith("Z") || hasTimezoneRegex.matcher(dateString).matches(); + } +} diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/AssetFileType.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/AssetFileType.java new file mode 100644 index 0000000000000..55e9664bdff7d --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/AssetFileType.java @@ -0,0 +1,297 @@ +/** + * 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.content; + +import java.util.Date; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; + +/** + * Serialization for the AssetFile entity + * + */ + +@XmlAccessorType(XmlAccessType.FIELD) +public class AssetFileType implements MediaServiceDTO { + + @XmlElement(name = "Id", namespace = Constants.ODATA_DATA_NS) + protected String id; + + @XmlElement(name = "Name", namespace = Constants.ODATA_DATA_NS) + protected String name; + + @XmlElement(name = "ContentFileSize", namespace = Constants.ODATA_DATA_NS) + protected Long contentFileSize; + + @XmlElement(name = "ParentAssetId", namespace = Constants.ODATA_DATA_NS) + protected String parentAssetId; + + @XmlElement(name = "EncryptionVersion", namespace = Constants.ODATA_DATA_NS) + protected String encryptionVersion; + + @XmlElement(name = "EncryptionScheme", namespace = Constants.ODATA_DATA_NS) + protected String encryptionScheme; + + @XmlElement(name = "IsEncrypted", namespace = Constants.ODATA_DATA_NS) + protected Boolean isEncrypted; + + @XmlElement(name = "EncryptionKeyId", namespace = Constants.ODATA_DATA_NS) + protected String encryptionKeyId; + + @XmlElement(name = "InitializationVector", namespace = Constants.ODATA_DATA_NS) + protected String initializationVector; + + @XmlElement(name = "IsPrimary", namespace = Constants.ODATA_DATA_NS) + protected Boolean isPrimary; + + @XmlElement(name = "LastModified", namespace = Constants.ODATA_DATA_NS) + protected Date lastModified; + + @XmlElement(name = "Created", namespace = Constants.ODATA_DATA_NS) + protected Date created; + + @XmlElement(name = "MimeType", namespace = Constants.ODATA_DATA_NS) + protected String mimeType; + + @XmlElement(name = "ContentChecksum", namespace = Constants.ODATA_DATA_NS) + protected String contentChecksum; + + /** + * @return the id + */ + public String getId() { + return id; + } + + /** + * @param id + * the id to set + */ + public AssetFileType setId(String id) { + this.id = id; + return this; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public AssetFileType setName(String name) { + this.name = name; + return this; + } + + /** + * @return the contentFileSize + */ + public Long getContentFileSize() { + return contentFileSize; + } + + /** + * @param contentFileSize + * the contentFileSize to set + */ + public AssetFileType setContentFileSize(Long contentFileSize) { + this.contentFileSize = contentFileSize; + return this; + } + + /** + * @return the parentAssetId + */ + public String getParentAssetId() { + return parentAssetId; + } + + /** + * @param parentAssetId + * the parentAssetId to set + */ + public AssetFileType setParentAssetId(String parentAssetId) { + this.parentAssetId = parentAssetId; + return this; + } + + /** + * @return the encryptionVersion + */ + public String getEncryptionVersion() { + return encryptionVersion; + } + + /** + * @param encryptionVersion + * the encryptionVersion to set + */ + public AssetFileType setEncryptionVersion(String encryptionVersion) { + this.encryptionVersion = encryptionVersion; + return this; + } + + /** + * @return the encryptionScheme + */ + public String getEncryptionScheme() { + return encryptionScheme; + } + + /** + * @param encryptionScheme + * the encryptionScheme to set + */ + public AssetFileType setEncryptionScheme(String encryptionScheme) { + this.encryptionScheme = encryptionScheme; + return this; + } + + /** + * @return the isEncrypted + */ + public Boolean getIsEncrypted() { + return isEncrypted; + } + + /** + * @param isEncrypted + * the isEncrypted to set + */ + public AssetFileType setIsEncrypted(Boolean isEncrypted) { + this.isEncrypted = isEncrypted; + return this; + } + + /** + * @return the encryptionKeyId + */ + public String getEncryptionKeyId() { + return encryptionKeyId; + } + + /** + * @param encryptionKeyId + * the encryptionKeyId to set + */ + public AssetFileType setEncryptionKeyId(String encryptionKeyId) { + this.encryptionKeyId = encryptionKeyId; + return this; + } + + /** + * @return the initializationVector + */ + public String getInitializationVector() { + return initializationVector; + } + + /** + * @param initializationVector + * the initializationVector to set + */ + public AssetFileType setInitializationVector(String initializationVector) { + this.initializationVector = initializationVector; + return this; + } + + /** + * @return the isPrimary + */ + public Boolean getIsPrimary() { + return isPrimary; + } + + /** + * @param isPrimary + * the isPrimary to set + */ + public AssetFileType setIsPrimary(Boolean isPrimary) { + this.isPrimary = isPrimary; + return this; + } + + /** + * @return the lastModified + */ + public Date getLastModified() { + return lastModified; + } + + /** + * @param lastModified + * the lastModified to set + */ + public AssetFileType setLastModified(Date lastModified) { + this.lastModified = lastModified; + return this; + } + + /** + * @return the created + */ + public Date getCreated() { + return created; + } + + /** + * @param created + * the created to set + */ + public AssetFileType setCreated(Date created) { + this.created = created; + return this; + } + + /** + * @return the mimeType + */ + public String getMimeType() { + return mimeType; + } + + /** + * @param mimeType + * the mimeType to set + */ + public AssetFileType setMimeType(String mimeType) { + this.mimeType = mimeType; + return this; + } + + /** + * @return the contentChecksum + */ + public String getContentChecksum() { + return contentChecksum; + } + + /** + * @param contentChecksum + * the contentChecksum to set + */ + public AssetFileType setContentChecksum(String contentChecksum) { + this.contentChecksum = contentChecksum; + return this; + } +} diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/ObjectFactory.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/ObjectFactory.java index 624f562f76bbf..e6fb9ff83c151 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/ObjectFactory.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/ObjectFactory.java @@ -103,4 +103,13 @@ public TaskType createTaskType() { public ContentKeyRestType createContentKeyRestType() { return new ContentKeyRestType(); } + + /** + * Create an instance of {@link AssetFileType} + * + * @return a new AssetFileType instance. + */ + public AssetFileType createAssetFileType() { + return new AssetFileType(); + } } diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/package-info.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/package-info.java new file mode 100644 index 0000000000000..74e691082fe56 --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/implementation/content/package-info.java @@ -0,0 +1,22 @@ +/** + * 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. + */ + +@XmlJavaTypeAdapter(value = ODataDateAdapter.class, type = java.util.Date.class) +package com.microsoft.windowsazure.services.media.implementation.content; + +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import com.microsoft.windowsazure.services.media.implementation.ODataDateAdapter; + diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFile.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFile.java new file mode 100644 index 0000000000000..0d6e6c884f598 --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFile.java @@ -0,0 +1,336 @@ +/* + * 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 java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import com.microsoft.windowsazure.services.core.ServiceException; +import com.microsoft.windowsazure.services.media.implementation.content.AssetFileType; +import com.microsoft.windowsazure.services.media.implementation.entities.DefaultActionOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.DefaultDeleteOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.DefaultGetOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.DefaultListOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityCreationOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityDeleteOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityGetOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityListOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityOperationBase; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityOperationSingleResultBase; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityUpdateOperation; +import com.sun.jersey.api.client.GenericType; + +public class AssetFile { + private static final String ENTITY_SET = "Files"; + + // Prevent instantiation + private AssetFile() { + } + + public static Creator create(String parentAssetId, String name) { + return new Creator(parentAssetId, name); + } + + public static class Creator extends EntityOperationSingleResultBase implements + EntityCreationOperation { + private final String parentAssetId; + private final String name; + private String contentChecksum; + private Long contentFileSize; + private String encryptionKeyId; + private String encryptionScheme; + private String encryptionVersion; + private String initializationVector; + private Boolean isEncrypted; + private Boolean isPrimary; + private String mimeType; + + private Creator(String parentAssetId, String name) { + super(ENTITY_SET, AssetFileInfo.class); + this.parentAssetId = parentAssetId; + this.name = name; + } + + @Override + public Object getRequestContents() throws ServiceException { + AssetFileType content = new AssetFileType().setName(name).setParentAssetId(parentAssetId) + .setContentChecksum(contentChecksum).setContentFileSize(contentFileSize) + .setEncryptionKeyId(encryptionKeyId).setEncryptionScheme(encryptionScheme) + .setEncryptionVersion(encryptionVersion).setInitializationVector(initializationVector) + .setIsEncrypted(isEncrypted).setIsPrimary(isPrimary).setMimeType(mimeType); + + return content; + } + + /** + * @param contentChecksum + * the contentChecksum to set + */ + public Creator setContentChecksum(String contentChecksum) { + this.contentChecksum = contentChecksum; + return this; + } + + /** + * @param contentFileSize + * the contentFileSize to set + */ + public Creator setContentFileSize(Long contentFileSize) { + this.contentFileSize = contentFileSize; + return this; + } + + /** + * @param encryptionKeyId + * the encryptionKeyId to set + */ + public Creator setEncryptionKeyId(String encryptionKeyId) { + this.encryptionKeyId = encryptionKeyId; + return this; + } + + /** + * @param encryptionScheme + * the encryptionScheme to set + */ + public Creator setEncryptionScheme(String encryptionScheme) { + this.encryptionScheme = encryptionScheme; + return this; + } + + /** + * @param encryptionVersion + * the encryptionVersion to set + */ + public Creator setEncryptionVersion(String encryptionVersion) { + this.encryptionVersion = encryptionVersion; + return this; + } + + /** + * @param initializationVector + * the initializationVector to set + */ + public Creator setInitializationVector(String initializationVector) { + this.initializationVector = initializationVector; + return this; + } + + /** + * @param isEncrypted + * the isEncrypted to set + */ + public Creator setIsEncrypted(Boolean isEncrypted) { + this.isEncrypted = isEncrypted; + return this; + } + + /** + * @param isPrimary + * the isPrimary to set + */ + public Creator setIsPrimary(Boolean isPrimary) { + this.isPrimary = isPrimary; + return this; + } + + /** + * @param mimeType + * the mimeType to set + */ + public Creator setMimeType(String mimeType) { + this.mimeType = mimeType; + return this; + } + + } + + /** + * Call the CreateFileInfos action on the server for the given asset + * + * @param assetId + * asset to create file infos for + * @return The action operation object to pass to rest proxy. + * @throws UnsupportedEncodingException + */ + public static EntityActionOperation createFileInfos(String assetId) { + String encodedId; + try { + encodedId = URLEncoder.encode(assetId, "UTF-8"); + } + catch (UnsupportedEncodingException ex) { + // This can never happen unless JVM is broken + throw new RuntimeException(ex); + } + return new DefaultActionOperation("CreateFileInfos").addQueryParameter("assetid", "'" + encodedId + "'"); + } + + /** + * Call the service to get a single asset file entity + * + * @param assetFileId + * id of file to get + * @return the get operation to pass to rest proxy + */ + public static EntityGetOperation get(String assetFileId) { + return new DefaultGetOperation(ENTITY_SET, assetFileId, AssetFileInfo.class); + } + + /** + * Calls the service to list all files + * + * @return The list operation to pass to rest proxy. + */ + public static EntityListOperation list() { + return new DefaultListOperation(ENTITY_SET, new GenericType>() { + }); + } + + /** + * Calls the service to list files for an asset + * + * @param assetId + * asset to list files for + * @return the list operation object + */ + public static EntityListOperation list(String assetId) { + String assetUri = new EntityOperationBase.EntityIdUriBuilder("Assets", assetId).getUri() + "/Files"; + return new DefaultListOperation(assetUri, new GenericType>() { + }); + } + + public static Updater update(String assetFileId) { + return new Updater(assetFileId); + } + + public static class Updater extends EntityOperationBase implements EntityUpdateOperation { + private String contentChecksum; + private Long contentFileSize; + private String encryptionKeyId; + private String encryptionScheme; + private String encryptionVersion; + private String initializationVector; + private Boolean isEncrypted; + private Boolean isPrimary; + private String mimeType; + + private Updater(String assetFileId) { + super(new EntityOperationBase.EntityIdUriBuilder(ENTITY_SET, assetFileId)); + } + + @Override + public Object getRequestContents() { + return new AssetFileType().setContentChecksum(contentChecksum).setContentFileSize(contentFileSize) + .setEncryptionKeyId(encryptionKeyId).setEncryptionScheme(encryptionScheme) + .setEncryptionVersion(encryptionVersion).setInitializationVector(initializationVector) + .setIsEncrypted(isEncrypted).setIsPrimary(isPrimary).setMimeType(mimeType); + } + + /** + * @param contentChecksum + * the contentChecksum to set + */ + public Updater setContentChecksum(String contentChecksum) { + this.contentChecksum = contentChecksum; + return this; + } + + /** + * @param contentFileSize + * the contentFileSize to set + */ + public Updater setContentFileSize(Long contentFileSize) { + this.contentFileSize = contentFileSize; + return this; + } + + /** + * @param encryptionKeyId + * the encryptionKeyId to set + */ + public Updater setEncryptionKeyId(String encryptionKeyId) { + this.encryptionKeyId = encryptionKeyId; + return this; + } + + /** + * @param encryptionScheme + * the encryptionScheme to set + */ + public Updater setEncryptionScheme(String encryptionScheme) { + this.encryptionScheme = encryptionScheme; + return this; + } + + /** + * @param encryptionVersion + * the encryptionVersion to set + */ + public Updater setEncryptionVersion(String encryptionVersion) { + this.encryptionVersion = encryptionVersion; + return this; + } + + /** + * @param initializationVector + * the initializationVector to set + */ + public Updater setInitializationVector(String initializationVector) { + this.initializationVector = initializationVector; + return this; + } + + /** + * @param isEncrypted + * the isEncrypted to set + */ + public Updater setIsEncrypted(Boolean isEncrypted) { + this.isEncrypted = isEncrypted; + return this; + } + + /** + * @param isPrimary + * the isPrimary to set + */ + public Updater setIsPrimary(Boolean isPrimary) { + this.isPrimary = isPrimary; + return this; + } + + /** + * @param mimeType + * the mimeType to set + */ + public Updater setMimeType(String mimeType) { + this.mimeType = mimeType; + return this; + } + + } + + /** + * Calls the service to delete an asset file entity + * + * @param assetFileId + * file to delete + * @return the delete operation object + */ + public static EntityDeleteOperation delete(String assetFileId) { + return new DefaultDeleteOperation(ENTITY_SET, assetFileId); + } +} diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFileInfo.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFileInfo.java new file mode 100644 index 0000000000000..b4ca2ee5add04 --- /dev/null +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFileInfo.java @@ -0,0 +1,162 @@ +/* + * 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 java.util.Date; + +import com.microsoft.windowsazure.services.media.implementation.ODataEntity; +import com.microsoft.windowsazure.services.media.implementation.atom.EntryType; +import com.microsoft.windowsazure.services.media.implementation.content.AssetFileType; + +/** + * The Class AssetFileInfo. + */ +public class AssetFileInfo extends ODataEntity { + + public AssetFileInfo(EntryType entry, AssetFileType content) { + super(entry, content); + } + + public AssetFileInfo() { + super(new AssetFileType()); + } + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + return this.getContent().getId(); + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return this.getContent().getName(); + } + + /** + * Gets the content file size. + * + * @return the content file size + */ + public Long getContentFileSize() { + return this.getContent().getContentFileSize(); + } + + /** + * Gets the parent asset id. + * + * @return the parent asset id + */ + public String getParentAssetId() { + return this.getContent().getParentAssetId(); + } + + /** + * Gets the encryption version. + * + * @return the encryption version + */ + public String getEncryptionVersion() { + return this.getContent().getEncryptionVersion(); + } + + /** + * Gets the encryption scheme. + * + * @return the encryption scheme + */ + public String getEncryptionScheme() { + return this.getContent().getEncryptionScheme(); + } + + /** + * Gets the checks if is encrypted. + * + * @return the checks if is encrypted + */ + public Boolean getIsEncrypted() { + return this.getContent().getIsEncrypted(); + } + + /** + * Gets the encryption key id. + * + * @return the encryption key id + */ + public String getEncryptionKeyId() { + return this.getContent().getEncryptionKeyId(); + } + + /** + * Gets the initialization vector. + * + * @return the initialization vector + */ + public String getInitializationVector() { + return this.getContent().getInitializationVector(); + } + + /** + * Gets the checks if is primary. + * + * @return the checks if is primary + */ + public Boolean getIsPrimary() { + return this.getContent().getIsPrimary(); + } + + /** + * Gets the last modified. + * + * @return the last modified + */ + public Date getLastModified() { + return this.getContent().getLastModified(); + } + + /** + * Gets the created. + * + * @return the created + */ + public Date getCreated() { + return this.getContent().getCreated(); + } + + /** + * Gets the mime type. + * + * @return the mime type + */ + public String getMimeType() { + return this.getContent().getMimeType(); + } + + /** + * Gets the content checksum. + * + * @return the content checksum + */ + public String getContentChecksum() { + return this.getContent().getContentChecksum(); + } +} diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/FileInfo.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/FileInfo.java deleted file mode 100644 index 9585a76233a67..0000000000000 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/FileInfo.java +++ /dev/null @@ -1,373 +0,0 @@ -/* - * 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 java.util.Date; - -/** - * The Class FileInfo. - */ -public class FileInfo { - - /** The id. */ - private String id; - - /** The name. */ - private String name; - - /** The content file size. */ - private int contentFileSize; - - /** The parent asset id. */ - private String parentAssetId; - - /** The encryption version. */ - private String encryptionVersion; - - /** The encryption scheme. */ - private String encryptionScheme; - - /** The is encrypted. */ - private Boolean isEncrypted; - - /** The encryption key id. */ - private String encryptionKeyId; - - /** The initialization vector. */ - private String initializationVector; - - /** The is primary. */ - private Boolean isPrimary; - - /** The last modified. */ - private Date lastModified; - - /** The created. */ - private Date created; - - /** The mime type. */ - private String mimeType; - - /** The content checksum. */ - private String contentChecksum; - - /** - * Gets the id. - * - * @return the id - */ - public String getId() { - return this.id; - } - - /** - * Sets the id. - * - * @param id - * the id - * @return the file info - */ - public FileInfo setId(String id) { - this.id = id; - return this; - } - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return this.name; - } - - /** - * Sets the name. - * - * @param name - * the name - * @return the file info - */ - public FileInfo setName(String name) { - this.name = name; - return this; - } - - /** - * Gets the content file size. - * - * @return the content file size - */ - public int getContentFileSize() { - return this.contentFileSize; - } - - /** - * Sets the content file size. - * - * @param contentFileSize - * the content file size - * @return the file info - */ - public FileInfo setContentFileSize(int contentFileSize) { - this.contentFileSize = contentFileSize; - return this; - } - - /** - * Gets the parent asset id. - * - * @return the parent asset id - */ - public String getParentAssetId() { - return this.parentAssetId; - } - - /** - * Sets the parent asset id. - * - * @param parentAssetId - * the parent asset id - * @return the file info - */ - public FileInfo setParentAssetId(String parentAssetId) { - this.parentAssetId = parentAssetId; - return this; - } - - /** - * Gets the created. - * - * @return the created - */ - public Date getCreated() { - return this.created; - } - - /** - * Sets the create. - * - * @param created - * the created - * @return the file info - */ - public FileInfo setCreate(Date created) { - this.created = created; - return this; - } - - /** - * Gets the last modified. - * - * @return the last modified - */ - public Date getLastModified() { - return this.lastModified; - } - - /** - * Sets the last modified. - * - * @param lastModified - * the last modified - * @return the file info - */ - public FileInfo setLastModified(Date lastModified) { - this.lastModified = lastModified; - return this; - } - - /** - * Sets the encryption version. - * - * @param encryptionVersion - * the encryption version - * @return the file info - */ - public FileInfo setEncryptionVersion(String encryptionVersion) { - this.encryptionVersion = encryptionVersion; - return this; - } - - /** - * Gets the encryption version. - * - * @return the encryption version - */ - public String getEncryptionVersion() { - return this.encryptionVersion; - } - - /** - * Sets the encryption scheme. - * - * @param encryptionScheme - * the encryption scheme - * @return the file info - */ - public FileInfo setEncryptionScheme(String encryptionScheme) { - this.encryptionScheme = encryptionScheme; - return this; - } - - /** - * Gets the encryption scheme. - * - * @return the encryption scheme - */ - public String getEncryptionScheme() { - return this.encryptionScheme; - } - - /** - * Sets the is encrypted. - * - * @param isEncrypted - * the is encrypted - * @return the file info - */ - public FileInfo setIsEncrypted(Boolean isEncrypted) { - this.isEncrypted = isEncrypted; - return this; - } - - /** - * Gets the checks if is encrypted. - * - * @return the checks if is encrypted - */ - public Boolean getIsEncrypted() { - return this.isEncrypted; - } - - /** - * Sets the encryption key id. - * - * @param encryptionKeyId - * the encryption key id - * @return the file info - */ - public FileInfo setEncryptionKeyId(String encryptionKeyId) { - this.encryptionKeyId = encryptionKeyId; - return this; - } - - /** - * Gets the encryption key id. - * - * @return the encryption key id - */ - public String getEncryptionKeyId() { - return this.encryptionKeyId; - } - - /** - * Sets the initialization vector. - * - * @param expectedInitializationVector - * the expected initialization vector - * @return the file info - */ - public FileInfo setInitializationVector(String expectedInitializationVector) { - this.initializationVector = expectedInitializationVector; - return this; - } - - /** - * Gets the initialization vector. - * - * @return the initialization vector - */ - public String getInitializationVector() { - return this.initializationVector; - } - - /** - * Sets the is primary. - * - * @param isPrimary - * the is primary - * @return the file info - */ - public FileInfo setIsPrimary(Boolean isPrimary) { - this.isPrimary = isPrimary; - return this; - } - - /** - * Gets the checks if is primary. - * - * @return the checks if is primary - */ - public Boolean getIsPrimary() { - return this.isPrimary; - } - - /** - * Sets the created. - * - * @param created - * the created - * @return the file info - */ - public FileInfo setCreated(Date created) { - this.created = created; - return this; - } - - /** - * Sets the mime type. - * - * @param mimeType - * the mime type - * @return the file info - */ - public FileInfo setMimeType(String mimeType) { - this.mimeType = mimeType; - return this; - } - - /** - * Gets the mime type. - * - * @return the mime type - */ - public String getMimeType() { - return this.mimeType; - } - - /** - * Sets the content checksum. - * - * @param contentChecksum - * the content checksum - * @return the file info - */ - public FileInfo setContentChecksum(String contentChecksum) { - this.contentChecksum = contentChecksum; - return this; - } - - /** - * Gets the content checksum. - * - * @return the content checksum - */ - public String getContentChecksum() { - return this.contentChecksum; - } - -} diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AssetFileIntegrationTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AssetFileIntegrationTest.java new file mode 100644 index 0000000000000..093dbcf246295 --- /dev/null +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AssetFileIntegrationTest.java @@ -0,0 +1,234 @@ +/** + * 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; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.EnumSet; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.microsoft.windowsazure.services.core.ServiceException; +import com.microsoft.windowsazure.services.media.models.AccessPolicy; +import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo; +import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission; +import com.microsoft.windowsazure.services.media.models.Asset; +import com.microsoft.windowsazure.services.media.models.AssetFile; +import com.microsoft.windowsazure.services.media.models.AssetFileInfo; +import com.microsoft.windowsazure.services.media.models.AssetInfo; +import com.microsoft.windowsazure.services.media.models.ListResult; +import com.microsoft.windowsazure.services.media.models.Locator; +import com.microsoft.windowsazure.services.media.models.LocatorInfo; +import com.microsoft.windowsazure.services.media.models.LocatorType; + +public class AssetFileIntegrationTest extends IntegrationTestBase { + + // Some dummy binary data for uploading + private static byte[] firstPrimes = new byte[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; + private static byte[] onesAndZeros = new byte[] { 1, 0, 1, 0, 1, 0, 1, 0 }; + private static byte[] countingUp = new byte[] { 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + + private static final String BLOB_NAME = "primes.bin"; + private static final String BLOB_NAME_2 = "primes2.bin"; + + private static AccessPolicyInfo writePolicy; + + @BeforeClass + public static void setup() throws Exception { + IntegrationTestBase.setup(); + + writePolicy = createWritePolicy("uploadWritePolicy", 30); + } + + @Test + public void canCreateFileForUploadedBlob() throws Exception { + AssetInfo asset = createTestAsset("createFileForUploadedBlob"); + LocatorInfo locator = createLocator(writePolicy, asset, 5, 10); + WritableBlobContainerContract blobWriter = MediaService.createBlobWriter(locator); + + createAndUploadBlob(blobWriter, BLOB_NAME, firstPrimes); + + service.action(AssetFile.createFileInfos(asset.getId())); + + ListResult files = service.list(AssetFile.list()); + + assertEquals(1, files.size()); + AssetFileInfo file = files.get(0); + assertEquals(BLOB_NAME, file.getName()); + } + + @Test + public void canCreateFileEntityDirectly() throws Exception { + AssetInfo asset = createTestAsset("createFileEntityDirectly"); + LocatorInfo locator = createLocator(writePolicy, asset, 5, 10); + WritableBlobContainerContract blobWriter = MediaService.createBlobWriter(locator); + + createAndUploadBlob(blobWriter, BLOB_NAME_2, firstPrimes); + + service.create(AssetFile.create(asset.getId(), BLOB_NAME_2)); + + ListResult files = service.list(AssetFile.list(asset.getId())); + + boolean found = false; + for (AssetFileInfo file : files) { + if (file.getName().equals(BLOB_NAME_2)) { + found = true; + break; + } + } + + assertTrue(found); + } + + @Test + public void canCreateAssetWithMultipleFiles() throws Exception { + AssetInfo asset = createTestAsset("createWithMultipleFiles"); + AccessPolicyInfo policy = createWritePolicy("createWithMultipleFiles", 10); + LocatorInfo locator = createLocator(policy, asset, 5, 10); + + WritableBlobContainerContract blobWriter = MediaService.createBlobWriter(locator); + + createAndUploadBlob(blobWriter, "blob1.bin", firstPrimes); + createAndUploadBlob(blobWriter, "blob2.bin", onesAndZeros); + createAndUploadBlob(blobWriter, "blob3.bin", countingUp); + + AssetFileInfo file1 = service.create(AssetFile.create(asset.getId(), "blob1.bin").setIsPrimary(true) + .setIsEncrypted(false).setContentFileSize(new Long(firstPrimes.length))); + + AssetFileInfo file2 = service.create(AssetFile.create(asset.getId(), "blob2.bin").setIsPrimary(false) + .setIsEncrypted(false).setContentFileSize(new Long(onesAndZeros.length))); + + AssetFileInfo file3 = service.create(AssetFile.create(asset.getId(), "blob3.bin").setIsPrimary(false) + .setIsEncrypted(false).setContentFileSize(new Long(countingUp.length))); + + ListResult files = service.list(AssetFile.list(asset.getId())); + + assertEquals(3, files.size()); + + ArrayList results = new ArrayList(files); + Collections.sort(results, new Comparator() { + @Override + public int compare(AssetFileInfo o1, AssetFileInfo o2) { + return o1.getName().compareTo(o2.getName()); + } + }); + + assertAssetFileInfoEquals(file1, results.get(0)); + assertAssetFileInfoEquals(file2, results.get(1)); + assertAssetFileInfoEquals(file3, results.get(2)); + } + + @Test + public void canCreateFileAndThenUpdateIt() throws Exception { + AssetInfo asset = createTestAsset("createAndUpdate"); + AccessPolicyInfo policy = createWritePolicy("createAndUpdate", 10); + LocatorInfo locator = createLocator(policy, asset, 5, 10); + WritableBlobContainerContract blobWriter = MediaService.createBlobWriter(locator); + + createAndUploadBlob(blobWriter, "toUpdate.bin", firstPrimes); + + AssetFileInfo file = service.create(AssetFile.create(asset.getId(), "toUpdate.bin")); + + service.update(AssetFile.update(file.getId()).setMimeType("application/octet-stream")); + + AssetFileInfo fromServer = service.get(AssetFile.get(file.getId())); + + assertEquals("application/octet-stream", fromServer.getMimeType()); + } + + @Test + public void canDeleteFileFromAsset() throws Exception { + AssetInfo asset = createTestAsset("deleteFile"); + AccessPolicyInfo policy = createWritePolicy("deleteFile", 10); + LocatorInfo locator = createLocator(policy, asset, 5, 10); + WritableBlobContainerContract blobWriter = MediaService.createBlobWriter(locator); + + createAndUploadBlob(blobWriter, "todelete.bin", firstPrimes); + createAndUploadBlob(blobWriter, "tokeep.bin", onesAndZeros); + + service.action(AssetFile.createFileInfos(asset.getId())); + + ListResult originalFiles = service.list(AssetFile.list(asset.getId())); + assertEquals(2, originalFiles.size()); + + for (AssetFileInfo file : originalFiles) { + if (file.getName().equals("todelete.bin")) { + service.delete(AssetFile.delete(file.getId())); + break; + } + } + + ListResult newFiles = service.list(AssetFile.list(asset.getId())); + assertEquals(1, newFiles.size()); + assertEquals("tokeep.bin", newFiles.get(0).getName()); + } + + // + // Helper functions to create various media services entities + // + private static AssetInfo createTestAsset(String name) throws ServiceException { + return service.create(Asset.create().setName(testAssetPrefix + name)); + } + + private static AccessPolicyInfo createWritePolicy(String name, int durationInMinutes) throws ServiceException { + return service.create(AccessPolicy.create(testPolicyPrefix + name, durationInMinutes, + EnumSet.of(AccessPolicyPermission.WRITE))); + } + + private static LocatorInfo createLocator(AccessPolicyInfo accessPolicy, AssetInfo asset, int startDeltaMinutes, + int expirationDeltaMinutes) throws ServiceException { + + Date now = new Date(); + Date start = new Date(now.getTime() - (startDeltaMinutes * 60 * 1000)); + Date expire = new Date(now.getTime() + (expirationDeltaMinutes * 60 * 1000)); + + return service.create(Locator.create(accessPolicy.getId(), asset.getId(), LocatorType.SAS) + .setStartDateTime(start).setExpirationDateTime(expire)); + } + + private static void createAndUploadBlob(WritableBlobContainerContract blobWriter, String blobName, byte[] data) + throws ServiceException { + InputStream blobContent = new ByteArrayInputStream(data); + blobWriter.createBlockBlob(blobName, blobContent); + } + + // + // Assertion helpers + // + + private void assertAssetFileInfoEquals(AssetFileInfo expected, AssetFileInfo actual) { + assertEquals(expected.getId(), actual.getId()); + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getParentAssetId(), actual.getParentAssetId()); + assertEquals(expected.getIsPrimary(), actual.getIsPrimary()); + assertEquals(expected.getIsEncrypted(), actual.getIsEncrypted()); + assertEquals(expected.getEncryptionKeyId(), actual.getEncryptionKeyId()); + assertEquals(expected.getEncryptionScheme(), actual.getEncryptionScheme()); + assertEquals(expected.getEncryptionVersion(), actual.getEncryptionVersion()); + assertEquals(expected.getInitializationVector(), actual.getInitializationVector()); + assertDateApproxEquals(expected.getCreated(), actual.getCreated()); + assertDateApproxEquals(expected.getLastModified(), actual.getLastModified()); + assertEquals(expected.getMimeType(), actual.getMimeType()); + } + +} diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/IntegrationTestBase.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/IntegrationTestBase.java index 512b5e67c4e26..d7a2a9c13e846 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/IntegrationTestBase.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/IntegrationTestBase.java @@ -194,13 +194,6 @@ protected void assertDateApproxEquals(String message, Date expected, Date actual else { long diffInMilliseconds = Math.abs(expected.getTime() - actual.getTime()); - // TODO: Remove this time-zone workaround when fixed: - // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/413 - if (diffInMilliseconds > deltaInMilliseconds) { - // Just hard-code time-zone offset of 8 hours for now. - diffInMilliseconds = Math.abs(diffInMilliseconds - 8 * 60 * 60 * 1000); - } - if (diffInMilliseconds > deltaInMilliseconds) { assertEquals(message, expected, actual); } diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/ODataDateParsingTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/ODataDateParsingTest.java new file mode 100644 index 0000000000000..a6c39586bcd7a --- /dev/null +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/implementation/ODataDateParsingTest.java @@ -0,0 +1,143 @@ +/** + * Copyright 2011 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.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests around parsing dates - OData requires date + * strings without timezones be treated as UTC. + * ISO spec, and Java default, is to use local timezone. + * So we need to plug in to tweak Jaxb to use OData + * conventions. + * + */ +public class ODataDateParsingTest { + private static TimeZone utc; + private static TimeZone pst; + + @BeforeClass + public static void setupClass() { + utc = TimeZone.getDefault(); + utc.setRawOffset(0); + + pst = TimeZone.getDefault(); + pst.setRawOffset(-8 * 60 * 60 * 1000); + } + + @Test + public void canConvertDateToString() throws Exception { + + Calendar sampleTime = new GregorianCalendar(2012, 11, 28, 17, 43, 12); + sampleTime.setTimeZone(utc); + + Date sampleDate = sampleTime.getTime(); + + String formatted = new ODataDateAdapter().marshal(sampleDate); + + assertEquals("2012-12-28T17:43:12Z", formatted); + + } + + @Test + public void stringWithTimezoneRoundTripsCorrectly() throws Exception { + String exampleDate = "2012-11-28T17:43:12-08:00"; + + Date parsedTime = new ODataDateAdapter().unmarshal(exampleDate); + + Calendar expectedTime = new GregorianCalendar(2012, 10, 28, 17, 43, 12); + expectedTime.setTimeZone(pst); + + assertEquals(expectedTime.getTimeInMillis(), parsedTime.getTime()); + } + + @Test + public void stringWithUTCTimezoneRoundTripsCorrectly() throws Exception { + String exampleDate = "2012-11-28T17:43:12Z"; + + Date parsedTime = new ODataDateAdapter().unmarshal(exampleDate); + + Calendar expectedTime = new GregorianCalendar(2012, 10, 28, 17, 43, 12); + expectedTime.setTimeZone(utc); + + assertEquals(expectedTime.getTimeInMillis(), parsedTime.getTime()); + } + + @Test + public void stringWithNoTimezoneActsAsUTC() throws Exception { + String exampleDate = "2012-11-28T17:43:12"; + + Date parsedTime = new ODataDateAdapter().unmarshal(exampleDate); + + Calendar expectedTime = new GregorianCalendar(2012, 10, 28, 17, 43, 12); + expectedTime.setTimeZone(utc); + + assertEquals(expectedTime.getTimeInMillis(), parsedTime.getTime()); + } + + @Test + public void stringWithFractionalTimeReturnsCorrectMillisecondsTo100nsBoundary() throws Exception { + String exampleDate = "2012-11-28T17:43:12.1234567Z"; + + Date parsedTime = new ODataDateAdapter().unmarshal(exampleDate); + + Calendar timeToNearestSecond = Calendar.getInstance(); + timeToNearestSecond.setTimeZone(utc); + timeToNearestSecond.set(2012, 10, 28, 17, 43, 12); + timeToNearestSecond.set(Calendar.MILLISECOND, 0); + + long millis = parsedTime.getTime() - timeToNearestSecond.getTimeInMillis(); + + assertEquals(123, millis); + } + + @Test + public void stringWithFractionalTimeReturnsCorrectMillisecondsAsFractionNotCount() throws Exception { + String exampleDate = "2012-11-28T17:43:12.1Z"; + + Date parsedTime = new ODataDateAdapter().unmarshal(exampleDate); + + Calendar timeToNearestSecond = Calendar.getInstance(); + timeToNearestSecond.setTimeZone(utc); + timeToNearestSecond.set(2012, 10, 28, 17, 43, 12); + timeToNearestSecond.set(Calendar.MILLISECOND, 0); + + long millis = parsedTime.getTime() - timeToNearestSecond.getTimeInMillis(); + assertEquals(100, millis); + + } + + @Test + public void stringWithFractionalSecondsAndTimezoneOffsetParses() throws Exception { + String exampleDate = "2012-11-28T17:43:12.1-08:00"; + Date parsedTime = new ODataDateAdapter().unmarshal(exampleDate); + + Calendar expectedTime = new GregorianCalendar(2012, 10, 28, 17, 43, 12); + expectedTime.setTimeZone(pst); + expectedTime.set(Calendar.MILLISECOND, 100); + + assertEquals(expectedTime.getTimeInMillis(), parsedTime.getTime()); + + } +} diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/AssetFileEntityTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/AssetFileEntityTest.java new file mode 100644 index 0000000000000..299eb71440775 --- /dev/null +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/AssetFileEntityTest.java @@ -0,0 +1,135 @@ +package com.microsoft.windowsazure.services.media.models; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.microsoft.windowsazure.services.media.implementation.content.AssetFileType; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityActionOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityCreationOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityDeleteOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityGetOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityListOperation; +import com.microsoft.windowsazure.services.media.implementation.entities.EntityUpdateOperation; + +public class AssetFileEntityTest { + private final String exampleAssetId = "nb:cid:UUID:bfe1c840-36c3-4a78-9b63-38e6eebd94c2"; + private final String exampleFileId = "nb:cid:UUID:49a229ad-8cc5-470f-9e0b-11838283aa58"; + private final String encodedAssetId = "'nb%3Acid%3AUUID%3Abfe1c840-36c3-4a78-9b63-38e6eebd94c2'"; + private final String encodedFileId = "'nb%3Acid%3AUUID%3A49a229ad-8cc5-470f-9e0b-11838283aa58'"; + + public AssetFileEntityTest() throws Exception { + + } + + @Test + public void createFileInfosHasExpectedUri() throws Exception { + EntityActionOperation action = AssetFile.createFileInfos(exampleAssetId); + + assertEquals("CreateFileInfos", action.getUri()); + assertEquals(encodedAssetId, action.getQueryParameters().getFirst("assetid")); + } + + @Test + public void createWithOptionsSetsOptionsAsExpected() throws Exception { + String expectedName = "newFile.mp4"; + Long expectedSize = 65432L; + String expectedChecksum = "check"; + String expectedEncryptionKey = "ooh, secret!"; + String expectedEncryptionScheme = "scheme"; + String expectedEncryptionVersion = "some version"; + String expectedInitializationVector = "cross product"; + Boolean expectedIsEncrypted = true; + Boolean expectedIsPrimary = true; + String expectedMimeType = "application/octet-stream"; + + EntityCreationOperation creator = AssetFile.create(exampleAssetId, expectedName) + .setContentChecksum(expectedChecksum).setContentFileSize(expectedSize) + .setEncryptionKeyId(expectedEncryptionKey).setEncryptionScheme(expectedEncryptionScheme) + .setEncryptionVersion(expectedEncryptionVersion).setInitializationVector(expectedInitializationVector) + .setIsEncrypted(expectedIsEncrypted).setIsPrimary(expectedIsPrimary).setMimeType(expectedMimeType); + + AssetFileType payload = (AssetFileType) creator.getRequestContents(); + + assertEquals(expectedName, payload.getName()); + assertEquals(exampleAssetId, payload.getParentAssetId()); + assertEquals(expectedSize, payload.getContentFileSize()); + assertEquals(expectedChecksum, payload.getContentChecksum()); + assertEquals(expectedEncryptionKey, payload.getEncryptionKeyId()); + assertEquals(expectedEncryptionScheme, payload.getEncryptionScheme()); + assertEquals(expectedEncryptionVersion, payload.getEncryptionVersion()); + assertEquals(expectedInitializationVector, payload.getInitializationVector()); + assertEquals(expectedIsEncrypted, payload.getIsEncrypted()); + assertEquals(expectedIsPrimary, payload.getIsPrimary()); + assertEquals(expectedMimeType, payload.getMimeType()); + assertNull(payload.getCreated()); + assertNull(payload.getLastModified()); + } + + @Test + public void getByIdHasCorrectUri() throws Exception { + String expectedUri = String.format("Files(%s)", encodedFileId); + EntityGetOperation getter = AssetFile.get(exampleFileId); + + assertEquals(expectedUri, getter.getUri()); + } + + @Test + public void listFileInfosForAnAssetHasCorrectUri() throws Exception { + String expectedUri = String.format("Assets(%s)/Files", encodedAssetId); + EntityListOperation lister = AssetFile.list(exampleAssetId); + + assertEquals(expectedUri, lister.getUri()); + } + + @Test + public void listAllFileInfosHasCorrectUri() throws Exception { + String expectedUri = "Files"; + EntityListOperation lister = AssetFile.list(); + assertEquals(expectedUri, lister.getUri()); + } + + @Test + public void updateWithAllOptionsHasCorrectPayload() throws Exception { + Long expectedSize = 65432L; + String expectedChecksum = "check"; + String expectedEncryptionKey = "ooh, secret!"; + String expectedEncryptionScheme = "scheme"; + String expectedEncryptionVersion = "some version"; + String expectedInitializationVector = "cross product"; + Boolean expectedIsEncrypted = true; + Boolean expectedIsPrimary = true; + String expectedMimeType = "application/octet-stream"; + + EntityUpdateOperation updater = AssetFile.update(exampleFileId).setContentChecksum(expectedChecksum) + .setContentFileSize(expectedSize).setEncryptionKeyId(expectedEncryptionKey) + .setEncryptionScheme(expectedEncryptionScheme).setEncryptionVersion(expectedEncryptionVersion) + .setInitializationVector(expectedInitializationVector).setIsEncrypted(expectedIsEncrypted) + .setIsPrimary(expectedIsPrimary).setMimeType(expectedMimeType); + + AssetFileType payload = (AssetFileType) updater.getRequestContents(); + + assertNull(payload.getName()); + assertNull(payload.getId()); + assertNull(payload.getParentAssetId()); + assertEquals(expectedSize, payload.getContentFileSize()); + assertEquals(expectedChecksum, payload.getContentChecksum()); + assertEquals(expectedEncryptionKey, payload.getEncryptionKeyId()); + assertEquals(expectedEncryptionScheme, payload.getEncryptionScheme()); + assertEquals(expectedEncryptionVersion, payload.getEncryptionVersion()); + assertEquals(expectedInitializationVector, payload.getInitializationVector()); + assertEquals(expectedIsEncrypted, payload.getIsEncrypted()); + assertEquals(expectedIsPrimary, payload.getIsPrimary()); + assertEquals(expectedMimeType, payload.getMimeType()); + assertNull(payload.getCreated()); + assertNull(payload.getLastModified()); + } + + @Test + public void deleteHasCorrectUri() throws Exception { + String expectedUri = String.format("Files(%s)", encodedFileId); + EntityDeleteOperation deleter = AssetFile.delete(exampleFileId); + + assertEquals(expectedUri, deleter.getUri()); + } +} diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/FileInfoTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/AssetFileInfoTest.java similarity index 54% rename from microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/FileInfoTest.java rename to microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/AssetFileInfoTest.java index 3e32e0626a7ab..455fc336ebf6c 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/FileInfoTest.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/models/AssetFileInfoTest.java @@ -20,16 +20,16 @@ import org.junit.Test; -public class FileInfoTest { +import com.microsoft.windowsazure.services.media.implementation.content.AssetFileType; + +public class AssetFileInfoTest { @Test public void testGetSetId() { - // Arrange String expectedId = "testId"; - FileInfo file = new FileInfo(); + AssetFileInfo file = new AssetFileInfo(null, new AssetFileType().setId(expectedId)); - // Act - String actualId = file.setId(expectedId).getId(); + String actualId = file.getId(); // Assert assertEquals(expectedId, actualId); @@ -37,12 +37,10 @@ public void testGetSetId() { @Test public void testGetSetName() { - // Arrange String expectedName = "testName"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setName(expectedName)); - // Act - String actualName = fileInfo.setName(expectedName).getName(); + String actualName = fileInfo.getName(); // Assert assertEquals(expectedName, actualName); @@ -51,41 +49,34 @@ public void testGetSetName() { @Test public void testGetSetContentFileSize() { // Arrange - int expectedContentFileSize = 1234; - FileInfo fileInfo = new FileInfo(); + Long expectedContentFileSize = 1234l; + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setContentFileSize(expectedContentFileSize)); // Act - int actualContentFileSize = fileInfo.setContentFileSize(expectedContentFileSize).getContentFileSize(); + long actualContentFileSize = fileInfo.getContentFileSize().longValue(); // Assert - assertEquals(expectedContentFileSize, actualContentFileSize); + assertEquals(expectedContentFileSize.longValue(), actualContentFileSize); } @Test public void testGetSetParentAssetId() { - // Arrange String expectedParentAssetId = "testParentAssetId"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setParentAssetId(expectedParentAssetId)); - // Act - String actualParentAssetId = fileInfo.setParentAssetId(expectedParentAssetId).getParentAssetId(); + String actualParentAssetId = fileInfo.getParentAssetId(); - // Assert assertEquals(expectedParentAssetId, actualParentAssetId); } @Test public void testGetSetEncryptionVersion() { - // Arrange String expectedEncryptionVersion = "testEncryptionVersion"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setEncryptionVersion(expectedEncryptionVersion)); - // Act - String actualEncryptionVersion = fileInfo.setEncryptionVersion(expectedEncryptionVersion) - .getEncryptionVersion(); + String actualEncryptionVersion = fileInfo.getEncryptionVersion(); - // Assert assertEquals(expectedEncryptionVersion, actualEncryptionVersion); } @@ -93,10 +84,10 @@ public void testGetSetEncryptionVersion() { public void testGetSetEncryptionScheme() { // Arrange String expectedEncryptionScheme = "testEncryptionScheme"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setEncryptionScheme(expectedEncryptionScheme)); // Act - String actualEncryptionScheme = fileInfo.setEncryptionScheme(expectedEncryptionScheme).getEncryptionScheme(); + String actualEncryptionScheme = fileInfo.getEncryptionScheme(); // Assert assertEquals(expectedEncryptionScheme, actualEncryptionScheme); @@ -106,10 +97,10 @@ public void testGetSetEncryptionScheme() { public void testGetSetIsEncrypted() { // Arrange Boolean expectedIsEncrypted = true; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setIsEncrypted(expectedIsEncrypted)); // Act - Boolean actualIsEncrypted = fileInfo.setIsEncrypted(expectedIsEncrypted).getIsEncrypted(); + Boolean actualIsEncrypted = fileInfo.getIsEncrypted(); // Assert assertEquals(expectedIsEncrypted, actualIsEncrypted); @@ -117,28 +108,21 @@ public void testGetSetIsEncrypted() { @Test public void testGetSetEncryptionKeyId() { - // Arrange String expectedEncryptionKeyId = "testEncryptionKeyId"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setEncryptionKeyId(expectedEncryptionKeyId)); - // Act - String actualEncryptionKeyId = fileInfo.setEncryptionKeyId(expectedEncryptionKeyId).getEncryptionKeyId(); + String actualEncryptionKeyId = fileInfo.getEncryptionKeyId(); - // Assert assertEquals(expectedEncryptionKeyId, actualEncryptionKeyId); } @Test public void testGetSetInitializationVector() { - // Arrange String expectedInitializationVector = "testInitializationVector"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setInitializationVector(expectedInitializationVector)); - // Act - String actualInitializationVector = fileInfo.setInitializationVector(expectedInitializationVector) - .getInitializationVector(); + String actualInitializationVector = fileInfo.getInitializationVector(); - // Assert assertEquals(expectedInitializationVector, actualInitializationVector); } @@ -147,10 +131,10 @@ public void testGetSetInitializationVector() { public void testGetSetIsPrimary() { // Arrange Boolean expectedIsPrimary = true; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setIsPrimary(expectedIsPrimary)); // Act - Boolean actualIsPrimary = fileInfo.setIsPrimary(expectedIsPrimary).getIsPrimary(); + Boolean actualIsPrimary = fileInfo.getIsPrimary(); // Assert assertEquals(expectedIsPrimary, actualIsPrimary); @@ -158,53 +142,41 @@ public void testGetSetIsPrimary() { @Test public void testGetSetLastModified() { - // Arrange Date expectedLastModified = new Date(); - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setLastModified(expectedLastModified)); - // Act - Date actualLastModified = fileInfo.setLastModified(expectedLastModified).getLastModified(); + Date actualLastModified = fileInfo.getLastModified(); - // Assert assertEquals(expectedLastModified, actualLastModified); } @Test public void testGetSetCreated() { - // Arrange Date expectedCreated = new Date(); - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setCreated(expectedCreated)); - // Act - Date actualCreated = fileInfo.setCreated(expectedCreated).getCreated(); + Date actualCreated = fileInfo.getCreated(); - // Assert assertEquals(expectedCreated, actualCreated); } @Test public void testGetSetMimeType() { - // Arrange String expectedMimeType = "testMimeType"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setMimeType(expectedMimeType)); - // Act - String actualMimeType = fileInfo.setMimeType(expectedMimeType).getMimeType(); + String actualMimeType = fileInfo.getMimeType(); - // Assert assertEquals(expectedMimeType, actualMimeType); } @Test public void testGetSetContentChecksum() { - // Arrange String expectedContentChecksum = "testContentChecksum"; - FileInfo fileInfo = new FileInfo(); + AssetFileInfo fileInfo = new AssetFileInfo(null, new AssetFileType().setContentChecksum(expectedContentChecksum)); - // Act - String actualContentChecksum = fileInfo.setContentChecksum(expectedContentChecksum).getContentChecksum(); + String actualContentChecksum = fileInfo.getContentChecksum(); - // Assert assertEquals(expectedContentChecksum, actualContentChecksum); } } diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceMocks.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceMocks.java index 5703a8d6e4c01..cfedb89502d4c 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceMocks.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceMocks.java @@ -19,15 +19,15 @@ import java.util.List; import com.microsoft.windowsazure.services.media.models.AssetInfo; -import com.microsoft.windowsazure.services.media.models.FileInfo; +import com.microsoft.windowsazure.services.media.models.AssetFileInfo; // TODO: Remove when no longer needed. // Tracked by https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/457 class MediaServiceMocks { static class MockMediaContract { - public List getAssetFiles(String id) { - return new ArrayList(); + public List getAssetFiles(String id) { + return new ArrayList(); } public JobInfo createJob(CreateJobOptions jobOptions) { @@ -45,11 +45,11 @@ public List getJobOutputMediaAssets(String id) { public void cancelJob(String id) { } - public List getFiles() { - return new ArrayList(); + public List getFiles() { + return new ArrayList(); } - public FileInfo createFileInfo(FileInfo fi) { + public AssetFileInfo createFileInfo(AssetFileInfo fi) { return null; } } diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceValidation.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceValidation.java index 4a442a3c89b7b..c2517b8d8c8f2 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceValidation.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceValidation.java @@ -32,7 +32,7 @@ import com.microsoft.windowsazure.services.media.models.AssetInfo; import com.microsoft.windowsazure.services.media.models.AssetState; import com.microsoft.windowsazure.services.media.models.EncryptionOption; -import com.microsoft.windowsazure.services.media.models.FileInfo; +import com.microsoft.windowsazure.services.media.models.AssetFileInfo; import com.microsoft.windowsazure.services.media.models.ListResult; import com.microsoft.windowsazure.services.scenarios.MediaServiceMocks.CreateTaskOptions; import com.microsoft.windowsazure.services.scenarios.MediaServiceMocks.JobInfo; @@ -55,7 +55,7 @@ public void validateAsset(AssetInfo asset, String name, EncryptionOption encrypt assertEquals("asset.getOptions", encryption, asset.getOptions()); // Verify no files by default. - List initialFiles = serviceMock.getAssetFiles(asset.getId()); + List initialFiles = serviceMock.getAssetFiles(asset.getId()); assertNotNull("initialFiles", initialFiles); assertEquals("initialFiles.size", 0, initialFiles.size()); @@ -91,7 +91,7 @@ public void validateAssetSortedPages(List> pages, List inputFiles) throws ServiceException, IOException, NoSuchAlgorithmException { - List assetFiles = serviceMock.getAssetFiles(asset.getId()); + List assetFiles = serviceMock.getAssetFiles(asset.getId()); assertNotNull("assetFiles", assetFiles); assertEquals("assetFiles.size", inputFiles.size(), assetFiles.size()); @@ -107,11 +107,11 @@ public void validateAssetFiles(AssetInfo asset, Hashtable i // * Compare these properties: IsEncrypted, InitializationVector, EncryptionKeyId, EncryptionScheme, EncryptionVersion // Compare the asset files with all files - List allFiles = serviceMock.getFiles(); - for (FileInfo assetFile : assetFiles) { + List allFiles = serviceMock.getFiles(); + for (AssetFileInfo assetFile : assetFiles) { assertEquals("fi.getParentAssetId", asset.getId(), assetFile.getParentAssetId()); - FileInfo match = null; - for (FileInfo aFile : allFiles) { + AssetFileInfo match = null; + for (AssetFileInfo aFile : allFiles) { if (aFile.getId().equals(assetFile.getId())) { match = aFile; break; @@ -175,7 +175,7 @@ private InputStream getInputStreamWithRetry(URL file) throws InterruptedExceptio return reader; } - public void assertFileInfosEqual(String message, FileInfo fi, FileInfo match) { + public void assertFileInfosEqual(String message, AssetFileInfo fi, AssetFileInfo match) { assertNotNull(message + ":fi", fi); assertNotNull(message + ":match", match); assertEquals(message + ":getContentChecksum", fi.getContentChecksum(), match.getContentChecksum()); diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceWrapper.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceWrapper.java index a13be128cb899..8c6aa7d32e56d 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceWrapper.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceWrapper.java @@ -32,6 +32,7 @@ import com.microsoft.windowsazure.services.media.MediaContract; import com.microsoft.windowsazure.services.media.MediaService; import com.microsoft.windowsazure.services.media.WritableBlobContainerContract; +import com.microsoft.windowsazure.services.media.implementation.content.AssetFileType; import com.microsoft.windowsazure.services.media.implementation.entities.EntityListOperation; import com.microsoft.windowsazure.services.media.models.AccessPolicy; import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo; @@ -39,7 +40,7 @@ import com.microsoft.windowsazure.services.media.models.Asset; import com.microsoft.windowsazure.services.media.models.AssetInfo; import com.microsoft.windowsazure.services.media.models.EncryptionOption; -import com.microsoft.windowsazure.services.media.models.FileInfo; +import com.microsoft.windowsazure.services.media.models.AssetFileInfo; import com.microsoft.windowsazure.services.media.models.ListResult; import com.microsoft.windowsazure.services.media.models.Locator; import com.microsoft.windowsazure.services.media.models.LocatorInfo; @@ -155,8 +156,9 @@ public void uploadFilesToAsset(AssetInfo asset, int uploadWindowInMinutes, Hasht String md5 = Base64.encode(md5hash); System.out.println("md5: " + md5); - FileInfo fi = new FileInfo().setContentChecksum(md5).setContentFileSize(countingStream.getCount()) - .setIsPrimary(isFirst).setName(fileName).setParentAssetId(asset.getAlternateId()); + AssetFileInfo fi = new AssetFileInfo(null, new AssetFileType().setContentChecksum(md5) + .setContentFileSize(new Long(countingStream.getCount())).setIsPrimary(isFirst).setName(fileName) + .setParentAssetId(asset.getAlternateId())); serviceMock.createFileInfo(fi); isFirst = false; @@ -301,8 +303,8 @@ private List createOriginUrlsForStreamingContentWorker(AssetInfo asset, int availabilityWindowInMinutes, EnumSet.of(AccessPolicyPermission.READ))); LocatorInfo readLocator = service.create(Locator.create(readAP.getId(), asset.getId(), locatorType)); - List publishedFiles = serviceMock.getAssetFiles(asset.getId()); - for (FileInfo fi : publishedFiles) { + List publishedFiles = serviceMock.getAssetFiles(asset.getId()); + for (AssetFileInfo fi : publishedFiles) { if (isSmooth) { // Smooth Streaming format ends with ".ism*" int index = fi.getName().lastIndexOf('.');