2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA 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.pnfsimulator.simulator.client;
23 import org.mockito.Mock;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.notNull;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.MockitoAnnotations.initMocks;
33 import org.springframework.web.client.HttpClientErrorException;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.HttpEntity;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.client.RestTemplate;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
42 class RestTemplateAdapterImplTest {
44 private RestTemplateAdapter adapter;
47 private RestTemplate restTemplate;
49 ResponseEntity<String> response;
54 adapter = new RestTemplateAdapterImpl(restTemplate);
58 void send_should_successfully_send_request_given_valid_http_url()
59 throws HttpClientErrorException {
61 String content = "test-msg";
62 String urlHttp = "http://valid-url";
65 restTemplate.postForEntity(
67 (HttpEntity) notNull(),
70 ).thenReturn(response);
72 adapter.send(content, urlHttp);
75 restTemplate, times(1)).postForEntity(
77 (HttpEntity) notNull(),
80 verify(response).getStatusCode();
84 void send_should_successfully_send_request_given_valid_https_url()
85 throws HttpClientErrorException {
87 String content = "test-msg";
88 String urlHttps = "https://valid-url";
91 restTemplate.postForEntity(
93 (HttpEntity) notNull(),
96 ).thenReturn(response);
98 adapter.send(content, urlHttps);
101 restTemplate, times(1)).postForEntity(
103 (HttpEntity) notNull(),
106 verify(response).getStatusCode();
110 void send_should_not_send_request_given_invalid_url()
111 throws HttpClientErrorException {
113 String content = "test-msg";
114 String url = "http://invalid-url";
117 new HttpClientErrorException(HttpStatus.BAD_REQUEST)).when(
118 restTemplate).postForEntity(
120 (HttpEntity) notNull(),
124 adapter.send(content, url);
127 restTemplate, times(1)).postForEntity(
129 (HttpEntity) notNull(),
132 verify(response, never()).getStatusCode();