DFC shall provide a bearer authorization token in HTTP
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / http / HttpUtilsTest.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018-2023 Nordix Foundation. All rights reserved.
4  * Modifications Copyright (C) 2021 Nokia. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. 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 distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  * ============LICENSE_END========================================================================
16  */
17
18 package org.oran.datafile.http;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.net.URISyntaxException;
25
26 import org.apache.hc.core5.net.URIBuilder;
27 import org.junit.jupiter.api.Test;
28 import org.oran.datafile.model.FileServerData;
29
30 class HttpUtilsTest {
31
32     private static final String XNF_ADDRESS = "127.0.0.1";
33     private static final int PORT = 443;
34     private static final String FRAGMENT = "thisIsTheFragment";
35     private static final String USERNAME = "bob";
36     private static final String PASSWORD = "123";
37
38     @Test
39     void shouldReturnSuccessfulResponse() {
40         assertTrue(HttpUtils.isSuccessfulResponseCodeWithDataRouter(200));
41     }
42
43     @Test
44     void shouldReturnBadResponse() {
45         assertFalse(HttpUtils.isSuccessfulResponseCodeWithDataRouter(404));
46     }
47
48     @Test
49     void prepareUri_UriWithoutPort() {
50         FileServerData serverData =
51             FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).build();
52         String REMOTE_FILE = "any";
53
54         String retrievedUri = HttpUtils.prepareUri("http", serverData, REMOTE_FILE, 80);
55         assertTrue(retrievedUri.startsWith("http://" + XNF_ADDRESS + ":80"));
56     }
57
58     @Test
59     void prepareUri_verifyUriWithoutTokenAndWithoutFragment() throws URISyntaxException {
60         String file = "/file";
61         String expected = "http://" + XNF_ADDRESS + ":" + PORT + file;
62         assertEquals(expected, HttpUtils.prepareUri("http", fileServerDataNoTokenNoFragment(), file, 443));
63     }
64
65     private FileServerData fileServerDataNoTokenNoFragment() throws URISyntaxException {
66         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
67             .queryParameters(new URIBuilder("").getQueryParams()).uriRawFragment("").build();
68     }
69 }