com.basho.riak.client.response.TestDefaultHttpResponse.java Source code

Java tutorial

Introduction

Here is the source code for com.basho.riak.client.response.TestDefaultHttpResponse.java

Source

/*
 * This file is provided 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 com.basho.riak.client.response;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.junit.Test;

public class TestDefaultHttpResponse {

    DefaultHttpResponse impl;

    @Test
    public void status_2xx_300_and_304_success_for_head() {
        HeadMethod head = new HeadMethod();

        for (int i = 200; i < 300; i++) {
            impl = new DefaultHttpResponse(null, null, i, null, null, null, head);
            assertTrue(impl.isSuccess());
        }

        impl = new DefaultHttpResponse(null, null, 300, null, null, null, head);
        assertTrue(impl.isSuccess());

        impl = new DefaultHttpResponse(null, null, 304, null, null, null, head);
        assertTrue(impl.isSuccess());
    }

    @Test
    public void status_2xx_300_and_304_success_for_get() {
        GetMethod get = new GetMethod();

        for (int i = 200; i < 300; i++) {
            impl = new DefaultHttpResponse(null, null, i, null, null, null, get);
            assertTrue(impl.isSuccess());
        }

        impl = new DefaultHttpResponse(null, null, 300, null, null, null, get);
        assertTrue(impl.isSuccess());

        impl = new DefaultHttpResponse(null, null, 304, null, null, null, get);
        assertTrue(impl.isSuccess());
    }

    @Test
    public void status_2xx_and_404_success_for_delete() {
        DeleteMethod delete = new DeleteMethod();

        for (int i = 200; i < 300; i++) {
            impl = new DefaultHttpResponse(null, null, i, null, null, null, delete);
            assertTrue(impl.isSuccess());
        }

        impl = new DefaultHttpResponse(null, null, 404, null, null, null, delete);
        assertTrue(impl.isSuccess());
    }

    @Test
    public void status_4xx_is_error() {
        for (int i = 400; i < 500; i++) {
            impl = new DefaultHttpResponse(null, null, i, null, null, null, null);
            assertTrue(impl.isError());
        }
    }

    @Test
    public void status_5xx_is_error() {
        for (int i = 500; i < 600; i++) {
            impl = new DefaultHttpResponse(null, null, i, null, null, null, null);
            assertTrue(impl.isError());
        }
    }

    @Test
    public void status_less_than_100_is_error() {
        for (int i = -1; i < 100; i++) {
            impl = new DefaultHttpResponse(null, null, i, null, null, null, null);
            assertTrue(impl.isError());
        }
    }

    @Test
    public void null_http_method_with_200_is_still_success() {
        impl = new DefaultHttpResponse(null, null, 200, null, null, null, null);
        assertTrue(impl.isSuccess());
    }

    @Test
    public void headers_are_never_null() {
        impl = new DefaultHttpResponse(null, null, 200, null, null, null, null);
        assertNotNull(impl.getHttpHeaders());
    }

    @Test
    public void close_closes_underlying_http_connection() {
        HttpMethod mockHttpMethod = mock(HttpMethod.class);
        impl = new DefaultHttpResponse(null, null, 200, null, null, null, mockHttpMethod);
        impl.close();
        verify(mockHttpMethod).releaseConnection();
    }
}