summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/asdc/src/test/java/org/onap/sdc/dcae/utils/SDCResponseErrorHandlerTest.java
blob: 05eb4b1f47f41d115185b12426f5f62661498edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.onap.sdc.dcae.utils;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;

import java.io.IOException;
import java.io.InputStream;

@RunWith(MockitoJUnitRunner.class)
public class SDCResponseErrorHandlerTest {
    @InjectMocks
    private SDCResponseErrorHandler classUnderTest;
    private ClientHttpResponse clientHttpResponse;
    private HttpStatus httpStatus;

    @Before
    public void setup() {
        clientHttpResponse = new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return httpStatus;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return httpStatus.value();
            }

            @Override
            public String getStatusText() throws IOException {
                return null;
            }

            @Override
            public void close() {

            }

            @Override
            public InputStream getBody() throws IOException {
                return null;
            }

            @Override
            public HttpHeaders getHeaders() {
                return new HttpHeaders();
            }
        };
    }

    @Test(expected = HttpClientErrorException.class)
    public void handleError_haveError_throwsClientException() throws IOException {
        httpStatus = HttpStatus.EXPECTATION_FAILED;
        classUnderTest.handleError(clientHttpResponse);
    }

    @Test(expected = HttpServerErrorException.class)
    public void handleError_haveError_throwsServerException() throws IOException {
        httpStatus = HttpStatus.BAD_GATEWAY;
        classUnderTest.handleError(clientHttpResponse);
    }

    @Test
    public void hasError_haveClientError_returnTrue() throws IOException {
        httpStatus = HttpStatus.EXPECTATION_FAILED;
        boolean result = classUnderTest.hasError(clientHttpResponse);
        Assert.assertTrue(result);
    }

    @Test
    public void hasError_haveServerError_returnTrue() throws IOException {
        httpStatus = HttpStatus.BAD_GATEWAY;
        boolean result = classUnderTest.hasError(clientHttpResponse);
        Assert.assertTrue(result);
    }

    @Test
    public void hasError_200OK_returnFalse() throws IOException {
        httpStatus = HttpStatus.OK;
        boolean result = classUnderTest.hasError(clientHttpResponse);
        Assert.assertFalse(result);
    }

}