1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File NimbusJwtTest.java

 

Code metrics

0
55
15
1
188
145
15
0.27
3.67
15
1

Classes

Class Line # Actions
NimbusJwtTest 29 55 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    package org.europa.together.application;
2   
3    import static org.junit.jupiter.api.Assertions.*;
4    import static com.google.code.beanmatchers.BeanMatchers.*;
5    import java.sql.SQLException;
6    import java.util.ArrayList;
7    import java.util.List;
8    import org.europa.together.JUnit5Preperator;
9    import org.europa.together.business.DatabaseActions;
10    import org.europa.together.business.JsonWebToken;
11    import org.europa.together.business.Logger;
12    import org.europa.together.domain.ConfigurationDO;
13    import org.europa.together.domain.LogLevel;
14    import static org.hamcrest.MatcherAssert.assertThat;
15    import org.junit.jupiter.api.AfterAll;
16    import org.junit.jupiter.api.AfterEach;
17    import org.junit.jupiter.api.Assumptions;
18    import org.junit.jupiter.api.BeforeAll;
19    import org.junit.jupiter.api.BeforeEach;
20    import org.junit.jupiter.api.Test;
21    import org.junit.jupiter.api.extension.ExtendWith;
22    import org.springframework.beans.factory.annotation.Autowired;
23    import org.springframework.test.context.ContextConfiguration;
24    import org.springframework.test.context.junit.jupiter.SpringExtension;
25   
26    @SuppressWarnings("unchecked")
27    @ExtendWith({SpringExtension.class, JUnit5Preperator.class})
28    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
29    public class NimbusJwtTest {
30   
31    private static final Logger LOGGER = new LogbackLogger(NimbusJwtTest.class);
32   
33    private static final String FLUSH_TABLE
34    = "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";";
35    private static final String FILE
36    = "org/europa/together/sql/jwt-config.sql";
37   
38    private static DatabaseActions jdbcActions
39    = JUnit5Preperator.JDBC_CONNECTION;
40   
41    @Autowired
42    JsonWebToken jsonWebToken;
43   
44    String validToken
45    = "eyJhbGciOiJIUzUxMiJ9.e0hhbGxvIFdvcmxkIEpTT059.i2C8x1zuXKnLgorrthDLj9gHQppUo4WPzmK_kPXFoNa2mnsNa3nwle6X68cdyTVtdH-x5cLreuYI8A5PMqoLOQ";
46   
47    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
48  1 toggle @BeforeAll
49    static void setUp() {
50  1 Assumptions.assumeTrue(JUnit5Preperator.isConnected(), "JDBC DBMS Connection failed.");
51  1 jdbcActions.executeSqlFromClasspath(FILE);
52   
53  1 LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG);
54    }
55   
 
56  1 toggle @AfterAll
57    static void tearDown() throws SQLException {
58    }
59   
 
60  11 toggle @BeforeEach
61    void testCaseInitialization() {
62  11 jdbcActions.executeSqlFromClasspath(FILE);
63    }
64   
 
65  11 toggle @AfterEach
66    void testCaseTermination() throws SQLException {
67  11 jdbcActions.executeQuery(FLUSH_TABLE);
68    }
69    //</editor-fold>
70   
 
71  1 toggle @Test
72    void constructor() {
73  1 LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG);
74  1 assertThat(NimbusJwt.class, hasValidBeanConstructor());
75    }
76   
 
77  1 toggle @Test
78    void buildHMAC512SignedJws() throws Exception {
79  1 LOGGER.log("TEST CASE: buildHMAC512SignedJws", LogLevel.DEBUG);
80   
81  1 String singedJws = jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}");
82  1 assertEquals(validToken.length(), singedJws.length());
83  1 assertNotEquals(validToken, singedJws);
84    }
85   
 
86  1 toggle @Test
87    void failBuildHMAC512SignedJwsEmptySecret() throws Exception {
88  1 LOGGER.log("TEST CASE: failBuildHMAC512SignedJwsEmptySecret", LogLevel.DEBUG);
89   
90  1 jdbcActions.executeQuery(FLUSH_TABLE);
91  1 jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql");
92   
93  1 assertThrows(Exception.class, () -> {
94  1 jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}");
95    });
96    }
97   
 
