package com.ning.billing.recurly.model;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.ning.billing.recurly.TestUtils;
import org.testng.annotations.Test;

import java.io.IOException;

import static org.testng.AssertJUnit.assertEquals;

public class TestRevenueScheduleType extends TestModelBase {

    @Test(groups = "fast")
    public void testSerializationAllValues() throws Exception {
        for (RevenueScheduleType revenueScheduleType : RevenueScheduleType.values()) {
            testSerializationCommon(revenueScheduleType, revenueScheduleType.name().toLowerCase());
        }
    }


    @Test(groups = "fast")
    public void testDeserializationAllValues() throws Exception {
        for (RevenueScheduleType revenueScheduleType : RevenueScheduleType.values()) {
            testDeserializationCommon(revenueScheduleType, revenueScheduleType.name().toLowerCase());
        }
    }

    @Test(groups = "fast")
    public void testSerializationNullValue() throws Exception {
        Adjustment adjustment = TestUtils.createRandomAdjustment();
        adjustment.setRevenueScheduleType(null);
        String xml = xmlMapper.writeValueAsString(adjustment);
        assert(!xml.contains("<revenue_schedule_type>"));
    }

    @Test(groups = "fast")
    public void testDeserializationWhenElementNotPresent() throws Exception {
        final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                      "<adjustment type=\"credit\" href=\"https://api.recurly.com/v2/adjustments/626db120a84102b1809909071c701c60\">\n" +
                                      "</adjustment>";
        Adjustment adjustment = xmlMapper.readValue(xml, Adjustment.class);
        assertEquals(adjustment.getRevenueScheduleType(), null);
    }

    @Test(groups = "fast")
    public void testDeserializationWhenValueNotPresent() throws Exception {
        final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                      "<adjustment type=\"credit\" href=\"https://api.recurly.com/v2/adjustments/626db120a84102b1809909071c701c60\">\n" +
                                        "<revenue_schedule_type></revenue_schedule_type>\n" +
                                      "</adjustment>";
        Adjustment adjustment = xmlMapper.readValue(xml, Adjustment.class);
        assertEquals(adjustment.getRevenueScheduleType(), null);
    }

    @Test(groups = "fast", expectedExceptions = InvalidFormatException.class)
    public void testDeserializationWhenValueUnknown() throws IOException {
        final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                      "<adjustment type=\"credit\" href=\"https://api.recurly.com/v2/adjustments/626db120a84102b1809909071c701c60\">\n" +
                                        "<revenue_schedule_type>covfefe</revenue_schedule_type>\n" +
                                      "</adjustment>";
        xmlMapper.readValue(xml, Adjustment.class);

    }

    private void testSerializationCommon(RevenueScheduleType revenueScheduleType, String expectedSerializedValue) throws Exception {
        Adjustment adjustment = TestUtils.createRandomAdjustment();
        adjustment.setRevenueScheduleType(revenueScheduleType);
        String xml = xmlMapper.writeValueAsString(adjustment);
        assert(xml.contains("<revenue_schedule_type>"+ expectedSerializedValue +"</revenue_schedule_type>"));
    }

     private void testDeserializationCommon(RevenueScheduleType revenueScheduleType, String value) throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                      "<adjustment type=\"credit\" href=\"https://api.recurly.com/v2/adjustments/626db120a84102b1809909071c701c60\">\n" +
                                        "<revenue_schedule_type>" + value + "</revenue_schedule_type>\n" +
                                      "</adjustment>";

        Adjustment adjustment = xmlMapper.readValue(xml, Adjustment.class);
        assertEquals(adjustment.getRevenueScheduleType(), revenueScheduleType);
    }
}

@@ -856,37 +856,16 @@
 duleType
-, revenueScheduleType
 .name().
@@ -869,32 +869,53 @@
 ().toLowerCase()
+, revenueScheduleType
 );%0A        %7D%0A   
@@ -3402,16 +3402,17 @@
 e_type%3E%22
+ 
 + expect
@@ -3430,16 +3430,17 @@
 dValue +
+ 
 %22%3C/reven
@@ -3504,32 +3504,46 @@
 alizationCommon(
+String value, 
 RevenueScheduleT
@@ -3538,33 +3538,41 @@
 nueScheduleType 
-r
+expectedR
 evenueScheduleTy
@@ -3577,22 +3577,8 @@
 Type
-, String value
 ) th
@@ -4111,25 +4111,33 @@
 duleType(), 
-r
+expectedR
 evenueSchedu

Add missing deserialization testg