Improve Test coverage of DFC
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / oauth2 / SecurityContextTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.oran.datafile.oauth2;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.nio.file.Path;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.junit.jupiter.MockitoExtension;
35
36 @ExtendWith(MockitoExtension.class)
37 class SecurityContextTest {
38
39     @BeforeEach
40     void setUp() {
41         MockitoAnnotations.initMocks(this);
42     }
43
44     @Test
45     void testConstructorWithAuthTokenFilename() {
46         SecurityContext securityContext = new SecurityContext("auth-token-file.txt");
47         assertNotNull(securityContext.getAuthTokenFilePath());
48         assertEquals(Path.of("auth-token-file.txt"), securityContext.getAuthTokenFilePath());
49     }
50
51     @Test
52     void testConstructorWithoutAuthTokenFilename() {
53         SecurityContext securityContext = new SecurityContext("");
54         assertNull(securityContext.getAuthTokenFilePath());
55     }
56
57     @Test
58     void testIsConfigured() {
59         SecurityContext securityContext = new SecurityContext("auth-token-file.txt");
60         assertTrue(securityContext.isConfigured());
61     }
62
63     @Test
64     void testIsNotConfigured() {
65         SecurityContext securityContext = new SecurityContext("");
66         assertFalse(securityContext.isConfigured());
67     }
68
69     @Test
70     void testGetBearerAuthToken() {
71         assertEquals("", SecurityContext.getInstance().getBearerAuthToken());
72         assertEquals("", (new SecurityContext("foo.txt")).getBearerAuthToken());
73     }
74
75     @Test
76     void testGetBearerAuthTokenWhenNotConfigured() {
77         SecurityContext securityContext = new SecurityContext("");
78         assertEquals("", securityContext.getBearerAuthToken());
79     }
80 }
81