/**
 * Copyright (c) 2022-2024 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.binding.rest.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.ow2.petals.binding.rest.RESTConstants.ProvidesParameter.HTTP_CODE;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Test;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class HttpStatusBuilderTest {

    private static final String ELT_NAME = "my-element";

    @Test
    public void httpCodeBuilder() throws Exception {
        final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();

        // Test against a simple valid HTTP Code
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "404");
            assertEquals(404, HttpStatusBuilder.build(elt, HTTP_CODE));
        }

        // Test against a simple invalid HTTP Code
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "invalid");
            try {
                HttpStatusBuilder.build(elt, HTTP_CODE);
            } catch (final PEtALSCDKException e) {
                assertTrue(e.getMessage().contains("must be an integer value"));
            }
        }

        // Test against a missing HTTP Code
        {
            final Element elt = document.createElement(ELT_NAME);
            try {
                HttpStatusBuilder.build(elt, HTTP_CODE);
            } catch (final PEtALSCDKException e) {
                assertTrue(e.getMessage().contains("is required on element"));
            }
        }

        // Test against an empty HTTP Code
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "");
            try {
                HttpStatusBuilder.build(elt, HTTP_CODE);
            } catch (final PEtALSCDKException e) {
                assertTrue(e.getMessage().contains("is required on element"));
            }
        }
    }

    @Test
    public void httpCodesBuilder() throws Exception {
        final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();

        // Test against a missing HTTP Code list
        {
            final Element elt = document.createElement(ELT_NAME);
            try {
                HttpStatusBuilder.buildCollection(elt, HTTP_CODE);
            } catch (final PEtALSCDKException e) {
                assertTrue(e.getMessage().contains("is required on element"));
            }
        }

        // Test against an empty HTTP Code list
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "");
            try {
                HttpStatusBuilder.buildCollection(elt, HTTP_CODE);
            } catch (final PEtALSCDKException e) {
                assertTrue(e.getMessage().contains("is required on element"));
            }
        }

        // Test against a HTTP Code list with an only one valid HTTP Code
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "404");
            final int[] httpCodes = HttpStatusBuilder.buildCollection(elt, HTTP_CODE);
            assertEquals(1, httpCodes.length);
            assertEquals(404, httpCodes[0]);
        }

        // Test against a HTTP Code list with several valid HTTP Codes
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "404|407|409");
            final int[] httpCodes = HttpStatusBuilder.buildCollection(elt, HTTP_CODE);
            assertEquals(3, httpCodes.length);
            assertEquals(404, httpCodes[0]);
            assertEquals(407, httpCodes[1]);
            assertEquals(409, httpCodes[2]);
        }

        // Test against a HTTP Code list with an invalid HTTP Code
        {
            final Element elt = document.createElement(ELT_NAME);
            elt.setAttribute(HTTP_CODE, "404|invalid|407");
            try {
                HttpStatusBuilder.buildCollection(elt, HTTP_CODE);
            } catch (final PEtALSCDKException e) {
                assertTrue(e.getMessage().contains("must contains integer values separated by '|'"));
            }
        }
    }
}
