2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.net.MalformedURLException;
33 import java.net.SocketTimeoutException;
35 import javax.ws.rs.NotFoundException;
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriBuilderException;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.ArgumentMatchers;
44 import org.onap.so.utils.TargetEntity;
45 import org.mockito.junit.MockitoJUnitRunner;
47 @RunWith(MockitoJUnitRunner.class)
48 public class RestClientTest {
51 private final HttpClientFactory httpClientFactory = new HttpClientFactory();
53 private RestProperties props;
57 public void retries() throws Exception {
58 RestClient spy = buildSpy();
59 RestRequest mockCallable = mock(RestRequest.class);
60 when(mockCallable.call()).thenThrow(new WebApplicationException(new SocketTimeoutException()));
61 doReturn(mockCallable).when(spy).buildRequest(any(String.class), ArgumentMatchers.isNull());
64 } catch (Exception e) {
65 //we expect an exception, ignore it
67 verify(mockCallable, times(3)).call();
72 public void exceptionDoNotRetry() throws Exception {
73 RestClient spy = buildSpy();
74 RestRequest mockCallable = mock(RestRequest.class);
75 when(mockCallable.call()).thenThrow(new WebApplicationException(new NotFoundException()));
76 doReturn(mockCallable).when(spy).buildRequest(any(String.class), ArgumentMatchers.isNull());
79 } catch (Exception e) {
80 //we expect an exception, ignore it
82 verify(mockCallable, times(1)).call();
85 private RestClient buildSpy() throws MalformedURLException, IllegalArgumentException, UriBuilderException {
86 RestClient client = httpClientFactory
87 .newJsonClient(UriBuilder.fromUri("http://localhost/test").build().toURL(), TargetEntity.BPMN);