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