Improve Test coverage of pmproducer
[nonrtric/plt/ranpm.git] / pmproducer / src / test / java / org / oran / pmproducer / oauth2 / SecurityContextTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 Nordix Foundation
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.oran.pmproducer.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