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.assertThrows;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.junit.jupiter.MockitoExtension;
33 import org.oran.pmproducer.exceptions.ServiceException;
34 import org.springframework.test.context.ContextConfiguration;
36 @ContextConfiguration(classes = {OAuthBearerTokenJwtTest.class})
37 @ExtendWith(MockitoExtension.class)
38 class OAuthBearerTokenJwtTest {
40 private OAuthBearerTokenJwt token;
43 void setUp() throws ServiceException, JsonProcessingException {
44 String validJwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; // Replace with a valid JWT token for testing
45 token = OAuthBearerTokenJwt.create(validJwt);
49 void testCreateValidToken() {
54 void testCreateInvalidToken() {
55 assertThrows(ServiceException.class, () -> OAuthBearerTokenJwt.create("invalid_token"));
59 void testTokenValue() {
60 assertEquals("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", token.value());
64 void testTokenScope() {
65 assertEquals(0, token.scope().size());
66 assertFalse(token.scope().contains(""));
70 void testTokenLifetimeMs() {
71 assertEquals(Long.MAX_VALUE, token.lifetimeMs());
75 void testTokenPrincipalName() {
76 assertEquals("1234567890", token.principalName());
80 void testTokenStartTimeMs() {
81 assertEquals(1516239022L, token.startTimeMs());
85 void testCreateTokenFromInvalidPayload() throws ServiceException {
86 // Create a JWT with an invalid payload (missing fields)
87 String invalidPayload = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
88 assertThrows(ServiceException.class, () -> OAuthBearerTokenJwt.create(invalidPayload));
92 void testCreateTokenWithValidPayload() throws ServiceException, JsonProcessingException {
93 // Create a JWT with a valid payload
94 String validPayload = "eyJzdWIiOiAiVGVzdCIsICJleHAiOiAxNjM1MTUwMDAwLCAiaWF0IjogMTYzNTA5NTAwMCwgInNjb3BlIjogInNjb3BlX3Rva2VuIiwgImp0aSI6ICJmb28ifQ==";
95 OAuthBearerTokenJwt jwt = OAuthBearerTokenJwt.create("header." + validPayload + ".signature");
98 assertEquals("header." + validPayload + ".signature", jwt.value());
99 assertEquals(1, jwt.scope().size());
100 assertEquals("scope_token", jwt.scope().iterator().next());
101 assertEquals("Test", jwt.principalName());
102 assertEquals(1635095000, jwt.startTimeMs());
106 void testCreateThrowsExceptionWithInvalidToken() throws ServiceException {
107 String tokenRaw = "your_mocked_token_here";
108 assertThrows(ServiceException.class, () -> OAuthBearerTokenJwt.create(tokenRaw));