/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.elasticjob.http.pojo;

import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
import org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobConfigurationException;
import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class HttpParamTest {
    
    @Test
    void assertNewWithEmptyUrl() {
        assertThrows(JobConfigurationException.class, () -> new HttpParam(new Properties()));
    }
    
    @Test
    void assertNewWithEmptyMethod() {
        Properties props = new Properties();
        props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
        assertThrows(JobConfigurationException.class, () -> new HttpParam(props));
    }
    
    @Test
    void assertNew() {
        Properties props = new Properties();
        props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
        props.setProperty(HttpJobProperties.METHOD_KEY, "POST");
        HttpParam actual = new HttpParam(props);
        assertThat(actual.getUrl(), is("foo/url"));
        assertThat(actual.getMethod(), is("POST"));
        assertThat(actual.getConnectTimeoutMilliseconds(), is(3000));
        assertThat(actual.getReadTimeoutMilliseconds(), is(5000));
    }
    
    @Test
    void assertIsWriteMethodWithGet() {
        Properties props = new Properties();
        props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
        props.setProperty(HttpJobProperties.METHOD_KEY, "GET");
        HttpParam actual = new HttpParam(props);
        assertFalse(actual.isWriteMethod());
    }
    
    @Test
    void assertIsWriteMethodWithPost() {
        Properties props = new Properties();
        props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
        props.setProperty(HttpJobProperties.METHOD_KEY, "POST");
        HttpParam actual = new HttpParam(props);
        assertTrue(actual.isWriteMethod());
    }
    
    @Test
    void assertIsWriteMethodWithPut() {
        Properties props = new Properties();
        props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
        props.setProperty(HttpJobProperties.METHOD_KEY, "PUT");
        HttpParam actual = new HttpParam(props);
        assertTrue(actual.isWriteMethod());
    }
    
    @Test
    void assertIsWriteMethodWithDelete() {
        Properties props = new Properties();
        props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
        props.setProperty(HttpJobProperties.METHOD_KEY, "DELETE");
        HttpParam actual = new HttpParam(props);
        assertTrue(actual.isWriteMethod());
    }
}
