Improve Test coverage of DFC
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / oauth2 / OAuthBearerTokenJwtTest.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.assertThrows;
27
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;
34
35 @ContextConfiguration(classes = {OAuthBearerTokenJwtTest.class})
36 @ExtendWith(MockitoExtension.class)
37 class OAuthBearerTokenJwtTest {
38
39     private OAuthBearerTokenJwt token;
40
41     @BeforeEach
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);
45     }
46
47     @Test
48     void testCreateValidToken() {
49         assertNotNull(token);
50     }
51
52     @Test
53     void testCreateInvalidToken() {
54         assertThrows(DatafileTaskException.class, () -> OAuthBearerTokenJwt.create("invalid_token"));
55     }
56
57     @Test
58     void testTokenValue() {
59         assertEquals("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", token.value());
60     }
61
62     @Test
63     void testTokenScope() {
64         assertEquals(0, token.scope().size());
65         assertFalse(token.scope().contains(""));
66     }
67
68     @Test
69     void testTokenLifetimeMs() {
70         assertEquals(Long.MAX_VALUE, token.lifetimeMs());
71     }
72
73     @Test
74     void testTokenPrincipalName() {
75         assertEquals("1234567890", token.principalName());
76     }
77
78     @Test
79     void testTokenStartTimeMs() {
80         assertEquals(1516239022L, token.startTimeMs());
81     }
82
83     @Test
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));
88     }
89
90     @Test
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");
95
96         assertNotNull(jwt);
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());
102     }
103
104     @Test
105     void testCreateThrowsExceptionWithInvalidToken() throws DatafileTaskException {
106         String tokenRaw = "your_mocked_token_here";
107         assertThrows(DatafileTaskException.class, () -> OAuthBearerTokenJwt.create(tokenRaw));
108     }
109 }