2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.pmproducer.oauth2;
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;
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;
36 @ExtendWith(MockitoExtension.class)
37 class SecurityContextTest {
41 MockitoAnnotations.initMocks(this);
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());
52 void testConstructorWithoutAuthTokenFilename() {
53 SecurityContext securityContext = new SecurityContext("");
54 assertNull(securityContext.getAuthTokenFilePath());
58 void testIsConfigured() {
59 SecurityContext securityContext = new SecurityContext("auth-token-file.txt");
60 assertTrue(securityContext.isConfigured());
64 void testIsNotConfigured() {
65 SecurityContext securityContext = new SecurityContext("");
66 assertFalse(securityContext.isConfigured());
70 void testGetBearerAuthToken() {
71 assertEquals("", SecurityContext.getInstance().getBearerAuthToken());
72 assertEquals("", (new SecurityContext("foo.txt")).getBearerAuthToken());
76 void testGetBearerAuthTokenWhenNotConfigured() {
77 SecurityContext securityContext = new SecurityContext("");
78 assertEquals("", securityContext.getBearerAuthToken());