1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File NimbusJwtTest.java

 

Code metrics

0
32
11
1
136
107
11
0.34
2.91
11
1

Classes

Class Line # Actions
NimbusJwtTest 31 32 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 7 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 org.europa.together.exceptions.JsonProcessingException;
15    import static org.hamcrest.MatcherAssert.assertThat;
16    import org.junit.jupiter.api.AfterAll;
17    import org.junit.jupiter.api.AfterEach;
18    import org.junit.jupiter.api.Assumptions;
19    import org.junit.jupiter.api.BeforeAll;
20    import org.junit.jupiter.api.BeforeEach;
21    import org.junit.jupiter.api.Test;
22    import org.junit.jupiter.api.extension.ExtendWith;
23    import org.springframework.beans.factory.annotation.Autowired;
24    import org.springframework.test.context.ContextConfiguration;
25    import org.springframework.test.context.junit.jupiter.SpringExtension;
26   
27    @SuppressWarnings("unchecked")
28    @ExtendWith({JUnit5Preperator.class})
29    @ExtendWith(SpringExtension.class)
30    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
31    public class NimbusJwtTest {
32   
33    private static final Logger LOGGER = new LogbackLogger(NimbusJwtTest.class);
34   
35    private static final String FLUSH_TABLE
36    = "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";";
37    private static final String FILE
38    = "org/europa/together/sql/jwt-test.sql";
39   
40    private static DatabaseActions jdbcActions = new JdbcActions();
41   
42    private String jws
43    = "eyJhbGciOiJIUzUxMiJ9.e0hhbGxvIFdvcmxkIEpTT059.H4_JPE4135eUQUnAJrQD9nkLavNuEBYxxp4FnU7k7FOZbjWtiE-F6DjnThaxBpTRzuGpck0H3lud_H0U7_mHbQ";
44   
45    @Autowired
46    JsonWebToken jsonWebToken;
47   
48    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
49  1 toggle @BeforeAll
50    static void setUp() {
51  1 Assumptions.assumeTrue(jdbcActions.connect("test"), "JDBC DBMS Connection failed.");
52  1 jdbcActions.executeSqlFromClasspath(FILE);
53   
54  1 LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG);
55    }
56   
 
57  1 toggle @AfterAll
58    static void tearDown() throws SQLException {
59  1 jdbcActions.executeQuery(FLUSH_TABLE);
60    }
61   
 
62  7 toggle @BeforeEach
63    void testCaseInitialization() {
64    }
65   
 
66  7 toggle @AfterEach
67    void testCaseTermination() {
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 assertEquals(jws, jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}"));
82    }
83   
 
84  1 toggle @Test
85    void buildHMAC512SignedJwt() throws Exception {
86  1 LOGGER.log("TEST CASE: buildHMAC512SignedJwt", LogLevel.DEBUG);
87   
88  1 String issuer = "https://togehter-platform.org";
89  1 String subject = "togetherPlatform-API";
90  1 List<String> audience = new ArrayList<>();
91  1 audience.add("https://app.togehter-platform.org");
92   
93  1 assertEquals(383, jsonWebToken.buildHMAC512SignedJwt(issuer, subject, audience).length());
94    }
95   
 
96  1 toggle @Test
97    void parseHMAC512SingedJws() throws Exception {
98  1 LOGGER.log("TEST CASE: parseHMAC512SingedJws", LogLevel.DEBUG);
99   
100  1 assertEquals("{Hallo World JSON}", jsonWebToken.parseHMAC512SingedJws(jws));
101    }
102   
 
103  1 toggle @Test
104    void failParseHMAC512SingedJws() throws Exception {
105  1 LOGGER.log("TEST CASE: failParseHMAC512SingedJws", LogLevel.DEBUG);
106   
107  1 assertThrows(Exception.class, () -> {
108  1 jsonWebToken.parseHMAC512SingedJws(jws.substring(1));
109    });
110    }
111   
 
112  1 toggle @Test
113    void parseHMAC512SingedJwt() throws Exception {
114  1 LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG);
115   
116  1 String issuer = "https://togehter-platform.org";
117  1 String subject = "togetherPlatform-API";
118  1 List<String> audience = new ArrayList<>();
119  1 audience.add("https://app.togehter-platform.org");
120  1 String jwt = jsonWebToken.buildHMAC512SignedJwt(issuer, subject, audience);
121   
122  1 String payload = jsonWebToken.parseHMAC512SingedJwt(jwt);
123  1 LOGGER.log("JWT Payload: " + payload, LogLevel.DEBUG);
124  1 assertEquals("{\"sub\":\"togetherPlatform-API\",", payload.substring(0, 30));
125    }
126   
 
127  1 toggle @Test
128    void failparseHMAC512SingedJwt() throws Exception {
129  1 LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG);
130   
131  1 assertThrows(Exception.class, () -> {
132  1 String jwt = jsonWebToken.buildHMAC512SignedJwt("", "", null);
133  1 jsonWebToken.parseHMAC512SingedJwt(jwt.substring(1));
134    });
135    }
136    }