Remove outdated doc for A1 Adaptor
[integration.git] / test / mocks / masspnfsim / pnf-sim-lightweight / src / test / java / org / onap / pnfsimulator / simulator / client / RestTemplateAdapterImplTest.java
1 /*
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
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.pnfsimulator.simulator.client;
22
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;
32
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;
38
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41
42 class RestTemplateAdapterImplTest {
43
44     private RestTemplateAdapter adapter;
45
46     @Mock
47     private RestTemplate restTemplate;
48     @Mock
49     ResponseEntity<String> response;
50
51     @BeforeEach
52     void setup() {
53         initMocks(this);
54         adapter = new RestTemplateAdapterImpl(restTemplate);
55     }
56
57     @Test
58     void send_should_successfully_send_request_given_valid_http_url()
59     throws HttpClientErrorException {
60
61         String content = "test-msg";
62         String urlHttp = "http://valid-url";
63
64         when(
65             restTemplate.postForEntity(
66                 (String) notNull(),
67                 (HttpEntity) notNull(),
68                 eq(String.class)
69             )
70         ).thenReturn(response);
71
72         adapter.send(content, urlHttp);
73
74         verify(
75             restTemplate, times(1)).postForEntity(
76             (String) notNull(),
77             (HttpEntity) notNull(),
78             eq(String.class)
79         );
80         verify(response).getStatusCode();
81     }
82
83     @Test
84     void send_should_successfully_send_request_given_valid_https_url()
85     throws HttpClientErrorException {
86
87         String content = "test-msg";
88         String urlHttps = "https://valid-url";
89
90         when(
91             restTemplate.postForEntity(
92                 (String) notNull(),
93                 (HttpEntity) notNull(),
94                 eq(String.class)
95             )
96         ).thenReturn(response);
97
98         adapter.send(content, urlHttps);
99
100         verify(
101             restTemplate, times(1)).postForEntity(
102             (String) notNull(),
103             (HttpEntity) notNull(),
104             eq(String.class)
105         );
106         verify(response).getStatusCode();
107     }
108
109     @Test
110     void send_should_not_send_request_given_invalid_url()
111     throws HttpClientErrorException {
112
113         String content = "test-msg";
114         String url = "http://invalid-url";
115
116         doThrow(
117             new HttpClientErrorException(HttpStatus.BAD_REQUEST)).when(
118             restTemplate).postForEntity(
119             (String) notNull(),
120             (HttpEntity) notNull(),
121             eq(String.class)
122         );
123
124         adapter.send(content, url);
125
126         verify(
127             restTemplate, times(1)).postForEntity(
128             (String) notNull(),
129             (HttpEntity) notNull(),
130             eq(String.class)
131         );
132         verify(response, never()).getStatusCode();
133     }
134 }