98  1 toggle @Test
99    void parseHMAC512SingedJws() throws Exception {
100  1 LOGGER.log("TEST CASE: parseHMAC512SingedJws", LogLevel.DEBUG);
101   
102  1 String singedJws = jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}");
103  1 assertEquals("{Hallo World JSON}", jsonWebToken.parseHMAC512SingedJws(singedJws));
104    }
105   
 
106  1 toggle @Test
107    void failParseHMAC512SingedJws() throws Exception {
108  1 LOGGER.log("TEST CASE: failParseHMAC512SingedJws", LogLevel.DEBUG);
109   
110  1 assertThrows(Exception.class, () -> {
111  1 jsonWebToken.parseHMAC512SingedJws("fail parsing");
112    });
113    }
114   
 
115  1 toggle @Test
116    void parseHMAC512invalidSigningJws() throws Exception {
117  1 LOGGER.log("TEST CASE: parseHMAC512invalidSigningJws", LogLevel.DEBUG);
118   
119  1 jdbcActions.executeQuery(FLUSH_TABLE);
120  1 jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql");
121   
122  1 assertThrows(Exception.class, () -> {
123  1 jsonWebToken.parseHMAC512SingedJws("invalid");
124    });
125    }
126   
 
127  1 toggle @Test
128    void buildHMAC512SignedJwt() throws Exception {
129  1 LOGGER.log("TEST CASE: buildHMAC512SignedJwt", LogLevel.DEBUG);
130   
131  1 List<String> audience = new ArrayList<>();
132  1 audience.add("https://app.togehter-platform.org");
133  1 String singedJwt = jsonWebToken.buildHMAC512SignedJwt(audience);
134   
135  1 assertEquals(364, singedJwt.length());
136    }
137   
 
138  1 toggle @Test
139    void failBuildHMAC512SignedJwtEmptySecret() throws Exception {
140  1 LOGGER.log("TEST CASE: failBuildHMAC512SignedJwtEmptySecret", LogLevel.DEBUG);
141   
142  1 jdbcActions.executeQuery(FLUSH_TABLE);
143  1 jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql");
144   
145  1 assertThrows(Exception.class, () -> {
146  1 List<String> audience = new ArrayList<>();
147  1 audience.add("https://app.togehter-platform.org");
148   
149  1 jsonWebToken.buildHMAC512SignedJwt(audience);
150    });
151    }
152   
 
153  1 toggle @Test
154    void parseHMAC512SingedJwt() throws Exception {
155  1 LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG);
156   
157  1 List<String> audience = new ArrayList<>();
158  1 audience.add("https://app.togehter-platform.org");
159  1 String jwt = jsonWebToken.buildHMAC512SignedJwt(audience);
160   
161  1 String payload = jsonWebToken.parseHMAC512SingedJwt(jwt);
162  1 LOGGER.log("JWT Payload: " + payload, LogLevel.DEBUG);
163  1 assertEquals("{\"sub\":\"tp-jws\",", payload.substring(0, 16));
164    }
165   
 
166  1 toggle @Test
167    void failparseHMAC512SingedJwt() throws Exception {
168  1 LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG);
169   
170  1 assertThrows(Exception.class, () -> {
171  1 String jwt = jsonWebToken.buildHMAC512SignedJwt("", "", null);
172  1 jsonWebToken.parseHMAC512SingedJwt("invalid");
173    });
174    }
175   
 
176  1 toggle @Test
177    void parseHMAC512invalidSigningJwt() throws Exception {
178  1 LOGGER.log("TEST CASE: parseHMAC512invalidSigningJwt", LogLevel.DEBUG);
179   
180  1 jdbcActions.executeQuery(FLUSH_TABLE);
181  1 jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql");
182   
183  1 assertThrows(Exception.class, () -> {
184  1 jsonWebToken.parseHMAC512SingedJwt("null");
185    });
186    }
187   
188    }