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.name().toLowerCase(), revenueScheduleType);
        }
    }

    @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(String value, RevenueScheduleType expectedRevenueScheduleType) 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(), expectedRevenueScheduleType);
    }
}

@@ -1,8 +1,710 @@
+/*%0A  * Copyright 2010-2014 Ning, Inc.%0A  * Copyright 2014-2015 The Billing Project, LLC%0A  *%0A  * The Billing Project licenses this file to you under the Apache License, version 2.0%0A  * (the %22License%22); you may not use this file except in compliance with the%0A  * License.  You may obtain a copy of the License at:%0A  *%0A  *    http://www.apache.org/licenses/LICENSE-2.0%0A  *%0A  * Unless required by applicable law or agreed to in writing, software%0A  * distributed under the License is distributed on an %22AS IS%22 BASIS, WITHOUT%0A  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the%0A  * License for the specific language governing permissions and limitations%0A  * under the License.%0A  */%0A%0A
 package 

add license