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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.oran.datafile.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.assertThrows;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.oran.datafile.exceptions.DatafileTaskException;
33 import org.springframework.test.context.ContextConfiguration;
35 @ContextConfiguration(classes = {OAuthBearerTokenJwtTest.class})
36 @ExtendWith(MockitoExtension.class)
37 class OAuthBearerTokenJwtTest {
39 private OAuthBearerTokenJwt token;
42 void setUp() throws DatafileTaskException {
43 String validJwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; // Replace with a valid JWT token for testing
44 token = OAuthBearerTokenJwt.create(validJwt);
48 void testCreateValidToken() {
53 void testCreateInvalidToken() {
54 assertThrows(DatafileTaskException.class, () -> OAuthBearerTokenJwt.create("invalid_token"));
58 void testTokenValue() {
59 assertEquals("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", token.value());
63 void testTokenScope() {
64 assertEquals(0, token.scope().size());
65 assertFalse(token.scope().contains(""));
69 void testTokenLifetimeMs() {
70 assertEquals(Long.MAX_VALUE, token.lifetimeMs());
74 void testTokenPrincipalName() {
75 assertEquals("1234567890", token.principalName());
79 void testTokenStartTimeMs() {
80 assertEquals(1516239022L, token.startTimeMs());
84 void testCreateTokenFromInvalidPayload() throws DatafileTaskException {
85 // Create a JWT with an invalid payload (missing fields)
86 String invalidPayload = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
87 assertThrows(DatafileTaskException.class, () -> OAuthBearerTokenJwt.create(invalidPayload));
91 void testCreateTokenWithValidPayload() throws DatafileTaskException {
92 // Create a JWT with a valid payload
93 String validPayload = "eyJzdWIiOiAiVGVzdCIsICJleHAiOiAxNjM1MTUwMDAwLCAiaWF0IjogMTYzNTA5NTAwMCwgInNjb3BlIjogInNjb3BlX3Rva2VuIiwgImp0aSI6ICJmb28ifQ==";
94 OAuthBearerTokenJwt jwt = OAuthBearerTokenJwt.create("header." + validPayload + ".signature");
97 assertEquals("header." + validPayload + ".signature", jwt.value());
98 assertEquals(1, jwt.scope().size());
99 assertEquals("scope_token", jwt.scope().iterator().next());
100 assertEquals("Test", jwt.principalName());
101 assertEquals(1635095000, jwt.startTimeMs());
105 void testCreateThrowsExceptionWithInvalidToken() throws DatafileTaskException {
106 String tokenRaw = "your_mocked_token_here";
107 assertThrows(DatafileTaskException.class, () -> OAuthBearerTokenJwt.create(tokenRaw));