1443f04f3a02e4497e8ea2ed5fbff6c74264bd52
[so.git] / common / src / test / java / org / onap / so / client / RestClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 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.client;
22
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.net.MalformedURLException;
32 import java.net.SocketTimeoutException;
33
34 import javax.ws.rs.NotFoundException;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.UriBuilder;
37 import javax.ws.rs.core.UriBuilderException;
38
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.runners.MockitoJUnitRunner;
43 import org.onap.so.utils.TargetEntity;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class RestClientTest {
47
48         
49         @Mock
50         private RestProperties props;
51         
52         
53         @Test
54         public void retries() throws Exception {
55                 RestClient spy = buildSpy();
56                 RestRequest mockCallable = mock(RestRequest.class);
57                 when(mockCallable.call()).thenThrow(new WebApplicationException(new SocketTimeoutException()));
58                 doReturn(mockCallable).when(spy).buildRequest(any(String.class), any(Object.class));
59                 try {
60                         spy.get();
61                 } catch (Exception e) {
62                         //we expect an exception, ignore it
63                 }
64                 verify(mockCallable, times(3)).call();
65                 
66         }
67         
68         @Test
69         public void exceptionDoNotRetry() throws Exception {
70                 RestClient spy = buildSpy();
71                 RestRequest mockCallable = mock(RestRequest.class);
72                 when(mockCallable.call()).thenThrow(new WebApplicationException(new NotFoundException()));
73                 doReturn(mockCallable).when(spy).buildRequest(any(String.class), any(Object.class));
74                 try {
75                         spy.get();
76                 } catch (Exception e) {
77                         //we expect an exception, ignore it
78                 }
79                 verify(mockCallable, times(1)).call();
80                 
81         }
82         private RestClient buildSpy() throws MalformedURLException, IllegalArgumentException, UriBuilderException {
83                 RestClient client = new HttpClient(UriBuilder.fromUri("http://localhost/test").build().toURL(), "application/json", TargetEntity.BPMN);
84                 
85                 return spy(client);
86         }
87 }