YoutrackUnmarshallerTest.java 3.05 KB
package pl.itcrowd.youtrack.api.rest;

import org.junit.Test;
import pl.itcrowd.youtrack.api.YoutrackUnmarshaller;
import pl.itcrowd.youtrack.api.exceptions.YoutrackAPIException;

import javax.xml.bind.JAXBException;
import java.io.IOException;

import static junit.framework.Assert.assertEquals;

public class YoutrackUnmarshallerTest {

    @Test
    public void unmarshalEmptyError() throws JAXBException, IOException
    {
//        Given
        final String extendedErrorResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error></error>";

//        When
        final String errorText = YoutrackUnmarshaller.unmarshalError(extendedErrorResponse);

//        THen
        assertEquals("", errorText);
    }

    @Test
    public void unmarshalError() throws JAXBException, IOException
    {
//        Given
        final String extendedErrorResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error>Estimated completion time</error>";

//        When
        final String errorText = YoutrackUnmarshaller.unmarshalError(extendedErrorResponse);

//        THen
        assertEquals("Estimated completion time", errorText);
    }

    @Test
    public void unmarshalExtendedError() throws JAXBException, IOException
    {
//        Given
        final String extendedErrorResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error><message>Estimate task before starting work on it</message><field>Estimated completion time</field></error>";

//        When
        final String errorText = YoutrackUnmarshaller.unmarshalError(extendedErrorResponse);

//        Then
        assertEquals("Estimate task before starting work on it", errorText);
    }

    @Test
    public void unmarshalExtendedErrorWithMessageOnly() throws JAXBException, IOException
    {
//        Given
        final String extendedErrorResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error><message>Estimate task before starting work on it</message></error>";

//        When
        final String errorText = YoutrackUnmarshaller.unmarshalError(extendedErrorResponse);

//        Then
        assertEquals("Estimate task before starting work on it", errorText);
    }

    @Test
    public void unmarshalExtendedErrorWithEmptyMessage() throws JAXBException, IOException
    {
//        Given
        final String extendedErrorResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error><message></message><field>Estimated completion time</field></error>";

//        When
        final String errorText = YoutrackUnmarshaller.unmarshalError(extendedErrorResponse);

//        Then
        assertEquals("", errorText);
    }

    @Test(expected = YoutrackAPIException.class)
    public void unmarshalExtendedErrorWithoutMessage() throws JAXBException, IOException
    {
//        Given
        final String extendedErrorResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error><field>Estimated completion time</field></error>";

//        When
        YoutrackUnmarshaller.unmarshalError(extendedErrorResponse);
    }
}