

package org.wso2.msf4j.beanconversion;

import org.testng.annotations.Test;
import org.wso2.msf4j.internal.beanconversion.BeanConverter;
import org.wso2.msf4j.internal.router.Category;
import org.wso2.msf4j.internal.router.Pet;
import org.wso2.msf4j.internal.router.XmlBean;

import java.nio.ByteBuffer;

import static org.testng.AssertJUnit.assertEquals;

/**
 * Tests the functionality of BeanConverter.
 */
public class BeanConverterTest {

    @Test
    public void testJsonBeanConversionTextJson() throws BeanConversionException {
        Pet pet = makePet();
        ByteBuffer json = BeanConverter.getConverter("text/json").toMedia(pet);
        Pet pet1 = (Pet) BeanConverter.getConverter("text/json").toObject(json, Pet.class);
        assertEquals(pet.getId(), pet1.getId());
        assertEquals(pet.getDetails(), pet1.getDetails());
        assertEquals(pet.getImage(), pet1.getImage());
        assertEquals(pet.getCategory().getName(), pet1.getCategory().getName());
        assertEquals(pet.getAgeMonths(), pet1.getAgeMonths());
        assertEquals(pet.getPrice(), pet1.getPrice(), 0);
        assertEquals(pet.getDateAdded(), pet1.getDateAdded());
    }

    @Test
    public void testJsonBeanConversionApplicationJson() throws BeanConversionException {
        Pet original = makePet();
        ByteBuffer json = BeanConverter.getConverter("application/json").toMedia(original);
        Pet result = (Pet) BeanConverter.getConverter("application/json").toObject(json, Pet.class);
        assertEquals(original.getId(), result.getId());
        assertEquals(original.getDetails(), result.getDetails());
        assertEquals(original.getImage(), result.getImage());
        assertEquals(original.getCategory().getName(), result.getCategory().getName());
        assertEquals(original.getAgeMonths(), result.getAgeMonths());
        assertEquals(original.getPrice(), result.getPrice(), 0);
        assertEquals(original.getDateAdded(), result.getDateAdded());
    }

    @Test
    public void testTextPlainBeanConversion() throws BeanConversionException {
        String val = "Test_String";
        ByteBuffer media = BeanConverter.getConverter("text/plain").toMedia(val);
        Object obj1 = BeanConverter.getConverter("text/plain").toObject(media, null);
        assertEquals(obj1, val);
    }

    @Test
    public void testAnyBeanConversion() throws BeanConversionException {
        String original = "Test_String";
        ByteBuffer media = BeanConverter.getConverter("*/*").toMedia(original);
        Object result = BeanConverter.getConverter("*/*").toObject(media, null);
        assertEquals(original, result);
    }

    @Test
    public void testXmlBeanConversion() throws BeanConversionException {
        XmlBean original = makeXmlBan();
        ByteBuffer xml = BeanConverter.getConverter("text/xml").toMedia(original);
        XmlBean result = (XmlBean) BeanConverter.getConverter("text/xml").toObject(xml, XmlBean.class);
        assertEquals(original.getName(), result.getName());
        assertEquals(original.getId(), result.getId());
        assertEquals(original.getValue(), result.getValue());
    }

    private XmlBean makeXmlBan() {
        XmlBean xmlBean = new XmlBean();
        xmlBean.setId(12);
        xmlBean.setName("xml-bean-name");
        xmlBean.setValue(457);
        return xmlBean;
    }

    private Pet makePet() {
        Pet pet = new Pet();
        pet.setCategory(new Category("dog"));
        pet.setAgeMonths(3);
        pet.setDetails("small-cat");
        pet.setPrice(10.5f);
        pet.setImage("cat.png");
        return pet;
    }
}
