Upgrade dt-be-main
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / test / java / org / onap / sdc / dcae / utils / SDCResponseErrorHandlerTest.java
1 package org.onap.sdc.dcae.utils;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.InjectMocks;
8 import org.mockito.runners.MockitoJUnitRunner;
9 import org.springframework.http.HttpHeaders;
10 import org.springframework.http.HttpStatus;
11 import org.springframework.http.client.ClientHttpResponse;
12 import org.springframework.web.client.HttpClientErrorException;
13 import org.springframework.web.client.HttpServerErrorException;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17
18 @RunWith(MockitoJUnitRunner.class)
19 public class SDCResponseErrorHandlerTest {
20     @InjectMocks
21     private SDCResponseErrorHandler classUnderTest;
22     private ClientHttpResponse clientHttpResponse;
23     private HttpStatus httpStatus;
24
25     @Before
26     public void setup() {
27         clientHttpResponse = new ClientHttpResponse() {
28             @Override
29             public HttpStatus getStatusCode() throws IOException {
30                 return httpStatus;
31             }
32
33             @Override
34             public int getRawStatusCode() throws IOException {
35                 return 0;
36             }
37
38             @Override
39             public String getStatusText() throws IOException {
40                 return null;
41             }
42
43             @Override
44             public void close() {
45
46             }
47
48             @Override
49             public InputStream getBody() throws IOException {
50                 return null;
51             }
52
53             @Override
54             public HttpHeaders getHeaders() {
55                 return new HttpHeaders();
56             }
57         };
58     }
59
60     @Test(expected = HttpClientErrorException.class)
61     public void handleError_haveError_throwsClientException() throws IOException {
62         httpStatus = HttpStatus.EXPECTATION_FAILED;
63         classUnderTest.handleError(clientHttpResponse);
64     }
65
66     @Test(expected = HttpServerErrorException.class)
67     public void handleError_haveError_throwsServerException() throws IOException {
68         httpStatus = HttpStatus.BAD_GATEWAY;
69         classUnderTest.handleError(clientHttpResponse);
70     }
71
72     @Test
73     public void hasError_haveClientError_returnTrue() throws IOException {
74         httpStatus = HttpStatus.EXPECTATION_FAILED;
75         boolean result = classUnderTest.hasError(clientHttpResponse);
76         Assert.assertTrue(result);
77     }
78
79     @Test
80     public void hasError_haveServerError_returnTrue() throws IOException {
81         httpStatus = HttpStatus.BAD_GATEWAY;
82         boolean result = classUnderTest.hasError(clientHttpResponse);
83         Assert.assertTrue(result);
84     }
85
86     @Test
87     public void hasError_200OK_returnFalse() throws IOException {
88         httpStatus = HttpStatus.OK;
89         boolean result = classUnderTest.hasError(clientHttpResponse);
90         Assert.assertFalse(result);
91     }
92
93 }