564121b7b586ddb92acc9e37714cd433d0143cc7
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandler / recipe / CamundaClientErrorHandlerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandler.recipe;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.client.ClientHttpResponse;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33
34 public class CamundaClientErrorHandlerTest {
35         
36         private ClientHttpResponse clientHttpResponse;
37         private CamundaClientErrorHandler clientErrorHandler;
38         
39         @Before
40         public void before() {
41                 clientHttpResponse = Mockito.mock(ClientHttpResponse.class);
42                 clientErrorHandler = new CamundaClientErrorHandler();
43         }
44         
45         @Test
46         public void handleError_SERVER_ERROR_Test() throws IOException {
47                 Mockito.when(clientHttpResponse.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
48                 Mockito.when(clientHttpResponse.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes())); 
49                 clientErrorHandler.handleError(clientHttpResponse);
50                 boolean serverHasError = clientErrorHandler.hasError(clientHttpResponse);
51                 assertEquals(true, serverHasError);
52         }
53         
54         @Test
55         public void handleError_CLIENT_ERROR_Test() throws IOException {
56                 Mockito.when(clientHttpResponse.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
57                 Mockito.when(clientHttpResponse.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes())); 
58                 clientErrorHandler.handleError(clientHttpResponse);
59                 boolean clientHasError = clientErrorHandler.hasError(clientHttpResponse);
60                 assertEquals(true, clientHasError);
61         }       
62         
63         @Test
64         public void handleError_SUCCESS_Test() throws IOException {
65                 Mockito.when(clientHttpResponse.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
66                 Mockito.when(clientHttpResponse.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes())); 
67                 clientErrorHandler.handleError(clientHttpResponse);
68                 boolean hasNoError = clientErrorHandler.hasError(clientHttpResponse);
69                 assertEquals(false, hasNoError);
70         }       
71         
72 }