/**
 * 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 static org.junit.Assert.*;

import java.net.URLEncoder;

import javax.xml.bind.JAXBElement;

import org.junit.Test;

import com.microsoft.windowsazure.services.media.implementation.atom.EntryType;
import com.microsoft.windowsazure.services.media.implementation.content.TaskType;

/**
 * Tests for the methods and factories of the Task entity.
 */
public class TaskEntityTest {
    static final String sampleTaskId = "nb:cid:UUID:1151b8bd-9ada-4e7f-9787-8dfa49968eab";
    private final String expectedUri = String.format("Tasks('%s')", URLEncoder.encode(sampleTaskId, "UTF-8"));

    private TaskType getTaskType(EntryType entryType) {
        for (Object child : entryType.getEntryChildren()) {
            if (child instanceof JAXBElement) {
                @SuppressWarnings("rawtypes")
                JAXBElement element = (JAXBElement) child;
                if (element.getDeclaredType() == TaskType.class) {
                    return (TaskType) element.getValue();
                }
            }
        }
        return null;
    }

    public TaskEntityTest() throws Exception {
    }

    @Test
    public void taskCreateReturnsDefaultCreatePayload() {
        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedMediaProcessorId, taskType.getMediaProcessorId());
        assertEquals(expectedTaskBody, taskType.getTaskBody());
    }

    @Test
    public void taskCreateCanSetTaskName() {
        String expectedName = "TaskCreateCanSetTaskName";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody).setName(expectedName)
                .getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedName, taskType.getName());
    }

    @Test
    public void taskCreateCanSetConfiguration() {
        String expectedConfiguration = "TaskCreateCanSetTaskCofniguration";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setConfiguration(expectedConfiguration).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedConfiguration, taskType.getConfiguration());
    }

    @Test
    public void taskCreateCanSetPriority() {
        Integer expectedPriority = 3;

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setPriority(expectedPriority).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedPriority, taskType.getPriority());
    }

    @Test
    public void taskCreateCanSetTaskBody() {
        String expectedTaskBodyResult = "expectedTaskBodyResult";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setTaskBody(expectedTaskBodyResult).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedTaskBodyResult, taskType.getTaskBody());
    }

    @Test
    public void taskCreateCanSetEncryptionKeyId() {
        String expectedEncryptionKeyId = "expectedEncryptionKeyId";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setEncryptionKeyId(expectedEncryptionKeyId).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedEncryptionKeyId, taskType.getTaskBody());
    }

    @Test
    public void taskCreateCanSetEncryptionScheme() {
        String expectedEncryptionScheme = "expectedEncryptionScheme";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setEncryptionScheme(expectedEncryptionScheme).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedEncryptionScheme, taskType.getEncryptionScheme());
    }

    @Test
    public void taskCreateCanSetEncryptionVersion() {
        String expectedEncryptionVersion = "expectedEncryptionVersion";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setEncryptionVersion(expectedEncryptionVersion).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedEncryptionVersion, taskType.getTaskBody());
    }

    @Test
    public void taskCreateCanSetInitializationVector() {
        String expectedInitializationVector = "expectedEncryptionKeyId";

        String expectedMediaProcessorId = "expectedMediaProcessorId";
        String expectedTaskBody = "expectedTaskBody";

        TaskType taskType = getTaskType(Task.create(expectedMediaProcessorId, expectedTaskBody)
                .setEncryptionKeyId(expectedInitializationVector).getEntryType());

        assertNotNull(taskType);
        assertEquals(expectedInitializationVector, taskType.getTaskBody());
    }
}

@@ -774,24 +774,106 @@
 unit.Test;%0A%0A
+import com.microsoft.windowsazure.services.media.implementation.atom.ContentType;%0A
 import com.m
@@ -1645,52 +1645,377 @@
  == 
-TaskType.class) %7B%0A                    return
+ContentType.class) %7B%0A                    ContentType contentType = (ContentType) element.getValue();%0A                    for (Object grandChild : contentType.getContent()) %7B%0A                        if (grandChild instanceof JAXBElement) %7B%0A                            JAXBElement contentElement = (JAXBElement) grandChild;%0A                            TaskType taskType =
  (Ta
@@ -2018,25 +2018,32 @@
  (TaskType) 
-e
+contentE
 lement.getVa
@@ -2041,32 +2041,158 @@
 ent.getValue();%0A
+                            return taskType;%0A                        %7D%0A                    %7D%0A                    return null;%0A
                 
@@ -5351,32 +5351,39 @@
 taskType.get
-TaskBody
+EncryptionKeyId
 ());%0A    %7D%0A%0A
@@ -6468,32 +6468,41 @@
 taskType.get
-TaskBody
+EncryptionVersion
 ());%0A    %7D%0A%0A
@@ -7039,32 +7039,44 @@
 taskType.get
-TaskBody
+InitializationVector
 ());%0A    %7D%0A%7D

fix a broken unit test.