Remove Information Coordinator Service
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / test / java / org / o_ran_sc / nonrtric / sdnc_a1 / northbound / restadapter / RestAdapterImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter;
22
23 import static org.junit.Assert.assertEquals;
24 import java.io.IOException;
25 import okhttp3.mockwebserver.MockResponse;
26 import okhttp3.mockwebserver.MockWebServer;
27 import okhttp3.mockwebserver.RecordedRequest;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.web.client.RestClientException;
34
35 public class RestAdapterImplTest {
36     private static MockWebServer mockWebServer;
37     private static RestAdapter adapterUnderTest;
38
39     private static final String VALID_PROTOCOL = "http";
40     private static final String INVALID_PROTOCOL = "ftp";
41     private static final String REQUEST_URL = "/test";
42     private static final String TEST_BODY = "test";
43     private static final Integer SUCCESS_CODE = 200;
44     private static final Integer ERROR_CODE = 500;
45
46     @Before
47     public void init() throws IOException {
48         mockWebServer = new MockWebServer();
49         mockWebServer.start();
50         adapterUnderTest = new RestAdapterImpl();
51     }
52
53     @After
54     public void tearDown() throws IOException {
55         mockWebServer.shutdown();
56     }
57
58     @Test
59     public void testInvalidUrlOrProtocol() throws InterruptedException {
60         ResponseEntity<String> response = adapterUnderTest.get("://localhost:" + mockWebServer.getPort() + REQUEST_URL,
61                 String.class);
62         assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatusCodeValue());
63         response = adapterUnderTest.get(INVALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL,
64                 String.class);
65         assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatusCodeValue());
66     }
67
68     @Test
69     public void testGetNoError() throws InterruptedException {
70         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE).setBody(TEST_BODY));
71         ResponseEntity<String> response = adapterUnderTest.get(VALID_PROTOCOL + "://localhost:"
72                 + mockWebServer.getPort() + REQUEST_URL, String.class);
73         RecordedRequest recordedRequest = mockWebServer.takeRequest();
74         assertEquals(TEST_BODY, response.getBody());
75         assertEquals(SUCCESS_CODE.intValue(), response.getStatusCodeValue());
76         assertEquals("GET", recordedRequest.getMethod());
77         assertEquals(REQUEST_URL, recordedRequest.getPath());
78     }
79
80     @Test(expected = RestClientException.class)
81     public void testGetError() {
82         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
83         adapterUnderTest.get(VALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL, String.class);
84     }
85
86     @Test
87     public void testPutNoError() throws InterruptedException {
88         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE).setBody(TEST_BODY));
89         ResponseEntity<String> response = adapterUnderTest.put(VALID_PROTOCOL + "://localhost:"
90                 + mockWebServer.getPort() + REQUEST_URL, TEST_BODY, String.class);
91         RecordedRequest recordedRequest = mockWebServer.takeRequest();
92         assertEquals(TEST_BODY, response.getBody());
93         assertEquals(SUCCESS_CODE.intValue(), response.getStatusCodeValue());
94         assertEquals("PUT", recordedRequest.getMethod());
95         assertEquals(REQUEST_URL, recordedRequest.getPath());
96         assertEquals(TEST_BODY, recordedRequest.getBody().readUtf8());
97     }
98
99     @Test(expected = RestClientException.class)
100     public void testPutError() {
101         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
102         adapterUnderTest.put(VALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL, TEST_BODY,
103                 String.class);
104     }
105
106     @Test
107     public void testDeleteNoError() throws InterruptedException {
108         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
109         ResponseEntity<String> response = adapterUnderTest.delete(VALID_PROTOCOL + "://localhost:"
110                 + mockWebServer.getPort() + REQUEST_URL);
111         RecordedRequest recordedRequest = mockWebServer.takeRequest();
112         assertEquals(SUCCESS_CODE.intValue(), response.getStatusCodeValue());
113         assertEquals("DELETE", recordedRequest.getMethod());
114         assertEquals(REQUEST_URL, recordedRequest.getPath());
115     }
116
117     @Test(expected = RestClientException.class)
118     public void testDeleteError() {
119         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
120         adapterUnderTest.delete(VALID_PROTOCOL + "://localhost:" + mockWebServer.getPort() + REQUEST_URL);
121     }
122 }